1 import * as path from "path"
2 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
3 import { createRule } from "../../create-rule"
4 import { getApiRouteSegments } from "../../util/api-route"
5
6 type MessageIds = "unknownRouteParam"
7
8 const VALID_PARAM_FOLDER = /^\[([a-zA-Z_][a-zA-Z0-9_]*)\]$/
9 const ROUTE_FILE_BASENAMES = new Set(["route.ts", "route.js"])
10
11 function collectParams(segments: string[]): Set<string> {
12 const params = new Set<string>()
13 for (const segment of segments) {
14 const m = segment.match(VALID_PARAM_FOLDER)
15 if (m) {
16 params.add(m[1])
17 }
18 }
19 return params
20 }
21
22 function isReqParams(node: TSESTree.Node): boolean {
23 return (
24 node.type === AST_NODE_TYPES.MemberExpression &&
25 !node.computed &&
26 node.object.type === AST_NODE_TYPES.Identifier &&
27 node.object.name === "req" &&
28 node.property.type === AST_NODE_TYPES.Identifier &&
29 node.property.name === "params"
30 )
31 }
32
33 export const rule = createRule<[], MessageIds>({
34 name: "route-params-must-be-defined",
35 meta: {
36 type: "problem",
37 docs: {
38 description:
39 "Properties accessed on `req.params` in an API route handler must correspond to a `[param]` folder in the route path.",
40 },
41 messages: {
42 unknownRouteParam:
43 "Route parameter `{{name}}` is not defined in the route path. Available params: {{available}}.",
44 },
45 schema: [],
46 },
47 defaultOptions: [],
48 create(context) {
49 const filename = context.filename
50 if (!filename || filename.startsWith("<")) {
51 return {}
52 }
53 if (!ROUTE_FILE_BASENAMES.has(path.basename(filename))) {
54 return {}
55 }
56
57 const segments = getApiRouteSegments(filename)
58 if (!segments) {
59 return {}
60 }
61
62 const params = collectParams(segments)
63 const available = params.size
64 ? Array.from(params)
65 .map((p) => `\`${p}\``)
66 .join(", ")
67 : "(none)"
68
69 function report(node: TSESTree.Node, name: string) {
70 if (params.has(name)) {
71 return
72 }
73 context.report({
74 node,
75 messageId: "unknownRouteParam",
76 data: { name, available },
77 })
78 }
79
80 return {
81 MemberExpression(node) {
82 if (!isReqParams(node.object)) {
83 return
84 }
85 if (node.computed) {
86 const prop = node.property
87 if (
88 prop.type === AST_NODE_TYPES.Literal &&
89 typeof prop.value === "string"
90 ) {
91 report(prop, prop.value)
92 }
93 return
94 }
95 if (node.property.type === AST_NODE_TYPES.Identifier) {
96 report(node.property, node.property.name)
97 }
98 },
99 VariableDeclarator(node) {
100 if (!node.init || !isReqParams(node.init)) {
101 return
102 }
103 if (node.id.type !== AST_NODE_TYPES.ObjectPattern) {
104 return
105 }
106 for (const prop of node.id.properties) {
107 if (prop.type !== AST_NODE_TYPES.Property) {
108 continue
109 }
110 if (prop.computed) {
111 continue
112 }
113 const key = prop.key
114 if (key.type === AST_NODE_TYPES.Identifier) {
115 report(key, key.name)
116 } else if (
117 key.type === AST_NODE_TYPES.Literal &&
118 typeof key.value === "string"
119 ) {
120 report(key, key.value)
121 }
122 }
123 },
124 }
125 },
126 })
127
128 export default rule