@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,437 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.Linter = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _allowedFunctionCalls = require("@atlaskit/eslint-utils/allowed-function-calls");
11
+ var _findIdentifierNode = require("@atlaskit/eslint-utils/find-identifier-node");
12
+ var _findVariable = require("@atlaskit/eslint-utils/find-variable");
13
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
16
+ function isReferenceToCssVar(variable) {
17
+ var definitions = variable.defs;
18
+ return definitions.every(function (definition) {
19
+ if (definition.type !== 'Variable') {
20
+ return false;
21
+ }
22
+ var initializer = definition.node.init;
23
+ if (!initializer) {
24
+ return false;
25
+ }
26
+ return initializer.type === 'Literal' && typeof initializer.value === 'string' && initializer.value.startsWith('--');
27
+ });
28
+ }
29
+ var Linter = exports.Linter = /*#__PURE__*/function () {
30
+ function Linter(context, baseNode) {
31
+ (0, _classCallCheck2.default)(this, Linter);
32
+ this.context = context;
33
+ this.baseNode = baseNode;
34
+ this.allowedDynamicKeys = (0, _allowedFunctionCalls.getAllowedDynamicKeys)(context.options);
35
+ this.allowedFunctionCalls = (0, _allowedFunctionCalls.getAllowedFunctionCalls)(context.options);
36
+ }
37
+
38
+ /**
39
+ * Wrapper around `context.report` to provide type checking on the message ID.
40
+ */
41
+ (0, _createClass2.default)(Linter, [{
42
+ key: "report",
43
+ value: function report(descriptor) {
44
+ this.context.report(descriptor);
45
+ }
46
+
47
+ /**
48
+ * Fallback case to lint any identifier that is not otherwise handled.
49
+ */
50
+ }, {
51
+ key: "lintUnhandledIdentifier",
52
+ value: function lintUnhandledIdentifier(identifier) {
53
+ this.report({
54
+ node: identifier,
55
+ messageId: 'no-variables'
56
+ });
57
+ }
58
+ }, {
59
+ key: "lintIdentifier",
60
+ value: function lintIdentifier(value) {
61
+ var _this = this;
62
+ if (value.name === 'undefined') {
63
+ /**
64
+ * `undefined` gets caught implicitly anyway by other checks below,
65
+ * but we are explicitly handling it here.
66
+ *
67
+ * We could potentially introduce a distinct message for it as well.
68
+ */
69
+ this.lintUnhandledIdentifier(value);
70
+ return;
71
+ }
72
+ var variable = (0, _findVariable.findVariable)({
73
+ identifier: value,
74
+ sourceCode: this.context.sourceCode
75
+ });
76
+ if (!variable) {
77
+ this.lintUnhandledIdentifier(value);
78
+ return;
79
+ }
80
+
81
+ /**
82
+ * Allows using whitelisted imported values
83
+ */
84
+ if ((0, _allowedFunctionCalls.isAllowListedVariable)({
85
+ allowList: this.allowedFunctionCalls,
86
+ variable: variable
87
+ })) {
88
+ return;
89
+ }
90
+ var definitions = variable.defs;
91
+ if (definitions.length === 0) {
92
+ this.lintUnhandledIdentifier(value);
93
+ return;
94
+ }
95
+ definitions.forEach(function (definition) {
96
+ if (definition.type === 'Variable') {
97
+ var initializer = definition.node.init;
98
+ if (!initializer) {
99
+ _this.lintUnhandledIdentifier(value);
100
+ return;
101
+ }
102
+
103
+ /**
104
+ * Variables can be used if they resolve to a literal.
105
+ */
106
+ if (initializer.type === 'Literal') {
107
+ return;
108
+ }
109
+
110
+ /**
111
+ * Not allowing tagged template expressions through variables.
112
+ */
113
+ if (initializer.type === 'TaggedTemplateExpression') {
114
+ _this.report({
115
+ node: value,
116
+ messageId: 'no-function-calls'
117
+ });
118
+ return;
119
+ }
120
+
121
+ /**
122
+ * Variables can be used if they resolve to an allowed function call.
123
+ */
124
+ if (initializer.type === 'CallExpression') {
125
+ if (_this.isAllowedFunctionCall(initializer)) {
126
+ return;
127
+ }
128
+ _this.report({
129
+ node: value,
130
+ messageId: 'no-function-calls'
131
+ });
132
+ return;
133
+ }
134
+ if (initializer.type === 'TemplateLiteral') {
135
+ var _iterator = _createForOfIteratorHelper(initializer.expressions),
136
+ _step;
137
+ try {
138
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
139
+ var expression = _step.value;
140
+ _this.lintValue(expression);
141
+ }
142
+ } catch (err) {
143
+ _iterator.e(err);
144
+ } finally {
145
+ _iterator.f();
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({
157
+ node: value,
158
+ messageId: 'no-object-references'
159
+ });
160
+ return;
161
+ }
162
+ }
163
+ _this.lintUnhandledIdentifier(value);
164
+ });
165
+ }
166
+ }, {
167
+ key: "isAllowedDynamicKey",
168
+ value: function isAllowedDynamicKey(expression) {
169
+ var identifier = (0, _findIdentifierNode.findIdentifierNode)(expression);
170
+ if (!identifier) {
171
+ return false;
172
+ }
173
+ var variable = (0, _findVariable.findVariable)({
174
+ identifier: identifier,
175
+ sourceCode: this.context.sourceCode
176
+ });
177
+ if (!variable) {
178
+ return false;
179
+ }
180
+ return isReferenceToCssVar(variable) || (0, _allowedFunctionCalls.isAllowListedVariable)({
181
+ allowList: this.allowedDynamicKeys,
182
+ variable: variable
183
+ });
184
+ }
185
+ }, {
186
+ key: "lintKey",
187
+ value: function lintKey(property) {
188
+ /**
189
+ * If it's not computed then it must be a plain string.
190
+ *
191
+ * Private identifiers cannot be computed,
192
+ * but we need the extra check for type narrowing.
193
+ */
194
+ if (!property.computed || property.key.type === 'PrivateIdentifier') {
195
+ return;
196
+ }
197
+
198
+ /**
199
+ * Some dynamic keys are explicitly allowed.
200
+ */
201
+ if (this.isAllowedDynamicKey(property.key)) {
202
+ return;
203
+ }
204
+ this.report({
205
+ node: property.key,
206
+ messageId: 'no-dynamic-keys'
207
+ });
208
+ }
209
+ }, {
210
+ key: "isAllowedFunctionCall",
211
+ value: function isAllowedFunctionCall(value) {
212
+ var identifier = (0, _findIdentifierNode.findIdentifierNode)(value);
213
+ if (!identifier) {
214
+ return false;
215
+ }
216
+ var variable = (0, _findVariable.findVariable)({
217
+ identifier: identifier,
218
+ sourceCode: this.context.sourceCode
219
+ });
220
+ if (!variable) {
221
+ return false;
222
+ }
223
+ return (0, _allowedFunctionCalls.isAllowListedVariable)({
224
+ allowList: this.allowedFunctionCalls,
225
+ variable: variable
226
+ });
227
+ }
228
+ }, {
229
+ key: "lintFunctionCall",
230
+ value: function lintFunctionCall(value) {
231
+ if (this.isAllowedFunctionCall(value)) {
232
+ return;
233
+ }
234
+ this.report({
235
+ node: value,
236
+ messageId: 'no-function-calls'
237
+ });
238
+ }
239
+ }, {
240
+ key: "lintObjectExpression",
241
+ value: function lintObjectExpression(expression) {
242
+ var _iterator2 = _createForOfIteratorHelper(expression.properties),
243
+ _step2;
244
+ try {
245
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
246
+ var property = _step2.value;
247
+ if (property.type === 'SpreadElement') {
248
+ this.report({
249
+ node: property,
250
+ messageId: 'no-spread-elements'
251
+ });
252
+ } else {
253
+ this.lintKey(property);
254
+ this.lintValue(property.value);
255
+ }
256
+ }
257
+ } catch (err) {
258
+ _iterator2.e(err);
259
+ } finally {
260
+ _iterator2.f();
261
+ }
262
+ }
263
+ }, {
264
+ key: "lintUnhandledValue",
265
+ value: function lintUnhandledValue(value) {
266
+ /**
267
+ * Default to blocking value if it isn't explictly handled.
268
+ */
269
+ this.report({
270
+ node: value,
271
+ messageId: 'no-unsafe-values'
272
+ });
273
+ }
274
+ }, {
275
+ key: "lintValue",
276
+ value: function lintValue(value) {
277
+ /**
278
+ * Literals are always allowed by this rule.
279
+ */
280
+ if (value.type === 'Literal') {
281
+ return;
282
+ }
283
+
284
+ /**
285
+ * Allow negative numbers.
286
+ */
287
+ if (value.type === 'UnaryExpression' && value.operator === '-' && value.argument.type === 'Literal') {
288
+ return;
289
+ }
290
+ if (value.type === 'TemplateLiteral') {
291
+ var _iterator3 = _createForOfIteratorHelper(value.expressions),
292
+ _step3;
293
+ try {
294
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
295
+ var expression = _step3.value;
296
+ this.lintValue(expression);
297
+ }
298
+ } catch (err) {
299
+ _iterator3.e(err);
300
+ } finally {
301
+ _iterator3.f();
302
+ }
303
+ return;
304
+ }
305
+ if (value.type === 'ConditionalExpression' || value.type === 'LogicalExpression') {
306
+ this.report({
307
+ node: value,
308
+ messageId: 'no-conditional-values'
309
+ });
310
+ return;
311
+ }
312
+ if (value.type === 'BinaryExpression') {
313
+ this.lintValue(value.left);
314
+ this.lintValue(value.right);
315
+ return;
316
+ }
317
+ if (value.type === 'ObjectExpression') {
318
+ this.lintObjectExpression(value);
319
+ return;
320
+ }
321
+ if (value.type === 'Identifier') {
322
+ this.lintIdentifier(value);
323
+ return;
324
+ }
325
+ if (value.type === 'TaggedTemplateExpression') {
326
+ this.report({
327
+ node: value,
328
+ messageId: 'no-function-calls'
329
+ });
330
+ return;
331
+ }
332
+ if (value.type === 'CallExpression') {
333
+ this.lintFunctionCall(value);
334
+ return;
335
+ }
336
+ if (value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression') {
337
+ /**
338
+ * Function values are handled by `no-dynamic-styles` instead
339
+ */
340
+ return;
341
+ }
342
+ if (value.type === 'MemberExpression') {
343
+ this.report({
344
+ node: value,
345
+ messageId: 'no-object-access'
346
+ });
347
+ return;
348
+ }
349
+ if (value.type === 'ArrayExpression') {
350
+ this.report({
351
+ node: value,
352
+ messageId: 'no-array-values'
353
+ });
354
+ return;
355
+ }
356
+ this.lintUnhandledValue(value);
357
+ }
358
+ }, {
359
+ key: "lintArgument",
360
+ value: function lintArgument(argument) {
361
+ if (argument.type === 'ArrowFunctionExpression') {
362
+ if (argument.body.type === 'ObjectExpression') {
363
+ // Something in this form:
364
+ //
365
+ // const styles = styled.div(
366
+ // (props) => ({ color: props.height }),
367
+ // );
368
+ this.lintArgument(argument.body);
369
+ }
370
+ return;
371
+ }
372
+ if (argument.type === 'ArrayExpression') {
373
+ var _iterator4 = _createForOfIteratorHelper(argument.elements),
374
+ _step4;
375
+ try {
376
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
377
+ var element = _step4.value;
378
+ if (element === null) {
379
+ continue;
380
+ }
381
+ this.lintArgument(element);
382
+ }
383
+ } catch (err) {
384
+ _iterator4.e(err);
385
+ } finally {
386
+ _iterator4.f();
387
+ }
388
+ return;
389
+ }
390
+ if (argument.type === 'ObjectExpression') {
391
+ this.lintObjectExpression(argument);
392
+ return;
393
+ }
394
+ if (argument.type === 'CallExpression') {
395
+ this.lintFunctionCall(argument);
396
+ return;
397
+ }
398
+ if (argument.type === 'ConditionalExpression' || argument.type === 'LogicalExpression') {
399
+ this.report({
400
+ node: argument,
401
+ messageId: 'no-conditional-values'
402
+ });
403
+ return;
404
+ }
405
+ if (argument.type === 'Identifier' || argument.type === 'MemberExpression') {
406
+ this.report({
407
+ node: argument,
408
+ messageId: 'no-identifier-arguments'
409
+ });
410
+ return;
411
+ }
412
+ this.lintUnhandledValue(argument);
413
+ }
414
+ }, {
415
+ key: "lintArguments",
416
+ value: function lintArguments(args) {
417
+ var _iterator5 = _createForOfIteratorHelper(args),
418
+ _step5;
419
+ try {
420
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
421
+ var argument = _step5.value;
422
+ this.lintArgument(argument);
423
+ }
424
+ } catch (err) {
425
+ _iterator5.e(err);
426
+ } finally {
427
+ _iterator5.f();
428
+ }
429
+ }
430
+ }, {
431
+ key: "run",
432
+ value: function run() {
433
+ this.lintArguments(this.baseNode.arguments);
434
+ }
435
+ }]);
436
+ return Linter;
437
+ }();
@@ -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,80 @@
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
+ const 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 const 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,
55
+ type: 'problem',
56
+ schema
57
+ },
58
+ create(context) {
59
+ const importSources = getImportSources(context);
60
+ return {
61
+ CallExpression(node) {
62
+ const {
63
+ references
64
+ } = context.sourceCode.getScope(node);
65
+ 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)) {
66
+ return;
67
+ }
68
+ if (isStyled(node.callee, references, importSources) && (node.parent.type === 'CallExpression' || node.parent.type === 'TaggedTemplateExpression')) {
69
+ /**
70
+ * If it is of the form styled(Base)({}) we don't want to lint the inner function arguments.
71
+ */
72
+ return;
73
+ }
74
+ const linter = new Linter(context, node);
75
+ linter.run();
76
+ }
77
+ };
78
+ }
79
+ });
80
+ export default rule;
@@ -0,0 +1,72 @@
1
+ import outdent from 'outdent';
2
+ const whereToGetHelp = "If you think this should be allowed, or if you're running into issues, reach out in #help-ui-styling-standard";
3
+ export const messages = {
4
+ 'no-array-values': outdent`
5
+ Arrays are not allowed in style objects. Support is inconsistent between styling libraries.
6
+
7
+ Compose styles with the css prop instead.
8
+
9
+ ${whereToGetHelp}
10
+ `,
11
+ 'no-conditional-values': outdent`
12
+ Conditional expressions are not allowed in style objects.
13
+
14
+ Conditionally apply styles with the css prop instead.
15
+
16
+ ${whereToGetHelp}
17
+ `,
18
+ 'no-dynamic-keys': outdent`
19
+ Dynamic keys are not allowed in style objects.
20
+
21
+ ${whereToGetHelp}
22
+ `,
23
+ 'no-function-calls': outdent`
24
+ Function calls are not allowed in style objects.
25
+
26
+ ${whereToGetHelp}
27
+ `,
28
+ 'no-identifier-arguments': outdent`
29
+ Variable arguments are not allowed.
30
+
31
+ Inline the object value, or compose styles with the css prop instead.
32
+
33
+ ${whereToGetHelp}
34
+ `,
35
+ 'no-object-access': outdent`
36
+ Accessing object values is not allowed in style objects.
37
+
38
+ If you are accessing the component's props, use the style prop to apply dynamic values instead.
39
+
40
+ ${whereToGetHelp}
41
+ `,
42
+ 'no-object-references': outdent`
43
+ Referencing objects is not allowed in style objects.
44
+
45
+ Inline the object value instead.
46
+
47
+ ${whereToGetHelp}
48
+ `,
49
+ 'no-spread-elements': outdent`
50
+ Spread elements are not allowed in style objects.
51
+
52
+ Compose styles with the css prop instead.
53
+
54
+ ${whereToGetHelp}
55
+ `,
56
+ // Fallback case if a more specific message is not applicable
57
+ 'no-unsafe-values': outdent`
58
+ Unsafe values are not allowed in style objects.
59
+
60
+ ${whereToGetHelp}
61
+ `,
62
+ 'no-variables': outdent`
63
+ Variables are not allowed in style objects, unless they:
64
+
65
+ - Have a simple, static value.
66
+ - Are defined in the same file.
67
+
68
+ Use the style prop to apply dynamic values instead.
69
+
70
+ ${whereToGetHelp}
71
+ `
72
+ };