@atlaskit/eslint-plugin-design-system 8.37.3 → 9.0.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 (78) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +1 -0
  3. package/constellation/consistent-css-prop-usage/usage.mdx +3 -19
  4. package/constellation/index/usage.mdx +1 -0
  5. package/constellation/use-tokens-space/usage.mdx +30 -0
  6. package/dist/cjs/ast-nodes/object-entry.js +35 -0
  7. package/dist/cjs/ast-nodes/object.js +12 -0
  8. package/dist/cjs/presets/all.codegen.js +2 -1
  9. package/dist/cjs/rules/consistent-css-prop-usage/index.js +53 -85
  10. package/dist/cjs/rules/index.codegen.js +3 -1
  11. package/dist/cjs/rules/use-tokens-space/index.js +54 -0
  12. package/dist/cjs/rules/use-tokens-space/transformers/index.js +12 -0
  13. package/dist/cjs/rules/use-tokens-space/transformers/style-property/index.js +130 -0
  14. package/dist/cjs/rules/use-tokens-space/transformers/style-property/style-map.js +80 -0
  15. package/dist/cjs/rules/use-tokens-space/transformers/style-property/supported.js +14 -0
  16. package/dist/cjs/rules/use-tokens-space/transformers/style-property/upsert-import-declaration.js +37 -0
  17. package/dist/cjs/rules/use-tokens-space/utils/index.js +12 -0
  18. package/dist/cjs/rules/use-tokens-space/utils/is-string-or-number.js +9 -0
  19. package/dist/cjs/rules/utils/is-supported-import.js +12 -3
  20. package/dist/es2019/ast-nodes/object-entry.js +37 -3
  21. package/dist/es2019/ast-nodes/object.js +12 -0
  22. package/dist/es2019/presets/all.codegen.js +2 -1
  23. package/dist/es2019/rules/consistent-css-prop-usage/index.js +21 -48
  24. package/dist/es2019/rules/index.codegen.js +3 -1
  25. package/dist/es2019/rules/use-tokens-space/index.js +38 -0
  26. package/dist/es2019/rules/use-tokens-space/transformers/index.js +1 -0
  27. package/dist/es2019/rules/use-tokens-space/transformers/style-property/index.js +121 -0
  28. package/dist/es2019/rules/use-tokens-space/transformers/style-property/style-map.js +152 -0
  29. package/dist/es2019/rules/use-tokens-space/transformers/style-property/supported.js +8 -0
  30. package/dist/es2019/rules/use-tokens-space/transformers/style-property/upsert-import-declaration.js +30 -0
  31. package/dist/es2019/rules/use-tokens-space/utils/index.js +1 -0
  32. package/dist/es2019/rules/use-tokens-space/utils/is-string-or-number.js +3 -0
  33. package/dist/es2019/rules/utils/is-supported-import.js +15 -2
  34. package/dist/esm/ast-nodes/object-entry.js +37 -3
  35. package/dist/esm/ast-nodes/object.js +12 -0
  36. package/dist/esm/presets/all.codegen.js +2 -1
  37. package/dist/esm/rules/consistent-css-prop-usage/index.js +53 -85
  38. package/dist/esm/rules/index.codegen.js +3 -1
  39. package/dist/esm/rules/use-tokens-space/index.js +48 -0
  40. package/dist/esm/rules/use-tokens-space/transformers/index.js +1 -0
  41. package/dist/esm/rules/use-tokens-space/transformers/style-property/index.js +120 -0
  42. package/dist/esm/rules/use-tokens-space/transformers/style-property/style-map.js +73 -0
  43. package/dist/esm/rules/use-tokens-space/transformers/style-property/supported.js +8 -0
  44. package/dist/esm/rules/use-tokens-space/transformers/style-property/upsert-import-declaration.js +29 -0
  45. package/dist/esm/rules/use-tokens-space/utils/index.js +1 -0
  46. package/dist/esm/rules/use-tokens-space/utils/is-string-or-number.js +3 -0
  47. package/dist/esm/rules/utils/is-supported-import.js +11 -2
  48. package/dist/types/ast-nodes/object-entry.d.ts +12 -2
  49. package/dist/types/ast-nodes/object.d.ts +1 -0
  50. package/dist/types/index.codegen.d.ts +1 -0
  51. package/dist/types/presets/all.codegen.d.ts +2 -1
  52. package/dist/types/rules/consistent-css-prop-usage/types.d.ts +1 -2
  53. package/dist/types/rules/index.codegen.d.ts +1 -0
  54. package/dist/types/rules/use-tokens-space/index.d.ts +3 -0
  55. package/dist/types/rules/use-tokens-space/transformers/index.d.ts +1 -0
  56. package/dist/types/rules/use-tokens-space/transformers/style-property/index.d.ts +26 -0
  57. package/dist/types/rules/use-tokens-space/transformers/style-property/style-map.d.ts +7 -0
  58. package/dist/types/rules/use-tokens-space/transformers/style-property/supported.d.ts +6 -0
  59. package/dist/types/rules/use-tokens-space/transformers/style-property/upsert-import-declaration.d.ts +14 -0
  60. package/dist/types/rules/use-tokens-space/utils/index.d.ts +1 -0
  61. package/dist/types/rules/use-tokens-space/utils/is-string-or-number.d.ts +1 -0
  62. package/dist/types/rules/utils/is-supported-import.d.ts +5 -4
  63. package/dist/types-ts4.5/ast-nodes/object-entry.d.ts +12 -2
  64. package/dist/types-ts4.5/ast-nodes/object.d.ts +1 -0
  65. package/dist/types-ts4.5/index.codegen.d.ts +1 -0
  66. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  67. package/dist/types-ts4.5/rules/consistent-css-prop-usage/types.d.ts +1 -2
  68. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  69. package/dist/types-ts4.5/rules/use-tokens-space/index.d.ts +3 -0
  70. package/dist/types-ts4.5/rules/use-tokens-space/transformers/index.d.ts +1 -0
  71. package/dist/types-ts4.5/rules/use-tokens-space/transformers/style-property/index.d.ts +26 -0
  72. package/dist/types-ts4.5/rules/use-tokens-space/transformers/style-property/style-map.d.ts +7 -0
  73. package/dist/types-ts4.5/rules/use-tokens-space/transformers/style-property/supported.d.ts +6 -0
  74. package/dist/types-ts4.5/rules/use-tokens-space/transformers/style-property/upsert-import-declaration.d.ts +14 -0
  75. package/dist/types-ts4.5/rules/use-tokens-space/utils/index.d.ts +1 -0
  76. package/dist/types-ts4.5/rules/use-tokens-space/utils/is-string-or-number.d.ts +1 -0
  77. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +5 -4
  78. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # @atlaskit/eslint-plugin-design-system
2
2
 
3
+ ## 9.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#83454](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/83454) [`be8b7ad6ff8e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/be8b7ad6ff8e) - Remove name autofixer from `consistent-css-prop-usage`. Variables for css / xcss / cssMap functions will no longer be required to end with "Styles".
8
+
9
+ [BREAKING] Some rule options have been changed:
10
+
11
+ - `fixNamesOnly` and `autoFixNames` have been removed, as there is no longer an autofixer that enforces variable names.
12
+ - If you use `fixNamesOnly: true`, we recommend switching to using `autoFix: false`.
13
+ - Users of the `autoFixNames` option should remove this from their configuration.
14
+ - `autoFix` option has been added. This controls whether the remaining autofixers should run or not (e.g. hoisting `css` function calls, wrapping objects in `css` function calls), and is `true` by default.
15
+
16
+ ## 8.38.0
17
+
18
+ ### Minor Changes
19
+
20
+ - [#74844](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/74844) [`7c7b8a771792`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/7c7b8a771792) - Created `use-tokens-space` lint rule to replace `ensure-design-token-usage` rule for space values.
21
+
3
22
  ## 8.37.3
4
23
 
5
24
  ### Patch Changes
package/README.md CHANGED
@@ -78,6 +78,7 @@ module.exports = {
78
78
  | <a href="./src/rules/use-href-in-link-item/README.md">use-href-in-link-item</a> | Inform developers of eventual requirement of `href` prop in `LinkItem` component. Elements with a `link` role require an `href` attribute for users to properly navigate, particularly those using assistive technologies. If no valid `href` is required for your use case, consider using a `ButtonItem` instead. | Yes | Yes | Yes |
79
79
  | <a href="./src/rules/use-primitives/README.md">use-primitives</a> | Encourage the usage of primitives components. | | Yes | Yes |
80
80
  | <a href="./src/rules/use-primitives-text/README.md">use-primitives-text</a> | Encourage the usage of text components. | | Yes | Yes |
81
+ | <a href="./src/rules/use-tokens-space/README.md">use-tokens-space</a> | Enforces usage of space design tokens rather than hard-coded values. | | Yes | Yes |
81
82
  | <a href="./src/rules/use-tokens-typography/README.md">use-tokens-typography</a> | Enforces usage of design tokens for typography properties rather than hard-coded values. | | Yes | Yes |
82
83
  | <a href="./src/rules/use-visually-hidden/README.md">use-visually-hidden</a> | Enforce usage of the visually hidden component. | Yes | Yes | |
83
84
 
@@ -16,7 +16,6 @@ This rule checks for the following cases:
16
16
  - Styles must be defined in the same file as their usage, and not be imported.
17
17
  - Styles should not contain spread operators (e.g. `css({ ...spreadStyles })`).
18
18
  - Styles must all be defined at the top of the file, or at the bottom of the file.
19
- - Styles must be in a variable whose name ends in `styles` (or `Styles`).
20
19
 
21
20
  This rule also has an autofixer that enforces and fixes the code (where practical) to meet the above requirements.
22
21
 
@@ -81,7 +80,7 @@ function Button({ children }) {
81
80
 
82
81
  ### Correct
83
82
 
84
- **Using the css() function to create a style object that follows the naming convention (ends in Styles) and passing it as a variable into the css={...} JSX attribute.**
83
+ **Using the css() function to create a style object and passing it as a variable into the css={...} JSX attribute.**
85
84
 
86
85
  With the following options turned on:
87
86
 
@@ -181,23 +180,8 @@ Whether to exclude `css` attributes of React components from being affected by t
181
180
 
182
181
  This is `false` by default.
183
182
 
184
- ### autoFixNames
183
+ ### autoFix
185
184
 
186
- When set to `true`, this rule will _turn on_ an autofixer that adds `Styles` at the end of existing style variables.
187
-
188
- ```tsx
189
- // vvvvv will be renamed to `myCssStyles`
190
- const myCss = { color: 'blue' };
191
- ```
192
-
193
- Note that when hoisting styles, the name of the newly created style variable follows its own naming scheme (`styles`, `styles_1`, `styles_2`, ...) and is not affected by the value of `autoFixNames`.
185
+ When set to `true`, this rule will turn on the autofixer. Set this to `false` if you do not want the autofixers to run.
194
186
 
195
187
  This is `true` by default.
196
-
197
- ### fixNamesOnly
198
-
199
- When set to `true`, this rule will _turn off_ all autofixers other than the autofixer adding `Styles` to the end of existing style variables (which is controlled by `autoFixNames`). In other words, styles will no longer be hoisted and styles will no longer be wrapped in a `css`/`xcss` function call.
200
-
201
- This is `false` by default.
202
-
203
- If `autoFixNames` is `false` and `fixNamesOnly` is `true`, all autofixers will be turned off.
@@ -40,6 +40,7 @@ This plugin contains rules that should be used when working with the [Atlassian
40
40
  | <a href="use-href-in-link-item/usage">use-href-in-link-item</a> | Inform developers of eventual requirement of `href` prop in `LinkItem` component. Elements with a `link` role require an `href` attribute for users to properly navigate, particularly those using assistive technologies. If no valid `href` is required for your use case, consider using a `ButtonItem` instead. | Yes | Yes | Yes |
41
41
  | <a href="use-primitives/usage">use-primitives</a> | Encourage the usage of primitives components. | | Yes | Yes |
42
42
  | <a href="use-primitives-text/usage">use-primitives-text</a> | Encourage the usage of text components. | | Yes | Yes |
43
+ | <a href="use-tokens-space/usage">use-tokens-space</a> | Enforces usage of space design tokens rather than hard-coded values. | | Yes | Yes |
43
44
  | <a href="use-tokens-typography/usage">use-tokens-typography</a> | Enforces usage of design tokens for typography properties rather than hard-coded values. | | Yes | Yes |
44
45
  | <a href="use-visually-hidden/usage">use-visually-hidden</a> | Enforce usage of the visually hidden component. | Yes | Yes | |
45
46
 
@@ -0,0 +1,30 @@
1
+ # use-tokens-space
2
+
3
+ ## Examples
4
+
5
+ This rule marks code as violations when a space token should be used. It will auto-fix values that can be mapped 1:1 with an ADS space token. Values that can't be mapped to a token will still be reported, however no auto-fix will happen.
6
+
7
+ ### Incorrect
8
+
9
+ ```jsx
10
+ const someStyles = css({
11
+ padding: '8px';
12
+ ^^^^^
13
+ })
14
+ ```
15
+
16
+ ### Correct
17
+
18
+ ```jsx
19
+ import { token } from '@atlaskit/tokens'
20
+
21
+ const someStyles = css({
22
+ padding: token('space.100');
23
+ })
24
+ ```
25
+
26
+ See the list of available space tokens on the [ADS website](https://atlassian.design/foundations/spacing#space-tokens).
27
+
28
+ For Atlassians:
29
+
30
+ - See the [Token Migration Guide](https://go.atlassian.com/space-token-migration) for instructions on how to migrate to tokens when a suggestion is not provided by the ESLint rule.
@@ -5,7 +5,42 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.ObjectEntry = void 0;
7
7
  var _eslintCodemodUtils = require("eslint-codemod-utils");
8
+ /* eslint-disable @repo/internal/react/require-jsdoc */
9
+
8
10
  var ObjectEntry = exports.ObjectEntry = {
11
+ getProperty: function getProperty(node) {
12
+ if ((0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Identifier')) {
13
+ return {
14
+ type: 'Identifier',
15
+ value: node.key.name
16
+ };
17
+ }
18
+ if ((0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Literal') && node.key.value) {
19
+ return {
20
+ type: 'Literal',
21
+ value: node.key.value.toString()
22
+ };
23
+ }
24
+ return {
25
+ type: undefined,
26
+ value: undefined
27
+ };
28
+ },
29
+ getValue: function getValue(node) {
30
+ // The value is a number, like `-3`
31
+ if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'UnaryExpression') && (0, _eslintCodemodUtils.isNodeOfType)(node.value.argument, 'Literal') && node.value.argument.raw) {
32
+ if (node.value.operator === '-') {
33
+ return -1 * Number.parseInt(node.value.argument.raw);
34
+ }
35
+ return Number.parseInt(node.value.argument.raw);
36
+ }
37
+
38
+ // The value is a string, like `'4px'`
39
+ if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'Literal') && node.value.value) {
40
+ return node.value.value;
41
+ }
42
+ return undefined;
43
+ },
9
44
  deleteEntry: function deleteEntry(node, context, fixer) {
10
45
  var _lastToken;
11
46
  // context.getSourceCode() is deprecated in favour of context.sourceCode, however this returns undefined for some reason
@@ -105,5 +105,17 @@ var ASTObjectExpression = exports.Object = {
105
105
  key: (0, _eslintCodemodUtils.identifier)(key),
106
106
  value: (0, _eslintCodemodUtils.literal)(value)
107
107
  }).toString(), ", "));
108
+ },
109
+ recurse: function recurse(node, callback) {
110
+ node.properties.forEach(function (entry) {
111
+ // Call the callback first, in case the user wants to do something with SpreadElements
112
+ callback(entry);
113
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(entry, 'Property')) {
114
+ return;
115
+ }
116
+ if ((0, _eslintCodemodUtils.isNodeOfType)(entry.value, 'ObjectExpression')) {
117
+ ASTObjectExpression.recurse(entry.value, callback);
118
+ }
119
+ });
108
120
  }
109
121
  };
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::d90c2cf5e100daf98915f9467f2e5663>>
9
+ * @codegen <<SignedSource::d95217b658f807294de3c81123068bf1>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -41,6 +41,7 @@ var _default = exports.default = {
41
41
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
42
42
  '@atlaskit/design-system/use-primitives': 'warn',
43
43
  '@atlaskit/design-system/use-primitives-text': 'warn',
44
+ '@atlaskit/design-system/use-tokens-space': 'error',
44
45
  '@atlaskit/design-system/use-tokens-typography': 'warn',
45
46
  '@atlaskit/design-system/use-visually-hidden': 'error'
46
47
  }
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.default = void 0;
8
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
9
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
10
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
@@ -40,6 +39,18 @@ var getProgramNode = function getProgramNode(expression) {
40
39
  }
41
40
  return expression.parent;
42
41
  };
42
+ var isDeclaredInsideComponent = function isDeclaredInsideComponent(expression) {
43
+ // These nodes imply that there is a distinct own scope (function scope / block scope),
44
+ // and so the presence of them means that expression was not defined in the module scope.
45
+ var NOT_MODULE_SCOPE = ['ArrowFunctionExpression', 'BlockStatement', 'ClassDeclaration', 'FunctionExpression'];
46
+ while (expression.type !== 'Program') {
47
+ if (NOT_MODULE_SCOPE.includes(expression.type)) {
48
+ return true;
49
+ }
50
+ expression = expression.parent;
51
+ }
52
+ return false;
53
+ };
43
54
  var JSXExpressionLinter = /*#__PURE__*/function () {
44
55
  // File-level tracking of styles hoisted from the cssAtTopOfModule/cssAtBottomOfModule fixers.
45
56
 
@@ -119,13 +130,13 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
119
130
  });
120
131
  return;
121
132
  }
122
- if (identifier.parent.parent.parent.type !== 'Program') {
133
+ if (isDeclaredInsideComponent(identifier)) {
123
134
  // When variable is declared inside the component
124
135
  this.context.report({
125
136
  node: sourceIdentifier,
126
137
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
127
138
  fix: function fix(fixer) {
128
- if (_this.configuration.fixNamesOnly) {
139
+ if (!_this.configuration.autoFix) {
129
140
  return [];
130
141
  }
131
142
  return _this.fixCssNotInModuleScope(fixer, identifier, false);
@@ -147,7 +158,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
147
158
  node: identifier,
148
159
  messageId: 'cssObjectTypeOnly',
149
160
  fix: function fix(fixer) {
150
- if (_this.configuration.fixNamesOnly) {
161
+ if (!_this.configuration.autoFix) {
151
162
  return [];
152
163
  }
153
164
  return _this.addCssFunctionCall(fixer, identifier.parent);
@@ -323,10 +334,11 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
323
334
  // The last value is the bottom of the file
324
335
  fixerNodePlacement = programNode.body[programNode.body.length - 1];
325
336
  } else {
337
+ var _ref3;
326
338
  // Place after the last ImportDeclaration
327
- fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
339
+ fixerNodePlacement = (_ref3 = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
328
340
  return node.type !== 'ImportDeclaration';
329
- });
341
+ })) !== null && _ref3 !== void 0 ? _ref3 : fixerNodePlacement;
330
342
  }
331
343
  var moduleString;
332
344
  var fixes = [];
@@ -410,7 +422,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
410
422
  node: expression,
411
423
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
412
424
  fix: function fix(fixer) {
413
- if (_this3.configuration.fixNamesOnly) {
425
+ if (!_this3.configuration.autoFix) {
414
426
  return [];
415
427
  }
416
428
 
@@ -455,8 +467,7 @@ var defaultConfig = {
455
467
  cssImportSource: _isSupportedImport.CSS_IN_JS_IMPORTS.compiled,
456
468
  xcssImportSource: _isSupportedImport.CSS_IN_JS_IMPORTS.atlaskitPrimitives,
457
469
  excludeReactComponents: false,
458
- fixNamesOnly: false,
459
- autoFixNames: true
470
+ autoFix: true
460
471
  };
461
472
  var rule = (0, _createRule.createLintRule)({
462
473
  meta: {
@@ -475,8 +486,7 @@ var rule = (0, _createRule.createLintRule)({
475
486
  cssObjectTypeOnly: "Create styles using objects passed to a css function call, e.g. `css({ textAlign: 'center'; })`.",
476
487
  cssInModule: "Imported styles should not be used; instead define in the module, import a component, or use a design token.",
477
488
  cssArrayStylesOnly: "Compose styles with an array on the css prop instead of using object spread.",
478
- noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles').",
479
- shouldEndInStyles: 'Declared styles should end in "styles".'
489
+ noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles')."
480
490
  },
481
491
  schema: [{
482
492
  type: 'object',
@@ -500,10 +510,7 @@ var rule = (0, _createRule.createLintRule)({
500
510
  excludeReactComponents: {
501
511
  type: 'boolean'
502
512
  },
503
- autoFixNames: {
504
- type: 'boolean'
505
- },
506
- fixNamesOnly: {
513
+ autoFix: {
507
514
  type: 'boolean'
508
515
  }
509
516
  },
@@ -511,80 +518,41 @@ var rule = (0, _createRule.createLintRule)({
511
518
  }]
512
519
  },
513
520
  create: function create(context) {
514
- var _ref3;
515
521
  var mergedConfig = (0, _assign.default)({}, defaultConfig, context.options[0]);
516
- var declarationSuffix = 'Styles';
517
- var callSelectorFunctions = [].concat((0, _toConsumableArray2.default)(mergedConfig.cssFunctions), ['cssMap']);
518
- var callSelector = callSelectorFunctions.map(function (fn) {
519
- return "CallExpression[callee.name=".concat(fn, "]");
520
- }).join(',');
521
- return _ref3 = {}, (0, _defineProperty2.default)(_ref3, callSelector, function (node) {
522
- if (node.parent.type !== 'VariableDeclarator') {
523
- // We aren't interested in these that don't have a parent.
524
- return;
525
- }
526
- var identifier = node.parent.id;
527
- if (identifier.type === 'Identifier' && (identifier.name.endsWith(declarationSuffix) || identifier.name.startsWith(declarationSuffix.toLowerCase()) || identifier.name === declarationSuffix.toLowerCase())) {
528
- // Already prefixed! Nothing to do.
529
- return;
530
- }
531
- if (!mergedConfig.autoFixNames) {
532
- return;
533
- }
534
- context.report({
535
- node: identifier,
536
- messageId: 'shouldEndInStyles',
537
- fix: function fix(fixer) {
538
- var _context$getScope$var;
539
- var identifierName = identifier.type === 'Identifier' ? identifier.name : '';
540
- var references = ((_context$getScope$var = context.getScope().variables.find(function (varb) {
541
- return varb.name === identifierName;
542
- })) === null || _context$getScope$var === void 0 ? void 0 : _context$getScope$var.references) || [];
543
- var newIdentifierName = "".concat(identifierName
544
- // Remove "Style" if it is already defined.
545
- .replace(/Style$/, '')).concat(declarationSuffix);
546
- return references.filter(function (ref) {
547
- return ref.identifier.name === identifierName;
548
- }).map(function (ref) {
549
- if (ref.identifier.parent && ref.identifier.parent.shorthand) {
550
- return fixer.replaceText(ref.identifier, "".concat(identifierName, ": ").concat(newIdentifierName));
551
- }
552
- return fixer.replaceText(ref.identifier, newIdentifierName);
553
- });
554
- }
555
- });
556
- }), (0, _defineProperty2.default)(_ref3, "JSXAttribute", function JSXAttribute(nodeOriginal) {
557
- var node = nodeOriginal;
558
- var name = node.name,
559
- value = node.value;
560
- if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
561
- // e.g. <item.before />
562
- if (node.parent.name.type === 'JSXMemberExpression') {
563
- return;
564
- }
565
- // e.g. <div />, <MenuItem />
566
- if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
567
- return;
568
- }
569
- }
570
- if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
571
- // When not a jsx expression. For eg. css=""
572
- if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
573
- context.report({
574
- node: node,
575
- messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
576
- });
577
- return;
522
+ return {
523
+ JSXAttribute: function JSXAttribute(nodeOriginal) {
524
+ var node = nodeOriginal;
525
+ var name = node.name,
526
+ value = node.value;
527
+ if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
528
+ // e.g. <item.before />
529
+ if (node.parent.name.type === 'JSXMemberExpression') {
530
+ return;
531
+ }
532
+ // e.g. <div />, <MenuItem />
533
+ if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
534
+ return;
535
+ }
578
536
  }
579
- if (value.expression.type === 'JSXEmptyExpression') {
580
- // e.g. the comment in
581
- // <div css={/* Hello there */} />
582
- return;
537
+ if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
538
+ // When not a jsx expression. For eg. css=""
539
+ if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
540
+ context.report({
541
+ node: node,
542
+ messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
543
+ });
544
+ return;
545
+ }
546
+ if (value.expression.type === 'JSXEmptyExpression') {
547
+ // e.g. the comment in
548
+ // <div css={/* Hello there */} />
549
+ return;
550
+ }
551
+ var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
552
+ linter.run();
583
553
  }
584
- var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
585
- linter.run();
586
554
  }
587
- }), _ref3;
555
+ };
588
556
  }
589
557
  });
590
558
  var _default = exports.default = rule;
@@ -34,11 +34,12 @@ var _useHeadingLevelInSpotlightCard = _interopRequireDefault(require("./use-head
34
34
  var _useHrefInLinkItem = _interopRequireDefault(require("./use-href-in-link-item"));
35
35
  var _usePrimitives = _interopRequireDefault(require("./use-primitives"));
36
36
  var _usePrimitivesText = _interopRequireDefault(require("./use-primitives-text"));
37
+ var _useTokensSpace = _interopRequireDefault(require("./use-tokens-space"));
37
38
  var _useTokensTypography = _interopRequireDefault(require("./use-tokens-typography"));
38
39
  var _useVisuallyHidden = _interopRequireDefault(require("./use-visually-hidden"));
39
40
  /**
40
41
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
41
- * @codegen <<SignedSource::c283cd7ede5e813a9119cd707d339273>>
42
+ * @codegen <<SignedSource::d1c25758089a050334359276ede0ca3a>>
42
43
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
43
44
  */
44
45
  var _default = exports.default = {
@@ -71,6 +72,7 @@ var _default = exports.default = {
71
72
  'use-href-in-link-item': _useHrefInLinkItem.default,
72
73
  'use-primitives': _usePrimitives.default,
73
74
  'use-primitives-text': _usePrimitivesText.default,
75
+ 'use-tokens-space': _useTokensSpace.default,
74
76
  'use-tokens-typography': _useTokensTypography.default,
75
77
  'use-visually-hidden': _useVisuallyHidden.default
76
78
  };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _createRule = require("../utils/create-rule");
8
+ var _transformers = require("./transformers");
9
+ var rule = (0, _createRule.createLintRule)({
10
+ meta: {
11
+ name: 'use-tokens-space',
12
+ type: 'problem',
13
+ fixable: 'code',
14
+ hasSuggestions: true,
15
+ docs: {
16
+ description: 'Enforces usage of space design tokens rather than hard-coded values.',
17
+ recommended: false,
18
+ severity: 'error'
19
+ },
20
+ messages: {
21
+ noRawSpacingValues: 'The use of spacing primitives or tokens is preferred over the direct application of spacing properties.'
22
+ }
23
+ },
24
+ create: function create(context) {
25
+ return {
26
+ 'CallExpression[callee.name="css"] ObjectExpression Property': function CallExpressionCalleeNameCssObjectExpressionProperty(node) {
27
+ return _transformers.StyleProperty.lint(node, {
28
+ context: context
29
+ });
30
+ },
31
+ 'CallExpression[callee.name="keyframes"] ObjectExpression Property': function CallExpressionCalleeNameKeyframesObjectExpressionProperty(node) {
32
+ return _transformers.StyleProperty.lint(node, {
33
+ context: context
34
+ });
35
+ },
36
+ 'CallExpression[callee.name="cssMap"] ObjectExpression Property': function CallExpressionCalleeNameCssMapObjectExpressionProperty(node) {
37
+ return _transformers.StyleProperty.lint(node, {
38
+ context: context
39
+ });
40
+ },
41
+ 'CallExpression[callee.object.name=styled] ObjectExpression Property': function CallExpressionCalleeObjectNameStyledObjectExpressionProperty(node) {
42
+ return _transformers.StyleProperty.lint(node, {
43
+ context: context
44
+ });
45
+ },
46
+ 'CallExpression[callee.object.name=styled2] ObjectExpression Property': function CallExpressionCalleeObjectNameStyled2ObjectExpressionProperty(node) {
47
+ return _transformers.StyleProperty.lint(node, {
48
+ context: context
49
+ });
50
+ }
51
+ };
52
+ }
53
+ });
54
+ var _default = exports.default = rule;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "StyleProperty", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _styleProperty.StyleProperty;
10
+ }
11
+ });
12
+ var _styleProperty = require("./style-property");
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.StyleProperty = void 0;
9
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
10
+ var ast = _interopRequireWildcard(require("../../../../ast-nodes"));
11
+ var _utils = require("../../utils");
12
+ var _styleMap = require("./style-map");
13
+ var _supported = _interopRequireDefault(require("./supported"));
14
+ var _upsertImportDeclaration = require("./upsert-import-declaration");
15
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
16
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
17
+ /* eslint-disable @repo/internal/react/require-jsdoc */
18
+
19
+ var messageId = 'noRawSpacingValues';
20
+ var StyleProperty = exports.StyleProperty = {
21
+ lint: function lint(node, _ref) {
22
+ var context = _ref.context;
23
+ // Check whether all criteria needed to make a transformation are met
24
+ var _StyleProperty$_check = StyleProperty._check(node),
25
+ success = _StyleProperty$_check.success,
26
+ ref = _StyleProperty$_check.ref;
27
+ if (!success) {
28
+ return;
29
+ }
30
+ context.report({
31
+ node: ref.node.value,
32
+ messageId: messageId,
33
+ fix: ref.token ? StyleProperty._fix(ref, context) : undefined
34
+ });
35
+ },
36
+ _check: function _check(node) {
37
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) {
38
+ return {
39
+ success: false,
40
+ ref: undefined
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Currently, we support values like:
46
+ * ```
47
+ * {
48
+ * padding: '8px', // value.type is Literal
49
+ * margin: -8, // value.type is UnaryExpression
50
+ * }
51
+ * ```
52
+ */
53
+ if (!((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'Literal') || (0, _eslintCodemodUtils.isNodeOfType)(node.value, 'UnaryExpression'))) {
54
+ return {
55
+ success: false,
56
+ ref: undefined
57
+ };
58
+ }
59
+ var _ast$ObjectEntry$getP = ast.ObjectEntry.getProperty(node),
60
+ property = _ast$ObjectEntry$getP.value;
61
+
62
+ // Bail if the property is not `padding`, `margin`, etc
63
+ if (!property || !_styleMap.styleMap[property]) {
64
+ return {
65
+ success: false,
66
+ ref: undefined
67
+ };
68
+ }
69
+ var value = ast.ObjectEntry.getValue(node);
70
+
71
+ // This is mainly useful as a type guard, so the checks after don't have to have duplicate checks for other types.
72
+ if (!(0, _utils.isStringOrNumber)(value)) {
73
+ return {
74
+ success: false,
75
+ ref: undefined
76
+ };
77
+ }
78
+
79
+ // ignore CSS vars. See: https://stash.atlassian.com/projects/ATLASSIAN/repos/atlassian-frontend-monorepo/pull-requests/74844/overview?commentId=6741571
80
+ if (value.toString().startsWith('var(')) {
81
+ return {
82
+ success: false,
83
+ ref: undefined
84
+ };
85
+ }
86
+
87
+ // There are valid values to ignore, such as `margin: auto`
88
+ if (_supported.default.values.ignore.includes(value)) {
89
+ return {
90
+ success: false,
91
+ ref: undefined
92
+ };
93
+ }
94
+
95
+ // Don't report on stuff like `padding: '8px 16px'`.
96
+ // We may iterate to handle values like this in future.
97
+ if (value.toString().includes(' ')) {
98
+ return {
99
+ success: false,
100
+ ref: undefined
101
+ };
102
+ }
103
+ var ref = {
104
+ node: node,
105
+ token: _styleMap.styleMap[property][value],
106
+ fallback: value
107
+ };
108
+ return {
109
+ success: true,
110
+ ref: ref
111
+ };
112
+ },
113
+ /**
114
+ * All required validation steps have been taken care of before this
115
+ * transformer is called, so it just goes ahead providing all necessary fixes
116
+ */
117
+ _fix: function _fix(ref, context) {
118
+ return function (fixer) {
119
+ var importFix = (0, _upsertImportDeclaration.upsertImportDeclaration)({
120
+ module: '@atlaskit/tokens',
121
+ specifiers: ['token']
122
+ }, context, fixer);
123
+ var tokenCall = ref.fallback ? "token('".concat(ref.token, "', '").concat(ref.fallback, "')") : "token('".concat(ref.token, "')");
124
+ var tokenFix = fixer.replaceText(ref.node.value, tokenCall);
125
+ return [importFix, tokenFix].filter(function (fix) {
126
+ return Boolean(fix);
127
+ }); // Some of the transformers can return arrays with undefined, so filter them out
128
+ };
129
+ }
130
+ };