|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { ADMIN_SDK_SOURCE } from "../../constants" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
|
|
5
|
type MessageIds = "mustBeStringLiteral" | "templateLiteralMustBeString" |
|
6
|
|
|
7
|
const DEFINE_WIDGET_CONFIG = "defineWidgetConfig" |
|
8
|
|
|
9
|
function isStringLiteral(node: TSESTree.Node): boolean { |
|
10
|
return node.type === AST_NODE_TYPES.Literal && typeof node.value === "string" |
|
11
|
} |
|
12
|
|
|
13
|
export const rule = createRule<[], MessageIds>({ |
|
14
|
name: "widget-zone-must-be-string-literal", |
|
15
|
meta: { |
|
16
|
type: "problem", |
|
17
|
docs: { |
|
18
|
description: |
|
19
|
"Widget `zone` in `defineWidgetConfig` must be a string literal (or array of string literals).", |
|
20
|
}, |
|
21
|
messages: { |
|
22
|
mustBeStringLiteral: |
|
23
|
"Widget `zone` must be a string literal or array of string literals, not {{kind}}.", |
|
24
|
templateLiteralMustBeString: |
|
25
|
"Widget `zone` must be a string literal. Template literals are not allowed; use a plain string instead.", |
|
26
|
}, |
|
27
|
fixable: "code", |
|
28
|
schema: [], |
|
29
|
}, |
|
30
|
defaultOptions: [], |
|
31
|
create(context) { |
|
32
|
const localNames = new Set<string>() |
|
33
|
|
|
34
|
function checkZoneValue(value: TSESTree.Node) { |
|
35
|
if (isStringLiteral(value)) { |
|
36
|
return |
|
37
|
} |
|
38
|
|
|
39
|
if (value.type === AST_NODE_TYPES.TemplateLiteral) { |
|
40
|
if (value.expressions.length === 0 && value.quasis.length === 1) { |
|
41
|
context.report({ |
|
42
|
node: value, |
|
43
|
messageId: "templateLiteralMustBeString", |
|
44
|
fix(fixer) { |
|
45
|
const cooked = value.quasis[0].value.cooked ?? "" |
|
46
|
const raw = cooked.replace(/(["\\])/g, "\\$1") |
|
47
|
return fixer.replaceText(value, `"${raw}"`) |
|
48
|
}, |
|
49
|
}) |
|
50
|
return |
|
51
|
} |
|
52
|
context.report({ |
|
53
|
node: value, |
|
54
|
messageId: "templateLiteralMustBeString", |
|
55
|
}) |
|
56
|
return |
|
57
|
} |
|
58
|
|
|
59
|
if (value.type === AST_NODE_TYPES.ArrayExpression) { |
|
60
|
for (const element of value.elements) { |
|
61
|
if (!element) { |
|
62
|
continue |
|
63
|
} |
|
64
|
if (element.type === AST_NODE_TYPES.SpreadElement) { |
|
65
|
context.report({ |
|
66
|
node: element, |
|
67
|
messageId: "mustBeStringLiteral", |
|
68
|
data: { kind: "a spread element" }, |
|
69
|
}) |
|
70
|
continue |
|
71
|
} |
|
72
|
if (isStringLiteral(element)) { |
|
73
|
continue |
|
74
|
} |
|
75
|
if (element.type === AST_NODE_TYPES.TemplateLiteral) { |
|
76
|
checkZoneValue(element) |
|
77
|
continue |
|
78
|
} |
|
79
|
context.report({ |
|
80
|
node: element, |
|
81
|
messageId: "mustBeStringLiteral", |
|
82
|
data: { kind: describeKind(element) }, |
|
83
|
}) |
|
84
|
} |
|
85
|
return |
|
86
|
} |
|
87
|
|
|
88
|
context.report({ |
|
89
|
node: value, |
|
90
|
messageId: "mustBeStringLiteral", |
|
91
|
data: { kind: describeKind(value) }, |
|
92
|
}) |
|
93
|
} |
|
94
|
|
|
95
|
return { |
|
96
|
ImportDeclaration(node) { |
|
97
|
if (node.source.value !== ADMIN_SDK_SOURCE) { |
|
98
|
return |
|
99
|
} |
|
100
|
for (const spec of node.specifiers) { |
|
101
|
if ( |
|
102
|
spec.type === AST_NODE_TYPES.ImportSpecifier && |
|
103
|
spec.imported.type === AST_NODE_TYPES.Identifier && |
|
104
|
spec.imported.name === DEFINE_WIDGET_CONFIG |
|
105
|
) { |
|
106
|
localNames.add(spec.local.name) |
|
107
|
} |
|
108
|
} |
|
109
|
}, |
|
110
|
CallExpression(node) { |
|
111
|
if ( |
|
112
|
node.callee.type !== AST_NODE_TYPES.Identifier || |
|
113
|
!localNames.has(node.callee.name) |
|
114
|
) { |
|
115
|
return |
|
116
|
} |
|
117
|
const first = node.arguments[0] |
|
118
|
if (!first || first.type !== AST_NODE_TYPES.ObjectExpression) { |
|
119
|
return |
|
120
|
} |
|
121
|
for (const prop of first.properties) { |
|
122
|
if (prop.type !== AST_NODE_TYPES.Property) { |
|
123
|
continue |
|
124
|
} |
|
125
|
if (prop.computed) { |
|
126
|
continue |
|
127
|
} |
|
128
|
const key = prop.key |
|
129
|
const keyName = |
|
130
|
key.type === AST_NODE_TYPES.Identifier |
|
131
|
? key.name |
|
132
|
: key.type === AST_NODE_TYPES.Literal && |
|
133
|
typeof key.value === "string" |
|
134
|
? key.value |
|
135
|
: null |
|
136
|
if (keyName !== "zone") { |
|
137
|
continue |
|
138
|
} |
|
139
|
checkZoneValue(prop.value as TSESTree.Node) |
|
140
|
} |
|
141
|
}, |
|
142
|
} |
|
143
|
}, |
|
144
|
}) |
|
145
|
|
|
146
|
function describeKind(node: TSESTree.Node): string { |
|
147
|
switch (node.type) { |
|
148
|
case AST_NODE_TYPES.Identifier: |
|
149
|
return "a variable reference" |
|
150
|
case AST_NODE_TYPES.MemberExpression: |
|
151
|
return "a member expression" |
|
152
|
case AST_NODE_TYPES.CallExpression: |
|
153
|
return "a function call" |
|
154
|
case AST_NODE_TYPES.ConditionalExpression: |
|
155
|
return "a conditional expression" |
|
156
|
case AST_NODE_TYPES.Literal: |
|
157
|
return "a non-string literal" |
|
158
|
default: |
|
159
|
return "a dynamic expression" |
|
160
|
} |
|
161
|
} |
|
162
|
|
|
163
|
export default rule |