|
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, |
|
8
|
} from "../../constants" |
|
9
|
import { isResolveCallee } from "../../util/container" |
|
10
|
|
|
11
|
type MessageIds = "preferModulesEnum" |
|
12
|
|
|
13
|
export const rule = createRule<[], MessageIds>({ |
|
14
|
name: "prefer-modules-enum", |
|
15
|
meta: { |
|
16
|
type: "suggestion", |
|
17
|
docs: { |
|
18
|
description: |
|
19
|
"Prefer `Modules.*` enum members over magic module-name strings in `resolve(...)` calls.", |
|
20
|
}, |
|
21
|
fixable: "code", |
|
22
|
messages: { |
|
23
|
preferModulesEnum: |
|
24
|
"Use `Modules.{{enumMember}}` instead of the magic string `{{key}}`.", |
|
25
|
}, |
|
26
|
schema: [], |
|
27
|
}, |
|
28
|
defaultOptions: [], |
|
29
|
create(context) { |
|
30
|
const modulesLocalNames = new Set<string>() |
|
31
|
let frameworkUtilsImportNode: TSESTree.ImportDeclaration | null = null |
|
32
|
|
|
33
|
function addModulesImport( |
|
34
|
fixer: TSESLint.RuleFixer |
|
35
|
): TSESLint.RuleFix | null { |
|
36
|
if (frameworkUtilsImportNode) { |
|
37
|
const specifiers = frameworkUtilsImportNode.specifiers.filter( |
|
38
|
(s): s is TSESTree.ImportSpecifier => |
|
39
|
s.type === AST_NODE_TYPES.ImportSpecifier |
|
40
|
) |
|
41
|
if (specifiers.length === 0) { |
|
42
|
return null |
|
43
|
} |
|
44
|
const last = specifiers[specifiers.length - 1] |
|
45
|
return fixer.insertTextAfter(last, `, ${MODULES_ENUM}`) |
|
46
|
} |
|
47
|
const program = context.sourceCode.ast |
|
48
|
const first = program.body[0] |
|
49
|
const importLine = `import { ${MODULES_ENUM} } from "${FRAMEWORK_UTILS_SOURCE}"\n` |
|
50
|
if (!first) { |
|
51
|
return fixer.insertTextAfterRange([0, 0], importLine) |
|
52
|
} |
|
53
|
return fixer.insertTextBefore(first, importLine) |
|
54
|
} |
|
55
|
|
|
56
|
return { |
|
57
|
ImportDeclaration(node) { |
|
58
|
if (node.source.value !== FRAMEWORK_UTILS_SOURCE) { |
|
59
|
return |
|
60
|
} |
|
61
|
frameworkUtilsImportNode = node |
|
62
|
for (const specifier of node.specifiers) { |
|
63
|
if ( |
|
64
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
65
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
66
|
specifier.imported.name === MODULES_ENUM |
|
67
|
) { |
|
68
|
modulesLocalNames.add(specifier.local.name) |
|
69
|
} |
|
70
|
} |
|
71
|
}, |
|
72
|
|
|
73
|
CallExpression(node) { |
|
74
|
if (!isResolveCallee(node.callee)) { |
|
75
|
return |
|
76
|
} |
|
77
|
const arg = node.arguments[0] |
|
78
|
if (!arg) { |
|
79
|
return |
|
80
|
} |
|
81
|
if (arg.type !== AST_NODE_TYPES.Literal) { |
|
82
|
return |
|
83
|
} |
|
84
|
if (typeof arg.value !== "string") { |
|
85
|
return |
|
86
|
} |
|
87
|
const enumMember = MODULES_BY_VALUE[arg.value] |
|
88
|
if (!enumMember) { |
|
89
|
return |
|
90
|
} |
|
91
|
|
|
92
|
context.report({ |
|
93
|
node: arg, |
|
94
|
messageId: "preferModulesEnum", |
|
95
|
data: { key: arg.value, enumMember }, |
|
96
|
fix(fixer) { |
|
97
|
const fixes: TSESLint.RuleFix[] = [] |
|
98
|
const localName = |
|
99
|
modulesLocalNames.size > 0 |
|
100
|
? (modulesLocalNames.values().next().value as string) |
|
101
|
: MODULES_ENUM |
|
102
|
fixes.push(fixer.replaceText(arg, `${localName}.${enumMember}`)) |
|
103
|
if (modulesLocalNames.size === 0) { |
|
104
|
const importFix = addModulesImport(fixer) |
|
105
|
if (!importFix) { |
|
106
|
return null |
|
107
|
} |
|
108
|
fixes.push(importFix) |
|
109
|
modulesLocalNames.add(MODULES_ENUM) |
|
110
|
} |
|
111
|
return fixes |
|
112
|
}, |
|
113
|
}) |
|
114
|
}, |
|
115
|
} |
|
116
|
}, |
|
117
|
}) |
|
118
|
|
|
119
|
export default rule |