|
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 { FRAMEWORK_UTILS_SOURCE } from "../../constants" |
|
5
|
import { isResolveCallee } from "../../util/container" |
|
6
|
|
|
7
|
type MessageIds = "preferRegistrationKey" |
|
8
|
|
|
9
|
const CONTAINER_REGISTRATION_KEYS = "ContainerRegistrationKeys" |
|
10
|
|
|
11
|
/** |
|
12
|
* Map of known registration-key string values → enum member name. |
|
13
|
* Sourced from `packages/core/utils/src/common/container.ts`. |
|
14
|
* `"remoteLink"` is intentionally omitted — it's deprecated and handled by |
|
15
|
* `prefer-link-over-remote-link`. |
|
16
|
*/ |
|
17
|
const KEYS_BY_VALUE: Record<string, string> = { |
|
18
|
query: "QUERY", |
|
19
|
link: "LINK", |
|
20
|
logger: "LOGGER", |
|
21
|
manager: "MANAGER", |
|
22
|
configModule: "CONFIG_MODULE", |
|
23
|
remoteQuery: "REMOTE_QUERY", |
|
24
|
__pg_connection__: "PG_CONNECTION", |
|
25
|
featureFlagRouter: "FEATURE_FLAG_ROUTER", |
|
26
|
} |
|
27
|
|
|
28
|
export const rule = createRule<[], MessageIds>({ |
|
29
|
name: "prefer-container-registration-keys", |
|
30
|
meta: { |
|
31
|
type: "suggestion", |
|
32
|
docs: { |
|
33
|
description: |
|
34
|
"Prefer `ContainerRegistrationKeys.*` members over magic strings in `resolve(...)` calls.", |
|
35
|
}, |
|
36
|
fixable: "code", |
|
37
|
messages: { |
|
38
|
preferRegistrationKey: |
|
39
|
"Use `ContainerRegistrationKeys.{{enumMember}}` instead of the magic string `{{key}}`.", |
|
40
|
}, |
|
41
|
schema: [], |
|
42
|
}, |
|
43
|
defaultOptions: [], |
|
44
|
create(context) { |
|
45
|
const keysLocalNames = new Set<string>() |
|
46
|
let frameworkUtilsImportNode: TSESTree.ImportDeclaration | null = null |
|
47
|
|
|
48
|
function addKeysImport(fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null { |
|
49
|
if (frameworkUtilsImportNode) { |
|
50
|
const specifiers = frameworkUtilsImportNode.specifiers.filter( |
|
51
|
(s): s is TSESTree.ImportSpecifier => |
|
52
|
s.type === AST_NODE_TYPES.ImportSpecifier |
|
53
|
) |
|
54
|
if (specifiers.length === 0) { |
|
55
|
return null |
|
56
|
} |
|
57
|
const last = specifiers[specifiers.length - 1] |
|
58
|
return fixer.insertTextAfter(last, `, ${CONTAINER_REGISTRATION_KEYS}`) |
|
59
|
} |
|
60
|
const program = context.sourceCode.ast |
|
61
|
const first = program.body[0] |
|
62
|
const importLine = `import { ${CONTAINER_REGISTRATION_KEYS} } from "${FRAMEWORK_UTILS_SOURCE}"\n` |
|
63
|
if (!first) { |
|
64
|
return fixer.insertTextAfterRange([0, 0], importLine) |
|
65
|
} |
|
66
|
return fixer.insertTextBefore(first, importLine) |
|
67
|
} |
|
68
|
|
|
69
|
return { |
|
70
|
ImportDeclaration(node) { |
|
71
|
if (node.source.value !== FRAMEWORK_UTILS_SOURCE) { |
|
72
|
return |
|
73
|
} |
|
74
|
frameworkUtilsImportNode = node |
|
75
|
for (const specifier of node.specifiers) { |
|
76
|
if ( |
|
77
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
78
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
79
|
specifier.imported.name === CONTAINER_REGISTRATION_KEYS |
|
80
|
) { |
|
81
|
keysLocalNames.add(specifier.local.name) |
|
82
|
} |
|
83
|
} |
|
84
|
}, |
|
85
|
|
|
86
|
CallExpression(node) { |
|
87
|
if (!isResolveCallee(node.callee)) { |
|
88
|
return |
|
89
|
} |
|
90
|
const arg = node.arguments[0] |
|
91
|
if (!arg) { |
|
92
|
return |
|
93
|
} |
|
94
|
if (arg.type !== AST_NODE_TYPES.Literal) { |
|
95
|
return |
|
96
|
} |
|
97
|
if (typeof arg.value !== "string") { |
|
98
|
return |
|
99
|
} |
|
100
|
const enumMember = KEYS_BY_VALUE[arg.value] |
|
101
|
if (!enumMember) { |
|
102
|
return |
|
103
|
} |
|
104
|
|
|
105
|
context.report({ |
|
106
|
node: arg, |
|
107
|
messageId: "preferRegistrationKey", |
|
108
|
data: { key: arg.value, enumMember }, |
|
109
|
fix(fixer) { |
|
110
|
const fixes: TSESLint.RuleFix[] = [] |
|
111
|
const localName = |
|
112
|
keysLocalNames.size > 0 |
|
113
|
? (keysLocalNames.values().next().value as string) |
|
114
|
: CONTAINER_REGISTRATION_KEYS |
|
115
|
fixes.push(fixer.replaceText(arg, `${localName}.${enumMember}`)) |
|
116
|
if (keysLocalNames.size === 0) { |
|
117
|
const importFix = addKeysImport(fixer) |
|
118
|
if (!importFix) { |
|
119
|
return null |
|
120
|
} |
|
121
|
fixes.push(importFix) |
|
122
|
keysLocalNames.add(CONTAINER_REGISTRATION_KEYS) |
|
123
|
} |
|
124
|
return fixes |
|
125
|
}, |
|
126
|
}) |
|
127
|
}, |
|
128
|
} |
|
129
|
}, |
|
130
|
}) |
|
131
|
|
|
132
|
export default rule |