Repository navigation
Commit ea9a657
committed
feat(router-generator): allow escaping any special string in file-based routing
This change extends the router-generator's escaping mechanism to allow ANY
special string to be escaped using square bracket syntax, not just dots.
## Problem
Previously, the router-generator only supported escaping dots using [.]
syntax (e.g., script[.]js.tsx -> /script.js). However, special strings
like 'index', 'route', 'lazy', and underscores ('_') could not be escaped,
making it impossible to create routes with literal paths like /index,
/route, /lazy, or paths containing literal underscores.
## Solution
Extended the escaping mechanism to support:
1. **Escaped special suffixes**: [index], [route], [lazy], [component], etc.
- [index].tsx -> /index (literal path, NOT treated as index route)
- [route].tsx -> /route (literal path, NOT treated as layout config)
- [lazy].tsx -> /lazy (literal path, NOT treated as lazy-loaded route)
2. **Escaped underscores in any position**:
- Leading: [_]layout.tsx -> /_layout (literal, NOT pathless layout)
- Trailing: blog[_].tsx -> /blog_ (literal, NOT escaping parent layout)
- Middle: foo[_]bar.tsx -> /foo_bar (literal underscore preserved)
- Multiple: api[_]v2[_]users.tsx -> /api_v2_users
- Fully escaped: [_prefix_middle_suffix].tsx -> /_prefix_middle_suffix
3. **Custom token escaping**: Works with custom indexToken and routeToken
- [_1nd3x].tsx with indexToken='_1nd3x' -> /_1nd3x (literal path)
- posts.[_r0ut3_].tsx with routeToken='_r0ut3_' -> /posts/_r0ut3_
## Implementation Details
### src/utils.ts
- Removed '_' from DISALLOWED_ESCAPE_CHARS to allow underscore escaping
- Added isSegmentEscaped() to check if a segment was bracket-escaped
- Added hasEscapedLeadingUnderscore() to detect [_] prefix or [_...] patterns
- Added hasEscapedTrailingUnderscore() to detect [_] suffix or [..._] patterns
- Added removeUnderscoresWithEscape() to preserve escaped underscores
- Added removeLayoutSegmentsWithEscape() to preserve escaped layout segments
- Added isSegmentPathless() to check for non-escaped leading underscores
- Updated inferFullPath() to use escape-aware functions
### src/filesystem/physical/getRouteNodes.ts
- Added escape checking for special suffixes (route, lazy, component, etc.)
- Updated isValidPathlessLayoutRoute() to check for escaped underscores
- Updated getRouteMeta() to accept originalRoutePath and check escaping
### src/generator.ts
- Updated handleNode() to use escape-aware path processing functions
- Compute trimmedOriginalPath alongside trimmedPath for escape detection
## Tests Added
- escaped-special-strings: Tests escaping of index, route, lazy, and
underscores in various positions (leading, trailing, middle, multiple)
- escaped-custom-tokens: Tests escaping with custom indexToken and
routeToken configurations1 parent 27d1c5e commit ea9a657
24 files changed
Lines changed: 852 additions & 60 deletions
File tree
- packages/router-generator
- src
- filesystem/physical
- tests
- generator
- escaped-custom-tokens
- routes
- escaped-special-strings
- routes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
153 | 154 | | |
154 | 155 | | |
155 | 156 | | |
156 | | - | |
| 157 | + | |
157 | 158 | | |
158 | 159 | | |
159 | 160 | | |
| |||
164 | 165 | | |
165 | 166 | | |
166 | 167 | | |
167 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
168 | 176 | | |
169 | 177 | | |
170 | 178 | | |
| |||
189 | 197 | | |
190 | 198 | | |
191 | 199 | | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
205 | 222 | | |
206 | | - | |
207 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
208 | 229 | | |
209 | 230 | | |
210 | | - | |
211 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
212 | 256 | | |
213 | 257 | | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | 258 | | |
224 | 259 | | |
225 | 260 | | |
| |||
266 | 301 | | |
267 | 302 | | |
268 | 303 | | |
269 | | - | |
| 304 | + | |
| 305 | + | |
270 | 306 | | |
271 | 307 | | |
272 | 308 | | |
273 | 309 | | |
274 | 310 | | |
| 311 | + | |
275 | 312 | | |
276 | 313 | | |
277 | 314 | | |
| |||
292 | 329 | | |
293 | 330 | | |
294 | 331 | | |
295 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
296 | 346 | | |
297 | 347 | | |
298 | | - | |
| 348 | + | |
299 | 349 | | |
300 | 350 | | |
301 | | - | |
| 351 | + | |
302 | 352 | | |
303 | 353 | | |
304 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
305 | 358 | | |
306 | 359 | | |
307 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
308 | 364 | | |
309 | 365 | | |
310 | | - | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
311 | 370 | | |
312 | 371 | | |
313 | | - | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
314 | 376 | | |
315 | 377 | | |
316 | 378 | | |
| |||
323 | 385 | | |
324 | 386 | | |
325 | 387 | | |
| 388 | + | |
| 389 | + | |
326 | 390 | | |
327 | 391 | | |
328 | 392 | | |
329 | 393 | | |
330 | 394 | | |
| 395 | + | |
331 | 396 | | |
332 | 397 | | |
333 | 398 | | |
| |||
336 | 401 | | |
337 | 402 | | |
338 | 403 | | |
| 404 | + | |
339 | 405 | | |
340 | 406 | | |
341 | 407 | | |
342 | 408 | | |
343 | 409 | | |
344 | 410 | | |
| 411 | + | |
| 412 | + | |
345 | 413 | | |
| 414 | + | |
| 415 | + | |
346 | 416 | | |
347 | 417 | | |
348 | 418 | | |
| |||
352 | 422 | | |
353 | 423 | | |
354 | 424 | | |
| 425 | + | |
355 | 426 | | |
356 | 427 | | |
357 | | - | |
| 428 | + | |
| 429 | + | |
358 | 430 | | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
359 | 435 | | |
360 | 436 | | |
361 | 437 | | |
362 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
363 | 444 | | |
364 | 445 | | |
365 | 446 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | | - | |
| 37 | + | |
37 | 38 | | |
38 | | - | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
1363 | 1364 | | |
1364 | 1365 | | |
1365 | 1366 | | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
1366 | 1373 | | |
1367 | 1374 | | |
| 1375 | + | |
1368 | 1376 | | |
| 1377 | + | |
| 1378 | + | |
1369 | 1379 | | |
| 1380 | + | |
1370 | 1381 | | |
1371 | | - | |
| 1382 | + | |
1372 | 1383 | | |
1373 | 1384 | | |
| 1385 | + | |
1374 | 1386 | | |
1375 | | - | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
1376 | 1391 | | |
1377 | 1392 | | |
1378 | 1393 | | |
| |||
1431 | 1446 | | |
1432 | 1447 | | |
1433 | 1448 | | |
| 1449 | + | |
| 1450 | + | |
1434 | 1451 | | |
1435 | 1452 | | |
1436 | 1453 | | |
| |||
1442 | 1459 | | |
1443 | 1460 | | |
1444 | 1461 | | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
1445 | 1467 | | |
1446 | | - | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
1447 | 1475 | | |
1448 | 1476 | | |
1449 | 1477 | | |
| |||
0 commit comments