|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "missingNextCall" |
|
5
|
|
|
6
|
const DEFINE_MIDDLEWARES = "defineMiddlewares" |
|
7
|
|
|
8
|
type MiddlewareFn = |
|
9
|
| TSESTree.ArrowFunctionExpression |
|
10
|
| TSESTree.FunctionExpression |
|
11
|
|
|
12
|
function isMiddlewareFunctionNode(node: TSESTree.Node): node is MiddlewareFn { |
|
13
|
return ( |
|
14
|
node.type === AST_NODE_TYPES.ArrowFunctionExpression || |
|
15
|
node.type === AST_NODE_TYPES.FunctionExpression |
|
16
|
) |
|
17
|
} |
|
18
|
|
|
19
|
function getNextParamName(fn: MiddlewareFn): string | null { |
|
20
|
const third = fn.params[2] |
|
21
|
if (!third) { |
|
22
|
return null |
|
23
|
} |
|
24
|
if (third.type === AST_NODE_TYPES.Identifier) { |
|
25
|
return third.name |
|
26
|
} |
|
27
|
// Destructured / rest / assignment patterns aren't the canonical signature — |
|
28
|
// skip them so we don't false-positive on unusual middleware shapes. |
|
29
|
return null |
|
30
|
} |
|
31
|
|
|
32
|
function bodyReferencesIdentifier(body: TSESTree.Node, name: string): boolean { |
|
33
|
let found = false |
|
34
|
const stack: TSESTree.Node[] = [body] |
|
35
|
while (stack.length) { |
|
36
|
const node = stack.pop()! |
|
37
|
if (node.type === AST_NODE_TYPES.Identifier && node.name === name) { |
|
38
|
found = true |
|
39
|
break |
|
40
|
} |
|
41
|
for (const key of Object.keys(node) as Array<keyof typeof node>) { |
|
42
|
if (key === "parent") { |
|
43
|
continue |
|
44
|
} |
|
45
|
const value = (node as unknown as Record<string, unknown>)[key as string] |
|
46
|
if (!value) { |
|
47
|
continue |
|
48
|
} |
|
49
|
if (Array.isArray(value)) { |
|
50
|
for (const child of value) { |
|
51
|
if (child && typeof child === "object" && "type" in child) { |
|
52
|
stack.push(child as TSESTree.Node) |
|
53
|
} |
|
54
|
} |
|
55
|
} else if (typeof value === "object" && "type" in (value as object)) { |
|
56
|
stack.push(value as TSESTree.Node) |
|
57
|
} |
|
58
|
} |
|
59
|
} |
|
60
|
return found |
|
61
|
} |
|
62
|
|
|
63
|
function collectMiddlewareFunctions( |
|
64
|
objectArg: TSESTree.Node, |
|
65
|
out: MiddlewareFn[] |
|
66
|
): void { |
|
67
|
const stack: TSESTree.Node[] = [objectArg] |
|
68
|
while (stack.length) { |
|
69
|
const node = stack.pop()! |
|
70
|
if (node.type === AST_NODE_TYPES.Property) { |
|
71
|
const key = node.key |
|
72
|
const isMiddlewaresKey = |
|
73
|
(key.type === AST_NODE_TYPES.Identifier && |
|
74
|
key.name === "middlewares") || |
|
75
|
(key.type === AST_NODE_TYPES.Literal && key.value === "middlewares") |
|
76
|
if ( |
|
77
|
isMiddlewaresKey && |
|
78
|
node.value.type === AST_NODE_TYPES.ArrayExpression |
|
79
|
) { |
|
80
|
for (const el of node.value.elements) { |
|
81
|
if (el && isMiddlewareFunctionNode(el)) { |
|
82
|
out.push(el) |
|
83
|
} |
|
84
|
} |
|
85
|
continue |
|
86
|
} |
|
87
|
} |
|
88
|
for (const key of Object.keys(node) as Array<keyof typeof node>) { |
|
89
|
if (key === "parent") { |
|
90
|
continue |
|
91
|
} |
|
92
|
const value = (node as unknown as Record<string, unknown>)[key as string] |
|
93
|
if (!value) { |
|
94
|
continue |
|
95
|
} |
|
96
|
if (Array.isArray(value)) { |
|
97
|
for (const child of value) { |
|
98
|
if (child && typeof child === "object" && "type" in child) { |
|
99
|
stack.push(child as TSESTree.Node) |
|
100
|
} |
|
101
|
} |
|
102
|
} else if (typeof value === "object" && "type" in (value as object)) { |
|
103
|
stack.push(value as TSESTree.Node) |
|
104
|
} |
|
105
|
} |
|
106
|
} |
|
107
|
} |
|
108
|
|
|
109
|
export const rule = createRule<[], MessageIds>({ |
|
110
|
name: "middleware-must-call-next", |
|
111
|
meta: { |
|
112
|
type: "suggestion", |
|
113
|
docs: { |
|
114
|
description: |
|
115
|
"Middleware functions registered via `defineMiddlewares(...)` must reference their `next` parameter so the middleware chain continues.", |
|
116
|
}, |
|
117
|
messages: { |
|
118
|
missingNextCall: |
|
119
|
"Middleware function never references `{{name}}`. Call `{{name}}()` (or end the response) so the middleware chain continues.", |
|
120
|
}, |
|
121
|
schema: [], |
|
122
|
}, |
|
123
|
defaultOptions: [], |
|
124
|
create(context) { |
|
125
|
return { |
|
126
|
CallExpression(node) { |
|
127
|
if ( |
|
128
|
node.callee.type !== AST_NODE_TYPES.Identifier || |
|
129
|
node.callee.name !== DEFINE_MIDDLEWARES |
|
130
|
) { |
|
131
|
return |
|
132
|
} |
|
133
|
const arg = node.arguments[0] |
|
134
|
if (!arg || arg.type !== AST_NODE_TYPES.ObjectExpression) { |
|
135
|
return |
|
136
|
} |
|
137
|
|
|
138
|
const fns: MiddlewareFn[] = [] |
|
139
|
collectMiddlewareFunctions(arg, fns) |
|
140
|
|
|
141
|
for (const fn of fns) { |
|
142
|
const name = getNextParamName(fn) |
|
143
|
if (!name) { |
|
144
|
continue |
|
145
|
} |
|
146
|
if (!bodyReferencesIdentifier(fn.body, name)) { |
|
147
|
context.report({ |
|
148
|
node: fn, |
|
149
|
messageId: "missingNextCall", |
|
150
|
data: { name }, |
|
151
|
}) |
|
152
|
} |
|
153
|
} |
|
154
|
}, |
|
155
|
} |
|
156
|
}, |
|
157
|
}) |
|
158
|
|
|
159
|
export default rule |