1 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
2 import { isFunctionNode, walkAst } from "./ast"
3
4 export const DEFINE_MIDDLEWARES = "defineMiddlewares"
5 export const ALLOW_FIELDS = "allowFields"
6
7 /** A route object passed in the `routes` array of `defineMiddlewares(...)`. */
8 export interface MiddlewareRoute {
9 object: TSESTree.ObjectExpression
10 middlewares: TSESTree.Node[]
11 /** The `method` or `methods` property, when the route scopes itself to one. */
12 methodProperty: TSESTree.Property | null
13 }
14
15 const METHOD_KEYS = new Set(["method", "methods"])
16
17 const getStaticKeyName = (key: TSESTree.Node): string | null => {
18 if (key.type === AST_NODE_TYPES.Identifier) {
19 return key.name
20 }
21 if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") {
22 return key.value
23 }
24 return null
25 }
26
27 /**
28 * Returns the object literal argument of a `defineMiddlewares(...)` call, or
29 * `null` when the node isn't such a call or its argument isn't a literal.
30 */
31 export const getDefineMiddlewaresArg = (
32 node: TSESTree.CallExpression
33 ): TSESTree.ObjectExpression | null => {
34 if (
35 node.callee.type !== AST_NODE_TYPES.Identifier ||
36 node.callee.name !== DEFINE_MIDDLEWARES
37 ) {
38 return null
39 }
40 const arg = node.arguments[0]
41 return arg?.type === AST_NODE_TYPES.ObjectExpression ? arg : null
42 }
43
44 /**
45 * Collects every object literal under `root` that carries a `middlewares`
46 * array. Route objects are matched by shape rather than by position under
47 * `routes`, so routes built in a separate array or nested helper are still
48 * found.
49 */
50 export const collectMiddlewareRoutes = (
51 root: TSESTree.Node
52 ): MiddlewareRoute[] => {
53 const routes: MiddlewareRoute[] = []
54
55 walkAst(root, (node) => {
56 if (node.type !== AST_NODE_TYPES.ObjectExpression) {
57 return
58 }
59
60 let middlewares: TSESTree.Node[] | null = null
61 let methodProperty: TSESTree.Property | null = null
62
63 for (const prop of node.properties) {
64 if (prop.type !== AST_NODE_TYPES.Property || prop.computed) {
65 continue
66 }
67 const name = getStaticKeyName(prop.key)
68 if (name === "middlewares") {
69 if (prop.value.type === AST_NODE_TYPES.ArrayExpression) {
70 middlewares = prop.value.elements.filter(
71 (el): el is TSESTree.Expression | TSESTree.SpreadElement =>
72 el !== null
73 )
74 }
75 } else if (name && METHOD_KEYS.has(name)) {
76 methodProperty = prop
77 }
78 }
79
80 if (middlewares) {
81 routes.push({ object: node, middlewares, methodProperty })
82 }
83 })
84
85 return routes
86 }
87
88 /** Request properties that only a global middleware can meaningfully set. */
89 export type RequestFieldsProperty = "allowed" | "disallowed"
90
91 /**
92 * Finds the first expression under `node` that writes to one of the request's
93 * field-filtering properties, covering `req.allowed = [...]`,
94 * `req.allowed ??= []`, and `req.allowed.push(...)`.
95 *
96 * When `node` is a function, only its own first parameter counts as the
97 * request, so an unrelated object with a property of the same name doesn't
98 * match.
99 */
100 export const findRequestFieldsMutation = (
101 node: TSESTree.Node,
102 property: RequestFieldsProperty
103 ): TSESTree.Node | null => {
104 let requestName: string | null = null
105
106 if (isFunctionNode(node)) {
107 const first = node.params[0]
108 if (first?.type !== AST_NODE_TYPES.Identifier) {
109 return null
110 }
111 requestName = first.name
112 }
113
114 const isRequestRef = (candidate: TSESTree.Node): boolean =>
115 candidate.type === AST_NODE_TYPES.Identifier &&
116 (requestName === null || candidate.name === requestName)
117
118 const isFieldsMember = (candidate: TSESTree.Node): boolean =>
119 candidate.type === AST_NODE_TYPES.MemberExpression &&
120 !candidate.computed &&
121 candidate.property.type === AST_NODE_TYPES.Identifier &&
122 candidate.property.name === property &&
123 isRequestRef(candidate.object)
124
125 let found: TSESTree.Node | null = null
126
127 walkAst(node, (current) => {
128 if (found) {
129 return false
130 }
131
132 if (
133 current.type === AST_NODE_TYPES.AssignmentExpression &&
134 isFieldsMember(current.left)
135 ) {
136 found = current
137 return false
138 }
139
140 if (
141 current.type === AST_NODE_TYPES.CallExpression &&
142 current.callee.type === AST_NODE_TYPES.MemberExpression &&
143 isFieldsMember(current.callee.object)
144 ) {
145 found = current
146 return false
147 }
148
149 return true
150 })
151
152 return found
153 }
154
155 /** Finds the first `allowFields(...)` call under `node`. */
156 export const findAllowFieldsCall = (
157 node: TSESTree.Node
158 ): TSESTree.CallExpression | null => {
159 let found: TSESTree.CallExpression | null = null
160
161 walkAst(node, (current) => {
162 if (found) {
163 return false
164 }
165 if (
166 current.type === AST_NODE_TYPES.CallExpression &&
167 current.callee.type === AST_NODE_TYPES.Identifier &&
168 current.callee.name === ALLOW_FIELDS
169 ) {
170 found = current
171 return false
172 }
173
174 return true
175 })
176
177 return found
178 }
179
180 /** Convenience wrapper for the `allowed` property. */
181 export const findAllowedMutation = (
182 node: TSESTree.Node
183 ): TSESTree.Node | null => findRequestFieldsMutation(node, "allowed")