@atlaskit/eslint-plugin-design-system 8.23.2 → 8.23.4

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 (155) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +1 -0
  3. package/constellation/index/usage.mdx +39 -0
  4. package/dist/cjs/ast-nodes/index.js +7 -0
  5. package/dist/cjs/ast-nodes/object.js +84 -0
  6. package/dist/cjs/presets/all.codegen.js +2 -1
  7. package/dist/cjs/presets/recommended.codegen.js +2 -1
  8. package/dist/cjs/rules/consistent-css-prop-usage/index.js +25 -5
  9. package/dist/cjs/rules/index.codegen.js +3 -1
  10. package/dist/cjs/rules/local-cx-xcss/index.js +51 -0
  11. package/dist/cjs/rules/no-css-tagged-template-expression/create-no-tagged-template-expression-rule/index.js +3 -1
  12. package/dist/cjs/rules/no-css-tagged-template-expression/index.js +2 -2
  13. package/dist/cjs/rules/use-primitives/index.js +4 -30
  14. package/dist/cjs/rules/use-primitives/transformers/compiled-styled/convert-jsx-call-site.js +39 -0
  15. package/dist/cjs/rules/use-primitives/transformers/compiled-styled/convert-styled-component-call-to-jsx.js +44 -0
  16. package/dist/cjs/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-styled-component-call.js +5 -2
  17. package/dist/cjs/rules/use-primitives/transformers/compiled-styled/index.js +117 -0
  18. package/dist/cjs/rules/use-primitives/transformers/compiled-styled/supported.js +10 -0
  19. package/dist/cjs/rules/use-primitives/transformers/compiled-styled/upsert-import-declaration.js +37 -0
  20. package/dist/cjs/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +27 -0
  21. package/dist/cjs/rules/use-primitives/transformers/emotion-css/index.js +4 -47
  22. package/dist/cjs/rules/use-primitives/transformers/emotion-css/supported.js +2 -44
  23. package/dist/cjs/rules/use-primitives/transformers/emotion-css/upsert-import-declaration.js +37 -0
  24. package/dist/cjs/rules/use-primitives/transformers/index.js +10 -10
  25. package/dist/cjs/rules/use-primitives/utils/index.js +1 -43
  26. package/dist/cjs/rules/utils/is-supported-import.js +61 -0
  27. package/dist/es2019/ast-nodes/index.js +1 -0
  28. package/dist/es2019/ast-nodes/object.js +79 -0
  29. package/dist/es2019/presets/all.codegen.js +2 -1
  30. package/dist/es2019/presets/recommended.codegen.js +2 -1
  31. package/dist/es2019/rules/consistent-css-prop-usage/index.js +23 -5
  32. package/dist/es2019/rules/index.codegen.js +3 -1
  33. package/dist/es2019/rules/local-cx-xcss/index.js +45 -0
  34. package/dist/es2019/rules/no-css-tagged-template-expression/create-no-tagged-template-expression-rule/index.js +3 -1
  35. package/dist/es2019/rules/no-css-tagged-template-expression/index.js +2 -2
  36. package/dist/es2019/rules/use-primitives/index.js +4 -30
  37. package/dist/es2019/rules/use-primitives/transformers/compiled-styled/convert-jsx-call-site.js +29 -0
  38. package/dist/es2019/rules/use-primitives/transformers/compiled-styled/convert-styled-component-call-to-jsx.js +37 -0
  39. package/dist/es2019/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-styled-component-call.js +2 -2
  40. package/dist/es2019/rules/use-primitives/transformers/compiled-styled/index.js +107 -0
  41. package/dist/es2019/rules/use-primitives/transformers/compiled-styled/supported.js +4 -0
  42. package/dist/es2019/rules/use-primitives/transformers/compiled-styled/upsert-import-declaration.js +30 -0
  43. package/dist/es2019/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +19 -0
  44. package/dist/es2019/rules/use-primitives/transformers/emotion-css/index.js +4 -48
  45. package/dist/es2019/rules/use-primitives/transformers/emotion-css/supported.js +1 -43
  46. package/dist/es2019/rules/use-primitives/transformers/emotion-css/upsert-import-declaration.js +30 -0
  47. package/dist/es2019/rules/use-primitives/transformers/index.js +2 -2
  48. package/dist/es2019/rules/use-primitives/utils/index.js +1 -7
  49. package/dist/es2019/rules/utils/is-supported-import.js +52 -0
  50. package/dist/esm/ast-nodes/index.js +1 -0
  51. package/dist/esm/ast-nodes/object.js +79 -0
  52. package/dist/esm/presets/all.codegen.js +2 -1
  53. package/dist/esm/presets/recommended.codegen.js +2 -1
  54. package/dist/esm/rules/consistent-css-prop-usage/index.js +25 -5
  55. package/dist/esm/rules/index.codegen.js +3 -1
  56. package/dist/esm/rules/local-cx-xcss/index.js +45 -0
  57. package/dist/esm/rules/no-css-tagged-template-expression/create-no-tagged-template-expression-rule/index.js +3 -1
  58. package/dist/esm/rules/no-css-tagged-template-expression/index.js +2 -2
  59. package/dist/esm/rules/use-primitives/index.js +4 -30
  60. package/dist/esm/rules/use-primitives/transformers/compiled-styled/convert-jsx-call-site.js +30 -0
  61. package/dist/esm/rules/use-primitives/transformers/compiled-styled/convert-styled-component-call-to-jsx.js +38 -0
  62. package/dist/esm/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-styled-component-call.js +2 -2
  63. package/dist/esm/rules/use-primitives/transformers/compiled-styled/index.js +107 -0
  64. package/dist/esm/rules/use-primitives/transformers/compiled-styled/supported.js +4 -0
  65. package/dist/esm/rules/use-primitives/transformers/compiled-styled/upsert-import-declaration.js +29 -0
  66. package/dist/esm/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +19 -0
  67. package/dist/esm/rules/use-primitives/transformers/emotion-css/index.js +4 -47
  68. package/dist/esm/rules/use-primitives/transformers/emotion-css/supported.js +1 -43
  69. package/dist/esm/rules/use-primitives/transformers/emotion-css/upsert-import-declaration.js +29 -0
  70. package/dist/esm/rules/use-primitives/transformers/index.js +2 -2
  71. package/dist/esm/rules/use-primitives/utils/index.js +1 -7
  72. package/dist/esm/rules/utils/is-supported-import.js +54 -0
  73. package/dist/types/ast-nodes/index.d.ts +1 -0
  74. package/dist/types/ast-nodes/object.d.ts +39 -0
  75. package/dist/types/index.codegen.d.ts +2 -0
  76. package/dist/types/presets/all.codegen.d.ts +2 -1
  77. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  78. package/dist/types/rules/index.codegen.d.ts +1 -0
  79. package/dist/types/rules/local-cx-xcss/index.d.ts +3 -0
  80. package/dist/types/rules/no-css-tagged-template-expression/create-no-tagged-template-expression-rule/index.d.ts +3 -4
  81. package/dist/types/rules/use-primitives/transformers/compiled-styled/convert-jsx-call-site.d.ts +17 -0
  82. package/dist/types/rules/use-primitives/transformers/compiled-styled/convert-styled-component-call-to-jsx.d.ts +17 -0
  83. package/dist/types/rules/use-primitives/transformers/compiled-styled/index.d.ts +25 -0
  84. package/dist/types/rules/use-primitives/transformers/compiled-styled/supported.d.ts +2 -0
  85. package/dist/types/rules/use-primitives/transformers/compiled-styled/upsert-import-declaration.d.ts +14 -0
  86. package/dist/types/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.d.ts +7 -0
  87. package/dist/types/rules/use-primitives/transformers/emotion-css/index.d.ts +0 -19
  88. package/dist/types/rules/use-primitives/transformers/emotion-css/supported.d.ts +0 -7
  89. package/dist/types/rules/use-primitives/transformers/emotion-css/upsert-import-declaration.d.ts +14 -0
  90. package/dist/types/rules/use-primitives/transformers/index.d.ts +2 -2
  91. package/dist/types/rules/use-primitives/utils/index.d.ts +0 -6
  92. package/dist/types/rules/utils/is-supported-import.d.ts +16 -0
  93. package/dist/types-ts4.5/ast-nodes/index.d.ts +1 -0
  94. package/dist/types-ts4.5/ast-nodes/object.d.ts +39 -0
  95. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  96. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  97. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  98. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  99. package/dist/types-ts4.5/rules/local-cx-xcss/index.d.ts +3 -0
  100. package/dist/types-ts4.5/rules/no-css-tagged-template-expression/create-no-tagged-template-expression-rule/index.d.ts +3 -4
  101. package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/convert-jsx-call-site.d.ts +17 -0
  102. package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/convert-styled-component-call-to-jsx.d.ts +17 -0
  103. package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/index.d.ts +25 -0
  104. package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/supported.d.ts +2 -0
  105. package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/upsert-import-declaration.d.ts +14 -0
  106. package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.d.ts +7 -0
  107. package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/index.d.ts +0 -19
  108. package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/supported.d.ts +0 -7
  109. package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/upsert-import-declaration.d.ts +14 -0
  110. package/dist/types-ts4.5/rules/use-primitives/transformers/index.d.ts +2 -2
  111. package/dist/types-ts4.5/rules/use-primitives/utils/index.d.ts +0 -6
  112. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +16 -0
  113. package/package.json +1 -1
  114. package/dist/cjs/rules/no-css-tagged-template-expression/is-supported-import.js +0 -29
  115. package/dist/cjs/rules/use-primitives/transformers/jsx-element-to-box.js +0 -26
  116. package/dist/cjs/rules/use-primitives/transformers/styled-component-to-primitive.js +0 -68
  117. package/dist/cjs/rules/use-primitives/utils/contains-only-supported-attrs.js +0 -19
  118. package/dist/cjs/rules/use-primitives/utils/is-valid-tag-name.js +0 -13
  119. package/dist/cjs/rules/use-primitives/utils/update-jsx-element-name.js +0 -16
  120. package/dist/cjs/rules/use-primitives/utils/upsert-import-declaration.js +0 -80
  121. package/dist/es2019/rules/no-css-tagged-template-expression/is-supported-import.js +0 -21
  122. package/dist/es2019/rules/use-primitives/transformers/jsx-element-to-box.js +0 -16
  123. package/dist/es2019/rules/use-primitives/transformers/styled-component-to-primitive.js +0 -59
  124. package/dist/es2019/rules/use-primitives/utils/contains-only-supported-attrs.js +0 -13
  125. package/dist/es2019/rules/use-primitives/utils/is-valid-tag-name.js +0 -7
  126. package/dist/es2019/rules/use-primitives/utils/update-jsx-element-name.js +0 -12
  127. package/dist/es2019/rules/use-primitives/utils/upsert-import-declaration.js +0 -76
  128. package/dist/esm/rules/no-css-tagged-template-expression/is-supported-import.js +0 -23
  129. package/dist/esm/rules/use-primitives/transformers/jsx-element-to-box.js +0 -19
  130. package/dist/esm/rules/use-primitives/transformers/styled-component-to-primitive.js +0 -61
  131. package/dist/esm/rules/use-primitives/utils/contains-only-supported-attrs.js +0 -13
  132. package/dist/esm/rules/use-primitives/utils/is-valid-tag-name.js +0 -7
  133. package/dist/esm/rules/use-primitives/utils/update-jsx-element-name.js +0 -10
  134. package/dist/esm/rules/use-primitives/utils/upsert-import-declaration.js +0 -75
  135. package/dist/types/rules/no-css-tagged-template-expression/is-supported-import.d.ts +0 -12
  136. package/dist/types/rules/use-primitives/transformers/jsx-element-to-box.d.ts +0 -3
  137. package/dist/types/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +0 -13
  138. package/dist/types/rules/use-primitives/utils/contains-only-supported-attrs.d.ts +0 -2
  139. package/dist/types/rules/use-primitives/utils/is-valid-tag-name.d.ts +0 -3
  140. package/dist/types/rules/use-primitives/utils/update-jsx-element-name.d.ts +0 -3
  141. package/dist/types/rules/use-primitives/utils/upsert-import-declaration.d.ts +0 -11
  142. package/dist/types-ts4.5/rules/no-css-tagged-template-expression/is-supported-import.d.ts +0 -12
  143. package/dist/types-ts4.5/rules/use-primitives/transformers/jsx-element-to-box.d.ts +0 -3
  144. package/dist/types-ts4.5/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +0 -13
  145. package/dist/types-ts4.5/rules/use-primitives/utils/contains-only-supported-attrs.d.ts +0 -2
  146. package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-tag-name.d.ts +0 -3
  147. package/dist/types-ts4.5/rules/use-primitives/utils/update-jsx-element-name.d.ts +0 -3
  148. package/dist/types-ts4.5/rules/use-primitives/utils/upsert-import-declaration.d.ts +0 -11
  149. /package/dist/cjs/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-jsx-usage-to-transform.js +0 -0
  150. /package/dist/es2019/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-jsx-usage-to-transform.js +0 -0
  151. /package/dist/esm/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-jsx-usage-to-transform.js +0 -0
  152. /package/dist/types/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-jsx-usage-to-transform.d.ts +0 -0
  153. /package/dist/types/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-styled-component-call.d.ts +0 -0
  154. /package/dist/types-ts4.5/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-jsx-usage-to-transform.d.ts +0 -0
  155. /package/dist/types-ts4.5/rules/use-primitives/{utils → transformers/compiled-styled}/find-valid-styled-component-call.d.ts +0 -0
@@ -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::3b93cfbbe0ea14514b9600509632394b>>
3
+ * @codegen <<SignedSource::839224bfab98c1ddf6018dec5320968e>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  export default {
@@ -9,6 +9,7 @@ export default {
9
9
  '@atlaskit/design-system/consistent-css-prop-usage': 'error',
10
10
  '@atlaskit/design-system/ensure-design-token-usage': 'error',
11
11
  '@atlaskit/design-system/icon-label': 'warn',
12
+ '@atlaskit/design-system/local-cx-xcss': 'error',
12
13
  '@atlaskit/design-system/no-banned-imports': 'error',
13
14
  '@atlaskit/design-system/no-deprecated-apis': 'error',
14
15
  '@atlaskit/design-system/no-deprecated-design-token-usage': 'warn',
@@ -3,6 +3,8 @@
3
3
  import { getIdentifierInParentScope, isNodeOfType } from 'eslint-codemod-utils';
4
4
  import assign from 'lodash/assign';
5
5
  import { createLintRule } from '../utils/create-rule';
6
+ // File-level tracking of styles hoisted from the cssOnTopOfModule/cssAtBottomOfModule fixers
7
+ let hoistedCss = [];
6
8
  function isCssCallExpression(node, cssFunctions) {
7
9
  cssFunctions = [...cssFunctions, 'cssMap'];
8
10
  return !!(isNodeOfType(node, 'CallExpression') && node.callee && node.callee.type === 'Identifier' && cssFunctions.includes(node.callee.name) && node.arguments.length && node.arguments[0].type === 'ObjectExpression');
@@ -13,11 +15,11 @@ function findSpreadProperties(node) {
13
15
  // @ts-ignore
14
16
  property.type === 'ExperimentalSpreadProperty');
15
17
  }
16
- const getTopLevelNode = expression => {
18
+ const getProgramNode = expression => {
17
19
  while (expression.parent.type !== 'Program') {
18
20
  expression = expression.parent;
19
21
  }
20
- return expression;
22
+ return expression.parent;
21
23
  };
22
24
 
23
25
  // TODO: This can be optimised by implementing a fixer at the very end (Program:exit) and handling all validations at once
@@ -34,7 +36,7 @@ const getDeclaratorString = context => {
34
36
  var _scope;
35
37
  scope = (_scope = scope) === null || _scope === void 0 ? void 0 : _scope.upper;
36
38
  }
37
- const variables = scope.variables.map(variable => variable.name);
39
+ const variables = scope.variables.map(variable => variable.name).concat(hoistedCss);
38
40
  let count = 2;
39
41
  let declaratorName = 'styles';
40
42
 
@@ -47,6 +49,9 @@ const getDeclaratorString = context => {
47
49
  count++;
48
50
  }
49
51
  }
52
+
53
+ // Keep track of it by adding it to the hoistedCss global array
54
+ hoistedCss = [...hoistedCss, `${declaratorName}${count}`];
50
55
  return `${declaratorName}${count}`;
51
56
  };
52
57
  function analyzeIdentifier(context, sourceIdentifier, configuration) {
@@ -107,7 +112,17 @@ function analyzeIdentifier(context, sourceIdentifier, configuration) {
107
112
  */
108
113
  const fixCssNotInModuleScope = (fixer, context, configuration, node, cssAttributeName) => {
109
114
  const sourceCode = context.getSourceCode();
110
- const topLevelNode = getTopLevelNode(node);
115
+
116
+ // Get the program node in order to properly position the hoisted styles
117
+ const programNode = getProgramNode(node);
118
+ let fixerNodePlacement = programNode;
119
+ if (configuration.stylesPlacement === 'bottom') {
120
+ // The last value is the bottom of the file
121
+ fixerNodePlacement = programNode.body[programNode.body.length - 1];
122
+ } else {
123
+ // Place after the last ImportDeclaration
124
+ fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(node => node.type !== 'ImportDeclaration');
125
+ }
111
126
  let moduleString;
112
127
  let implementFixer = [];
113
128
  if (node.type === 'Identifier') {
@@ -129,7 +144,7 @@ const fixCssNotInModuleScope = (fixer, context, configuration, node, cssAttribut
129
144
  }
130
145
  return [...implementFixer,
131
146
  // Insert the node either before or after
132
- configuration.stylesPlacement === 'bottom' ? fixer.insertTextAfter(topLevelNode, '\n' + moduleString) : fixer.insertTextBefore(topLevelNode, moduleString + '\n')];
147
+ configuration.stylesPlacement === 'bottom' ? fixer.insertTextAfter(fixerNodePlacement, '\n' + moduleString) : fixer.insertTextBefore(fixerNodePlacement, moduleString + '\n')];
133
148
  };
134
149
 
135
150
  /**
@@ -255,6 +270,9 @@ const rule = createLintRule({
255
270
  name,
256
271
  value
257
272
  } = node;
273
+
274
+ // Always reset to empty array
275
+ hoistedCss = [];
258
276
  if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
259
277
  // When not a jsx expression. For eg. css=""
260
278
  if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::14cdfdcbd8b999ee097a1a5b245d7117>>
3
+ * @codegen <<SignedSource::a67afc33adf50db651edb1cf12e16ef3>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import consistentCssPropUsage from './consistent-css-prop-usage';
7
7
  import ensureDesignTokenUsage from './ensure-design-token-usage';
8
8
  import ensureDesignTokenUsagePreview from './ensure-design-token-usage-preview';
9
9
  import iconLabel from './icon-label';
10
+ import localCxXcss from './local-cx-xcss';
10
11
  import noBannedImports from './no-banned-imports';
11
12
  import noCssTaggedTemplateExpression from './no-css-tagged-template-expression';
12
13
  import noDeprecatedApis from './no-deprecated-apis';
@@ -30,6 +31,7 @@ export default {
30
31
  'ensure-design-token-usage': ensureDesignTokenUsage,
31
32
  'ensure-design-token-usage/preview': ensureDesignTokenUsagePreview,
32
33
  'icon-label': iconLabel,
34
+ 'local-cx-xcss': localCxXcss,
33
35
  'no-banned-imports': noBannedImports,
34
36
  'no-css-tagged-template-expression': noCssTaggedTemplateExpression,
35
37
  'no-deprecated-apis': noDeprecatedApis,
@@ -0,0 +1,45 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import { CSS_IN_JS_IMPORTS, isCxFunction } from '../utils/is-supported-import';
3
+ const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
4
+ function getParentJSXAttribute(node) {
5
+ let parent = node.parent;
6
+ while (parent && parent.type !== 'JSXAttribute') {
7
+ parent = parent.parent;
8
+ }
9
+ if (parent && parent.type === 'JSXAttribute') {
10
+ return parent;
11
+ }
12
+ return null;
13
+ }
14
+ export const rule = createLintRule({
15
+ meta: {
16
+ name: 'local-cx-xcss',
17
+ docs: {
18
+ description: 'Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element.',
19
+ recommended: true,
20
+ severity: 'error'
21
+ },
22
+ messages: {
23
+ 'local-cx-xcss': 'The cx function should only be declared inside the xcss prop to simplify tracking styles that are applied to a jsx element.'
24
+ },
25
+ type: 'problem'
26
+ },
27
+ create(context) {
28
+ return {
29
+ 'CallExpression[callee.name="cx"]': node => {
30
+ if (node.type === 'CallExpression' && isCxFunction(node.callee, context.getScope().references, IMPORT_SOURCES)) {
31
+ const parentJSXAttribute = getParentJSXAttribute(node);
32
+ const propName = parentJSXAttribute === null || parentJSXAttribute === void 0 ? void 0 : parentJSXAttribute.name.name.toString();
33
+ if (propName && /[xX]css$/.test(propName)) {
34
+ return;
35
+ }
36
+ context.report({
37
+ node,
38
+ messageId: 'local-cx-xcss'
39
+ });
40
+ }
41
+ }
42
+ };
43
+ }
44
+ });
45
+ export default rule;
@@ -1,15 +1,17 @@
1
1
  // Original source from Compiled https://github.com/atlassian-labs/compiled/blob/master/packages/eslint-plugin/src/utils/create-no-tagged-template-expression-rule/index.ts
2
2
  // eslint-disable-next-line import/no-extraneous-dependencies
3
3
 
4
+ import { CSS_IN_JS_IMPORTS } from '../../utils/is-supported-import';
4
5
  import { generate } from './generate';
5
6
  import { getTaggedTemplateExpressionOffset } from './get-tagged-template-expression-offset';
6
7
  import { toArguments } from './to-arguments';
8
+ const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.emotionReact, CSS_IN_JS_IMPORTS.emotionCore, CSS_IN_JS_IMPORTS.styledComponents];
7
9
  export const createNoTaggedTemplateExpressionRule = (isUsage, messageId) => context => ({
8
10
  TaggedTemplateExpression(node) {
9
11
  const {
10
12
  references
11
13
  } = context.getScope();
12
- if (!isUsage(node.tag, references)) {
14
+ if (!isUsage(node.tag, references, IMPORT_SOURCES)) {
13
15
  return;
14
16
  }
15
17
  context.report({
@@ -1,6 +1,6 @@
1
1
  import { createLintRule } from '../utils/create-rule';
2
+ import { isCss } from '../utils/is-supported-import';
2
3
  import { createNoTaggedTemplateExpressionRule } from './create-no-tagged-template-expression-rule';
3
- import { isSupportedImport } from './is-supported-import';
4
4
  const rule = createLintRule({
5
5
  meta: {
6
6
  name: 'no-css-tagged-template-expression',
@@ -15,6 +15,6 @@ const rule = createLintRule({
15
15
  unexpectedTaggedTemplate: 'Unexpected `css` tagged template expression'
16
16
  }
17
17
  },
18
- create: createNoTaggedTemplateExpressionRule(isSupportedImport('css'), 'unexpectedTaggedTemplate')
18
+ create: createNoTaggedTemplateExpressionRule(isCss, 'unexpectedTaggedTemplate')
19
19
  });
20
20
  export default rule;
@@ -1,9 +1,6 @@
1
- import { isNodeOfType } from 'eslint-codemod-utils';
2
1
  import { createLintRule } from '../utils/create-rule';
3
2
  import { getConfig } from './config';
4
- import { styledComponentToPrimitive } from './transformers';
5
- import { EmotionCSS } from './transformers/emotion-css';
6
- import { findValidJsxUsageToTransform, findValidStyledComponentCall, isValidCssPropertiesToTransform } from './utils';
3
+ import { CompiledStyled, EmotionCSS } from './transformers';
7
4
  const boxDocsUrl = 'https://atlassian.design/components/primitives/box';
8
5
  const rule = createLintRule({
9
6
  meta: {
@@ -25,32 +22,9 @@ const rule = createLintRule({
25
22
  return {
26
23
  // transforms styled.<html>(...) usages
27
24
  CallExpression(node) {
28
- if (!config.patterns.includes('compiled-styled-object')) {
29
- return;
30
- }
31
- if (!isNodeOfType(node, 'CallExpression')) {
32
- return;
33
- }
34
- const styledComponentVariableRef = findValidStyledComponentCall(node);
35
- if (!styledComponentVariableRef || !isNodeOfType(styledComponentVariableRef.id, 'Identifier') || !isValidCssPropertiesToTransform(node, config)) {
36
- return;
37
- }
38
- const styledComponentJsxRef = findValidJsxUsageToTransform(styledComponentVariableRef.id.name, context.getScope());
39
- if (!styledComponentJsxRef) {
40
- return;
41
- }
42
-
43
- // if we have both references at this point then we can offer a fix \o/
44
- context.report({
45
- node: styledComponentVariableRef,
46
- messageId: 'preferPrimitivesBox',
47
- suggest: [{
48
- desc: `Convert ${styledComponentVariableRef.id.name} to Box`,
49
- fix: styledComponentToPrimitive({
50
- stylesRef: styledComponentVariableRef,
51
- jsxRef: styledComponentJsxRef
52
- }, context)
53
- }]
25
+ CompiledStyled.lint(node, {
26
+ context,
27
+ config
54
28
  });
55
29
  },
56
30
  // transforms <div css={...}> usages
@@ -0,0 +1,29 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import * as ast from '../../../../ast-nodes';
3
+
4
+ /**
5
+ * Transforms a JSXElement from:
6
+ * ```
7
+ * <div>
8
+ * // ...
9
+ * </div>
10
+ * ```
11
+ * to
12
+ * ```
13
+ * <Box xcss={myStyles}>
14
+ * // ...
15
+ * </Box>
16
+ * ```
17
+ */
18
+ export const convertJsxCallSite = (jsxElement, newStylesVariableName, fixer) => {
19
+ const fixes = [];
20
+
21
+ // renames the JSX call site
22
+ if (isNodeOfType(jsxElement, 'JSXElement')) {
23
+ fixes.push(...ast.JSXElement.updateName(jsxElement, 'Box', fixer));
24
+ }
25
+
26
+ // adds xcss prop
27
+ fixes.push(fixer.insertTextAfter(jsxElement.openingElement.name, ` xcss={${newStylesVariableName}}`));
28
+ return fixes;
29
+ };
@@ -0,0 +1,37 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { styledObjectToXcssTokens } from '../css-to-xcss';
3
+
4
+ /**
5
+ * Transforms a variable like:
6
+ * ```
7
+ * const MyComponent = styled.div({
8
+ * padding: '8px',
9
+ * })
10
+ * ```
11
+ * to
12
+ * ```
13
+ * const myComponentStyles = xcss({
14
+ * padding: 'space.100',
15
+ * })
16
+ * ```
17
+ */
18
+ export const convertStyledComponentToXcss = (styles, newStylesVariableName, fixer) => {
19
+ const fixes = [];
20
+
21
+ // renames the variable from MyComponent to myComponentStyles
22
+ fixes.push(fixer.replaceText(styles.id, newStylesVariableName));
23
+
24
+ // renames the function call from styled.<tag> to xcss
25
+ if (styles.init && isNodeOfType(styles.init, 'CallExpression')) {
26
+ fixes.push(fixer.replaceText(styles.init.callee, 'xcss'));
27
+ }
28
+
29
+ // converts CSS values to XCSS-compatible tokens
30
+ if (styles.init && isNodeOfType(styles.init, 'CallExpression')) {
31
+ const objectExpression = styles.init.arguments[0];
32
+ if (isNodeOfType(objectExpression, 'ObjectExpression')) {
33
+ fixes.push(...styledObjectToXcssTokens(objectExpression, fixer));
34
+ }
35
+ }
36
+ return fixes;
37
+ };
@@ -1,5 +1,5 @@
1
1
  import { closestOfType, isNodeOfType } from 'eslint-codemod-utils';
2
- import { validPrimitiveElements } from './is-valid-tag-name';
2
+ import * as supported from './supported';
3
3
 
4
4
  /**
5
5
  * returns a variable reference if preconditions are favourable for
@@ -41,7 +41,7 @@ const isStyledCallExpression = call => {
41
41
  if (!isNodeOfType(call.callee.object, 'Identifier') || !isNodeOfType(call.callee.property, 'Identifier')) {
42
42
  return false;
43
43
  }
44
- if (/^styled2?$/.test(call.callee.object.name) && validPrimitiveElements.has(call.callee.property.name)) {
44
+ if (/^styled2?$/.test(call.callee.object.name) && supported.elements.includes(call.callee.property.name)) {
45
45
  return true;
46
46
  }
47
47
  return false;
@@ -0,0 +1,107 @@
1
+ /* eslint-disable @repo/internal/react/require-jsdoc */
2
+
3
+ import { isNodeOfType } from 'eslint-codemod-utils';
4
+ import * as ast from '../../../../ast-nodes';
5
+ import { isValidCssPropertiesToTransform } from '../../utils';
6
+ import { convertJsxCallSite } from './convert-jsx-call-site';
7
+ import { convertStyledComponentToXcss } from './convert-styled-component-call-to-jsx';
8
+ import { findValidJsxUsageToTransform } from './find-valid-jsx-usage-to-transform';
9
+ import { findValidStyledComponentCall } from './find-valid-styled-component-call';
10
+ import { upsertImportDeclaration } from './upsert-import-declaration';
11
+ export const CompiledStyled = {
12
+ lint(node, {
13
+ context,
14
+ config
15
+ }) {
16
+ if (!isNodeOfType(node, 'CallExpression')) {
17
+ return;
18
+ }
19
+
20
+ // Check whether all criteria needed to make a transformation are met
21
+ const {
22
+ success,
23
+ refs
24
+ } = CompiledStyled._check(node, {
25
+ context,
26
+ config
27
+ });
28
+ if (!success || !refs) {
29
+ return;
30
+ }
31
+ context.report({
32
+ node: refs.styles,
33
+ messageId: 'preferPrimitivesBox',
34
+ suggest: [{
35
+ desc: `Convert ${ast.JSXElement.getName(refs.jsxElement)} to Box`,
36
+ fix: CompiledStyled._fix(refs, context)
37
+ }]
38
+ });
39
+ },
40
+ _check(node, {
41
+ context,
42
+ config
43
+ }) {
44
+ if (!config.patterns.includes('compiled-styled-object')) {
45
+ return {
46
+ success: false
47
+ };
48
+ }
49
+ if (!isNodeOfType(node, 'CallExpression')) {
50
+ return {
51
+ success: false
52
+ };
53
+ }
54
+ const styledComponentVariableRef = findValidStyledComponentCall(node);
55
+ if (!styledComponentVariableRef || !isNodeOfType(styledComponentVariableRef.id, 'Identifier') || !isValidCssPropertiesToTransform(node, config)) {
56
+ return {
57
+ success: false
58
+ };
59
+ }
60
+ const styledComponentJsxRef = findValidJsxUsageToTransform(styledComponentVariableRef.id.name, context.getScope());
61
+ if (!styledComponentJsxRef) {
62
+ return {
63
+ success: false
64
+ };
65
+ }
66
+ if (!isNodeOfType(styledComponentJsxRef.parent, 'JSXElement')) {
67
+ return {
68
+ success: false
69
+ };
70
+ }
71
+ const importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
72
+
73
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
74
+ if (importDeclaration.length > 1) {
75
+ return {
76
+ success: false
77
+ };
78
+ }
79
+ return {
80
+ success: true,
81
+ refs: {
82
+ styles: styledComponentVariableRef,
83
+ jsxElement: styledComponentJsxRef.parent
84
+ }
85
+ };
86
+ },
87
+ /**
88
+ * All required validation steps have been taken care of before this
89
+ * transformer is called, so it just goes ahead providing all necessary fixes
90
+ */
91
+ _fix(refs, context) {
92
+ return fixer => {
93
+ // generates the new variable name: MyComponent -> myComponentStyles
94
+ const calculatedStylesVariableName = isNodeOfType(refs.styles.id, 'Identifier') && `${refs.styles.id.name.replace(refs.styles.id.name[0], refs.styles.id.name[0].toLowerCase())}Styles`;
95
+ if (!calculatedStylesVariableName) {
96
+ return [];
97
+ }
98
+ const importFixes = upsertImportDeclaration({
99
+ module: '@atlaskit/primitives',
100
+ specifiers: ['Box', 'xcss']
101
+ }, context, fixer);
102
+ const stylesFixes = convertStyledComponentToXcss(refs.styles, calculatedStylesVariableName, fixer);
103
+ const jsxFixes = convertJsxCallSite(refs.jsxElement, calculatedStylesVariableName, fixer);
104
+ return [importFixes, ...stylesFixes, ...jsxFixes].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
105
+ };
106
+ }
107
+ };
@@ -0,0 +1,4 @@
1
+ export const elements = ['div'];
2
+ export const attributes = ['css'
3
+ // 'data-testid'
4
+ ];
@@ -0,0 +1,30 @@
1
+ import * as ast from '../../../../ast-nodes';
2
+
3
+ /**
4
+ * Currently this is defined here because it's not very general purpose.
5
+ * If we were to move this to `ast-nodes`, half the implementation would be in `Root`,
6
+ * and the other half would be in `Import`.
7
+ *
8
+ * TODO: Refactor and move to `ast-nodes`
9
+ *
10
+ * Note: It does not handle default imports, namespace imports, or aliased imports.
11
+ */
12
+ export const upsertImportDeclaration = ({
13
+ module,
14
+ specifiers
15
+ }, context, fixer) => {
16
+ // Find any imports that match the packageName
17
+ const root = context.getSourceCode().ast.body;
18
+ const importDeclarations = ast.Root.findImportsByModule(root, module);
19
+
20
+ // The import doesn't exist yet, we can just insert a whole new one
21
+ if (importDeclarations.length === 0) {
22
+ return ast.Root.insertImport(root, {
23
+ module,
24
+ specifiers
25
+ }, fixer);
26
+ }
27
+
28
+ // The import exists so, modify the existing one
29
+ return ast.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer);
30
+ };
@@ -0,0 +1,19 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import * as ast from '../../../../ast-nodes';
3
+ import * as supported from './supported';
4
+
5
+ /**
6
+ * Check that every attribute in the JSXElement is something we support.
7
+ * We do this via a whitelist in `this.attributes`. The result is we exclude
8
+ * dangerous attrs like `id` and `style`.
9
+ */
10
+ export const containsOnlySupportedAttrs = node => {
11
+ const attrs = ast.JSXElement.getAttributes(node);
12
+ return attrs.every(attr => {
13
+ if (!isNodeOfType(attr, 'JSXAttribute')) {
14
+ return false;
15
+ }
16
+ const name = ast.JSXAttribute.getName(attr);
17
+ return supported.attributes.includes(name);
18
+ });
19
+ };
@@ -4,7 +4,9 @@ import { getIdentifierInParentScope, isNodeOfType } from 'eslint-codemod-utils';
4
4
  import * as ast from '../../../../ast-nodes';
5
5
  import { getVariableDefinitionValue, getVariableUsagesCount, isValidCssPropertiesToTransform } from '../../utils';
6
6
  import { cssToXcssTransformer } from '../css-to-xcss';
7
+ import { containsOnlySupportedAttrs } from './contains-only-supported-attrs';
7
8
  import * as supported from './supported';
9
+ import { upsertImportDeclaration } from './upsert-import-declaration';
8
10
  export const EmotionCSS = {
9
11
  lint(node, {
10
12
  context,
@@ -54,7 +56,7 @@ export const EmotionCSS = {
54
56
  }
55
57
 
56
58
  // Ignore elements that contain dangerous attributes like `id`.
57
- if (!this._containsOnlySupportedAttributes(node)) {
59
+ if (!containsOnlySupportedAttrs(node)) {
58
60
  return false;
59
61
  }
60
62
 
@@ -99,7 +101,7 @@ export const EmotionCSS = {
99
101
  context
100
102
  }) {
101
103
  return fixer => {
102
- const importFix = this._upsertImportDeclaration({
104
+ const importFix = upsertImportDeclaration({
103
105
  module: '@atlaskit/primitives',
104
106
  specifiers: ['Box', 'xcss']
105
107
  }, context, fixer);
@@ -109,51 +111,5 @@ export const EmotionCSS = {
109
111
  const cssToXcssTransform = cssToXcssTransformer(node, context, fixer);
110
112
  return [importFix, attributeFix, ...elementNameFixes, ...cssToXcssTransform].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
111
113
  };
112
- },
113
-
114
- /**
115
- * Check that every attribute in the JSXElement is something we support.
116
- * We do this via a whitelist in `this.attributes`. The result is we exclude
117
- * dangerous attrs like `id` and `style`.
118
- */
119
- _containsOnlySupportedAttributes(node) {
120
- const attrs = ast.JSXElement.getAttributes(node);
121
- return attrs.every(attr => {
122
- if (!isNodeOfType(attr, 'JSXAttribute')) {
123
- return false;
124
- }
125
- if (!isNodeOfType(attr.name, 'JSXIdentifier')) {
126
- return false;
127
- }
128
- return supported.attributes.includes(attr.name.name);
129
- });
130
- },
131
- /**
132
- * Currently this is defined here because it's not very general purpose.
133
- * If we were to move this to `ast-nodes`, half the implementation would be in `Root`,
134
- * and the other half would be in `Import`.
135
- *
136
- * TODO: Refactor and move to `ast-nodes`
137
- *
138
- * Note: It does not handle default imports, namespace imports, or aliased imports.
139
- */
140
- _upsertImportDeclaration({
141
- module,
142
- specifiers
143
- }, context, fixer) {
144
- // Find any imports that match the packageName
145
- const root = context.getSourceCode().ast.body;
146
- const importDeclarations = ast.Root.findImportsByModule(root, module);
147
-
148
- // The import doesn't exist yet, we can just insert a whole new one
149
- if (importDeclarations.length === 0) {
150
- return ast.Root.insertImport(root, {
151
- module,
152
- specifiers
153
- }, fixer);
154
- }
155
-
156
- // The import exists so, modify the existing one
157
- return ast.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer);
158
114
  }
159
115
  };
@@ -1,46 +1,4 @@
1
1
  export const elements = ['div'];
2
2
  export const attributes = ['css'
3
3
  // 'data-testid'
4
- ];
5
-
6
- // TODO: https://product-fabric.atlassian.net/browse/DSP-16054
7
- const spaceTokenMap = {
8
- '0px': 'space.0',
9
- '2px': 'space.025',
10
- '4px': 'space.050',
11
- '6px': 'space.075',
12
- '8px': 'space.100',
13
- '12px': 'space.150',
14
- '16px': 'space.200',
15
- '20px': 'space.250',
16
- '24px': 'space.300',
17
- '32px': 'space.400',
18
- '40px': 'space.500',
19
- '48px': 'space.600',
20
- '64px': 'space.800',
21
- '80px': 'space.1000'
22
- };
23
- export const styles = {
24
- padding: spaceTokenMap,
25
- paddingBlock: spaceTokenMap,
26
- paddingBlockEnd: spaceTokenMap,
27
- paddingBlockStart: spaceTokenMap,
28
- paddingBottom: spaceTokenMap,
29
- paddingInline: spaceTokenMap,
30
- paddingInlineEnd: spaceTokenMap,
31
- paddingInlineStart: spaceTokenMap,
32
- paddingLeft: spaceTokenMap,
33
- paddingRight: spaceTokenMap,
34
- paddingTop: spaceTokenMap,
35
- margin: spaceTokenMap,
36
- marginBlock: spaceTokenMap,
37
- marginBlockEnd: spaceTokenMap,
38
- marginBlockStart: spaceTokenMap,
39
- marginBottom: spaceTokenMap,
40
- marginInline: spaceTokenMap,
41
- marginInlineEnd: spaceTokenMap,
42
- marginInlineStart: spaceTokenMap,
43
- marginLeft: spaceTokenMap,
44
- marginRight: spaceTokenMap,
45
- marginTop: spaceTokenMap
46
- };
4
+ ];
@@ -0,0 +1,30 @@
1
+ import * as ast from '../../../../ast-nodes';
2
+
3
+ /**
4
+ * Currently this is defined here because it's not very general purpose.
5
+ * If we were to move this to `ast-nodes`, half the implementation would be in `Root`,
6
+ * and the other half would be in `Import`.
7
+ *
8
+ * TODO: Refactor and move to `ast-nodes`
9
+ *
10
+ * Note: It does not handle default imports, namespace imports, or aliased imports.
11
+ */
12
+ export const upsertImportDeclaration = ({
13
+ module,
14
+ specifiers
15
+ }, context, fixer) => {
16
+ // Find any imports that match the packageName
17
+ const root = context.getSourceCode().ast.body;
18
+ const importDeclarations = ast.Root.findImportsByModule(root, module);
19
+
20
+ // The import doesn't exist yet, we can just insert a whole new one
21
+ if (importDeclarations.length === 0) {
22
+ return ast.Root.insertImport(root, {
23
+ module,
24
+ specifiers
25
+ }, fixer);
26
+ }
27
+
28
+ // The import exists so, modify the existing one
29
+ return ast.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer);
30
+ };
@@ -1,3 +1,3 @@
1
1
  export { cssToXcssTransformer, supportedStylesMap, spaceTokenMap } from './css-to-xcss';
2
- export { jsxElementToBoxTransformer } from './jsx-element-to-box';
3
- export { styledComponentToPrimitive } from './styled-component-to-primitive';
2
+ export { CompiledStyled } from './compiled-styled';
3
+ export { EmotionCSS } from './emotion-css';