|
1
|
import type { TSESTree, TSESLint } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { |
|
5
|
FRAMEWORK_UTILS_SOURCE, |
|
6
|
MODULES_BY_VALUE, |
|
7
|
MODULES_ENUM as MODULES, |
|
8
|
} from "../../constants" |
|
9
|
import { isLinkReceiver, trackLinkBinding } from "../../util/link" |
|
10
|
|
|
11
|
type MessageIds = "preferEnumKey" |
|
12
|
|
|
13
|
const LINK_METHODS = new Set(["create", "dismiss", "delete", "restore"]) |
|
14
|
|
|
15
|
const REMOTE_LINK_STEPS = new Set([ |
|
16
|
"createRemoteLinkStep", |
|
17
|
"dismissRemoteLinkStep", |
|
18
|
"removeRemoteLinkStep", |
|
19
|
"updateRemoteLinksStep", |
|
20
|
]) |
|
21
|
|
|
22
|
function getStaticKey( |
|
23
|
prop: TSESTree.Property |
|
24
|
): { value: string; node: TSESTree.Node; isLiteral: boolean } | null { |
|
25
|
if (prop.computed) { |
|
26
|
return null |
|
27
|
} |
|
28
|
if (prop.key.type === AST_NODE_TYPES.Identifier) { |
|
29
|
return { value: prop.key.name, node: prop.key, isLiteral: false } |
|
30
|
} |
|
31
|
if ( |
|
32
|
prop.key.type === AST_NODE_TYPES.Literal && |
|
33
|
typeof prop.key.value === "string" |
|
34
|
) { |
|
35
|
return { value: prop.key.value, node: prop.key, isLiteral: true } |
|
36
|
} |
|
37
|
return null |
|
38
|
} |
|
39
|
|
|
40
|
export const rule = createRule<[], MessageIds>({ |
|
41
|
name: "link-create-keys-modules-enum", |
|
42
|
meta: { |
|
43
|
type: "suggestion", |
|
44
|
docs: { |
|
45
|
description: |
|
46
|
"Prefer `Modules.*` enum members over bare string keys in link calls and remote-link workflow steps.", |
|
47
|
}, |
|
48
|
fixable: "code", |
|
49
|
messages: { |
|
50
|
preferEnumKey: |
|
51
|
"Use `Modules.{{enumMember}}` instead of the bare key `{{key}}`.", |
|
52
|
}, |
|
53
|
schema: [], |
|
54
|
}, |
|
55
|
defaultOptions: [], |
|
56
|
create(context) { |
|
57
|
const modulesLocalNames = new Set<string>() |
|
58
|
const stepLocalNames = new Set<string>() |
|
59
|
const linkLocalNames = new Set<string>() |
|
60
|
let frameworkUtilsImportNode: TSESTree.ImportDeclaration | null = null |
|
61
|
|
|
62
|
function isLinkMethodCall(callee: TSESTree.Expression): boolean { |
|
63
|
if ( |
|
64
|
callee.type !== AST_NODE_TYPES.MemberExpression || |
|
65
|
callee.computed || |
|
66
|
callee.property.type !== AST_NODE_TYPES.Identifier || |
|
67
|
!LINK_METHODS.has(callee.property.name) |
|
68
|
) { |
|
69
|
return false |
|
70
|
} |
|
71
|
|
|
72
|
return isLinkReceiver(callee.object, linkLocalNames) |
|
73
|
} |
|
74
|
|
|
75
|
function isStepCall(callee: TSESTree.Expression): boolean { |
|
76
|
return ( |
|
77
|
callee.type === AST_NODE_TYPES.Identifier && |
|
78
|
stepLocalNames.has(callee.name) |
|
79
|
) |
|
80
|
} |
|
81
|
|
|
82
|
function checkObject(obj: TSESTree.ObjectExpression) { |
|
83
|
for (const prop of obj.properties) { |
|
84
|
if (prop.type !== AST_NODE_TYPES.Property) { |
|
85
|
continue |
|
86
|
} |
|
87
|
const key = getStaticKey(prop) |
|
88
|
if (!key) { |
|
89
|
continue |
|
90
|
} |
|
91
|
const enumMember = MODULES_BY_VALUE[key.value] |
|
92
|
if (!enumMember) { |
|
93
|
continue |
|
94
|
} |
|
95
|
|
|
96
|
context.report({ |
|
97
|
node: key.node, |
|
98
|
messageId: "preferEnumKey", |
|
99
|
data: { key: key.value, enumMember }, |
|
100
|
fix(fixer: TSESLint.RuleFixer) { |
|
101
|
const fixes: TSESLint.RuleFix[] = [] |
|
102
|
const modulesName = |
|
103
|
modulesLocalNames.size > 0 |
|
104
|
? (modulesLocalNames.values().next().value as string) |
|
105
|
: MODULES |
|
106
|
const replacement = `[${modulesName}.${enumMember}]` |
|
107
|
|
|
108
|
if (prop.shorthand) { |
|
109
|
// `{ product }` → `{ [Modules.PRODUCT]: product }` |
|
110
|
const valueText = context.sourceCode.getText(prop.value) |
|
111
|
fixes.push( |
|
112
|
fixer.replaceText(prop, `${replacement}: ${valueText}`) |
|
113
|
) |
|
114
|
} else { |
|
115
|
fixes.push(fixer.replaceText(key.node, replacement)) |
|
116
|
} |
|
117
|
|
|
118
|
if (modulesLocalNames.size === 0) { |
|
119
|
const importFix = addModulesImport(fixer) |
|
120
|
if (!importFix) { |
|
121
|
return null |
|
122
|
} |
|
123
|
fixes.push(importFix) |
|
124
|
modulesLocalNames.add(MODULES) |
|
125
|
} |
|
126
|
|
|
127
|
return fixes |
|
128
|
}, |
|
129
|
}) |
|
130
|
} |
|
131
|
} |
|
132
|
|
|
133
|
function addModulesImport( |
|
134
|
fixer: TSESLint.RuleFixer |
|
135
|
): TSESLint.RuleFix | null { |
|
136
|
if (frameworkUtilsImportNode) { |
|
137
|
const specifiers = frameworkUtilsImportNode.specifiers.filter( |
|
138
|
(s): s is TSESTree.ImportSpecifier => |
|
139
|
s.type === AST_NODE_TYPES.ImportSpecifier |
|
140
|
) |
|
141
|
if (specifiers.length === 0) { |
|
142
|
return null |
|
143
|
} |
|
144
|
const last = specifiers[specifiers.length - 1] |
|
145
|
return fixer.insertTextAfter(last, `, ${MODULES}`) |
|
146
|
} |
|
147
|
const program = context.sourceCode.ast |
|
148
|
const first = program.body[0] |
|
149
|
const importLine = `import { ${MODULES} } from "${FRAMEWORK_UTILS_SOURCE}"\n` |
|
150
|
if (!first) { |
|
151
|
return fixer.insertTextAfterRange([0, 0], importLine) |
|
152
|
} |
|
153
|
return fixer.insertTextBefore(first, importLine) |
|
154
|
} |
|
155
|
|
|
156
|
function checkFirstArg(arg: TSESTree.CallExpressionArgument) { |
|
157
|
if (arg.type === AST_NODE_TYPES.ObjectExpression) { |
|
158
|
checkObject(arg) |
|
159
|
return |
|
160
|
} |
|
161
|
if (arg.type === AST_NODE_TYPES.ArrayExpression) { |
|
162
|
for (const el of arg.elements) { |
|
163
|
if (el && el.type === AST_NODE_TYPES.ObjectExpression) { |
|
164
|
checkObject(el) |
|
165
|
} |
|
166
|
} |
|
167
|
} |
|
168
|
} |
|
169
|
|
|
170
|
return { |
|
171
|
ImportDeclaration(node) { |
|
172
|
if (node.source.value === FRAMEWORK_UTILS_SOURCE) { |
|
173
|
frameworkUtilsImportNode = node |
|
174
|
for (const specifier of node.specifiers) { |
|
175
|
if ( |
|
176
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
177
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
178
|
specifier.imported.name === MODULES |
|
179
|
) { |
|
180
|
modulesLocalNames.add(specifier.local.name) |
|
181
|
} |
|
182
|
} |
|
183
|
} |
|
184
|
if (node.source.value === "@medusajs/medusa/core-flows") { |
|
185
|
for (const specifier of node.specifiers) { |
|
186
|
if ( |
|
187
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
188
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
189
|
REMOTE_LINK_STEPS.has(specifier.imported.name) |
|
190
|
) { |
|
191
|
stepLocalNames.add(specifier.local.name) |
|
192
|
} |
|
193
|
} |
|
194
|
} |
|
195
|
}, |
|
196
|
|
|
197
|
VariableDeclarator(node) { |
|
198
|
trackLinkBinding(node, linkLocalNames) |
|
199
|
}, |
|
200
|
|
|
201
|
PropertyDefinition(node) { |
|
202
|
trackLinkBinding(node, linkLocalNames) |
|
203
|
}, |
|
204
|
|
|
205
|
CallExpression(node) { |
|
206
|
if (isLinkMethodCall(node.callee) || isStepCall(node.callee)) { |
|
207
|
const arg = node.arguments[0] |
|
208
|
if (arg) { |
|
209
|
checkFirstArg(arg) |
|
210
|
} |
|
211
|
} |
|
212
|
}, |
|
213
|
} |
|
214
|
}, |
|
215
|
}) |
|
216
|
|
|
217
|
export default rule |