Skip to content

Commit baa3d1d

Browse files
fix: Fix escape sequence handling for routes defined in __virtual.ts files (#6250)
1 parent 2fa4cfa commit baa3d1d

16 files changed

Lines changed: 311 additions & 2 deletions

File tree

‎packages/router-generator/src/filesystem/physical/getRouteNodes.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ export async function getRouteNodes(
114114
`${dir}/${removeExt(node.filePath)}`,
115115
)
116116
node.routePath = routePath
117+
// Keep originalRoutePath aligned with routePath for escape detection
118+
if (node.originalRoutePath) {
119+
node.originalRoutePath = `/${dir}${node.originalRoutePath}`
120+
}
117121
node.filePath = filePath
118122
})
119123

‎packages/router-generator/src/filesystem/virtual/getRouteNodes.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path, { join, resolve } from 'node:path'
22
import {
3+
determineInitialRoutePath,
34
removeExt,
45
removeLeadingSlash,
56
removeTrailingSlash,
@@ -156,6 +157,10 @@ export async function getRouteNodesRecursive(
156157
`${node.pathPrefix}/${removeExt(subtreeNode.filePath)}`,
157158
)
158159
subtreeNode.routePath = `${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.routePath}`
160+
// Keep originalRoutePath aligned with routePath for escape detection
161+
if (subtreeNode.originalRoutePath) {
162+
subtreeNode.originalRoutePath = `${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.originalRoutePath}`
163+
}
159164
subtreeNode.filePath = `${node.directory}/${subtreeNode.filePath}`
160165
})
161166
return routeNodes
@@ -186,14 +191,23 @@ export async function getRouteNodesRecursive(
186191
const lastSegment = node.path
187192
let routeNode: RouteNode
188193

189-
const routePath = `${parentRoutePath}/${removeLeadingSlash(lastSegment)}`
194+
// Process the segment to handle escape sequences like [_]
195+
const {
196+
routePath: escapedSegment,
197+
originalRoutePath: originalSegment,
198+
} = determineInitialRoutePath(removeLeadingSlash(lastSegment))
199+
const routePath = `${parentRoutePath}${escapedSegment}`
200+
// Store the original path with brackets for escape detection
201+
const originalRoutePath = `${parentRoutePath}${originalSegment}`
202+
190203
if (node.file) {
191204
const { filePath, variableName, fullPath } = getFile(node.file)
192205
routeNode = {
193206
filePath,
194207
fullPath,
195208
variableName,
196209
routePath,
210+
originalRoutePath,
197211
_fsRouteType: 'static',
198212
}
199213
} else {
@@ -202,6 +216,7 @@ export async function getRouteNodesRecursive(
202216
fullPath: '',
203217
variableName: routePathToVariable(routePath),
204218
routePath,
219+
originalRoutePath,
205220
isVirtual: true,
206221
_fsRouteType: 'static',
207222
}
@@ -235,13 +250,21 @@ export async function getRouteNodesRecursive(
235250
node.id = ensureLeadingUnderScore(fileNameWithoutExt)
236251
}
237252
const lastSegment = node.id
238-
const routePath = `${parentRoutePath}/${removeLeadingSlash(lastSegment)}`
253+
// Process the segment to handle escape sequences like [_]
254+
const {
255+
routePath: escapedSegment,
256+
originalRoutePath: originalSegment,
257+
} = determineInitialRoutePath(removeLeadingSlash(lastSegment))
258+
const routePath = `${parentRoutePath}${escapedSegment}`
259+
// Store the original path with brackets for escape detection
260+
const originalRoutePath = `${parentRoutePath}${originalSegment}`
239261

240262
const routeNode: RouteNode = {
241263
fullPath,
242264
filePath,
243265
variableName,
244266
routePath,
267+
originalRoutePath,
245268
_fsRouteType: 'pathless_layout',
246269
}
247270

‎packages/router-generator/tests/generator.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ function rewriteConfigByFolderName(folderName: string, config: Config) {
104104
case 'virtual-config-file-default-export':
105105
config.virtualRouteConfig = './routes.ts'
106106
break
107+
case 'virtual-with-escaped-underscore':
108+
{
109+
// Test case for escaped underscores in physical routes mounted via virtual config
110+
// This ensures originalRoutePath is correctly prefixed when paths are updated
111+
const virtualRouteConfig = rootRoute('__root.tsx', [
112+
index('index.tsx'),
113+
physical('/api', 'physical-routes'),
114+
])
115+
config.virtualRouteConfig = virtualRouteConfig
116+
}
117+
break
107118
case 'types-disabled':
108119
config.disableTypes = true
109120
config.generatedRouteTree =
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 { Route as rootRouteImport } from './routes/__root'
12+
import { Route as IndexRouteImport } from './routes/index'
13+
import { Route as nestedCallbackRouteImport } from './routes/nested/callback'
14+
import { Route as nestedAuthRouteImport } from './routes/nested/auth'
15+
import { Route as nestedHomeRouteImport } from './routes/nested/home'
16+
17+
const IndexRoute = IndexRouteImport.update({
18+
id: '/',
19+
path: '/',
20+
getParentRoute: () => rootRouteImport,
21+
} as any)
22+
const nestedCallbackRoute = nestedCallbackRouteImport.update({
23+
id: '/nested/_callback',
24+
path: '/nested/_callback',
25+
getParentRoute: () => rootRouteImport,
26+
} as any)
27+
const nestedAuthRoute = nestedAuthRouteImport.update({
28+
id: '/nested/_auth',
29+
path: '/nested/_auth',
30+
getParentRoute: () => rootRouteImport,
31+
} as any)
32+
const nestedHomeRoute = nestedHomeRouteImport.update({
33+
id: '/nested/',
34+
path: '/nested/',
35+
getParentRoute: () => rootRouteImport,
36+
} as any)
37+
38+
export interface FileRoutesByFullPath {
39+
'/': typeof IndexRoute
40+
'/nested': typeof nestedHomeRoute
41+
'/nested/_auth': typeof nestedAuthRoute
42+
'/nested/_callback': typeof nestedCallbackRoute
43+
}
44+
export interface FileRoutesByTo {
45+
'/': typeof IndexRoute
46+
'/nested': typeof nestedHomeRoute
47+
'/nested/_auth': typeof nestedAuthRoute
48+
'/nested/_callback': typeof nestedCallbackRoute
49+
}
50+
export interface FileRoutesById {
51+
__root__: typeof rootRouteImport
52+
'/': typeof IndexRoute
53+
'/nested/': typeof nestedHomeRoute
54+
'/nested/_auth': typeof nestedAuthRoute
55+
'/nested/_callback': typeof nestedCallbackRoute
56+
}
57+
export interface FileRouteTypes {
58+
fileRoutesByFullPath: FileRoutesByFullPath
59+
fullPaths: '/' | '/nested' | '/nested/_auth' | '/nested/_callback'
60+
fileRoutesByTo: FileRoutesByTo
61+
to: '/' | '/nested' | '/nested/_auth' | '/nested/_callback'
62+
id: '__root__' | '/' | '/nested/' | '/nested/_auth' | '/nested/_callback'
63+
fileRoutesById: FileRoutesById
64+
}
65+
export interface RootRouteChildren {
66+
IndexRoute: typeof IndexRoute
67+
nestedHomeRoute: typeof nestedHomeRoute
68+
nestedAuthRoute: typeof nestedAuthRoute
69+
nestedCallbackRoute: typeof nestedCallbackRoute
70+
}
71+
72+
declare module '@tanstack/react-router' {
73+
interface FileRoutesByPath {
74+
'/': {
75+
id: '/'
76+
path: '/'
77+
fullPath: '/'
78+
preLoaderRoute: typeof IndexRouteImport
79+
parentRoute: typeof rootRouteImport
80+
}
81+
'/nested/_callback': {
82+
id: '/nested/_callback'
83+
path: '/nested/_callback'
84+
fullPath: '/nested/_callback'
85+
preLoaderRoute: typeof nestedCallbackRouteImport
86+
parentRoute: typeof rootRouteImport
87+
}
88+
'/nested/_auth': {
89+
id: '/nested/_auth'
90+
path: '/nested/_auth'
91+
fullPath: '/nested/_auth'
92+
preLoaderRoute: typeof nestedAuthRouteImport
93+
parentRoute: typeof rootRouteImport
94+
}
95+
'/nested/': {
96+
id: '/nested/'
97+
path: '/nested'
98+
fullPath: '/nested'
99+
preLoaderRoute: typeof nestedHomeRouteImport
100+
parentRoute: typeof rootRouteImport
101+
}
102+
}
103+
}
104+
105+
const rootRouteChildren: RootRouteChildren = {
106+
IndexRoute: IndexRoute,
107+
nestedHomeRoute: nestedHomeRoute,
108+
nestedAuthRoute: nestedAuthRoute,
109+
nestedCallbackRoute: nestedCallbackRoute,
110+
}
111+
export const routeTree = rootRouteImport
112+
._addFileChildren(rootRouteChildren)
113+
._addFileTypes<FileRouteTypes>()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createRootRoute } from '@tanstack/react-router'
2+
export const Route = createRootRoute()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
export const Route = createFileRoute('/')()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
defineVirtualSubtreeConfig,
3+
index,
4+
route,
5+
} from '@tanstack/virtual-file-routes'
6+
7+
export default defineVirtualSubtreeConfig([
8+
index('home.tsx'),
9+
route('[_]auth', 'auth.tsx'),
10+
route('[_]callback', 'callback.tsx'),
11+
])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
export const Route = createFileRoute('/nested/_auth')()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
export const Route = createFileRoute('/nested/_callback')()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
export const Route = createFileRoute('/nested/')()

0 commit comments

Comments
 (0)