-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(router-generator): param name syntax check and warn #6472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sheraff
merged 6 commits into
main
from
feat-router-generator-param-name-syntax-checker
Jan 23, 2026
+274
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68cd16c
feat(router-generator): param name syntax check and warn
Sheraff 7e48e14
ci: apply automated fixes
autofix-ci[bot] 3c33832
better tests
Sheraff 6fd3ba5
ci: apply automated fixes
autofix-ci[bot] 1eac9e6
cleanup vi spy
Sheraff a517ae5
ci: apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import type { Logger } from './logger' | ||
|
|
||
| /** | ||
| * Regex for valid JavaScript identifier (param name) | ||
| * Must start with letter, underscore, or dollar sign | ||
| * Can contain letters, numbers, underscores, or dollar signs | ||
| */ | ||
| const VALID_PARAM_NAME_REGEX = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/ | ||
|
|
||
| interface ExtractedParam { | ||
| /** The param name without $ prefix (e.g., "userId", "optional") */ | ||
| paramName: string | ||
| /** Whether this param name is valid */ | ||
| isValid: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Extracts param names from a route path segment. | ||
| * | ||
| * Handles these patterns: | ||
| * - $paramName -> extract "paramName" | ||
| * - {$paramName} -> extract "paramName" | ||
| * - prefix{$paramName}suffix -> extract "paramName" | ||
| * - {-$paramName} -> extract "paramName" (optional) | ||
| * - prefix{-$paramName}suffix -> extract "paramName" (optional) | ||
| * - $ or {$} -> wildcard, skip validation | ||
| */ | ||
| function extractParamsFromSegment(segment: string): Array<ExtractedParam> { | ||
| const params: Array<ExtractedParam> = [] | ||
|
|
||
| // Skip empty segments | ||
| if (!segment || !segment.includes('$')) { | ||
| return params | ||
| } | ||
|
|
||
| // Check for wildcard ($ alone or {$}) | ||
| if (segment === '$' || segment === '{$}') { | ||
| return params // Wildcard, no param name to validate | ||
| } | ||
|
|
||
| // Pattern 1: Simple $paramName (entire segment starts with $) | ||
| if (segment.startsWith('$') && !segment.includes('{')) { | ||
| const paramName = segment.slice(1) | ||
| if (paramName) { | ||
| params.push({ | ||
| paramName, | ||
| isValid: VALID_PARAM_NAME_REGEX.test(paramName), | ||
| }) | ||
| } | ||
| return params | ||
| } | ||
|
|
||
| // Pattern 2: Braces pattern {$paramName} or {-$paramName} with optional prefix/suffix | ||
| // Match patterns like: prefix{$param}suffix, {$param}, {-$param} | ||
| const bracePattern = /\{(-?\$)([^}]*)\}/g | ||
| let match | ||
|
|
||
| while ((match = bracePattern.exec(segment)) !== null) { | ||
| const paramName = match[2] // The param name after $ or -$ | ||
|
|
||
| if (!paramName) { | ||
| // This is a wildcard {$} or {-$}, skip | ||
| continue | ||
| } | ||
|
|
||
| params.push({ | ||
| paramName, | ||
| isValid: VALID_PARAM_NAME_REGEX.test(paramName), | ||
| }) | ||
| } | ||
|
|
||
| return params | ||
| } | ||
|
|
||
| /** | ||
| * Extracts all params from a route path. | ||
| * | ||
| * @param path - The route path (e.g., "/users/$userId/posts/$postId") | ||
| * @returns Array of extracted params with validation info | ||
| */ | ||
| function extractParamsFromPath(path: string): Array<ExtractedParam> { | ||
| if (!path || !path.includes('$')) { | ||
| return [] | ||
| } | ||
|
|
||
| const segments = path.split('/') | ||
| const allParams: Array<ExtractedParam> = [] | ||
|
|
||
| for (const segment of segments) { | ||
| const params = extractParamsFromSegment(segment) | ||
| allParams.push(...params) | ||
| } | ||
|
|
||
| return allParams | ||
| } | ||
|
|
||
| /** | ||
| * Validates route params and logs warnings for invalid param names. | ||
| * | ||
| * @param routePath - The route path to validate | ||
| * @param filePath - The file path for error messages | ||
| * @param logger - Logger instance for warnings | ||
| */ | ||
| export function validateRouteParams( | ||
| routePath: string, | ||
| filePath: string, | ||
| logger: Logger, | ||
| ): void { | ||
| const params = extractParamsFromPath(routePath) | ||
| const invalidParams = params.filter((p) => !p.isValid) | ||
|
|
||
| for (const param of invalidParams) { | ||
| logger.warn( | ||
| `WARNING: Invalid param name "${param.paramName}" in route "${routePath}" (file: ${filePath}). ` + | ||
| `Param names must be valid JavaScript identifiers (match /[a-zA-Z_$][a-zA-Z0-9_$]*/).`, | ||
| ) | ||
| } | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
packages/router-generator/tests/generator/invalid-param-names/routeTree.snapshot.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* eslint-disable */ | ||
|
|
||
| // @ts-nocheck | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
|
|
||
| // This file was automatically generated by TanStack Router. | ||
| // You should NOT make any changes in this file as it will be overwritten. | ||
| // Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. | ||
|
|
||
| import { Route as rootRouteImport } from './routes/__root' | ||
| import { Route as ValidParamRouteImport } from './routes/$validParam' | ||
| import { Route as UserNameRouteImport } from './routes/$user-name' | ||
| import { Route as R123RouteImport } from './routes/$123' | ||
|
|
||
| const ValidParamRoute = ValidParamRouteImport.update({ | ||
| id: '/$validParam', | ||
| path: '/$validParam', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
| const UserNameRoute = UserNameRouteImport.update({ | ||
| id: '/$user-name', | ||
| path: '/$user-name', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
| const R123Route = R123RouteImport.update({ | ||
| id: '/$123', | ||
| path: '/$123', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
|
|
||
| export interface FileRoutesByFullPath { | ||
| '/$123': typeof R123Route | ||
| '/$user-name': typeof UserNameRoute | ||
| '/$validParam': typeof ValidParamRoute | ||
| } | ||
| export interface FileRoutesByTo { | ||
| '/$123': typeof R123Route | ||
| '/$user-name': typeof UserNameRoute | ||
| '/$validParam': typeof ValidParamRoute | ||
| } | ||
| export interface FileRoutesById { | ||
| __root__: typeof rootRouteImport | ||
| '/$123': typeof R123Route | ||
| '/$user-name': typeof UserNameRoute | ||
| '/$validParam': typeof ValidParamRoute | ||
| } | ||
| export interface FileRouteTypes { | ||
| fileRoutesByFullPath: FileRoutesByFullPath | ||
| fullPaths: '/$123' | '/$user-name' | '/$validParam' | ||
| fileRoutesByTo: FileRoutesByTo | ||
| to: '/$123' | '/$user-name' | '/$validParam' | ||
| id: '__root__' | '/$123' | '/$user-name' | '/$validParam' | ||
| fileRoutesById: FileRoutesById | ||
| } | ||
| export interface RootRouteChildren { | ||
| R123Route: typeof R123Route | ||
| UserNameRoute: typeof UserNameRoute | ||
| ValidParamRoute: typeof ValidParamRoute | ||
| } | ||
|
|
||
| declare module '@tanstack/react-router' { | ||
| interface FileRoutesByPath { | ||
| '/$validParam': { | ||
| id: '/$validParam' | ||
| path: '/$validParam' | ||
| fullPath: '/$validParam' | ||
| preLoaderRoute: typeof ValidParamRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| '/$user-name': { | ||
| id: '/$user-name' | ||
| path: '/$user-name' | ||
| fullPath: '/$user-name' | ||
| preLoaderRoute: typeof UserNameRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| '/$123': { | ||
| id: '/$123' | ||
| path: '/$123' | ||
| fullPath: '/$123' | ||
| preLoaderRoute: typeof R123RouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const rootRouteChildren: RootRouteChildren = { | ||
| R123Route: R123Route, | ||
| UserNameRoute: UserNameRoute, | ||
| ValidParamRoute: ValidParamRoute, | ||
| } | ||
| export const routeTree = rootRouteImport | ||
| ._addFileChildren(rootRouteChildren) | ||
| ._addFileTypes<FileRouteTypes>() |
5 changes: 5 additions & 0 deletions
5
packages/router-generator/tests/generator/invalid-param-names/routes/$123.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/$123')({ | ||
| component: () => <div>Invalid param starting with number</div>, | ||
| }) |
5 changes: 5 additions & 0 deletions
5
packages/router-generator/tests/generator/invalid-param-names/routes/$user-name.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/$user-name')({ | ||
| component: () => <div>Invalid param with hyphen</div>, | ||
| }) |
5 changes: 5 additions & 0 deletions
5
packages/router-generator/tests/generator/invalid-param-names/routes/$validParam.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/$validParam')({ | ||
| component: () => <div>Valid param</div>, | ||
| }) |
5 changes: 5 additions & 0 deletions
5
packages/router-generator/tests/generator/invalid-param-names/routes/__root.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createRootRoute, Outlet } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createRootRoute({ | ||
| component: () => <Outlet />, | ||
| }) |
36 changes: 36 additions & 0 deletions
36
packages/router-generator/tests/validate-route-params.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { join } from 'node:path' | ||
| import { afterAll, describe, expect, it, vi } from 'vitest' | ||
| import { Generator, getConfig } from '../src' | ||
|
|
||
| describe('validateRouteParams via generator', () => { | ||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| afterAll(() => { | ||
| warnSpy.mockRestore() | ||
| }) | ||
|
|
||
| it('should warn for invalid param names when running the generator', async () => { | ||
| const folderName = 'invalid-param-names' | ||
| const dir = join(process.cwd(), 'tests', 'generator', folderName) | ||
|
|
||
| const config = getConfig({ | ||
| disableLogging: false, // Enable logging to capture warnings | ||
| routesDirectory: dir + '/routes', | ||
| generatedRouteTree: dir + '/routeTree.gen.ts', | ||
| }) | ||
|
|
||
| const generator = new Generator({ config, root: dir }) | ||
| await generator.run() | ||
|
|
||
| // Should have warned about invalid params: $123 and $user-name | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Invalid param name'), | ||
| ) | ||
| expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('123')) | ||
| expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('user-name')) | ||
|
|
||
| // Should NOT have warned about $validParam | ||
| expect(warnSpy).not.toHaveBeenCalledWith( | ||
| expect.stringContaining('validParam'), | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.