@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.
Files changed (95) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/constellation/index/migration-guide.mdx +309 -0
  3. package/constellation/index/overview.mdx +4 -0
  4. package/constellation/index/props.mdx +9 -5
  5. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
  6. package/index.js +9 -9
  7. package/package.json +3 -3
  8. package/scripts/codegen.tsx +223 -284
  9. package/src/rules/__tests__/utils/_tester.tsx +24 -24
  10. package/src/rules/atlaskit-theme/README.md +21 -19
  11. package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
  12. package/src/rules/atlaskit-theme/index.ts +107 -115
  13. package/src/rules/convert-props-syntax/README.md +32 -22
  14. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
  15. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
  16. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
  17. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
  18. package/src/rules/convert-props-syntax/index.tsx +356 -386
  19. package/src/rules/enforce-style-prop/README.md +21 -18
  20. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
  21. package/src/rules/enforce-style-prop/index.tsx +144 -160
  22. package/src/rules/external-rules.tsx +163 -164
  23. package/src/rules/local-cx-xcss/README.md +10 -6
  24. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
  25. package/src/rules/local-cx-xcss/index.tsx +46 -56
  26. package/src/rules/no-array-arguments/README.md +2 -1
  27. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
  28. package/src/rules/no-array-arguments/index.tsx +68 -69
  29. package/src/rules/no-classname-prop/README.md +7 -4
  30. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
  31. package/src/rules/no-classname-prop/index.tsx +25 -25
  32. package/src/rules/no-container-queries/README.md +11 -9
  33. package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
  34. package/src/rules/no-container-queries/index.tsx +75 -81
  35. package/src/rules/no-dynamic-styles/README.md +16 -19
  36. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
  37. package/src/rules/no-dynamic-styles/index.tsx +45 -48
  38. package/src/rules/no-exported-styles/README.md +17 -12
  39. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
  40. package/src/rules/no-exported-styles/index.tsx +36 -36
  41. package/src/rules/no-global-styles/README.md +15 -14
  42. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
  43. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
  44. package/src/rules/no-global-styles/index.tsx +59 -62
  45. package/src/rules/no-important-styles/README.md +5 -3
  46. package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
  47. package/src/rules/no-important-styles/index.tsx +49 -49
  48. package/src/rules/no-imported-style-values/README.md +23 -15
  49. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
  50. package/src/rules/no-imported-style-values/index.tsx +116 -120
  51. package/src/rules/no-nested-selectors/README.md +15 -13
  52. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +128 -128
  53. package/src/rules/no-styled/README.md +14 -9
  54. package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
  55. package/src/rules/no-styled/index.tsx +34 -41
  56. package/src/rules/no-unsafe-selectors/README.md +103 -93
  57. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
  58. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
  59. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
  60. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
  61. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
  62. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
  63. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
  64. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
  65. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
  66. package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
  67. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
  68. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
  69. package/src/rules/no-unsafe-values/README.md +34 -40
  70. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
  71. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
  72. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
  73. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
  74. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
  75. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
  76. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
  77. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
  78. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
  79. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
  80. package/src/rules/no-unsafe-values/index.tsx +76 -78
  81. package/src/rules/no-unsafe-values/messages.tsx +12 -12
  82. package/src/rules/no-unsafe-values/utils.tsx +370 -382
  83. package/src/rules/use-compiled/README.md +11 -8
  84. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
  85. package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
  86. package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
  87. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
  88. package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
  89. package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
  90. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
  91. package/src/rules/use-compiled/index.tsx +203 -226
  92. package/src/rules/use-compiled/is-safe.tsx +113 -117
  93. package/src/rules/use-compiled/utils.tsx +14 -18
  94. package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
  95. package/src/rules/utils/create-rule.tsx +3 -6
@@ -1,18 +1,18 @@
1
1
  import type {
2
- CallExpression,
3
- Expression,
4
- Identifier,
5
- Property,
6
- ObjectExpression,
7
- Node,
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
- getAllowedDynamicKeys,
13
- getAllowedFunctionCalls,
14
- isAllowListedVariable,
15
- type AllowList,
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
- 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
- });
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
- 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 (
282
- value.type === 'ConditionalExpression' ||
283
- value.type === 'LogicalExpression'
284
- ) {
285
- this.report({ node: value, messageId: 'no-conditional-values' });
286
- return;
287
- }
288
-
289
- if (value.type === 'BinaryExpression') {
290
- this.lintValue(value.left);
291
- this.lintValue(value.right);
292
- return;
293
- }
294
-
295
- if (value.type === 'ObjectExpression') {
296
- this.lintObjectExpression(value);
297
- return;
298
- }
299
-
300
- if (value.type === 'Identifier') {
301
- this.lintIdentifier(value);
302
- return;
303
- }
304
-
305
- if (value.type === 'TaggedTemplateExpression') {
306
- this.report({ node: value, messageId: 'no-function-calls' });
307
- return;
308
- }
309
-
310
- if (value.type === 'CallExpression') {
311
- this.lintFunctionCall(value);
312
- return;
313
- }
314
-
315
- if (
316
- value.type === 'ArrowFunctionExpression' ||
317
- value.type === 'FunctionExpression'
318
- ) {
319
- /**
320
- * Function values are handled by `no-dynamic-styles` instead
321
- */
322
- return;
323
- }
324
-
325
- if (value.type === 'MemberExpression') {
326
- this.report({ node: value, messageId: 'no-object-access' });
327
- return;
328
- }
329
-
330
- if (value.type === 'ArrayExpression') {
331
- this.report({ node: value, messageId: 'no-array-values' });
332
- return;
333
- }
334
-
335
- this.lintUnhandledValue(value);
336
- }
337
-
338
- private lintArgument(argument: CallExpression['arguments'][number]) {
339
- if (argument.type === 'ArrowFunctionExpression') {
340
- if (argument.body.type === 'ObjectExpression') {
341
- // Something in this form:
342
- //
343
- // const styles = styled.div(
344
- // (props) => ({ color: props.height }),
345
- // );
346
- this.lintArgument(argument.body);
347
- }
348
- return;
349
- }
350
-
351
- if (argument.type === 'ArrayExpression') {
352
- for (const element of argument.elements) {
353
- if (element === null) {
354
- continue;
355
- }
356
- this.lintArgument(element);
357
- }
358
- return;
359
- }
360
-
361
- if (argument.type === 'ObjectExpression') {
362
- this.lintObjectExpression(argument);
363
- return;
364
- }
365
-
366
- if (argument.type === 'CallExpression') {
367
- this.lintFunctionCall(argument);
368
- return;
369
- }
370
-
371
- if (
372
- argument.type === 'ConditionalExpression' ||
373
- argument.type === 'LogicalExpression'
374
- ) {
375
- this.report({ node: argument, messageId: 'no-conditional-values' });
376
- return;
377
- }
378
-
379
- if (
380
- argument.type === 'Identifier' ||
381
- argument.type === 'MemberExpression'
382
- ) {
383
- this.report({ node: argument, messageId: 'no-identifier-arguments' });
384
- return;
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
  }