|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { FRAMEWORK_HTTP_SOURCE } from "../../constants" |
|
5
|
|
|
6
|
type MessageIds = "deprecatedRemoteQueryConfig" |
|
7
|
|
|
8
|
const DEPRECATED_PROPERTY = "remoteQueryConfig" |
|
9
|
const REPLACEMENT_PROPERTY = "queryConfig" |
|
10
|
|
|
11
|
const REQUEST_TYPE_NAMES: ReadonlySet<string> = new Set([ |
|
12
|
"MedusaRequest", |
|
13
|
"AuthenticatedMedusaRequest", |
|
14
|
"MedusaStoreRequest", |
|
15
|
]) |
|
16
|
|
|
17
|
const HTTP_HANDLER_NAMES: ReadonlySet<string> = new Set([ |
|
18
|
"GET", |
|
19
|
"POST", |
|
20
|
"PUT", |
|
21
|
"DELETE", |
|
22
|
"PATCH", |
|
23
|
"OPTIONS", |
|
24
|
"HEAD", |
|
25
|
]) |
|
26
|
|
|
27
|
type FunctionNode = |
|
28
|
| TSESTree.FunctionDeclaration |
|
29
|
| TSESTree.FunctionExpression |
|
30
|
| TSESTree.ArrowFunctionExpression |
|
31
|
|
|
32
|
function getEnclosingFunction(node: TSESTree.Node): FunctionNode | null { |
|
33
|
let current: TSESTree.Node | undefined = node.parent |
|
34
|
while (current) { |
|
35
|
if ( |
|
36
|
current.type === AST_NODE_TYPES.FunctionDeclaration || |
|
37
|
current.type === AST_NODE_TYPES.FunctionExpression || |
|
38
|
current.type === AST_NODE_TYPES.ArrowFunctionExpression |
|
39
|
) { |
|
40
|
return current |
|
41
|
} |
|
42
|
current = current.parent |
|
43
|
} |
|
44
|
return null |
|
45
|
} |
|
46
|
|
|
47
|
function getParamName(param: TSESTree.Parameter): string | null { |
|
48
|
if (param.type === AST_NODE_TYPES.Identifier) { |
|
49
|
return param.name |
|
50
|
} |
|
51
|
if ( |
|
52
|
param.type === AST_NODE_TYPES.AssignmentPattern && |
|
53
|
param.left.type === AST_NODE_TYPES.Identifier |
|
54
|
) { |
|
55
|
return param.left.name |
|
56
|
} |
|
57
|
return null |
|
58
|
} |
|
59
|
|
|
60
|
function getParamTypeAnnotation( |
|
61
|
param: TSESTree.Parameter |
|
62
|
): TSESTree.TSTypeAnnotation | undefined { |
|
63
|
if (param.type === AST_NODE_TYPES.Identifier) { |
|
64
|
return param.typeAnnotation |
|
65
|
} |
|
66
|
if (param.type === AST_NODE_TYPES.AssignmentPattern) { |
|
67
|
return param.left.type === AST_NODE_TYPES.Identifier |
|
68
|
? param.left.typeAnnotation |
|
69
|
: undefined |
|
70
|
} |
|
71
|
return undefined |
|
72
|
} |
|
73
|
|
|
74
|
function typeReferencesRequest( |
|
75
|
typeAnnotation: TSESTree.TSTypeAnnotation | undefined, |
|
76
|
requestLocalNames: ReadonlySet<string> |
|
77
|
): boolean { |
|
78
|
if (!typeAnnotation) { |
|
79
|
return false |
|
80
|
} |
|
81
|
const typeNode = typeAnnotation.typeAnnotation |
|
82
|
if (typeNode.type !== AST_NODE_TYPES.TSTypeReference) { |
|
83
|
return false |
|
84
|
} |
|
85
|
const name = typeNode.typeName |
|
86
|
if ( |
|
87
|
name.type === AST_NODE_TYPES.Identifier && |
|
88
|
requestLocalNames.has(name.name) |
|
89
|
) { |
|
90
|
return true |
|
91
|
} |
|
92
|
// Support `HttpTypes.MedusaRequest`, `HttpTypes.AuthenticatedMedusaRequest`. |
|
93
|
if ( |
|
94
|
name.type === AST_NODE_TYPES.TSQualifiedName && |
|
95
|
name.right.type === AST_NODE_TYPES.Identifier && |
|
96
|
REQUEST_TYPE_NAMES.has(name.right.name) |
|
97
|
) { |
|
98
|
return true |
|
99
|
} |
|
100
|
return false |
|
101
|
} |
|
102
|
|
|
103
|
function isExportedHandler(fn: FunctionNode): boolean { |
|
104
|
if (fn.type === AST_NODE_TYPES.FunctionDeclaration) { |
|
105
|
if ( |
|
106
|
fn.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration && |
|
107
|
fn.id && |
|
108
|
HTTP_HANDLER_NAMES.has(fn.id.name) |
|
109
|
) { |
|
110
|
return true |
|
111
|
} |
|
112
|
return false |
|
113
|
} |
|
114
|
// Arrow / function expression: must be initializer of an exported VariableDeclarator. |
|
115
|
const parent = fn.parent |
|
116
|
if (parent?.type !== AST_NODE_TYPES.VariableDeclarator) { |
|
117
|
return false |
|
118
|
} |
|
119
|
if (parent.id.type !== AST_NODE_TYPES.Identifier) { |
|
120
|
return false |
|
121
|
} |
|
122
|
if (!HTTP_HANDLER_NAMES.has(parent.id.name)) { |
|
123
|
return false |
|
124
|
} |
|
125
|
const decl = parent.parent |
|
126
|
if (decl?.type !== AST_NODE_TYPES.VariableDeclaration) { |
|
127
|
return false |
|
128
|
} |
|
129
|
return decl.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration |
|
130
|
} |
|
131
|
|
|
132
|
export const rule = createRule<[], MessageIds>({ |
|
133
|
name: "no-deprecated-remote-query-config", |
|
134
|
meta: { |
|
135
|
type: "suggestion", |
|
136
|
docs: { |
|
137
|
description: |
|
138
|
"Disallow `req.remoteQueryConfig`; use `req.queryConfig` instead (deprecated since Medusa v2.2.0).", |
|
139
|
}, |
|
140
|
fixable: "code", |
|
141
|
messages: { |
|
142
|
deprecatedRemoteQueryConfig: |
|
143
|
"`remoteQueryConfig` is deprecated; use `queryConfig` instead.", |
|
144
|
}, |
|
145
|
schema: [], |
|
146
|
}, |
|
147
|
defaultOptions: [], |
|
148
|
create(context) { |
|
149
|
const requestLocalNames = new Set<string>() |
|
150
|
|
|
151
|
return { |
|
152
|
ImportDeclaration(node) { |
|
153
|
if (node.source.value !== FRAMEWORK_HTTP_SOURCE) { |
|
154
|
return |
|
155
|
} |
|
156
|
for (const specifier of node.specifiers) { |
|
157
|
if ( |
|
158
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
159
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
160
|
REQUEST_TYPE_NAMES.has(specifier.imported.name) |
|
161
|
) { |
|
162
|
requestLocalNames.add(specifier.local.name) |
|
163
|
} |
|
164
|
} |
|
165
|
}, |
|
166
|
|
|
167
|
MemberExpression(node) { |
|
168
|
if (node.computed) { |
|
169
|
return |
|
170
|
} |
|
171
|
if (node.property.type !== AST_NODE_TYPES.Identifier) { |
|
172
|
return |
|
173
|
} |
|
174
|
if (node.property.name !== DEPRECATED_PROPERTY) { |
|
175
|
return |
|
176
|
} |
|
177
|
if (node.object.type !== AST_NODE_TYPES.Identifier) { |
|
178
|
return |
|
179
|
} |
|
180
|
|
|
181
|
const objectName = node.object.name |
|
182
|
|
|
183
|
let matched = false |
|
184
|
let fn: FunctionNode | null = getEnclosingFunction(node) |
|
185
|
while (fn) { |
|
186
|
const param = fn.params.find((p) => getParamName(p) === objectName) |
|
187
|
if (param) { |
|
188
|
if ( |
|
189
|
typeReferencesRequest( |
|
190
|
getParamTypeAnnotation(param), |
|
191
|
requestLocalNames |
|
192
|
) |
|
193
|
) { |
|
194
|
matched = true |
|
195
|
break |
|
196
|
} |
|
197
|
if ( |
|
198
|
fn.params[0] === param && |
|
199
|
(objectName === "req" || objectName === "request") && |
|
200
|
isExportedHandler(fn) |
|
201
|
) { |
|
202
|
matched = true |
|
203
|
break |
|
204
|
} |
|
205
|
// The name is bound by this function but doesn't satisfy either |
|
206
|
// signal — stop walking; an outer scope's `req` is shadowed. |
|
207
|
break |
|
208
|
} |
|
209
|
fn = getEnclosingFunction(fn) |
|
210
|
} |
|
211
|
|
|
212
|
if (!matched) { |
|
213
|
return |
|
214
|
} |
|
215
|
|
|
216
|
context.report({ |
|
217
|
node: node.property, |
|
218
|
messageId: "deprecatedRemoteQueryConfig", |
|
219
|
fix(fixer) { |
|
220
|
return fixer.replaceText(node.property, REPLACEMENT_PROPERTY) |
|
221
|
}, |
|
222
|
}) |
|
223
|
}, |
|
224
|
} |
|
225
|
}, |
|
226
|
}) |
|
227
|
|
|
228
|
export default rule |