Skip to content

Commit e939220

Browse files
authoredJan 31, 2025··
fix(router-generator): reset route-group Regex when calling handleNode (#3289)
1 parent d432efc commit e939220

File tree

11 files changed

+466
-0
lines changed

11 files changed

+466
-0
lines changed
 

‎packages/router-generator/src/generator.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
removeTrailingSlash,
1111
removeUnderscores,
1212
replaceBackslash,
13+
resetRegex,
1314
routePathToVariable,
1415
trimPathLeft,
1516
writeIfDifferent,
@@ -158,6 +159,11 @@ export async function generator(config: Config, root: string) {
158159
await handleRootNode(rootRouteNode)
159160

160161
const handleNode = async (node: RouteNode) => {
162+
// Do not remove this as we need to set the lastIndex to 0 as it
163+
// is necessary to reset the regex's index when using the global flag
164+
// otherwise it might not match the next time it's used
165+
resetRegex(routeGroupPatternRegex)
166+
161167
let parentRoute = hasParentRoute(routeNodes, node, node.routePath)
162168

163169
// if the parent route is a virtual parent route, we need to find the real parent route

‎packages/router-generator/src/utils.ts

+13
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ export async function writeIfDifferent(
150150

151151
return false
152152
}
153+
154+
/**
155+
* This function resets the regex index to 0 so that it can be reused
156+
* without having to create a new regex object or worry about the last
157+
* state when using the global flag.
158+
*
159+
* @param regex The regex object to reset
160+
* @returns
161+
*/
162+
export function resetRegex(regex: RegExp) {
163+
regex.lastIndex = 0
164+
return
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
/* eslint-disable */
2+
3+
// @ts-nocheck
4+
5+
// noinspection JSUnusedGlobalSymbols
6+
7+
// This file was automatically generated by TanStack Router.
8+
// You should NOT make any changes in this file as it will be overwritten.
9+
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
10+
11+
import { createFileRoute } from '@tanstack/react-router'
12+
13+
// Import Routes
14+
15+
import { Route as rootRoute } from './routes/__root'
16+
import { Route as groupCLayoutCImport } from './routes/(group-c)/_layout-c'
17+
import { Route as groupBLayoutBImport } from './routes/(group-b)/_layout-b'
18+
import { Route as groupALayoutAImport } from './routes/(group-a)/_layout-a'
19+
import { Route as groupCLayoutCIndexImport } from './routes/(group-c)/_layout-c/index'
20+
import { Route as groupBLayoutBDashboardImport } from './routes/(group-b)/_layout-b/dashboard'
21+
import { Route as groupALayoutASignupImport } from './routes/(group-a)/_layout-a/signup'
22+
import { Route as groupALayoutALoginImport } from './routes/(group-a)/_layout-a/login'
23+
24+
// Create Virtual Routes
25+
26+
const groupCImport = createFileRoute('/(group-c)')()
27+
const groupBImport = createFileRoute('/(group-b)')()
28+
const groupAImport = createFileRoute('/(group-a)')()
29+
30+
// Create/Update Routes
31+
32+
const groupCRoute = groupCImport.update({
33+
id: '/(group-c)',
34+
getParentRoute: () => rootRoute,
35+
} as any)
36+
37+
const groupBRoute = groupBImport.update({
38+
id: '/(group-b)',
39+
getParentRoute: () => rootRoute,
40+
} as any)
41+
42+
const groupARoute = groupAImport.update({
43+
id: '/(group-a)',
44+
getParentRoute: () => rootRoute,
45+
} as any)
46+
47+
const groupCLayoutCRoute = groupCLayoutCImport.update({
48+
id: '/_layout-c',
49+
getParentRoute: () => groupCRoute,
50+
} as any)
51+
52+
const groupBLayoutBRoute = groupBLayoutBImport.update({
53+
id: '/_layout-b',
54+
getParentRoute: () => groupBRoute,
55+
} as any)
56+
57+
const groupALayoutARoute = groupALayoutAImport.update({
58+
id: '/_layout-a',
59+
getParentRoute: () => groupARoute,
60+
} as any)
61+
62+
const groupCLayoutCIndexRoute = groupCLayoutCIndexImport.update({
63+
id: '/',
64+
path: '/',
65+
getParentRoute: () => groupCLayoutCRoute,
66+
} as any)
67+
68+
const groupBLayoutBDashboardRoute = groupBLayoutBDashboardImport.update({
69+
id: '/dashboard',
70+
path: '/dashboard',
71+
getParentRoute: () => groupBLayoutBRoute,
72+
} as any)
73+
74+
const groupALayoutASignupRoute = groupALayoutASignupImport.update({
75+
id: '/signup',
76+
path: '/signup',
77+
getParentRoute: () => groupALayoutARoute,
78+
} as any)
79+
80+
const groupALayoutALoginRoute = groupALayoutALoginImport.update({
81+
id: '/login',
82+
path: '/login',
83+
getParentRoute: () => groupALayoutARoute,
84+
} as any)
85+
86+
// Populate the FileRoutesByPath interface
87+
88+
declare module '@tanstack/react-router' {
89+
interface FileRoutesByPath {
90+
'/(group-a)': {
91+
id: '/(group-a)'
92+
path: '/'
93+
fullPath: '/'
94+
preLoaderRoute: typeof groupAImport
95+
parentRoute: typeof rootRoute
96+
}
97+
'/(group-a)/_layout-a': {
98+
id: '/(group-a)/_layout-a'
99+
path: '/'
100+
fullPath: '/'
101+
preLoaderRoute: typeof groupALayoutAImport
102+
parentRoute: typeof groupARoute
103+
}
104+
'/(group-b)': {
105+
id: '/(group-b)'
106+
path: '/'
107+
fullPath: '/'
108+
preLoaderRoute: typeof groupBImport
109+
parentRoute: typeof rootRoute
110+
}
111+
'/(group-b)/_layout-b': {
112+
id: '/(group-b)/_layout-b'
113+
path: '/'
114+
fullPath: '/'
115+
preLoaderRoute: typeof groupBLayoutBImport
116+
parentRoute: typeof groupBRoute
117+
}
118+
'/(group-c)': {
119+
id: '/(group-c)'
120+
path: '/'
121+
fullPath: '/'
122+
preLoaderRoute: typeof groupCImport
123+
parentRoute: typeof rootRoute
124+
}
125+
'/(group-c)/_layout-c': {
126+
id: '/(group-c)/_layout-c'
127+
path: '/'
128+
fullPath: '/'
129+
preLoaderRoute: typeof groupCLayoutCImport
130+
parentRoute: typeof groupCRoute
131+
}
132+
'/(group-a)/_layout-a/login': {
133+
id: '/(group-a)/_layout-a/login'
134+
path: '/login'
135+
fullPath: '/login'
136+
preLoaderRoute: typeof groupALayoutALoginImport
137+
parentRoute: typeof groupALayoutAImport
138+
}
139+
'/(group-a)/_layout-a/signup': {
140+
id: '/(group-a)/_layout-a/signup'
141+
path: '/signup'
142+
fullPath: '/signup'
143+
preLoaderRoute: typeof groupALayoutASignupImport
144+
parentRoute: typeof groupALayoutAImport
145+
}
146+
'/(group-b)/_layout-b/dashboard': {
147+
id: '/(group-b)/_layout-b/dashboard'
148+
path: '/dashboard'
149+
fullPath: '/dashboard'
150+
preLoaderRoute: typeof groupBLayoutBDashboardImport
151+
parentRoute: typeof groupBLayoutBImport
152+
}
153+
'/(group-c)/_layout-c/': {
154+
id: '/(group-c)/_layout-c/'
155+
path: '/'
156+
fullPath: '/'
157+
preLoaderRoute: typeof groupCLayoutCIndexImport
158+
parentRoute: typeof groupCLayoutCImport
159+
}
160+
}
161+
}
162+
163+
// Create and export the route tree
164+
165+
interface groupALayoutARouteChildren {
166+
groupALayoutALoginRoute: typeof groupALayoutALoginRoute
167+
groupALayoutASignupRoute: typeof groupALayoutASignupRoute
168+
}
169+
170+
const groupALayoutARouteChildren: groupALayoutARouteChildren = {
171+
groupALayoutALoginRoute: groupALayoutALoginRoute,
172+
groupALayoutASignupRoute: groupALayoutASignupRoute,
173+
}
174+
175+
const groupALayoutARouteWithChildren = groupALayoutARoute._addFileChildren(
176+
groupALayoutARouteChildren,
177+
)
178+
179+
interface groupARouteChildren {
180+
groupALayoutARoute: typeof groupALayoutARouteWithChildren
181+
}
182+
183+
const groupARouteChildren: groupARouteChildren = {
184+
groupALayoutARoute: groupALayoutARouteWithChildren,
185+
}
186+
187+
const groupARouteWithChildren =
188+
groupARoute._addFileChildren(groupARouteChildren)
189+
190+
interface groupBLayoutBRouteChildren {
191+
groupBLayoutBDashboardRoute: typeof groupBLayoutBDashboardRoute
192+
}
193+
194+
const groupBLayoutBRouteChildren: groupBLayoutBRouteChildren = {
195+
groupBLayoutBDashboardRoute: groupBLayoutBDashboardRoute,
196+
}
197+
198+
const groupBLayoutBRouteWithChildren = groupBLayoutBRoute._addFileChildren(
199+
groupBLayoutBRouteChildren,
200+
)
201+
202+
interface groupBRouteChildren {
203+
groupBLayoutBRoute: typeof groupBLayoutBRouteWithChildren
204+
}
205+
206+
const groupBRouteChildren: groupBRouteChildren = {
207+
groupBLayoutBRoute: groupBLayoutBRouteWithChildren,
208+
}
209+
210+
const groupBRouteWithChildren =
211+
groupBRoute._addFileChildren(groupBRouteChildren)
212+
213+
interface groupCLayoutCRouteChildren {
214+
groupCLayoutCIndexRoute: typeof groupCLayoutCIndexRoute
215+
}
216+
217+
const groupCLayoutCRouteChildren: groupCLayoutCRouteChildren = {
218+
groupCLayoutCIndexRoute: groupCLayoutCIndexRoute,
219+
}
220+
221+
const groupCLayoutCRouteWithChildren = groupCLayoutCRoute._addFileChildren(
222+
groupCLayoutCRouteChildren,
223+
)
224+
225+
interface groupCRouteChildren {
226+
groupCLayoutCRoute: typeof groupCLayoutCRouteWithChildren
227+
}
228+
229+
const groupCRouteChildren: groupCRouteChildren = {
230+
groupCLayoutCRoute: groupCLayoutCRouteWithChildren,
231+
}
232+
233+
const groupCRouteWithChildren =
234+
groupCRoute._addFileChildren(groupCRouteChildren)
235+
236+
export interface FileRoutesByFullPath {
237+
'/': typeof groupCLayoutCIndexRoute
238+
'/login': typeof groupALayoutALoginRoute
239+
'/signup': typeof groupALayoutASignupRoute
240+
'/dashboard': typeof groupBLayoutBDashboardRoute
241+
}
242+
243+
export interface FileRoutesByTo {
244+
'/': typeof groupCLayoutCIndexRoute
245+
'/login': typeof groupALayoutALoginRoute
246+
'/signup': typeof groupALayoutASignupRoute
247+
'/dashboard': typeof groupBLayoutBDashboardRoute
248+
}
249+
250+
export interface FileRoutesById {
251+
__root__: typeof rootRoute
252+
'/(group-a)': typeof groupARouteWithChildren
253+
'/(group-a)/_layout-a': typeof groupALayoutARouteWithChildren
254+
'/(group-b)': typeof groupBRouteWithChildren
255+
'/(group-b)/_layout-b': typeof groupBLayoutBRouteWithChildren
256+
'/(group-c)': typeof groupCRouteWithChildren
257+
'/(group-c)/_layout-c': typeof groupCLayoutCRouteWithChildren
258+
'/(group-a)/_layout-a/login': typeof groupALayoutALoginRoute
259+
'/(group-a)/_layout-a/signup': typeof groupALayoutASignupRoute
260+
'/(group-b)/_layout-b/dashboard': typeof groupBLayoutBDashboardRoute
261+
'/(group-c)/_layout-c/': typeof groupCLayoutCIndexRoute
262+
}
263+
264+
export interface FileRouteTypes {
265+
fileRoutesByFullPath: FileRoutesByFullPath
266+
fullPaths: '/' | '/login' | '/signup' | '/dashboard'
267+
fileRoutesByTo: FileRoutesByTo
268+
to: '/' | '/login' | '/signup' | '/dashboard'
269+
id:
270+
| '__root__'
271+
| '/(group-a)'
272+
| '/(group-a)/_layout-a'
273+
| '/(group-b)'
274+
| '/(group-b)/_layout-b'
275+
| '/(group-c)'
276+
| '/(group-c)/_layout-c'
277+
| '/(group-a)/_layout-a/login'
278+
| '/(group-a)/_layout-a/signup'
279+
| '/(group-b)/_layout-b/dashboard'
280+
| '/(group-c)/_layout-c/'
281+
fileRoutesById: FileRoutesById
282+
}
283+
284+
export interface RootRouteChildren {
285+
groupARoute: typeof groupARouteWithChildren
286+
groupBRoute: typeof groupBRouteWithChildren
287+
groupCRoute: typeof groupCRouteWithChildren
288+
}
289+
290+
const rootRouteChildren: RootRouteChildren = {
291+
groupARoute: groupARouteWithChildren,
292+
groupBRoute: groupBRouteWithChildren,
293+
groupCRoute: groupCRouteWithChildren,
294+
}
295+
296+
export const routeTree = rootRoute
297+
._addFileChildren(rootRouteChildren)
298+
._addFileTypes<FileRouteTypes>()
299+
300+
/* ROUTE_MANIFEST_START
301+
{
302+
"routes": {
303+
"__root__": {
304+
"filePath": "__root.tsx",
305+
"children": [
306+
"/(group-a)",
307+
"/(group-b)",
308+
"/(group-c)"
309+
]
310+
},
311+
"/(group-a)": {
312+
"filePath": "(group-a)",
313+
"children": [
314+
"/(group-a)/_layout-a"
315+
]
316+
},
317+
"/(group-a)/_layout-a": {
318+
"filePath": "(group-a)/_layout-a.tsx",
319+
"parent": "/(group-a)",
320+
"children": [
321+
"/(group-a)/_layout-a/login",
322+
"/(group-a)/_layout-a/signup"
323+
]
324+
},
325+
"/(group-b)": {
326+
"filePath": "(group-b)",
327+
"children": [
328+
"/(group-b)/_layout-b"
329+
]
330+
},
331+
"/(group-b)/_layout-b": {
332+
"filePath": "(group-b)/_layout-b.tsx",
333+
"parent": "/(group-b)",
334+
"children": [
335+
"/(group-b)/_layout-b/dashboard"
336+
]
337+
},
338+
"/(group-c)": {
339+
"filePath": "(group-c)",
340+
"children": [
341+
"/(group-c)/_layout-c"
342+
]
343+
},
344+
"/(group-c)/_layout-c": {
345+
"filePath": "(group-c)/_layout-c.tsx",
346+
"parent": "/(group-c)",
347+
"children": [
348+
"/(group-c)/_layout-c/"
349+
]
350+
},
351+
"/(group-a)/_layout-a/login": {
352+
"filePath": "(group-a)/_layout-a/login.tsx",
353+
"parent": "/(group-a)/_layout-a"
354+
},
355+
"/(group-a)/_layout-a/signup": {
356+
"filePath": "(group-a)/_layout-a/signup.tsx",
357+
"parent": "/(group-a)/_layout-a"
358+
},
359+
"/(group-b)/_layout-b/dashboard": {
360+
"filePath": "(group-b)/_layout-b/dashboard.tsx",
361+
"parent": "/(group-b)/_layout-b"
362+
},
363+
"/(group-c)/_layout-c/": {
364+
"filePath": "(group-c)/_layout-c/index.tsx",
365+
"parent": "/(group-c)/_layout-c"
366+
}
367+
}
368+
}
369+
ROUTE_MANIFEST_END */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-a)/_layout-a')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-a)/_layout-a"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-a)/_layout-a/login')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-a)/_layout-a/login"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-a)/_layout-a/signup')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-a)/_layout-a/signup"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-b)/_layout-b')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-b)/_layout-b"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-b)/_layout-b/dashboard')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-b)/_layout/dashboard"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-c)/_layout-c')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-c)/_layout-c"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/(group-c)/_layout-c/')({
4+
component: RouteComponent,
5+
})
6+
7+
function RouteComponent() {
8+
return <div>Hello "/(group-c)/_layout-c/"!</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react'
2+
import { Outlet, createRootRoute } from '@tanstack/react-router'
3+
4+
export const Route = createRootRoute({
5+
component: RootComponent,
6+
})
7+
8+
function RootComponent() {
9+
return (
10+
<React.Fragment>
11+
<div>Hello "__root"!</div>
12+
<Outlet />
13+
</React.Fragment>
14+
)
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.