@atlaskit/eslint-plugin-design-system 8.18.0 → 8.19.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 (89) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -0
  3. package/constellation/index/usage.mdx +51 -0
  4. package/dist/cjs/presets/all.codegen.js +2 -1
  5. package/dist/cjs/presets/recommended.codegen.js +2 -1
  6. package/dist/cjs/rules/index.codegen.js +3 -1
  7. package/dist/cjs/rules/no-unsafe-style-overrides/index.js +74 -0
  8. package/dist/cjs/rules/use-primitives/index.js +40 -35
  9. package/dist/cjs/rules/use-primitives/transformers/css-to-xcss.js +10 -3
  10. package/dist/cjs/rules/use-primitives/transformers/index.js +8 -1
  11. package/dist/cjs/rules/use-primitives/transformers/styled-component-to-primitive.js +68 -0
  12. package/dist/cjs/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +16 -0
  13. package/dist/cjs/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +39 -0
  14. package/dist/cjs/rules/use-primitives/utils/find-valid-styled-component-call.js +52 -0
  15. package/dist/cjs/rules/use-primitives/utils/index.js +21 -0
  16. package/dist/cjs/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +38 -0
  17. package/dist/cjs/rules/use-primitives/utils/is-valid-tag-name.js +2 -2
  18. package/dist/cjs/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
  19. package/dist/cjs/rules/utils/get-import-node-by-source.js +49 -1
  20. package/dist/cjs/rules/utils/jsx.js +17 -0
  21. package/dist/es2019/presets/all.codegen.js +2 -1
  22. package/dist/es2019/presets/recommended.codegen.js +2 -1
  23. package/dist/es2019/rules/index.codegen.js +3 -1
  24. package/dist/es2019/rules/no-unsafe-style-overrides/index.js +68 -0
  25. package/dist/es2019/rules/use-primitives/index.js +41 -37
  26. package/dist/es2019/rules/use-primitives/transformers/css-to-xcss.js +9 -6
  27. package/dist/es2019/rules/use-primitives/transformers/index.js +2 -1
  28. package/dist/es2019/rules/use-primitives/transformers/styled-component-to-primitive.js +59 -0
  29. package/dist/es2019/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +14 -0
  30. package/dist/es2019/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +32 -0
  31. package/dist/es2019/rules/use-primitives/utils/find-valid-styled-component-call.js +47 -0
  32. package/dist/es2019/rules/use-primitives/utils/index.js +3 -0
  33. package/dist/es2019/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +32 -0
  34. package/dist/es2019/rules/use-primitives/utils/is-valid-tag-name.js +1 -1
  35. package/dist/es2019/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
  36. package/dist/es2019/rules/utils/get-import-node-by-source.js +27 -0
  37. package/dist/es2019/rules/utils/jsx.js +16 -0
  38. package/dist/esm/presets/all.codegen.js +2 -1
  39. package/dist/esm/presets/recommended.codegen.js +2 -1
  40. package/dist/esm/rules/index.codegen.js +3 -1
  41. package/dist/esm/rules/no-unsafe-style-overrides/index.js +68 -0
  42. package/dist/esm/rules/use-primitives/index.js +41 -37
  43. package/dist/esm/rules/use-primitives/transformers/css-to-xcss.js +9 -2
  44. package/dist/esm/rules/use-primitives/transformers/index.js +2 -1
  45. package/dist/esm/rules/use-primitives/transformers/styled-component-to-primitive.js +61 -0
  46. package/dist/esm/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +16 -0
  47. package/dist/esm/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +34 -0
  48. package/dist/esm/rules/use-primitives/utils/find-valid-styled-component-call.js +47 -0
  49. package/dist/esm/rules/use-primitives/utils/index.js +3 -0
  50. package/dist/esm/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +32 -0
  51. package/dist/esm/rules/use-primitives/utils/is-valid-tag-name.js +1 -1
  52. package/dist/esm/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
  53. package/dist/esm/rules/utils/get-import-node-by-source.js +48 -0
  54. package/dist/esm/rules/utils/jsx.js +16 -0
  55. package/dist/types/index.codegen.d.ts +2 -0
  56. package/dist/types/presets/all.codegen.d.ts +2 -1
  57. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  58. package/dist/types/rules/index.codegen.d.ts +1 -0
  59. package/dist/types/rules/no-unsafe-style-overrides/index.d.ts +2 -0
  60. package/dist/types/rules/use-primitives/transformers/css-to-xcss.d.ts +2 -1
  61. package/dist/types/rules/use-primitives/transformers/index.d.ts +1 -0
  62. package/dist/types/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +13 -0
  63. package/dist/types/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +1 -1
  64. package/dist/types/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.d.ts +7 -0
  65. package/dist/types/rules/use-primitives/utils/find-valid-styled-component-call.d.ts +7 -0
  66. package/dist/types/rules/use-primitives/utils/index.d.ts +3 -0
  67. package/dist/types/rules/use-primitives/utils/is-valid-css-properties-to-transform.d.ts +3 -0
  68. package/dist/types/rules/use-primitives/utils/is-valid-tag-name.d.ts +1 -0
  69. package/dist/types/rules/use-primitives/utils/update-jsx-element-name.d.ts +1 -1
  70. package/dist/types/rules/utils/get-import-node-by-source.d.ts +9 -0
  71. package/dist/types/rules/utils/jsx.d.ts +2 -1
  72. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  73. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  74. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  75. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  76. package/dist/types-ts4.5/rules/no-unsafe-style-overrides/index.d.ts +2 -0
  77. package/dist/types-ts4.5/rules/use-primitives/transformers/css-to-xcss.d.ts +2 -1
  78. package/dist/types-ts4.5/rules/use-primitives/transformers/index.d.ts +1 -0
  79. package/dist/types-ts4.5/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +13 -0
  80. package/dist/types-ts4.5/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +1 -1
  81. package/dist/types-ts4.5/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.d.ts +7 -0
  82. package/dist/types-ts4.5/rules/use-primitives/utils/find-valid-styled-component-call.d.ts +7 -0
  83. package/dist/types-ts4.5/rules/use-primitives/utils/index.d.ts +3 -0
  84. package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-css-properties-to-transform.d.ts +3 -0
  85. package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-tag-name.d.ts +1 -0
  86. package/dist/types-ts4.5/rules/use-primitives/utils/update-jsx-element-name.d.ts +1 -1
  87. package/dist/types-ts4.5/rules/utils/get-import-node-by-source.d.ts +9 -0
  88. package/dist/types-ts4.5/rules/utils/jsx.d.ts +2 -1
  89. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atlaskit/eslint-plugin-design-system
2
2
 
3
+ ## 8.19.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#64899](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/64899) [`442878c001f9`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/442878c001f9) - Add `no-unsafe-style-overrides`. This rule marks any usage of unsafe style overrides as violations, such as usage of `css`, `theme`, and `cssFn` props.
8
+
9
+ ## 8.18.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#64857](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/64857) [`1be24644c029`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/1be24644c029) - `use-primitives` is now capable of converting styled components. This is guarded by a config flag so this release is only a patch.
14
+
3
15
  ## 8.18.0
4
16
 
5
17
  ### Minor Changes
package/README.md CHANGED
@@ -62,6 +62,7 @@ module.exports = {
62
62
  | <a href="./src/rules/no-nested-styles/README.md">no-nested-styles</a> | Disallows use of nested styles in `css` functions. | Yes | | |
63
63
  | <a href="./src/rules/no-physical-properties/README.md">no-physical-properties</a> | Disallow physical properties and values in `css` function calls. | | Yes | |
64
64
  | <a href="./src/rules/no-unsafe-design-token-usage/README.md">no-unsafe-design-token-usage</a> | Enforces design token usage is statically and locally analyzable. | Yes | Yes | |
65
+ | <a href="./src/rules/no-unsafe-style-overrides/README.md">no-unsafe-style-overrides</a> | Discourage usage of unsafe style overrides used against the Atlassian Design System. | Yes | | |
65
66
  | <a href="./src/rules/no-unsupported-drag-and-drop-libraries/README.md">no-unsupported-drag-and-drop-libraries</a> | Disallow importing unsupported drag and drop modules. | Yes | | |
66
67
  | <a href="./src/rules/prefer-primitives/README.md">prefer-primitives</a> | Increase awareness of primitive components via code hints. Strictly used for education purposes and discoverability. To enforce usage please refer to the `use-primitives` rule. | | | |
67
68
  | <a href="./src/rules/use-drawer-label/README.md">use-drawer-label</a> | Encourages to provide accessible name for Atlassian Design System Drawer component. | Yes | | Yes |
@@ -26,6 +26,7 @@ This plugin contains rules that should be used when working with the [Atlassian
26
26
  | <a href="#no-nested-styles">no-nested-styles</a> | Disallows use of nested styles in `css` functions. | Yes | | |
27
27
  | <a href="#no-physical-properties">no-physical-properties</a> | Disallow physical properties and values in `css` function calls. | | Yes | |
28
28
  | <a href="#no-unsafe-design-token-usage">no-unsafe-design-token-usage</a> | Enforces design token usage is statically and locally analyzable. | Yes | Yes | |
29
+ | <a href="#no-unsafe-style-overrides">no-unsafe-style-overrides</a> | Discourage usage of unsafe style overrides used against the Atlassian Design System. | Yes | | |
29
30
  | <a href="#no-unsupported-drag-and-drop-libraries">no-unsupported-drag-and-drop-libraries</a> | Disallow importing unsupported drag and drop modules. | Yes | | |
30
31
  | <a href="#prefer-primitives">prefer-primitives</a> | Increase awareness of primitive components via code hints. Strictly used for education purposes and discoverability. To enforce usage please refer to the `use-primitives` rule. | | | |
31
32
  | <a href="#use-drawer-label">use-drawer-label</a> | Encourages to provide accessible name for Atlassian Design System Drawer component. | Yes | | Yes |
@@ -666,6 +667,56 @@ This rule comes with options to aid in migrating to design tokens.
666
667
  When `true` the rule will mark token function usage as violations when fallbacks aren't defined.
667
668
  When `false` the rule will mark token function usage as violations when fallbacks are defined.
668
669
 
670
+ ## no-unsafe-style-overrides
671
+
672
+ Unsafe style overrides cause friction and incidents when internals of the component you're overriding change. They're inherently unbounded and everything is API that can change at a moments notice.
673
+
674
+ Instead, lean on composition, primitive components, and safe style overrides via the `xcss` prop where component authors declare what styles they want to support.
675
+
676
+ <h3>Examples</h3>
677
+
678
+ #### Incorrect
679
+
680
+ ```tsx
681
+ import Button from '@atlaskit/button';
682
+
683
+ <Button css={{ fontWeight: 500 }}>foo</Button>;
684
+ ^^^
685
+ ```
686
+
687
+ ```tsx
688
+ import { LinkItem } from '@atlaskit/menu';
689
+
690
+ <LinkItem cssFn={() => ({ '> div > div': { padding: 2 } })} />;
691
+ ^^^^^
692
+ ```
693
+
694
+ ```tsx
695
+ import { ButtonItem } from '@atlaskit/side-navigation';
696
+
697
+ <ButtonItem className="text-neutral-400" />;
698
+ ^^^^^^^^^
699
+ ```
700
+
701
+ #### Correct
702
+
703
+ ```tsx
704
+ <Button>
705
+ <strong>foo</strong>
706
+ </Button>
707
+ ```
708
+
709
+ ```tsx
710
+ const styles = css({ padding: 'var(--ds-space-100)' });
711
+
712
+ <Box as="a" xcss={styles}>
713
+ <Stack>
714
+ <Inline />
715
+ <Inline />
716
+ </Stack>
717
+ </Box>;
718
+ ```
719
+
669
720
  ## no-unsupported-drag-and-drop-libraries
670
721
 
671
722
  We encourage the use of Pragmatic drag and drop to power all drag and drop experiences - from table to external files. Pragmatic drag and drop is a performance optimised drag and drop framework that has been designed to power any drag and drop experience on any tech stack. Please avoid using alternative drag and drop libraries as it will lead to poorer performance, increased maintenance costs, (likely) worse accessibility and fragmented user experiences. See https://staging.atlassian.design/components/pragmatic-drag-and-drop/
@@ -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::37a857efd0971ab26170d5f1f526279b>>
9
+ * @codegen <<SignedSource::6efa1e48692b3e287d6dfcd500a5f0ab>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -25,6 +25,7 @@ var _default = exports.default = {
25
25
  '@atlaskit/design-system/no-nested-styles': 'error',
26
26
  '@atlaskit/design-system/no-physical-properties': 'error',
27
27
  '@atlaskit/design-system/no-unsafe-design-token-usage': 'error',
28
+ '@atlaskit/design-system/no-unsafe-style-overrides': 'warn',
28
29
  '@atlaskit/design-system/no-unsupported-drag-and-drop-libraries': 'error',
29
30
  '@atlaskit/design-system/prefer-primitives': 'warn',
30
31
  '@atlaskit/design-system/use-drawer-label': 'warn',
@@ -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::b3bce8efb2db1fed84370af608ca0d4c>>
9
+ * @codegen <<SignedSource::be810d87ec2d253e3b053dc06ff1b99a>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -21,6 +21,7 @@ var _default = exports.default = {
21
21
  '@atlaskit/design-system/no-deprecated-imports': 'error',
22
22
  '@atlaskit/design-system/no-nested-styles': 'error',
23
23
  '@atlaskit/design-system/no-unsafe-design-token-usage': 'error',
24
+ '@atlaskit/design-system/no-unsafe-style-overrides': 'warn',
24
25
  '@atlaskit/design-system/no-unsupported-drag-and-drop-libraries': 'error',
25
26
  '@atlaskit/design-system/use-drawer-label': 'warn',
26
27
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
@@ -18,6 +18,7 @@ var _noMargin = _interopRequireDefault(require("./no-margin"));
18
18
  var _noNestedStyles = _interopRequireDefault(require("./no-nested-styles"));
19
19
  var _noPhysicalProperties = _interopRequireDefault(require("./no-physical-properties"));
20
20
  var _noUnsafeDesignTokenUsage = _interopRequireDefault(require("./no-unsafe-design-token-usage"));
21
+ var _noUnsafeStyleOverrides = _interopRequireDefault(require("./no-unsafe-style-overrides"));
21
22
  var _noUnsupportedDragAndDropLibraries = _interopRequireDefault(require("./no-unsupported-drag-and-drop-libraries"));
22
23
  var _preferPrimitives = _interopRequireDefault(require("./prefer-primitives"));
23
24
  var _useDrawerLabel = _interopRequireDefault(require("./use-drawer-label"));
@@ -27,7 +28,7 @@ var _usePrimitives = _interopRequireDefault(require("./use-primitives"));
27
28
  var _useVisuallyHidden = _interopRequireDefault(require("./use-visually-hidden"));
28
29
  /**
29
30
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
30
- * @codegen <<SignedSource::e2cc357c90120dbbd89b2928dba968c0>>
31
+ * @codegen <<SignedSource::ab1f5b129d07027c228dbd79da5f3572>>
31
32
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
32
33
  */
33
34
  var _default = exports.default = {
@@ -44,6 +45,7 @@ var _default = exports.default = {
44
45
  'no-nested-styles': _noNestedStyles.default,
45
46
  'no-physical-properties': _noPhysicalProperties.default,
46
47
  'no-unsafe-design-token-usage': _noUnsafeDesignTokenUsage.default,
48
+ 'no-unsafe-style-overrides': _noUnsafeStyleOverrides.default,
47
49
  'no-unsupported-drag-and-drop-libraries': _noUnsupportedDragAndDropLibraries.default,
48
50
  'prefer-primitives': _preferPrimitives.default,
49
51
  'use-drawer-label': _useDrawerLabel.default,
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
8
+ var _createRule = require("../utils/create-rule");
9
+ var _getImportNodeBySource = require("../utils/get-import-node-by-source");
10
+ var _jsx = require("../utils/jsx");
11
+ var unsafeOverrides = ['css', 'className', 'theme', 'cssFn', 'styles'];
12
+ var rule = (0, _createRule.createLintRule)({
13
+ meta: {
14
+ docs: {
15
+ recommended: true,
16
+ // This should be an error but for now we're rolling it out as warn so we can actually get it into codebases.
17
+ severity: 'warn',
18
+ description: 'Discourage usage of unsafe style overrides used against the Atlassian Design System.'
19
+ },
20
+ name: 'no-unsafe-style-overrides',
21
+ messages: {
22
+ noUnsafeStyledOverride: 'Wrapping {{componentName}} in a styled component encourages unsafe style overrides which cause friction and incidents when its internals change.',
23
+ noUnsafeOverrides: 'The {{propName}} prop encourages unsafe style overrides which cause friction and incidents when {{componentName}} internals change.'
24
+ }
25
+ },
26
+ create: function create(context) {
27
+ return {
28
+ CallExpression: function CallExpression(node) {
29
+ if (node.callee.type !== 'Identifier' || !node.callee.name.toLowerCase().includes('styled')) {
30
+ // Ignore functions that don't look like styled().
31
+ return;
32
+ }
33
+ var firstArgument = node.arguments[0];
34
+ if (!firstArgument || firstArgument.type !== 'Identifier') {
35
+ return;
36
+ }
37
+ var moduleName = (0, _getImportNodeBySource.getModuleOfIdentifier)(context.getSourceCode(), firstArgument.name);
38
+ if (!moduleName || !moduleName.moduleName.startsWith('@atlaskit')) {
39
+ // Ignore styled calls with non-atlaskit components.
40
+ return;
41
+ }
42
+ context.report({
43
+ node: firstArgument,
44
+ messageId: 'noUnsafeStyledOverride',
45
+ data: {
46
+ componentName: moduleName.importName
47
+ }
48
+ });
49
+ },
50
+ JSXAttribute: function JSXAttribute(node) {
51
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXAttribute') || !(node.parent && (0, _eslintCodemodUtils.isNodeOfType)(node.parent, 'JSXOpeningElement'))) {
52
+ return;
53
+ }
54
+ var elementName = (0, _jsx.getJSXElementName)(node.parent);
55
+ var moduleName = (0, _getImportNodeBySource.getModuleOfIdentifier)(context.getSourceCode(), elementName);
56
+ if (!moduleName || !moduleName.moduleName.startsWith('@atlaskit')) {
57
+ return;
58
+ }
59
+ var propName = typeof node.name.name === 'string' ? node.name.name : node.name.name.name;
60
+ if (unsafeOverrides.includes(propName)) {
61
+ context.report({
62
+ node: node,
63
+ messageId: 'noUnsafeOverrides',
64
+ data: {
65
+ propName: propName,
66
+ componentName: moduleName.importName
67
+ }
68
+ });
69
+ }
70
+ }
71
+ };
72
+ }
73
+ });
74
+ var _default = exports.default = rule;
@@ -1,14 +1,19 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.default = void 0;
7
8
  var _eslintCodemodUtils = require("eslint-codemod-utils");
9
+ var _assign = _interopRequireDefault(require("lodash/assign"));
8
10
  var _createRule = require("../utils/create-rule");
9
11
  var _transformers = require("./transformers");
10
12
  var _utils = require("./utils");
11
13
  var boxDocsUrl = 'https://atlassian.design/components/primitives/box';
14
+ var defaultConfig = {
15
+ preview: false
16
+ };
12
17
  var rule = (0, _createRule.createLintRule)({
13
18
  meta: {
14
19
  name: 'use-primitives',
@@ -21,11 +26,43 @@ var rule = (0, _createRule.createLintRule)({
21
26
  severity: 'warn'
22
27
  },
23
28
  messages: {
24
- preferPrimitivesBox: "This \"{{element}}\" may be able to be replaced with a \"Box\". See ".concat(boxDocsUrl, " for guidance.")
29
+ preferPrimitivesBox: "This element can be replaced with a \"Box\" primitive. See ".concat(boxDocsUrl, " for additional guidance.")
25
30
  }
26
31
  },
27
32
  create: function create(context) {
33
+ var mergedConfig = (0, _assign.default)({}, defaultConfig, context.options[0]);
28
34
  return {
35
+ // transforms styled.<html>(...) usages
36
+ CallExpression: function CallExpression(node) {
37
+ if (!mergedConfig.preview) {
38
+ return;
39
+ }
40
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'CallExpression')) {
41
+ return;
42
+ }
43
+ var styledComponentVariableRef = (0, _utils.findValidStyledComponentCall)(node);
44
+ if (!styledComponentVariableRef || !(0, _eslintCodemodUtils.isNodeOfType)(styledComponentVariableRef.id, 'Identifier') || !(0, _utils.isValidCssPropertiesToTransform)(node)) {
45
+ return;
46
+ }
47
+ var styledComponentJsxRef = (0, _utils.findValidJsxUsageToTransform)(styledComponentVariableRef.id.name, context.getScope());
48
+ if (!styledComponentJsxRef) {
49
+ return;
50
+ }
51
+
52
+ // if we have both references at this point then we can offer a fix \o/
53
+ context.report({
54
+ node: styledComponentVariableRef,
55
+ messageId: 'preferPrimitivesBox',
56
+ suggest: [{
57
+ desc: "Convert ".concat(styledComponentVariableRef.id.name, " to Box"),
58
+ fix: (0, _transformers.styledComponentToPrimitive)({
59
+ stylesRef: styledComponentVariableRef,
60
+ jsxRef: styledComponentJsxRef
61
+ }, context)
62
+ }]
63
+ });
64
+ },
65
+ // transforms <div css={...}> usages
29
66
  JSXOpeningElement: function JSXOpeningElement(node) {
30
67
  if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXOpeningElement')) {
31
68
  return;
@@ -97,42 +134,10 @@ var shouldSuggestBox = function shouldSuggestBox(node, context
97
134
  // Find where `cssVariableName` is defined. We're looking for `const myStyles = css({...})`
98
135
  var cssVariableDefinition = (0, _eslintCodemodUtils.getIdentifierInParentScope)(context.getScope(), cssVariableName);
99
136
  var cssVariableValue = (0, _utils.getVariableDefinitionValue)(cssVariableDefinition);
100
-
101
137
  // Check if `cssVariableValue` is a function called `css()`
102
- if (!(0, _utils.isFunctionNamed)(cssVariableValue, 'css')) {
103
- return false;
104
- }
105
-
106
- // get the `{}` in `css({})`
107
- // Zero indexed
108
- var cssObjectExpression = (0, _utils.getFunctionArgumentAtPos)(cssVariableValue, 0);
109
-
110
- // Bail on empty `css()` calls
111
- if (!cssObjectExpression) {
112
- return false;
113
- }
114
- var cssObject = (0, _utils.convertASTObjectExpressionToJSObject)(cssObjectExpression);
115
-
116
- // Bail if there are less or more than 1 styles defined
117
- if (Object.keys(cssObject).length !== 1) {
118
- return false;
119
- }
120
-
121
- // NOTE: Our approach with this lint rule is to strictly whitelist css properties we can map.
122
- // It means we have to provide mappings for everything (e.g. `display: block`).
123
- // However, from a maker's experience, it's much better that the rule doesn't report (if we miss a mapping)
124
- // than the rule reporting on things that can't be mapped.
125
- var containsOnlyValidStyles = Object.keys(cssObject).every(function (styleProperty) {
126
- var styleValue = cssObject[styleProperty];
127
- return _transformers.supportedStylesMap[styleProperty] &&
128
- // Is the key something we can map
129
- _transformers.supportedStylesMap[styleProperty][styleValue] // Is the value something we can map
130
- ;
131
- });
132
-
133
- if (!containsOnlyValidStyles) {
138
+ if (!cssVariableValue || !(0, _utils.isFunctionNamed)(cssVariableValue, 'css')) {
134
139
  return false;
135
140
  }
136
- return true;
141
+ return (0, _utils.isValidCssPropertiesToTransform)(cssVariableValue.node.init);
137
142
  };
138
143
  var _default = exports.default = rule;
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.supportedStylesMap = exports.spaceTokenMap = exports.cssToXcssTransformer = void 0;
7
+ exports.supportedStylesMap = exports.styledObjectToXcssTokens = exports.spaceTokenMap = exports.cssToXcssTransformer = void 0;
8
8
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
9
  var _eslintCodemodUtils = require("eslint-codemod-utils");
10
10
  var _utils = require("../utils");
@@ -36,7 +36,14 @@ var cssToXcssTransformer = exports.cssToXcssTransformer = function cssToXcssTran
36
36
  var cssObjectExpression = (0, _utils.getFunctionArgumentAtPos)(cssVariableValue, 0);
37
37
  return [
38
38
  // Update `css` function name to `xcss`.
39
- fixer.replaceText(cssVariableValue.node.init.callee, (0, _eslintCodemodUtils.identifier)('xcss').toString())].concat((0, _toConsumableArray2.default)(cssObjectExpression.properties.map(function (entry) {
39
+ fixer.replaceText(cssVariableValue.node.init.callee, (0, _eslintCodemodUtils.identifier)('xcss').toString())].concat((0, _toConsumableArray2.default)(styledObjectToXcssTokens(cssObjectExpression, fixer)));
40
+ };
41
+
42
+ // Update css object values to xcss values. e.g. `'8px'` -> `'space.100'`
43
+ // Note: `properties` in this context is a group of AST nodes that make up a key/value pair in an object.
44
+ // e.g. `padding: '8px'`. For clarity, it's renamed to `entry` inside the `.map()`.
45
+ var styledObjectToXcssTokens = exports.styledObjectToXcssTokens = function styledObjectToXcssTokens(styles, fixer) {
46
+ return styles.properties.map(function (entry) {
40
47
  if (!(0, _eslintCodemodUtils.isNodeOfType)(entry, 'Property')) {
41
48
  return;
42
49
  }
@@ -51,7 +58,7 @@ var cssToXcssTransformer = exports.cssToXcssTransformer = function cssToXcssTran
51
58
  return;
52
59
  }
53
60
  return fixer.replaceText(entry.value, (0, _eslintCodemodUtils.literal)("'".concat(supportedStylesMap[entry.key.name][value], "'")).toString());
54
- })));
61
+ });
55
62
  };
56
63
  var spaceTokenMap = exports.spaceTokenMap = {
57
64
  '0px': 'space.0',
@@ -21,6 +21,12 @@ Object.defineProperty(exports, "spaceTokenMap", {
21
21
  return _cssToXcss.spaceTokenMap;
22
22
  }
23
23
  });
24
+ Object.defineProperty(exports, "styledComponentToPrimitive", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _styledComponentToPrimitive.styledComponentToPrimitive;
28
+ }
29
+ });
24
30
  Object.defineProperty(exports, "supportedStylesMap", {
25
31
  enumerable: true,
26
32
  get: function get() {
@@ -28,4 +34,5 @@ Object.defineProperty(exports, "supportedStylesMap", {
28
34
  }
29
35
  });
30
36
  var _cssToXcss = require("./css-to-xcss");
31
- var _jsxElementToBox = require("./jsx-element-to-box");
37
+ var _jsxElementToBox = require("./jsx-element-to-box");
38
+ var _styledComponentToPrimitive = require("./styled-component-to-primitive");
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.styledComponentToPrimitive = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
10
+ var _utils = require("../utils");
11
+ var _cssToXcss = require("./css-to-xcss");
12
+ /**
13
+ * All required validation steps have been taken care of before this
14
+ * transformer is called, so it just goes ahead providing all necessary fixes
15
+ */
16
+ var styledComponentToPrimitive = exports.styledComponentToPrimitive = function styledComponentToPrimitive(_ref, context) {
17
+ var stylesRef = _ref.stylesRef,
18
+ jsxRef = _ref.jsxRef;
19
+ return function (fixer) {
20
+ // generates the new variable name: MyComponent -> myComponentStyles
21
+ var calculatedStylesVariableName = (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.id, 'Identifier') && "".concat(stylesRef.id.name.replace(stylesRef.id.name[0], stylesRef.id.name[0].toLowerCase()), "Styles");
22
+ if (!calculatedStylesVariableName) {
23
+ return [];
24
+ }
25
+ var importFixes = (0, _utils.upsertImportDeclaration)({
26
+ packageName: '@atlaskit/primitives',
27
+ specifiers: ['Box', 'xcss']
28
+ }, context, fixer);
29
+ var stylesFixes = convertStyledComponentToXcss(stylesRef, calculatedStylesVariableName, fixer);
30
+ var jsxFixes = convertJsxCallSite(jsxRef, calculatedStylesVariableName, fixer);
31
+ return [importFixes].concat(stylesFixes, jsxFixes).filter(function (fix) {
32
+ return Boolean(fix);
33
+ }); // Some of the transformers can return arrays with undefined, so filter them out
34
+ };
35
+ };
36
+
37
+ var convertStyledComponentToXcss = function convertStyledComponentToXcss(stylesRef, newStylesVariableName, fixer) {
38
+ var fixes = [];
39
+
40
+ // renames the variable from MyComponent to myComponentStyles
41
+ fixes.push(fixer.replaceText(stylesRef.id, newStylesVariableName));
42
+
43
+ // renames the function call from styled.<tag> to xcss
44
+ if (stylesRef.init && (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.init, 'CallExpression')) {
45
+ fixes.push(fixer.replaceText(stylesRef.init.callee, 'xcss'));
46
+ }
47
+
48
+ // converts CSS values to XCSS-compatible tokens
49
+ if (stylesRef.init && (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.init, 'CallExpression')) {
50
+ var objectExpression = stylesRef.init.arguments[0];
51
+ if ((0, _eslintCodemodUtils.isNodeOfType)(objectExpression, 'ObjectExpression')) {
52
+ fixes.push.apply(fixes, (0, _toConsumableArray2.default)((0, _cssToXcss.styledObjectToXcssTokens)(objectExpression, fixer)));
53
+ }
54
+ }
55
+ return fixes;
56
+ };
57
+ var convertJsxCallSite = function convertJsxCallSite(jsxRef, newStylesVariableName, fixer) {
58
+ var fixes = [];
59
+
60
+ // renames the JSX call site
61
+ if ((0, _eslintCodemodUtils.isNodeOfType)(jsxRef.parent, 'JSXElement')) {
62
+ fixes.push.apply(fixes, (0, _toConsumableArray2.default)((0, _utils.updateJSXElementName)(jsxRef.parent, 'Box', fixer)));
63
+ }
64
+
65
+ // adds xcss prop
66
+ fixes.push(fixer.insertTextAfter(jsxRef.name, " xcss={".concat(newStylesVariableName, "}")));
67
+ return fixes;
68
+ };
@@ -12,6 +12,22 @@ var _eslintCodemodUtils = require("eslint-codemod-utils");
12
12
  */
13
13
  var convertASTObjectExpressionToJSObject = exports.convertASTObjectExpressionToJSObject = function convertASTObjectExpressionToJSObject(styles) {
14
14
  var styleObj = {};
15
+
16
+ // if we see any spread props we stop and return false to indicate this is unsupported
17
+ if (!styles.properties.every(function (prop) {
18
+ return (0, _eslintCodemodUtils.isNodeOfType)(prop, 'Property');
19
+ })) {
20
+ return false;
21
+ }
22
+
23
+ // TODO: We need to harden this logic.
24
+ // It currently generates a false positive for:
25
+ // styled.div({
26
+ // marginTop: "0px",
27
+ // marginBottom: token("space.100", "8px"),
28
+ // })
29
+ // as the value for `marginBottom` is not a string, so it is just skipped
30
+ // from the resulting map and this causes the rule to trigger when it shouldn't
15
31
  styles.properties.forEach(function (prop) {
16
32
  if (!(0, _eslintCodemodUtils.isNodeOfType)(prop, 'Property')) {
17
33
  return;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findValidJsxUsageToTransform = void 0;
7
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
8
+ /**
9
+ * Given a component name finds its JSX usages and performs some
10
+ * additional validations to ensure transformation can be done correctly
11
+ */
12
+ var findValidJsxUsageToTransform = exports.findValidJsxUsageToTransform = function findValidJsxUsageToTransform(componentName, scope) {
13
+ var variableDeclaration = scope.variables.find(function (v) {
14
+ return v.name === componentName;
15
+ });
16
+ if (!variableDeclaration) {
17
+ return;
18
+ }
19
+
20
+ // length here should be exactly 2 to indicate only two references:
21
+ // one being the variable declaration itself
22
+ // second being the JSX call site
23
+ // we might consider handling multiple local JSX call sites in the future
24
+ // but "this is good enough for now"™️
25
+ if (variableDeclaration.references.length !== 2) {
26
+ return;
27
+ }
28
+ var jsxUsage = variableDeclaration.references[1].identifier;
29
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxUsage, 'JSXIdentifier')) {
30
+ return;
31
+ }
32
+ var jsxOpeningElement = jsxUsage.parent;
33
+ // we could relatively easily support some safe attributes like
34
+ // "id" or "testId" but support will be expanded as we go
35
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxOpeningElement, 'JSXOpeningElement') || jsxOpeningElement.attributes.length > 0) {
36
+ return;
37
+ }
38
+ return jsxOpeningElement;
39
+ };
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findValidStyledComponentCall = void 0;
7
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
8
+ var _isValidTagName = require("./is-valid-tag-name");
9
+ /**
10
+ * returns a variable reference if preconditions are favourable for
11
+ * the transformation to proceed, undefined otherwise.
12
+ */
13
+
14
+ var findValidStyledComponentCall = exports.findValidStyledComponentCall = function findValidStyledComponentCall(node) {
15
+ // halts unless we are dealing with a styled component
16
+ if (!isStyledCallExpression(node)) {
17
+ return;
18
+ }
19
+ // halts if the component is being exported directly
20
+ if ((0, _eslintCodemodUtils.closestOfType)(node, 'ExportNamedDeclaration')) {
21
+ return;
22
+ }
23
+ var styledComponentVariableRef = node.parent;
24
+ // halts if the styled component is not assigned to a variable immediately
25
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(styledComponentVariableRef, 'VariableDeclarator')) {
26
+ return;
27
+ }
28
+ return styledComponentVariableRef;
29
+ };
30
+
31
+ /**
32
+ * Some verbose precondition checks but all it does is check
33
+ * a call expression is of form `styled.div` or `styled2.div`
34
+ *
35
+ * In the future it could be enhanced to double check `styled` and `styled2`
36
+ * are Compiled imports but as is should work for the majority of use cases
37
+ */
38
+ var isStyledCallExpression = function isStyledCallExpression(call) {
39
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(call, 'CallExpression')) {
40
+ return false;
41
+ }
42
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee, 'MemberExpression')) {
43
+ return false;
44
+ }
45
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee.object, 'Identifier') || !(0, _eslintCodemodUtils.isNodeOfType)(call.callee.property, 'Identifier')) {
46
+ return false;
47
+ }
48
+ if (/^styled2?$/.test(call.callee.object.name) && _isValidTagName.validPrimitiveElements.has(call.callee.property.name)) {
49
+ return true;
50
+ }
51
+ return false;
52
+ };
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "convertASTObjectExpressionToJSObject", {
15
15
  return _convertAstObjectExpressionToJsObject.convertASTObjectExpressionToJSObject;
16
16
  }
17
17
  });
18
+ Object.defineProperty(exports, "findValidJsxUsageToTransform", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _findValidJsxUsageToTransform.findValidJsxUsageToTransform;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "findValidStyledComponentCall", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _findValidStyledComponentCall.findValidStyledComponentCall;
28
+ }
29
+ });
18
30
  Object.defineProperty(exports, "getAttributeValueIdentifier", {
19
31
  enumerable: true,
20
32
  get: function get() {
@@ -51,6 +63,12 @@ Object.defineProperty(exports, "isFunctionNamed", {
51
63
  return _isFunctionNamed.isFunctionNamed;
52
64
  }
53
65
  });
66
+ Object.defineProperty(exports, "isValidCssPropertiesToTransform", {
67
+ enumerable: true,
68
+ get: function get() {
69
+ return _isValidCssPropertiesToTransform.isValidCssPropertiesToTransform;
70
+ }
71
+ });
54
72
  Object.defineProperty(exports, "isValidTagName", {
55
73
  enumerable: true,
56
74
  get: function get() {
@@ -77,12 +95,15 @@ Object.defineProperty(exports, "upsertImportDeclaration", {
77
95
  });
78
96
  var _containsOnlySupportedAttrs = require("./contains-only-supported-attrs");
79
97
  var _convertAstObjectExpressionToJsObject = require("./convert-ast-object-expression-to-js-object");
98
+ var _findValidJsxUsageToTransform = require("./find-valid-jsx-usage-to-transform");
99
+ var _findValidStyledComponentCall = require("./find-valid-styled-component-call");
80
100
  var _getAttributeValueIdentifier = require("./get-attribute-value-identifier");
81
101
  var _getFunctionArgumentAtPos = require("./get-function-argument-at-pos");
82
102
  var _getJsxAttributeByName = require("./get-jsx-attribute-by-name");
83
103
  var _getVariableDefinitionValue = require("./get-variable-definition-value");
84
104
  var _getVariableUsageCount = require("./get-variable-usage-count");
85
105
  var _isFunctionNamed = require("./is-function-named");
106
+ var _isValidCssPropertiesToTransform = require("./is-valid-css-properties-to-transform");
86
107
  var _isValidTagName = require("./is-valid-tag-name");
87
108
  var _updateJsxAttributeByName = require("./update-jsx-attribute-by-name");
88
109
  var _updateJsxElementName = require("./update-jsx-element-name");