@atlaskit/eslint-plugin-ui-styling-standard 0.10.0 → 0.11.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 (54) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-unsafe-values/usage.mdx +222 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +4 -2
  8. package/dist/cjs/rules/no-unsafe-values/index.js +85 -0
  9. package/dist/cjs/rules/no-unsafe-values/messages.js +24 -0
  10. package/dist/cjs/rules/no-unsafe-values/utils.js +437 -0
  11. package/dist/es2019/presets/all.codegen.js +2 -1
  12. package/dist/es2019/presets/recommended.codegen.js +2 -1
  13. package/dist/es2019/rules/index.codegen.js +4 -2
  14. package/dist/es2019/rules/no-unsafe-values/index.js +80 -0
  15. package/dist/es2019/rules/no-unsafe-values/messages.js +72 -0
  16. package/dist/es2019/rules/no-unsafe-values/utils.js +350 -0
  17. package/dist/esm/presets/all.codegen.js +2 -1
  18. package/dist/esm/presets/recommended.codegen.js +2 -1
  19. package/dist/esm/rules/index.codegen.js +4 -2
  20. package/dist/esm/rules/no-unsafe-values/index.js +79 -0
  21. package/dist/esm/rules/no-unsafe-values/messages.js +17 -0
  22. package/dist/esm/rules/no-unsafe-values/utils.js +430 -0
  23. package/dist/types/index.codegen.d.ts +2 -0
  24. package/dist/types/presets/all.codegen.d.ts +2 -1
  25. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  26. package/dist/types/rules/index.codegen.d.ts +1 -0
  27. package/dist/types/rules/no-unsafe-values/index.d.ts +2 -0
  28. package/dist/types/rules/no-unsafe-values/messages.d.ts +13 -0
  29. package/dist/types/rules/no-unsafe-values/utils.d.ts +28 -0
  30. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  31. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  32. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  33. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  34. package/dist/types-ts4.5/rules/no-unsafe-values/index.d.ts +2 -0
  35. package/dist/types-ts4.5/rules/no-unsafe-values/messages.d.ts +13 -0
  36. package/dist/types-ts4.5/rules/no-unsafe-values/utils.d.ts +28 -0
  37. package/package.json +4 -4
  38. package/src/presets/all.codegen.tsx +2 -1
  39. package/src/presets/recommended.codegen.tsx +2 -1
  40. package/src/rules/index.codegen.tsx +3 -1
  41. package/src/rules/no-unsafe-values/README.md +220 -0
  42. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +27 -0
  43. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +37 -0
  44. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +229 -0
  45. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +267 -0
  46. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +52 -0
  47. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +106 -0
  48. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +44 -0
  49. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +48 -0
  50. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +75 -0
  51. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +121 -0
  52. package/src/rules/no-unsafe-values/index.tsx +96 -0
  53. package/src/rules/no-unsafe-values/messages.tsx +86 -0
  54. package/src/rules/no-unsafe-values/utils.tsx +399 -0
@@ -0,0 +1,96 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import {
3
+ isCss,
4
+ getImportSources,
5
+ isStyled,
6
+ isKeyframes,
7
+ isCssMap,
8
+ isXcss,
9
+ } from '@atlaskit/eslint-utils/is-supported-import';
10
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
11
+ import { Linter } from './utils';
12
+ import { messages } from './messages';
13
+
14
+ const schema: JSONSchema4 = [
15
+ {
16
+ type: 'object',
17
+ additionalProperties: false,
18
+ properties: {
19
+ importSources: {
20
+ type: 'array',
21
+ items: { type: 'string' },
22
+ uniqueItems: true,
23
+ },
24
+ allowedFunctionCalls: {
25
+ type: 'array',
26
+ items: {
27
+ type: 'array',
28
+ minItems: 2,
29
+ maxItems: 2,
30
+ items: [{ type: 'string' }, { type: 'string' }],
31
+ },
32
+ uniqueItems: true,
33
+ },
34
+ allowedDynamicKeys: {
35
+ type: 'array',
36
+ items: {
37
+ type: 'array',
38
+ minItems: 2,
39
+ maxItems: 2,
40
+ items: [{ type: 'string' }, { type: 'string' }],
41
+ },
42
+ uniqueItems: true,
43
+ },
44
+ },
45
+ },
46
+ ];
47
+
48
+ export const rule = createLintRule({
49
+ meta: {
50
+ name: 'no-unsafe-values',
51
+ docs: {
52
+ description:
53
+ 'Disallows styles that are difficult/impossible to statically anaylze.',
54
+ recommended: true,
55
+ severity: 'warn',
56
+ },
57
+ messages,
58
+ type: 'problem',
59
+ schema,
60
+ },
61
+ create(context) {
62
+ const importSources = getImportSources(context);
63
+
64
+ return {
65
+ CallExpression(node) {
66
+ const { references } = context.sourceCode.getScope(node);
67
+
68
+ if (
69
+ !isCss(node.callee, references, importSources) &&
70
+ !isStyled(node.callee, references, importSources) &&
71
+ !isKeyframes(node.callee, references, importSources) &&
72
+ !isCssMap(node.callee, references, importSources) &&
73
+ !isXcss(node.callee, references, importSources)
74
+ ) {
75
+ return;
76
+ }
77
+
78
+ if (
79
+ isStyled(node.callee, references, importSources) &&
80
+ (node.parent.type === 'CallExpression' ||
81
+ node.parent.type === 'TaggedTemplateExpression')
82
+ ) {
83
+ /**
84
+ * If it is of the form styled(Base)({}) we don't want to lint the inner function arguments.
85
+ */
86
+ return;
87
+ }
88
+
89
+ const linter = new Linter(context, node);
90
+ linter.run();
91
+ },
92
+ };
93
+ },
94
+ });
95
+
96
+ export default rule;
@@ -0,0 +1,86 @@
1
+ import outdent from 'outdent';
2
+
3
+ const whereToGetHelp =
4
+ "If you think this should be allowed, or if you're running into issues, reach out in #help-ui-styling-standard";
5
+
6
+ export const messages = {
7
+ 'no-array-values': outdent`
8
+ Arrays are not allowed in style objects. Support is inconsistent between styling libraries.
9
+
10
+ Compose styles with the css prop instead.
11
+
12
+ ${whereToGetHelp}
13
+ `,
14
+
15
+ 'no-conditional-values': outdent`
16
+ Conditional expressions are not allowed in style objects.
17
+
18
+ Conditionally apply styles with the css prop instead.
19
+
20
+ ${whereToGetHelp}
21
+ `,
22
+
23
+ 'no-dynamic-keys': outdent`
24
+ Dynamic keys are not allowed in style objects.
25
+
26
+ ${whereToGetHelp}
27
+ `,
28
+
29
+ 'no-function-calls': outdent`
30
+ Function calls are not allowed in style objects.
31
+
32
+ ${whereToGetHelp}
33
+ `,
34
+
35
+ 'no-identifier-arguments': outdent`
36
+ Variable arguments are not allowed.
37
+
38
+ Inline the object value, or compose styles with the css prop instead.
39
+
40
+ ${whereToGetHelp}
41
+ `,
42
+
43
+ 'no-object-access': outdent`
44
+ Accessing object values is not allowed in style objects.
45
+
46
+ If you are accessing the component's props, use the style prop to apply dynamic values instead.
47
+
48
+ ${whereToGetHelp}
49
+ `,
50
+
51
+ 'no-object-references': outdent`
52
+ Referencing objects is not allowed in style objects.
53
+
54
+ Inline the object value instead.
55
+
56
+ ${whereToGetHelp}
57
+ `,
58
+
59
+ 'no-spread-elements': outdent`
60
+ Spread elements are not allowed in style objects.
61
+
62
+ Compose styles with the css prop instead.
63
+
64
+ ${whereToGetHelp}
65
+ `,
66
+
67
+ // Fallback case if a more specific message is not applicable
68
+ 'no-unsafe-values': outdent`
69
+ Unsafe values are not allowed in style objects.
70
+
71
+ ${whereToGetHelp}
72
+ `,
73
+
74
+ 'no-variables': outdent`
75
+ Variables are not allowed in style objects, unless they:
76
+
77
+ - Have a simple, static value.
78
+ - Are defined in the same file.
79
+
80
+ Use the style prop to apply dynamic values instead.
81
+
82
+ ${whereToGetHelp}
83
+ `,
84
+ };
85
+
86
+ export type MessageId = keyof typeof messages;
@@ -0,0 +1,399 @@
1
+ import type {
2
+ CallExpression,
3
+ Expression,
4
+ Identifier,
5
+ Property,
6
+ ObjectExpression,
7
+ Node,
8
+ } from 'estree-jsx';
9
+ import type { Rule } from 'eslint';
10
+
11
+ import {
12
+ getAllowedDynamicKeys,
13
+ getAllowedFunctionCalls,
14
+ isAllowListedVariable,
15
+ type AllowList,
16
+ } from '@atlaskit/eslint-utils/allowed-function-calls';
17
+ import type { Variable } from 'eslint-scope';
18
+ import { findIdentifierNode } from '@atlaskit/eslint-utils/find-identifier-node';
19
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
20
+
21
+ import type { MessageId } from './messages';
22
+
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
+ });
42
+ }
43
+
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
+ }
399
+ }