@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,7 +1,4 @@
1
- export { containsOnlySupportedAttrs } from './contains-only-supported-attrs';
2
1
  export { convertASTObjectExpressionToJSObject } from './convert-ast-object-expression-to-js-object';
3
- export { findValidJsxUsageToTransform } from './find-valid-jsx-usage-to-transform';
4
- export { findValidStyledComponentCall } from './find-valid-styled-component-call';
5
2
  export { getAttributeValueIdentifier } from './get-attribute-value-identifier';
6
3
  export { getFunctionArgumentAtPos } from './get-function-argument-at-pos';
7
4
  export { getJSXAttributeByName } from './get-jsx-attribute-by-name';
@@ -9,7 +6,4 @@ export { getVariableDefinitionValue } from './get-variable-definition-value';
9
6
  export { getVariableUsagesCount } from './get-variable-usage-count';
10
7
  export { isFunctionNamed } from './is-function-named';
11
8
  export { isValidCssPropertiesToTransform } from './is-valid-css-properties-to-transform';
12
- export { isValidTagName } from './is-valid-tag-name';
13
- export { updateJSXAttributeByName } from './update-jsx-attribute-by-name';
14
- export { updateJSXElementName } from './update-jsx-element-name';
15
- export { upsertImportDeclaration } from './upsert-import-declaration';
9
+ export { updateJSXAttributeByName } from './update-jsx-attribute-by-name';
@@ -0,0 +1,52 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+
3
+ export const CSS_IN_JS_IMPORTS = {
4
+ compiled: '@compiled/react',
5
+ emotionReact: '@emotion/react',
6
+ emotionCore: '@emotion/core',
7
+ styledComponents: 'styled-components',
8
+ atlaskitCss: '@atlaskit/css'
9
+ };
10
+
11
+ // List of CSS-in-JS libraries an import of a valid css, cx, cssMap, etc.
12
+ // function might originate from, e.g. @compiled/react, @emotion/core.
13
+ const isSupportedImportWrapper = functionName => {
14
+ // This will need to be extended to support default imports once we start
15
+ // checking cases like `import css from '@emotion/css'`
16
+ const checkDefinitionHasImport = (def, importSources) => {
17
+ var _def$parent, _def$parent2;
18
+ return def.node.type === 'ImportSpecifier' && def.node.imported.type === 'Identifier' && def.node.imported.name === functionName && ((_def$parent = def.parent) === null || _def$parent === void 0 ? void 0 : _def$parent.type) === 'ImportDeclaration' && importSources.includes((_def$parent2 = def.parent) === null || _def$parent2 === void 0 ? void 0 : _def$parent2.source.value);
19
+ };
20
+
21
+ /**
22
+ * Checks whether:
23
+ *
24
+ * 1. a function name `nodeToCheck` matches the name of the function we
25
+ * want to check for (e.g. `cx`, `css`, `cssMap`, or `keyframes`), and
26
+ * 2. whether `nodeToCheck` originates from one of the libraries listed
27
+ * in `importSources`.
28
+ *
29
+ * @param nodeToCheck the function callee we are checking (e.g. the `css` in `css()`)
30
+ * @param referencesInScope list of references that are in scope. We'll use this
31
+ * to check where the function callee is imported from.
32
+ * @param importSources list of libraries that we want to ensure `nodeToCheck`
33
+ * comes from
34
+ *
35
+ * @returns whether the above conditions are true
36
+ */
37
+ const isSupportedImport = (nodeToCheck, referencesInScope, importSources) => {
38
+ return nodeToCheck.type === 'Identifier' && referencesInScope.some(reference => {
39
+ var _reference$resolved;
40
+ return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
41
+ });
42
+ };
43
+ return isSupportedImport;
44
+ };
45
+
46
+ // Unused functions have been commented out until we implement corresponding
47
+ // eslint rules which use them
48
+ //
49
+ export const isCss = isSupportedImportWrapper('css');
50
+ export const isCxFunction = isSupportedImportWrapper('cx');
51
+ // export const isCssMap = isLibraryImportWrapper('cssMap');
52
+ // export const isKeyframes = isLibraryImportWrapper('keyframes');
@@ -2,4 +2,5 @@ export { FunctionCall } from './function-call';
2
2
  export { Import } from './import';
3
3
  export { JSXAttribute } from './jsx-attribute';
4
4
  export { JSXElement } from './jsx-element';
5
+ export { Object } from './object';
5
6
  export { Root } from './root';
@@ -0,0 +1,79 @@
1
+ /* eslint-disable @repo/internal/react/require-jsdoc */
2
+
3
+ import { identifier, isNodeOfType, literal, property } from 'eslint-codemod-utils';
4
+ var ASTObjectExpression = {
5
+ /**
6
+ * Returns `true` if an object contains a property with the specified name, `false` otherwise.
7
+ */
8
+ hasProperty: function hasProperty(node, name) {
9
+ return !!ASTObjectExpression.getProperty(node, name);
10
+ },
11
+ /**
12
+ * Returns a key-value pair like: `padding: '8px'` from: `{ padding: '8px' }`
13
+ */
14
+ getEntryByPropertyName: function getEntryByPropertyName(node, name) {
15
+ return node.properties.find(function (property) {
16
+ if (!isNodeOfType(property, 'Property')) {
17
+ return false;
18
+ }
19
+ if (!isNodeOfType(property.key, 'Identifier')) {
20
+ return false;
21
+ }
22
+ return property.key.name === name;
23
+ });
24
+ },
25
+ deleteEntry: function deleteEntry(node, name, fixer) {
26
+ var entry = ASTObjectExpression.getEntryByPropertyName(node, name);
27
+ if (!entry) {
28
+ return [];
29
+ }
30
+ return [fixer.remove(entry)];
31
+ },
32
+ /**
33
+ * Returns a only the property @type {Property['key']} like: `padding` from: `{ padding: '8px' }`
34
+ */
35
+ getProperty: function getProperty(node, name) {
36
+ var _ASTObjectExpression$;
37
+ return (_ASTObjectExpression$ = ASTObjectExpression.getEntryByPropertyName(node, name)) === null || _ASTObjectExpression$ === void 0 ? void 0 : _ASTObjectExpression$.key;
38
+ },
39
+ /**
40
+ * Returns a only the property @type {Property['value']} like: `'8px` from: `{ padding: '8px' }`
41
+ *
42
+ * Values can be basically anything, so be careful with this
43
+ */
44
+ getValueByPropertyName: function getValueByPropertyName(node, name) {
45
+ var _ASTObjectExpression$2;
46
+ return (_ASTObjectExpression$2 = ASTObjectExpression.getEntryByPropertyName(node, name)) === null || _ASTObjectExpression$2 === void 0 ? void 0 : _ASTObjectExpression$2.value;
47
+ },
48
+ containsSpreadProps: function containsSpreadProps(node) {
49
+ return node.properties.some(function (property) {
50
+ return isNodeOfType(property, 'SpreadElement');
51
+ });
52
+ },
53
+ updateValue: function updateValue(node, propertyName, newValue, fixer) {
54
+ var value = ASTObjectExpression.getValueByPropertyName(node, propertyName);
55
+ if (value === undefined) {
56
+ throw new Error("Object.updateValue: Could not get value of property ".concat(propertyName));
57
+ }
58
+ return fixer.replaceText(value, newValue);
59
+ },
60
+ /**
61
+ * Appends a key-value pair to the end of an object. For example:
62
+ * ```
63
+ * ast.Object.appendEntry(
64
+ * node, // { padding: 'space.100' }
65
+ * key, // 'margin',
66
+ * value, // 'space.200'
67
+ * fixer,
68
+ * )
69
+ * ```
70
+ * Will result in `{ padding: 'space.100', margin: 'space.200'}`
71
+ */
72
+ appendEntry: function appendEntry(node, key, value, fixer) {
73
+ return fixer.insertTextAfter(node.properties[node.properties.length - 1], "".concat(property({
74
+ key: identifier(key),
75
+ value: literal(value)
76
+ }).toString(), ", "));
77
+ }
78
+ };
79
+ export { ASTObjectExpression as Object };
@@ -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::d1a459e1ea71650f65b2890dc86cc398>>
3
+ * @codegen <<SignedSource::5026ba2cb55b3c1bcacbfe7fb7728a6c>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  export default {
@@ -10,6 +10,7 @@ export default {
10
10
  '@atlaskit/design-system/ensure-design-token-usage': 'error',
11
11
  '@atlaskit/design-system/ensure-design-token-usage/preview': 'warn',
12
12
  '@atlaskit/design-system/icon-label': 'warn',
13
+ '@atlaskit/design-system/local-cx-xcss': 'error',
13
14
  '@atlaskit/design-system/no-banned-imports': 'error',
14
15
  '@atlaskit/design-system/no-css-tagged-template-expression': 'error',
15
16
  '@atlaskit/design-system/no-deprecated-apis': 'error',
@@ -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',
@@ -6,6 +6,8 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
6
6
  import { getIdentifierInParentScope, isNodeOfType } from 'eslint-codemod-utils';
7
7
  import assign from 'lodash/assign';
8
8
  import { createLintRule } from '../utils/create-rule';
9
+ // File-level tracking of styles hoisted from the cssOnTopOfModule/cssAtBottomOfModule fixers
10
+ var hoistedCss = [];
9
11
  function isCssCallExpression(node, cssFunctions) {
10
12
  cssFunctions = [].concat(_toConsumableArray(cssFunctions), ['cssMap']);
11
13
  return !!(isNodeOfType(node, 'CallExpression') && node.callee && node.callee.type === 'Identifier' && cssFunctions.includes(node.callee.name) && node.arguments.length && node.arguments[0].type === 'ObjectExpression');
@@ -18,11 +20,11 @@ function findSpreadProperties(node) {
18
20
  property.type === 'ExperimentalSpreadProperty';
19
21
  });
20
22
  }
21
- var getTopLevelNode = function getTopLevelNode(expression) {
23
+ var getProgramNode = function getProgramNode(expression) {
22
24
  while (expression.parent.type !== 'Program') {
23
25
  expression = expression.parent;
24
26
  }
25
- return expression;
27
+ return expression.parent;
26
28
  };
27
29
 
28
30
  // TODO: This can be optimised by implementing a fixer at the very end (Program:exit) and handling all validations at once
@@ -41,7 +43,7 @@ var getDeclaratorString = function getDeclaratorString(context) {
41
43
  }
42
44
  var variables = scope.variables.map(function (variable) {
43
45
  return variable.name;
44
- });
46
+ }).concat(hoistedCss);
45
47
  var count = 2;
46
48
  var declaratorName = 'styles';
47
49
 
@@ -54,6 +56,9 @@ var getDeclaratorString = function getDeclaratorString(context) {
54
56
  count++;
55
57
  }
56
58
  }
59
+
60
+ // Keep track of it by adding it to the hoistedCss global array
61
+ hoistedCss = [].concat(_toConsumableArray(hoistedCss), ["".concat(declaratorName).concat(count)]);
57
62
  return "".concat(declaratorName).concat(count);
58
63
  };
59
64
  function analyzeIdentifier(context, sourceIdentifier, configuration) {
@@ -116,7 +121,19 @@ function analyzeIdentifier(context, sourceIdentifier, configuration) {
116
121
  */
117
122
  var fixCssNotInModuleScope = function fixCssNotInModuleScope(fixer, context, configuration, node, cssAttributeName) {
118
123
  var sourceCode = context.getSourceCode();
119
- var topLevelNode = getTopLevelNode(node);
124
+
125
+ // Get the program node in order to properly position the hoisted styles
126
+ var programNode = getProgramNode(node);
127
+ var fixerNodePlacement = programNode;
128
+ if (configuration.stylesPlacement === 'bottom') {
129
+ // The last value is the bottom of the file
130
+ fixerNodePlacement = programNode.body[programNode.body.length - 1];
131
+ } else {
132
+ // Place after the last ImportDeclaration
133
+ fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
134
+ return node.type !== 'ImportDeclaration';
135
+ });
136
+ }
120
137
  var moduleString;
121
138
  var implementFixer = [];
122
139
  if (node.type === 'Identifier') {
@@ -138,7 +155,7 @@ var fixCssNotInModuleScope = function fixCssNotInModuleScope(fixer, context, con
138
155
  }
139
156
  return [].concat(implementFixer, [
140
157
  // Insert the node either before or after
141
- configuration.stylesPlacement === 'bottom' ? fixer.insertTextAfter(topLevelNode, '\n' + moduleString) : fixer.insertTextBefore(topLevelNode, moduleString + '\n')]);
158
+ configuration.stylesPlacement === 'bottom' ? fixer.insertTextAfter(fixerNodePlacement, '\n' + moduleString) : fixer.insertTextBefore(fixerNodePlacement, moduleString + '\n')]);
142
159
  };
143
160
 
144
161
  /**
@@ -269,6 +286,9 @@ var rule = createLintRule({
269
286
  }), _defineProperty(_ref3, "JSXAttribute", function JSXAttribute(node) {
270
287
  var name = node.name,
271
288
  value = node.value;
289
+
290
+ // Always reset to empty array
291
+ hoistedCss = [];
272
292
  if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
273
293
  // When not a jsx expression. For eg. css=""
274
294
  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
+ var IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
4
+ function getParentJSXAttribute(node) {
5
+ var 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 var 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: function create(context) {
28
+ return {
29
+ 'CallExpression[callee.name="cx"]': function CallExpressionCalleeNameCx(node) {
30
+ if (node.type === 'CallExpression' && isCxFunction(node.callee, context.getScope().references, IMPORT_SOURCES)) {
31
+ var parentJSXAttribute = getParentJSXAttribute(node);
32
+ var 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: node,
38
+ messageId: 'local-cx-xcss'
39
+ });
40
+ }
41
+ }
42
+ };
43
+ }
44
+ });
45
+ export default rule;
@@ -2,16 +2,18 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
2
  // 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
3
3
  // eslint-disable-next-line import/no-extraneous-dependencies
4
4
 
5
+ import { CSS_IN_JS_IMPORTS } from '../../utils/is-supported-import';
5
6
  import { generate } from './generate';
6
7
  import { getTaggedTemplateExpressionOffset } from './get-tagged-template-expression-offset';
7
8
  import { toArguments } from './to-arguments';
9
+ var IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.emotionReact, CSS_IN_JS_IMPORTS.emotionCore, CSS_IN_JS_IMPORTS.styledComponents];
8
10
  export var createNoTaggedTemplateExpressionRule = function createNoTaggedTemplateExpressionRule(isUsage, messageId) {
9
11
  return function (context) {
10
12
  return {
11
13
  TaggedTemplateExpression: function TaggedTemplateExpression(node) {
12
14
  var _context$getScope = context.getScope(),
13
15
  references = _context$getScope.references;
14
- if (!isUsage(node.tag, references)) {
16
+ if (!isUsage(node.tag, references, IMPORT_SOURCES)) {
15
17
  return;
16
18
  }
17
19
  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
  var rule = createLintRule({
5
5
  meta: {
6
6
  name: 'no-css-tagged-template-expression',
@@ -15,6 +15,6 @@ var 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
  var boxDocsUrl = 'https://atlassian.design/components/primitives/box';
8
5
  var rule = createLintRule({
9
6
  meta: {
@@ -25,32 +22,9 @@ var rule = createLintRule({
25
22
  return {
26
23
  // transforms styled.<html>(...) usages
27
24
  CallExpression: function CallExpression(node) {
28
- if (!config.patterns.includes('compiled-styled-object')) {
29
- return;
30
- }
31
- if (!isNodeOfType(node, 'CallExpression')) {
32
- return;
33
- }
34
- var styledComponentVariableRef = findValidStyledComponentCall(node);
35
- if (!styledComponentVariableRef || !isNodeOfType(styledComponentVariableRef.id, 'Identifier') || !isValidCssPropertiesToTransform(node, config)) {
36
- return;
37
- }
38
- var 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 ".concat(styledComponentVariableRef.id.name, " to Box"),
49
- fix: styledComponentToPrimitive({
50
- stylesRef: styledComponentVariableRef,
51
- jsxRef: styledComponentJsxRef
52
- }, context)
53
- }]
25
+ CompiledStyled.lint(node, {
26
+ context: context,
27
+ config: config
54
28
  });
55
29
  },
56
30
  // transforms <div css={...}> usages
@@ -0,0 +1,30 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import { isNodeOfType } from 'eslint-codemod-utils';
3
+ import * as ast from '../../../../ast-nodes';
4
+
5
+ /**
6
+ * Transforms a JSXElement from:
7
+ * ```
8
+ * <div>
9
+ * // ...
10
+ * </div>
11
+ * ```
12
+ * to
13
+ * ```
14
+ * <Box xcss={myStyles}>
15
+ * // ...
16
+ * </Box>
17
+ * ```
18
+ */
19
+ export var convertJsxCallSite = function convertJsxCallSite(jsxElement, newStylesVariableName, fixer) {
20
+ var fixes = [];
21
+
22
+ // renames the JSX call site
23
+ if (isNodeOfType(jsxElement, 'JSXElement')) {
24
+ fixes.push.apply(fixes, _toConsumableArray(ast.JSXElement.updateName(jsxElement, 'Box', fixer)));
25
+ }
26
+
27
+ // adds xcss prop
28
+ fixes.push(fixer.insertTextAfter(jsxElement.openingElement.name, " xcss={".concat(newStylesVariableName, "}")));
29
+ return fixes;
30
+ };
@@ -0,0 +1,38 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import { isNodeOfType } from 'eslint-codemod-utils';
3
+ import { styledObjectToXcssTokens } from '../css-to-xcss';
4
+
5
+ /**
6
+ * Transforms a variable like:
7
+ * ```
8
+ * const MyComponent = styled.div({
9
+ * padding: '8px',
10
+ * })
11
+ * ```
12
+ * to
13
+ * ```
14
+ * const myComponentStyles = xcss({
15
+ * padding: 'space.100',
16
+ * })
17
+ * ```
18
+ */
19
+ export var convertStyledComponentToXcss = function convertStyledComponentToXcss(styles, newStylesVariableName, fixer) {
20
+ var fixes = [];
21
+
22
+ // renames the variable from MyComponent to myComponentStyles
23
+ fixes.push(fixer.replaceText(styles.id, newStylesVariableName));
24
+
25
+ // renames the function call from styled.<tag> to xcss
26
+ if (styles.init && isNodeOfType(styles.init, 'CallExpression')) {
27
+ fixes.push(fixer.replaceText(styles.init.callee, 'xcss'));
28
+ }
29
+
30
+ // converts CSS values to XCSS-compatible tokens
31
+ if (styles.init && isNodeOfType(styles.init, 'CallExpression')) {
32
+ var objectExpression = styles.init.arguments[0];
33
+ if (isNodeOfType(objectExpression, 'ObjectExpression')) {
34
+ fixes.push.apply(fixes, _toConsumableArray(styledObjectToXcssTokens(objectExpression, fixer)));
35
+ }
36
+ }
37
+ return fixes;
38
+ };
@@ -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 @@ var isStyledCallExpression = function 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
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ /* eslint-disable @repo/internal/react/require-jsdoc */
3
+
4
+ import { isNodeOfType } from 'eslint-codemod-utils';
5
+ import * as ast from '../../../../ast-nodes';
6
+ import { isValidCssPropertiesToTransform } from '../../utils';
7
+ import { convertJsxCallSite } from './convert-jsx-call-site';
8
+ import { convertStyledComponentToXcss } from './convert-styled-component-call-to-jsx';
9
+ import { findValidJsxUsageToTransform } from './find-valid-jsx-usage-to-transform';
10
+ import { findValidStyledComponentCall } from './find-valid-styled-component-call';
11
+ import { upsertImportDeclaration } from './upsert-import-declaration';
12
+ export var CompiledStyled = {
13
+ lint: function lint(node, _ref) {
14
+ var context = _ref.context,
15
+ config = _ref.config;
16
+ if (!isNodeOfType(node, 'CallExpression')) {
17
+ return;
18
+ }
19
+
20
+ // Check whether all criteria needed to make a transformation are met
21
+ var _CompiledStyled$_chec = CompiledStyled._check(node, {
22
+ context: context,
23
+ config: config
24
+ }),
25
+ success = _CompiledStyled$_chec.success,
26
+ refs = _CompiledStyled$_chec.refs;
27
+ if (!success || !refs) {
28
+ return;
29
+ }
30
+ context.report({
31
+ node: refs.styles,
32
+ messageId: 'preferPrimitivesBox',
33
+ suggest: [{
34
+ desc: "Convert ".concat(ast.JSXElement.getName(refs.jsxElement), " to Box"),
35
+ fix: CompiledStyled._fix(refs, context)
36
+ }]
37
+ });
38
+ },
39
+ _check: function _check(node, _ref2) {
40
+ var context = _ref2.context,
41
+ config = _ref2.config;
42
+ if (!config.patterns.includes('compiled-styled-object')) {
43
+ return {
44
+ success: false
45
+ };
46
+ }
47
+ if (!isNodeOfType(node, 'CallExpression')) {
48
+ return {
49
+ success: false
50
+ };
51
+ }
52
+ var styledComponentVariableRef = findValidStyledComponentCall(node);
53
+ if (!styledComponentVariableRef || !isNodeOfType(styledComponentVariableRef.id, 'Identifier') || !isValidCssPropertiesToTransform(node, config)) {
54
+ return {
55
+ success: false
56
+ };
57
+ }
58
+ var styledComponentJsxRef = findValidJsxUsageToTransform(styledComponentVariableRef.id.name, context.getScope());
59
+ if (!styledComponentJsxRef) {
60
+ return {
61
+ success: false
62
+ };
63
+ }
64
+ if (!isNodeOfType(styledComponentJsxRef.parent, 'JSXElement')) {
65
+ return {
66
+ success: false
67
+ };
68
+ }
69
+ var importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
70
+
71
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
72
+ if (importDeclaration.length > 1) {
73
+ return {
74
+ success: false
75
+ };
76
+ }
77
+ return {
78
+ success: true,
79
+ refs: {
80
+ styles: styledComponentVariableRef,
81
+ jsxElement: styledComponentJsxRef.parent
82
+ }
83
+ };
84
+ },
85
+ /**
86
+ * All required validation steps have been taken care of before this
87
+ * transformer is called, so it just goes ahead providing all necessary fixes
88
+ */
89
+ _fix: function _fix(refs, context) {
90
+ return function (fixer) {
91
+ // generates the new variable name: MyComponent -> myComponentStyles
92
+ var calculatedStylesVariableName = isNodeOfType(refs.styles.id, 'Identifier') && "".concat(refs.styles.id.name.replace(refs.styles.id.name[0], refs.styles.id.name[0].toLowerCase()), "Styles");
93
+ if (!calculatedStylesVariableName) {
94
+ return [];
95
+ }
96
+ var importFixes = upsertImportDeclaration({
97
+ module: '@atlaskit/primitives',
98
+ specifiers: ['Box', 'xcss']
99
+ }, context, fixer);
100
+ var stylesFixes = convertStyledComponentToXcss(refs.styles, calculatedStylesVariableName, fixer);
101
+ var jsxFixes = convertJsxCallSite(refs.jsxElement, calculatedStylesVariableName, fixer);
102
+ return [importFixes].concat(_toConsumableArray(stylesFixes), _toConsumableArray(jsxFixes)).filter(function (fix) {
103
+ return Boolean(fix);
104
+ }); // Some of the transformers can return arrays with undefined, so filter them out
105
+ };
106
+ }
107
+ };
@@ -0,0 +1,4 @@
1
+ export var elements = ['div'];
2
+ export var attributes = ['css'
3
+ // 'data-testid'
4
+ ];
@@ -0,0 +1,29 @@
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 var upsertImportDeclaration = function upsertImportDeclaration(_ref, context, fixer) {
13
+ var module = _ref.module,
14
+ specifiers = _ref.specifiers;
15
+ // Find any imports that match the packageName
16
+ var root = context.getSourceCode().ast.body;
17
+ var importDeclarations = ast.Root.findImportsByModule(root, module);
18
+
19
+ // The import doesn't exist yet, we can just insert a whole new one
20
+ if (importDeclarations.length === 0) {
21
+ return ast.Root.insertImport(root, {
22
+ module: module,
23
+ specifiers: specifiers
24
+ }, fixer);
25
+ }
26
+
27
+ // The import exists so, modify the existing one
28
+ return ast.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer);
29
+ };