@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,350 @@
1
+ import { getAllowedDynamicKeys, getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
2
+ import { findIdentifierNode } from '@atlaskit/eslint-utils/find-identifier-node';
3
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
4
+ function isReferenceToCssVar(variable) {
5
+ const definitions = variable.defs;
6
+ return definitions.every(definition => {
7
+ if (definition.type !== 'Variable') {
8
+ return false;
9
+ }
10
+ const initializer = definition.node.init;
11
+ if (!initializer) {
12
+ return false;
13
+ }
14
+ return initializer.type === 'Literal' && typeof initializer.value === 'string' && initializer.value.startsWith('--');
15
+ });
16
+ }
17
+ export class Linter {
18
+ constructor(context, baseNode) {
19
+ this.context = context;
20
+ this.baseNode = baseNode;
21
+ this.allowedDynamicKeys = getAllowedDynamicKeys(context.options);
22
+ this.allowedFunctionCalls = getAllowedFunctionCalls(context.options);
23
+ }
24
+
25
+ /**
26
+ * Wrapper around `context.report` to provide type checking on the message ID.
27
+ */
28
+ report(descriptor) {
29
+ this.context.report(descriptor);
30
+ }
31
+
32
+ /**
33
+ * Fallback case to lint any identifier that is not otherwise handled.
34
+ */
35
+ lintUnhandledIdentifier(identifier) {
36
+ this.report({
37
+ node: identifier,
38
+ messageId: 'no-variables'
39
+ });
40
+ }
41
+ lintIdentifier(value) {
42
+ if (value.name === 'undefined') {
43
+ /**
44
+ * `undefined` gets caught implicitly anyway by other checks below,
45
+ * but we are explicitly handling it here.
46
+ *
47
+ * We could potentially introduce a distinct message for it as well.
48
+ */
49
+ this.lintUnhandledIdentifier(value);
50
+ return;
51
+ }
52
+ const variable = findVariable({
53
+ identifier: value,
54
+ sourceCode: this.context.sourceCode
55
+ });
56
+ if (!variable) {
57
+ this.lintUnhandledIdentifier(value);
58
+ return;
59
+ }
60
+
61
+ /**
62
+ * Allows using whitelisted imported values
63
+ */
64
+ if (isAllowListedVariable({
65
+ allowList: this.allowedFunctionCalls,
66
+ variable
67
+ })) {
68
+ return;
69
+ }
70
+ const definitions = variable.defs;
71
+ if (definitions.length === 0) {
72
+ this.lintUnhandledIdentifier(value);
73
+ return;
74
+ }
75
+ definitions.forEach(definition => {
76
+ if (definition.type === 'Variable') {
77
+ const initializer = definition.node.init;
78
+ if (!initializer) {
79
+ this.lintUnhandledIdentifier(value);
80
+ return;
81
+ }
82
+
83
+ /**
84
+ * Variables can be used if they resolve to a literal.
85
+ */
86
+ if (initializer.type === 'Literal') {
87
+ return;
88
+ }
89
+
90
+ /**
91
+ * Not allowing tagged template expressions through variables.
92
+ */
93
+ if (initializer.type === 'TaggedTemplateExpression') {
94
+ this.report({
95
+ node: value,
96
+ messageId: 'no-function-calls'
97
+ });
98
+ return;
99
+ }
100
+
101
+ /**
102
+ * Variables can be used if they resolve to an allowed function call.
103
+ */
104
+ if (initializer.type === 'CallExpression') {
105
+ if (this.isAllowedFunctionCall(initializer)) {
106
+ return;
107
+ }
108
+ this.report({
109
+ node: value,
110
+ messageId: 'no-function-calls'
111
+ });
112
+ return;
113
+ }
114
+ if (initializer.type === 'TemplateLiteral') {
115
+ for (const expression of initializer.expressions) {
116
+ this.lintValue(expression);
117
+ }
118
+ return;
119
+ }
120
+
121
+ /**
122
+ * This branch is only here for clarity.
123
+ *
124
+ * We could fallback to `no-variables` for it too.
125
+ */
126
+ if (initializer.type === 'ObjectExpression') {
127
+ this.report({
128
+ node: value,
129
+ messageId: 'no-object-references'
130
+ });
131
+ return;
132
+ }
133
+ }
134
+ this.lintUnhandledIdentifier(value);
135
+ });
136
+ }
137
+ isAllowedDynamicKey(expression) {
138
+ const identifier = findIdentifierNode(expression);
139
+ if (!identifier) {
140
+ return false;
141
+ }
142
+ const variable = findVariable({
143
+ identifier,
144
+ sourceCode: this.context.sourceCode
145
+ });
146
+ if (!variable) {
147
+ return false;
148
+ }
149
+ return isReferenceToCssVar(variable) || isAllowListedVariable({
150
+ allowList: this.allowedDynamicKeys,
151
+ variable
152
+ });
153
+ }
154
+ lintKey(property) {
155
+ /**
156
+ * If it's not computed then it must be a plain string.
157
+ *
158
+ * Private identifiers cannot be computed,
159
+ * but we need the extra check for type narrowing.
160
+ */
161
+ if (!property.computed || property.key.type === 'PrivateIdentifier') {
162
+ return;
163
+ }
164
+
165
+ /**
166
+ * Some dynamic keys are explicitly allowed.
167
+ */
168
+ if (this.isAllowedDynamicKey(property.key)) {
169
+ return;
170
+ }
171
+ this.report({
172
+ node: property.key,
173
+ messageId: 'no-dynamic-keys'
174
+ });
175
+ }
176
+ isAllowedFunctionCall(value) {
177
+ const identifier = findIdentifierNode(value);
178
+ if (!identifier) {
179
+ return false;
180
+ }
181
+ const variable = findVariable({
182
+ identifier,
183
+ sourceCode: this.context.sourceCode
184
+ });
185
+ if (!variable) {
186
+ return false;
187
+ }
188
+ return isAllowListedVariable({
189
+ allowList: this.allowedFunctionCalls,
190
+ variable
191
+ });
192
+ }
193
+ lintFunctionCall(value) {
194
+ if (this.isAllowedFunctionCall(value)) {
195
+ return;
196
+ }
197
+ this.report({
198
+ node: value,
199
+ messageId: 'no-function-calls'
200
+ });
201
+ }
202
+ lintObjectExpression(expression) {
203
+ for (const property of expression.properties) {
204
+ if (property.type === 'SpreadElement') {
205
+ this.report({
206
+ node: property,
207
+ messageId: 'no-spread-elements'
208
+ });
209
+ } else {
210
+ this.lintKey(property);
211
+ this.lintValue(property.value);
212
+ }
213
+ }
214
+ }
215
+ lintUnhandledValue(value) {
216
+ /**
217
+ * Default to blocking value if it isn't explictly handled.
218
+ */
219
+ this.report({
220
+ node: value,
221
+ messageId: 'no-unsafe-values'
222
+ });
223
+ }
224
+ lintValue(value) {
225
+ /**
226
+ * Literals are always allowed by this rule.
227
+ */
228
+ if (value.type === 'Literal') {
229
+ return;
230
+ }
231
+
232
+ /**
233
+ * Allow negative numbers.
234
+ */
235
+ if (value.type === 'UnaryExpression' && value.operator === '-' && value.argument.type === 'Literal') {
236
+ return;
237
+ }
238
+ if (value.type === 'TemplateLiteral') {
239
+ for (const expression of value.expressions) {
240
+ this.lintValue(expression);
241
+ }
242
+ return;
243
+ }
244
+ if (value.type === 'ConditionalExpression' || value.type === 'LogicalExpression') {
245
+ this.report({
246
+ node: value,
247
+ messageId: 'no-conditional-values'
248
+ });
249
+ return;
250
+ }
251
+ if (value.type === 'BinaryExpression') {
252
+ this.lintValue(value.left);
253
+ this.lintValue(value.right);
254
+ return;
255
+ }
256
+ if (value.type === 'ObjectExpression') {
257
+ this.lintObjectExpression(value);
258
+ return;
259
+ }
260
+ if (value.type === 'Identifier') {
261
+ this.lintIdentifier(value);
262
+ return;
263
+ }
264
+ if (value.type === 'TaggedTemplateExpression') {
265
+ this.report({
266
+ node: value,
267
+ messageId: 'no-function-calls'
268
+ });
269
+ return;
270
+ }
271
+ if (value.type === 'CallExpression') {
272
+ this.lintFunctionCall(value);
273
+ return;
274
+ }
275
+ if (value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression') {
276
+ /**
277
+ * Function values are handled by `no-dynamic-styles` instead
278
+ */
279
+ return;
280
+ }
281
+ if (value.type === 'MemberExpression') {
282
+ this.report({
283
+ node: value,
284
+ messageId: 'no-object-access'
285
+ });
286
+ return;
287
+ }
288
+ if (value.type === 'ArrayExpression') {
289
+ this.report({
290
+ node: value,
291
+ messageId: 'no-array-values'
292
+ });
293
+ return;
294
+ }
295
+ this.lintUnhandledValue(value);
296
+ }
297
+ lintArgument(argument) {
298
+ if (argument.type === 'ArrowFunctionExpression') {
299
+ if (argument.body.type === 'ObjectExpression') {
300
+ // Something in this form:
301
+ //
302
+ // const styles = styled.div(
303
+ // (props) => ({ color: props.height }),
304
+ // );
305
+ this.lintArgument(argument.body);
306
+ }
307
+ return;
308
+ }
309
+ if (argument.type === 'ArrayExpression') {
310
+ for (const element of argument.elements) {
311
+ if (element === null) {
312
+ continue;
313
+ }
314
+ this.lintArgument(element);
315
+ }
316
+ return;
317
+ }
318
+ if (argument.type === 'ObjectExpression') {
319
+ this.lintObjectExpression(argument);
320
+ return;
321
+ }
322
+ if (argument.type === 'CallExpression') {
323
+ this.lintFunctionCall(argument);
324
+ return;
325
+ }
326
+ if (argument.type === 'ConditionalExpression' || argument.type === 'LogicalExpression') {
327
+ this.report({
328
+ node: argument,
329
+ messageId: 'no-conditional-values'
330
+ });
331
+ return;
332
+ }
333
+ if (argument.type === 'Identifier' || argument.type === 'MemberExpression') {
334
+ this.report({
335
+ node: argument,
336
+ messageId: 'no-identifier-arguments'
337
+ });
338
+ return;
339
+ }
340
+ this.lintUnhandledValue(argument);
341
+ }
342
+ lintArguments(args) {
343
+ for (const argument of args) {
344
+ this.lintArgument(argument);
345
+ }
346
+ }
347
+ run() {
348
+ this.lintArguments(this.baseNode.arguments);
349
+ }
350
+ }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
3
+ * @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
18
19
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
19
20
  excludeReactComponents: true
20
21
  }],
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
3
+ * @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
18
19
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
19
20
  excludeReactComponents: true
20
21
  }],
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
3
+ * @codegen <<SignedSource::edaedcad824fed1e108f90d909287584>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -12,6 +12,7 @@ import noContainerQueries from './no-container-queries';
12
12
  import noDynamicStyles from './no-dynamic-styles';
13
13
  import noImportantStyles from './no-important-styles';
14
14
  import noNestedSelectors from './no-nested-selectors';
15
+ import noUnsafeValues from './no-unsafe-values';
15
16
  export default {
16
17
  'convert-props-syntax': convertPropsSyntax,
17
18
  'enforce-style-prop': enforceStyleProp,
@@ -21,5 +22,6 @@ export default {
21
22
  'no-container-queries': noContainerQueries,
22
23
  'no-dynamic-styles': noDynamicStyles,
23
24
  'no-important-styles': noImportantStyles,
24
- 'no-nested-selectors': noNestedSelectors
25
+ 'no-nested-selectors': noNestedSelectors,
26
+ 'no-unsafe-values': noUnsafeValues
25
27
  };
@@ -0,0 +1,79 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import { isCss, getImportSources, isStyled, isKeyframes, isCssMap, isXcss } from '@atlaskit/eslint-utils/is-supported-import';
3
+ import { Linter } from './utils';
4
+ import { messages } from './messages';
5
+ var schema = [{
6
+ type: 'object',
7
+ additionalProperties: false,
8
+ properties: {
9
+ importSources: {
10
+ type: 'array',
11
+ items: {
12
+ type: 'string'
13
+ },
14
+ uniqueItems: true
15
+ },
16
+ allowedFunctionCalls: {
17
+ type: 'array',
18
+ items: {
19
+ type: 'array',
20
+ minItems: 2,
21
+ maxItems: 2,
22
+ items: [{
23
+ type: 'string'
24
+ }, {
25
+ type: 'string'
26
+ }]
27
+ },
28
+ uniqueItems: true
29
+ },
30
+ allowedDynamicKeys: {
31
+ type: 'array',
32
+ items: {
33
+ type: 'array',
34
+ minItems: 2,
35
+ maxItems: 2,
36
+ items: [{
37
+ type: 'string'
38
+ }, {
39
+ type: 'string'
40
+ }]
41
+ },
42
+ uniqueItems: true
43
+ }
44
+ }
45
+ }];
46
+ export var rule = createLintRule({
47
+ meta: {
48
+ name: 'no-unsafe-values',
49
+ docs: {
50
+ description: 'Disallows styles that are difficult/impossible to statically anaylze.',
51
+ recommended: true,
52
+ severity: 'warn'
53
+ },
54
+ messages: messages,
55
+ type: 'problem',
56
+ schema: schema
57
+ },
58
+ create: function create(context) {
59
+ var importSources = getImportSources(context);
60
+ return {
61
+ CallExpression: function CallExpression(node) {
62
+ var _context$sourceCode$g = context.sourceCode.getScope(node),
63
+ references = _context$sourceCode$g.references;
64
+ if (!isCss(node.callee, references, importSources) && !isStyled(node.callee, references, importSources) && !isKeyframes(node.callee, references, importSources) && !isCssMap(node.callee, references, importSources) && !isXcss(node.callee, references, importSources)) {
65
+ return;
66
+ }
67
+ if (isStyled(node.callee, references, importSources) && (node.parent.type === 'CallExpression' || node.parent.type === 'TaggedTemplateExpression')) {
68
+ /**
69
+ * If it is of the form styled(Base)({}) we don't want to lint the inner function arguments.
70
+ */
71
+ return;
72
+ }
73
+ var linter = new Linter(context, node);
74
+ linter.run();
75
+ }
76
+ };
77
+ }
78
+ });
79
+ export default rule;
@@ -0,0 +1,17 @@
1
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
2
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject10;
3
+ import outdent from 'outdent';
4
+ var whereToGetHelp = "If you think this should be allowed, or if you're running into issues, reach out in #help-ui-styling-standard";
5
+ export var messages = {
6
+ 'no-array-values': outdent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Arrays are not allowed in style objects. Support is inconsistent between styling libraries.\n\n Compose styles with the css prop instead.\n\n ", "\n "])), whereToGetHelp),
7
+ 'no-conditional-values': outdent(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n Conditional expressions are not allowed in style objects.\n\n Conditionally apply styles with the css prop instead.\n\n ", "\n "])), whereToGetHelp),
8
+ 'no-dynamic-keys': outdent(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n Dynamic keys are not allowed in style objects.\n\n ", "\n "])), whereToGetHelp),
9
+ 'no-function-calls': outdent(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n Function calls are not allowed in style objects.\n\n ", "\n "])), whereToGetHelp),
10
+ 'no-identifier-arguments': outdent(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n Variable arguments are not allowed.\n\n Inline the object value, or compose styles with the css prop instead.\n\n ", "\n "])), whereToGetHelp),
11
+ 'no-object-access': outdent(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n Accessing object values is not allowed in style objects.\n\n If you are accessing the component's props, use the style prop to apply dynamic values instead.\n\n ", "\n "])), whereToGetHelp),
12
+ 'no-object-references': outdent(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["\n Referencing objects is not allowed in style objects.\n\n Inline the object value instead.\n\n ", "\n "])), whereToGetHelp),
13
+ 'no-spread-elements': outdent(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral(["\n Spread elements are not allowed in style objects.\n\n Compose styles with the css prop instead.\n\n ", "\n "])), whereToGetHelp),
14
+ // Fallback case if a more specific message is not applicable
15
+ 'no-unsafe-values': outdent(_templateObject9 || (_templateObject9 = _taggedTemplateLiteral(["\n Unsafe values are not allowed in style objects.\n\n ", "\n "])), whereToGetHelp),
16
+ 'no-variables': outdent(_templateObject10 || (_templateObject10 = _taggedTemplateLiteral(["\n Variables are not allowed in style objects, unless they:\n\n - Have a simple, static value.\n - Are defined in the same file.\n\n Use the style prop to apply dynamic values instead.\n\n ", "\n "])), whereToGetHelp)
17
+ };