-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(eslint-plugin-router): route param name rule #6473
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
Conversation
|
View your CI Pipeline Execution ↗ for commit ed276e6
☁️ Nx Cloud last updated this comment at |
📝 WalkthroughWalkthroughAdds a new ESLint rule "route-param-names" that validates route parameter identifiers in TanStack Router path strings, along with constants, utilities for extracting/validating params, and comprehensive tests covering valid/invalid cases and multiple invocation shapes. Changes
Sequence Diagram(s)sequenceDiagram
participant Source as "Source File / AST"
participant ESLint as "ESLint Rule\nroute-param-names"
participant Utils as "Param Utils"
participant Reporter as "ESLint Reporter"
Note over Source,ESLint: Linter visits project files
Source->>ESLint: Detect router import/use and calls
ESLint->>ESLint: Determine call shape (path prop, first-arg, file-based, curried)
ESLint->>Utils: Extract path string(s) from literal/template/filepath
Utils->>Utils: extractParamsFromPath / isValidParamName / getInvalidParams
Utils-->>ESLint: Return invalid params list
ESLint->>Reporter: Report `invalidParamName` for each invalid param (with paramName)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@packages/eslint-plugin-router/src/rules/route-param-names/route-param-names.rule.ts`:
- Around line 46-60: The getStringLiteralValue function treats empty template
literals inconsistently because it checks node.quasis[0]?.value.cooked by
truthiness; change that check to explicitly test for null/undefined (e.g.,
cooked != null) so an empty string ("") is returned rather than null. Update the
TemplateLiteral branch (AST_NODE_TYPES.TemplateLiteral and
node.quasis[0].value.cooked) to capture the cooked value into a variable and
return it when it's not null/undefined, preserving existing behavior for
non-string Literal nodes.
- Around line 74-89: The rule currently only recognizes Identifier keys named
'path' in object arguments; update the property-key check in the createRoute
branch (where pathAsPropertySet is used and
reportInvalidParams/getStringLiteralValue are called) to also accept
string-literal keys. Replace the single check "prop.key.type ===
AST_NODE_TYPES.Identifier && prop.key.name === 'path'" with a condition that
allows either an Identifier named 'path' or a Literal whose value === 'path'
(i.e., check prop.key.type === AST_NODE_TYPES.Literal && prop.key.value ===
'path') before calling getStringLiteralValue and reportInvalidParams.
Problem
We used to extract all route params with regular expressions. This would intrinsically enforce a very strict format for the parameter names:
[a-zA-Z$][a-zA-Z0-9$]*, i.e. a valid javascript variable name.When we rewrote the matcher (#5722), regular params were not matched by a regex anymore (
$param) but more complex param syntaxes were still (e.g.prefix{-$optional}suffix). So we were only half enforcing the parameter name, which is not ideal.Now we would like to move completely off of regexes (#6470) and we should probably enforce some limitations on parameter names, or we risk preventing future evolutions of the syntax if parameter names are too lax, and users start using characters we would eventually like to assign special meaning to.
Solution
This PR proposes we add an ESLint rule so that such invalid parameter names can be detected in IDE and in CI.
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.