@tanstack/eslint-plugin-router 1.154.7 → 1.155.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/cjs/index.cjs +4 -2
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/rules/route-param-names/constants.cjs +13 -0
  4. package/dist/cjs/rules/route-param-names/constants.cjs.map +1 -0
  5. package/dist/cjs/rules/route-param-names/constants.d.cts +23 -0
  6. package/dist/cjs/rules/route-param-names/route-param-names.rule.cjs +98 -0
  7. package/dist/cjs/rules/route-param-names/route-param-names.rule.cjs.map +1 -0
  8. package/dist/cjs/rules/route-param-names/route-param-names.rule.d.cts +4 -0
  9. package/dist/cjs/rules/route-param-names/route-param-names.utils.cjs +61 -0
  10. package/dist/cjs/rules/route-param-names/route-param-names.utils.cjs.map +1 -0
  11. package/dist/cjs/rules/route-param-names/route-param-names.utils.d.cts +43 -0
  12. package/dist/cjs/rules.cjs +3 -1
  13. package/dist/cjs/rules.cjs.map +1 -1
  14. package/dist/esm/index.js +4 -2
  15. package/dist/esm/index.js.map +1 -1
  16. package/dist/esm/rules/route-param-names/constants.d.ts +23 -0
  17. package/dist/esm/rules/route-param-names/constants.js +13 -0
  18. package/dist/esm/rules/route-param-names/constants.js.map +1 -0
  19. package/dist/esm/rules/route-param-names/route-param-names.rule.d.ts +4 -0
  20. package/dist/esm/rules/route-param-names/route-param-names.rule.js +98 -0
  21. package/dist/esm/rules/route-param-names/route-param-names.rule.js.map +1 -0
  22. package/dist/esm/rules/route-param-names/route-param-names.utils.d.ts +43 -0
  23. package/dist/esm/rules/route-param-names/route-param-names.utils.js +61 -0
  24. package/dist/esm/rules/route-param-names/route-param-names.utils.js.map +1 -0
  25. package/dist/esm/rules.js +3 -1
  26. package/dist/esm/rules.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/__tests__/route-param-names.rule.test.ts +271 -0
  29. package/src/__tests__/route-param-names.utils.test.ts +174 -0
  30. package/src/index.ts +2 -0
  31. package/src/rules/route-param-names/constants.ts +36 -0
  32. package/src/rules/route-param-names/route-param-names.rule.ts +127 -0
  33. package/src/rules/route-param-names/route-param-names.utils.ts +122 -0
  34. package/src/rules.ts +2 -0
@@ -0,0 +1,122 @@
1
+ import { VALID_PARAM_NAME_REGEX } from './constants'
2
+
3
+ export interface ExtractedParam {
4
+ /** The full param string including $ prefix (e.g., "$userId", "-$optional") */
5
+ fullParam: string
6
+ /** The param name without $ prefix (e.g., "userId", "optional") */
7
+ paramName: string
8
+ /** Whether this is an optional param (prefixed with -$) */
9
+ isOptional: boolean
10
+ /** Whether this param name is valid */
11
+ isValid: boolean
12
+ }
13
+
14
+ /**
15
+ * Extracts param names from a route path segment.
16
+ *
17
+ * Handles these patterns:
18
+ * - $paramName -> extract "paramName"
19
+ * - {$paramName} -> extract "paramName"
20
+ * - prefix{$paramName}suffix -> extract "paramName"
21
+ * - {-$paramName} -> extract "paramName" (optional)
22
+ * - prefix{-$paramName}suffix -> extract "paramName" (optional)
23
+ * - $ or {$} -> wildcard, skip validation
24
+ */
25
+ export function extractParamsFromSegment(
26
+ segment: string,
27
+ ): Array<ExtractedParam> {
28
+ const params: Array<ExtractedParam> = []
29
+
30
+ // Skip empty segments
31
+ if (!segment || !segment.includes('$')) {
32
+ return params
33
+ }
34
+
35
+ // Check for wildcard ($ alone or {$})
36
+ if (segment === '$' || segment === '{$}') {
37
+ return params // Wildcard, no param name to validate
38
+ }
39
+
40
+ // Pattern 1: Simple $paramName (entire segment starts with $)
41
+ if (segment.startsWith('$') && !segment.includes('{')) {
42
+ const paramName = segment.slice(1)
43
+ if (paramName) {
44
+ params.push({
45
+ fullParam: segment,
46
+ paramName,
47
+ isOptional: false,
48
+ isValid: VALID_PARAM_NAME_REGEX.test(paramName),
49
+ })
50
+ }
51
+ return params
52
+ }
53
+
54
+ // Pattern 2: Braces pattern {$paramName} or {-$paramName} with optional prefix/suffix
55
+ // Match patterns like: prefix{$param}suffix, {$param}, {-$param}
56
+ const bracePattern = /\{(-?\$)([^}]*)\}/g
57
+ let match
58
+
59
+ while ((match = bracePattern.exec(segment)) !== null) {
60
+ const prefix = match[1] // "$" or "-$"
61
+ const paramName = match[2] // The param name after $ or -$
62
+
63
+ if (!paramName) {
64
+ // This is a wildcard {$} or {-$}, skip
65
+ continue
66
+ }
67
+
68
+ const isOptional = prefix === '-$'
69
+
70
+ params.push({
71
+ fullParam: `${prefix}${paramName}`,
72
+ paramName,
73
+ isOptional,
74
+ isValid: VALID_PARAM_NAME_REGEX.test(paramName),
75
+ })
76
+ }
77
+
78
+ return params
79
+ }
80
+
81
+ /**
82
+ * Extracts all params from a route path.
83
+ *
84
+ * @param path - The route path (e.g., "/users/$userId/posts/$postId")
85
+ * @returns Array of extracted params with validation info
86
+ */
87
+ export function extractParamsFromPath(path: string): Array<ExtractedParam> {
88
+ if (!path || !path.includes('$')) {
89
+ return []
90
+ }
91
+
92
+ const segments = path.split('/')
93
+ const allParams: Array<ExtractedParam> = []
94
+
95
+ for (const segment of segments) {
96
+ const params = extractParamsFromSegment(segment)
97
+ allParams.push(...params)
98
+ }
99
+
100
+ return allParams
101
+ }
102
+
103
+ /**
104
+ * Validates a single param name.
105
+ *
106
+ * @param paramName - The param name to validate (without $ prefix)
107
+ * @returns Whether the param name is valid
108
+ */
109
+ export function isValidParamName(paramName: string): boolean {
110
+ return VALID_PARAM_NAME_REGEX.test(paramName)
111
+ }
112
+
113
+ /**
114
+ * Gets all invalid params from a route path.
115
+ *
116
+ * @param path - The route path
117
+ * @returns Array of invalid param info
118
+ */
119
+ export function getInvalidParams(path: string): Array<ExtractedParam> {
120
+ const params = extractParamsFromPath(path)
121
+ return params.filter((p) => !p.isValid)
122
+ }
package/src/rules.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as createRoutePropertyOrder from './rules/create-route-property-order/create-route-property-order.rule'
2
+ import * as routeParamNames from './rules/route-param-names/route-param-names.rule'
2
3
  import type { ESLintUtils } from '@typescript-eslint/utils'
3
4
  import type { ExtraRuleDocs } from './types'
4
5
 
@@ -12,4 +13,5 @@ export const rules: Record<
12
13
  >
13
14
  > = {
14
15
  [createRoutePropertyOrder.name]: createRoutePropertyOrder.rule,
16
+ [routeParamNames.name]: routeParamNames.rule,
15
17
  }