|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
import { resolveStaticStringValue } from "../../util/ast" |
|
4
|
import { FRAMEWORK_UTILS_SOURCE } from "../../constants" |
|
5
|
|
|
6
|
type MessageIds = "invalidModuleName" |
|
7
|
|
|
8
|
const MODULE_NAME = "Module" |
|
9
|
const VALID_NAME_RE = /^[a-zA-Z0-9_]+$/ |
|
10
|
|
|
11
|
export const rule = createRule<[], MessageIds>({ |
|
12
|
name: "module-name-snake-case", |
|
13
|
meta: { |
|
14
|
type: "problem", |
|
15
|
docs: { |
|
16
|
description: |
|
17
|
"The name passed to `Module(name, ...)` must contain only alphanumeric characters and underscores.", |
|
18
|
}, |
|
19
|
messages: { |
|
20
|
invalidModuleName: |
|
21
|
"Module name {{name}} must contain only alphanumeric characters and underscores.", |
|
22
|
}, |
|
23
|
schema: [], |
|
24
|
}, |
|
25
|
defaultOptions: [], |
|
26
|
create(context) { |
|
27
|
const moduleLocalNames = new Set<string>() |
|
28
|
|
|
29
|
return { |
|
30
|
ImportDeclaration(node) { |
|
31
|
if (node.source.value !== FRAMEWORK_UTILS_SOURCE) { |
|
32
|
return |
|
33
|
} |
|
34
|
for (const specifier of node.specifiers) { |
|
35
|
if ( |
|
36
|
specifier.type === "ImportSpecifier" && |
|
37
|
specifier.imported.type === "Identifier" && |
|
38
|
specifier.imported.name === MODULE_NAME |
|
39
|
) { |
|
40
|
moduleLocalNames.add(specifier.local.name) |
|
41
|
} |
|
42
|
} |
|
43
|
}, |
|
44
|
|
|
45
|
CallExpression(node) { |
|
46
|
if (moduleLocalNames.size === 0) { |
|
47
|
return |
|
48
|
} |
|
49
|
const callee = node.callee |
|
50
|
if ( |
|
51
|
callee.type !== "Identifier" || |
|
52
|
!moduleLocalNames.has(callee.name) |
|
53
|
) { |
|
54
|
return |
|
55
|
} |
|
56
|
const firstArg = node.arguments[0] as |
|
57
|
| TSESTree.CallExpressionArgument |
|
58
|
| undefined |
|
59
|
if (!firstArg) { |
|
60
|
return |
|
61
|
} |
|
62
|
const resolved = resolveStaticStringValue( |
|
63
|
firstArg, |
|
64
|
context.sourceCode.getScope(firstArg) |
|
65
|
) |
|
66
|
if (resolved === null) { |
|
67
|
return |
|
68
|
} |
|
69
|
if (VALID_NAME_RE.test(resolved.value)) { |
|
70
|
return |
|
71
|
} |
|
72
|
context.report({ |
|
73
|
node: resolved.reportNode, |
|
74
|
messageId: "invalidModuleName", |
|
75
|
data: { name: JSON.stringify(resolved.value) }, |
|
76
|
}) |
|
77
|
}, |
|
78
|
} |
|
79
|
}, |
|
80
|
}) |
|
81
|
|
|
82
|
export default rule |