@atlaskit/eslint-plugin-ui-styling-standard 0.15.2 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/constellation/index/migration-guide.mdx +309 -0
- package/constellation/index/overview.mdx +4 -0
- package/constellation/index/props.mdx +9 -5
- package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
- package/index.js +9 -9
- package/package.json +3 -3
- package/scripts/codegen.tsx +223 -284
- package/src/rules/__tests__/utils/_tester.tsx +24 -24
- package/src/rules/atlaskit-theme/README.md +21 -19
- package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
- package/src/rules/atlaskit-theme/index.ts +107 -115
- package/src/rules/convert-props-syntax/README.md +32 -22
- package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
- package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
- package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
- package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
- package/src/rules/convert-props-syntax/index.tsx +356 -386
- package/src/rules/enforce-style-prop/README.md +21 -18
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
- package/src/rules/enforce-style-prop/index.tsx +144 -160
- package/src/rules/external-rules.tsx +163 -164
- package/src/rules/local-cx-xcss/README.md +10 -6
- package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
- package/src/rules/local-cx-xcss/index.tsx +46 -56
- package/src/rules/no-array-arguments/README.md +2 -1
- package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
- package/src/rules/no-array-arguments/index.tsx +68 -69
- package/src/rules/no-classname-prop/README.md +7 -4
- package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
- package/src/rules/no-classname-prop/index.tsx +25 -25
- package/src/rules/no-container-queries/README.md +11 -9
- package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
- package/src/rules/no-container-queries/index.tsx +75 -81
- package/src/rules/no-dynamic-styles/README.md +16 -19
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
- package/src/rules/no-dynamic-styles/index.tsx +45 -48
- package/src/rules/no-exported-styles/README.md +17 -12
- package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
- package/src/rules/no-exported-styles/index.tsx +36 -36
- package/src/rules/no-global-styles/README.md +15 -14
- package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
- package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
- package/src/rules/no-global-styles/index.tsx +59 -62
- package/src/rules/no-important-styles/README.md +5 -3
- package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
- package/src/rules/no-important-styles/index.tsx +49 -49
- package/src/rules/no-imported-style-values/README.md +23 -15
- package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
- package/src/rules/no-imported-style-values/index.tsx +116 -120
- package/src/rules/no-nested-selectors/README.md +15 -13
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +128 -128
- package/src/rules/no-styled/README.md +14 -9
- package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
- package/src/rules/no-styled/index.tsx +34 -41
- package/src/rules/no-unsafe-selectors/README.md +103 -93
- package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
- package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
- package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
- package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
- package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
- package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
- package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
- package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
- package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
- package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
- package/src/rules/no-unsafe-values/README.md +34 -40
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
- package/src/rules/no-unsafe-values/index.tsx +76 -78
- package/src/rules/no-unsafe-values/messages.tsx +12 -12
- package/src/rules/no-unsafe-values/utils.tsx +370 -382
- package/src/rules/use-compiled/README.md +11 -8
- package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
- package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
- package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
- package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
- package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
- package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
- package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
- package/src/rules/use-compiled/index.tsx +203 -226
- package/src/rules/use-compiled/is-safe.tsx +113 -117
- package/src/rules/use-compiled/utils.tsx +14 -18
- package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
- package/src/rules/utils/create-rule.tsx +3 -6
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
CallExpression,
|
|
3
|
+
Expression,
|
|
4
|
+
Identifier,
|
|
5
|
+
Property,
|
|
6
|
+
ObjectExpression,
|
|
7
|
+
Node,
|
|
8
8
|
} from 'estree-jsx';
|
|
9
9
|
import type { Rule } from 'eslint';
|
|
10
10
|
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
getAllowedDynamicKeys,
|
|
13
|
+
getAllowedFunctionCalls,
|
|
14
|
+
isAllowListedVariable,
|
|
15
|
+
type AllowList,
|
|
16
16
|
} from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
17
17
|
import type { Variable } from 'eslint-scope';
|
|
18
18
|
import { findIdentifierNode } from '@atlaskit/eslint-utils/find-identifier-node';
|
|
@@ -21,379 +21,367 @@ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
|
21
21
|
import type { MessageId } from './messages';
|
|
22
22
|
|
|
23
23
|
function isReferenceToCssVar(variable: Variable) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
const definitions = variable.defs;
|
|
25
|
+
|
|
26
|
+
return definitions.every((definition) => {
|
|
27
|
+
if (definition.type !== 'Variable') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const initializer = definition.node.init;
|
|
32
|
+
if (!initializer) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
initializer.type === 'Literal' &&
|
|
38
|
+
typeof initializer.value === 'string' &&
|
|
39
|
+
initializer.value.startsWith('--')
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export class Linter {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
this.lintUnhandledValue(argument);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
private lintArguments(args: CallExpression['arguments']) {
|
|
391
|
-
for (const argument of args) {
|
|
392
|
-
this.lintArgument(argument);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
run(): void {
|
|
397
|
-
this.lintArguments(this.baseNode.arguments);
|
|
398
|
-
}
|
|
45
|
+
private readonly allowedDynamicKeys: AllowList;
|
|
46
|
+
private readonly allowedFunctionCalls: AllowList;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly context: Rule.RuleContext,
|
|
50
|
+
private readonly baseNode: CallExpression,
|
|
51
|
+
) {
|
|
52
|
+
this.allowedDynamicKeys = getAllowedDynamicKeys(context.options);
|
|
53
|
+
this.allowedFunctionCalls = getAllowedFunctionCalls(context.options);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wrapper around `context.report` to provide type checking on the message ID.
|
|
58
|
+
*/
|
|
59
|
+
private report(descriptor: { node: Node; messageId: MessageId }) {
|
|
60
|
+
this.context.report(descriptor);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Fallback case to lint any identifier that is not otherwise handled.
|
|
65
|
+
*/
|
|
66
|
+
private lintUnhandledIdentifier(identifier: Identifier) {
|
|
67
|
+
this.report({ node: identifier, messageId: 'no-variables' });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private lintIdentifier(value: Identifier) {
|
|
71
|
+
if (value.name === 'undefined') {
|
|
72
|
+
/**
|
|
73
|
+
* `undefined` gets caught implicitly anyway by other checks below,
|
|
74
|
+
* but we are explicitly handling it here.
|
|
75
|
+
*
|
|
76
|
+
* We could potentially introduce a distinct message for it as well.
|
|
77
|
+
*/
|
|
78
|
+
this.lintUnhandledIdentifier(value);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const variable = findVariable({
|
|
83
|
+
identifier: value,
|
|
84
|
+
sourceCode: this.context.sourceCode,
|
|
85
|
+
});
|
|
86
|
+
if (!variable) {
|
|
87
|
+
this.lintUnhandledIdentifier(value);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Allows using whitelisted imported values
|
|
93
|
+
*/
|
|
94
|
+
if (
|
|
95
|
+
isAllowListedVariable({
|
|
96
|
+
allowList: this.allowedFunctionCalls,
|
|
97
|
+
variable,
|
|
98
|
+
})
|
|
99
|
+
) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const definitions = variable.defs;
|
|
104
|
+
if (definitions.length === 0) {
|
|
105
|
+
this.lintUnhandledIdentifier(value);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
definitions.forEach((definition) => {
|
|
110
|
+
if (definition.type === 'Variable') {
|
|
111
|
+
const initializer = definition.node.init;
|
|
112
|
+
if (!initializer) {
|
|
113
|
+
this.lintUnhandledIdentifier(value);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Variables can be used if they resolve to a literal.
|
|
119
|
+
*/
|
|
120
|
+
if (initializer.type === 'Literal') {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Not allowing tagged template expressions through variables.
|
|
126
|
+
*/
|
|
127
|
+
if (initializer.type === 'TaggedTemplateExpression') {
|
|
128
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Variables can be used if they resolve to an allowed function call.
|
|
134
|
+
*/
|
|
135
|
+
if (initializer.type === 'CallExpression') {
|
|
136
|
+
if (this.isAllowedFunctionCall(initializer)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (initializer.type === 'TemplateLiteral') {
|
|
144
|
+
for (const expression of initializer.expressions) {
|
|
145
|
+
this.lintValue(expression);
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* This branch is only here for clarity.
|
|
152
|
+
*
|
|
153
|
+
* We could fallback to `no-variables` for it too.
|
|
154
|
+
*/
|
|
155
|
+
if (initializer.type === 'ObjectExpression') {
|
|
156
|
+
this.report({ node: value, messageId: 'no-object-references' });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.lintUnhandledIdentifier(value);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private isAllowedDynamicKey(expression: Expression) {
|
|
166
|
+
const identifier = findIdentifierNode(expression);
|
|
167
|
+
if (!identifier) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const variable = findVariable({
|
|
172
|
+
identifier,
|
|
173
|
+
sourceCode: this.context.sourceCode,
|
|
174
|
+
});
|
|
175
|
+
if (!variable) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
isReferenceToCssVar(variable) ||
|
|
181
|
+
isAllowListedVariable({ allowList: this.allowedDynamicKeys, variable })
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private lintKey(property: Property) {
|
|
186
|
+
/**
|
|
187
|
+
* If it's not computed then it must be a plain string.
|
|
188
|
+
*
|
|
189
|
+
* Private identifiers cannot be computed,
|
|
190
|
+
* but we need the extra check for type narrowing.
|
|
191
|
+
*/
|
|
192
|
+
if (!property.computed || property.key.type === 'PrivateIdentifier') {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Some dynamic keys are explicitly allowed.
|
|
198
|
+
*/
|
|
199
|
+
if (this.isAllowedDynamicKey(property.key)) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.report({ node: property.key, messageId: 'no-dynamic-keys' });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private isAllowedFunctionCall(value: CallExpression) {
|
|
207
|
+
const identifier = findIdentifierNode(value);
|
|
208
|
+
if (!identifier) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const variable = findVariable({
|
|
213
|
+
identifier,
|
|
214
|
+
sourceCode: this.context.sourceCode,
|
|
215
|
+
});
|
|
216
|
+
if (!variable) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return isAllowListedVariable({
|
|
221
|
+
allowList: this.allowedFunctionCalls,
|
|
222
|
+
variable,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private lintFunctionCall(value: CallExpression) {
|
|
227
|
+
if (this.isAllowedFunctionCall(value)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private lintObjectExpression(expression: ObjectExpression) {
|
|
235
|
+
for (const property of expression.properties) {
|
|
236
|
+
if (property.type === 'SpreadElement') {
|
|
237
|
+
this.report({
|
|
238
|
+
node: property,
|
|
239
|
+
messageId: 'no-spread-elements',
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
this.lintKey(property);
|
|
243
|
+
this.lintValue(property.value);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private lintUnhandledValue(value: Node) {
|
|
249
|
+
/**
|
|
250
|
+
* Default to blocking value if it isn't explictly handled.
|
|
251
|
+
*/
|
|
252
|
+
this.report({ node: value, messageId: 'no-unsafe-values' });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private lintValue(value: Property['value']): void {
|
|
256
|
+
/**
|
|
257
|
+
* Literals are always allowed by this rule.
|
|
258
|
+
*/
|
|
259
|
+
if (value.type === 'Literal') {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Allow negative numbers.
|
|
265
|
+
*/
|
|
266
|
+
if (
|
|
267
|
+
value.type === 'UnaryExpression' &&
|
|
268
|
+
value.operator === '-' &&
|
|
269
|
+
value.argument.type === 'Literal'
|
|
270
|
+
) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (value.type === 'TemplateLiteral') {
|
|
275
|
+
for (const expression of value.expressions) {
|
|
276
|
+
this.lintValue(expression);
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (value.type === 'ConditionalExpression' || value.type === 'LogicalExpression') {
|
|
282
|
+
this.report({ node: value, messageId: 'no-conditional-values' });
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (value.type === 'BinaryExpression') {
|
|
287
|
+
this.lintValue(value.left);
|
|
288
|
+
this.lintValue(value.right);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (value.type === 'ObjectExpression') {
|
|
293
|
+
this.lintObjectExpression(value);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (value.type === 'Identifier') {
|
|
298
|
+
this.lintIdentifier(value);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (value.type === 'TaggedTemplateExpression') {
|
|
303
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (value.type === 'CallExpression') {
|
|
308
|
+
this.lintFunctionCall(value);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression') {
|
|
313
|
+
/**
|
|
314
|
+
* Function values are handled by `no-dynamic-styles` instead
|
|
315
|
+
*/
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (value.type === 'MemberExpression') {
|
|
320
|
+
this.report({ node: value, messageId: 'no-object-access' });
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (value.type === 'ArrayExpression') {
|
|
325
|
+
this.report({ node: value, messageId: 'no-array-values' });
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
this.lintUnhandledValue(value);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private lintArgument(argument: CallExpression['arguments'][number]) {
|
|
333
|
+
if (argument.type === 'ArrowFunctionExpression') {
|
|
334
|
+
if (argument.body.type === 'ObjectExpression') {
|
|
335
|
+
// Something in this form:
|
|
336
|
+
//
|
|
337
|
+
// const styles = styled.div(
|
|
338
|
+
// (props) => ({ color: props.height }),
|
|
339
|
+
// );
|
|
340
|
+
this.lintArgument(argument.body);
|
|
341
|
+
}
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (argument.type === 'ArrayExpression') {
|
|
346
|
+
for (const element of argument.elements) {
|
|
347
|
+
if (element === null) {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
this.lintArgument(element);
|
|
351
|
+
}
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (argument.type === 'ObjectExpression') {
|
|
356
|
+
this.lintObjectExpression(argument);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (argument.type === 'CallExpression') {
|
|
361
|
+
this.lintFunctionCall(argument);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (argument.type === 'ConditionalExpression' || argument.type === 'LogicalExpression') {
|
|
366
|
+
this.report({ node: argument, messageId: 'no-conditional-values' });
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (argument.type === 'Identifier' || argument.type === 'MemberExpression') {
|
|
371
|
+
this.report({ node: argument, messageId: 'no-identifier-arguments' });
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
this.lintUnhandledValue(argument);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private lintArguments(args: CallExpression['arguments']) {
|
|
379
|
+
for (const argument of args) {
|
|
380
|
+
this.lintArgument(argument);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
run(): void {
|
|
385
|
+
this.lintArguments(this.baseNode.arguments);
|
|
386
|
+
}
|
|
399
387
|
}
|