@atlaskit/eslint-plugin-ui-styling-standard 0.11.6 → 0.13.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 (61) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +2 -0
  3. package/constellation/index/overview.mdx +3 -1
  4. package/constellation/no-global-styles/usage.mdx +59 -0
  5. package/constellation/use-compiled/usage.mdx +54 -0
  6. package/dist/cjs/presets/all.codegen.js +3 -1
  7. package/dist/cjs/presets/recommended.codegen.js +3 -1
  8. package/dist/cjs/rules/index.codegen.js +6 -2
  9. package/dist/cjs/rules/no-global-styles/index.js +100 -0
  10. package/dist/cjs/rules/use-compiled/index.js +257 -0
  11. package/dist/cjs/rules/use-compiled/is-safe.js +130 -0
  12. package/dist/cjs/rules/use-compiled/utils.js +26 -0
  13. package/dist/es2019/presets/all.codegen.js +3 -1
  14. package/dist/es2019/presets/recommended.codegen.js +3 -1
  15. package/dist/es2019/rules/index.codegen.js +6 -2
  16. package/dist/es2019/rules/no-global-styles/index.js +67 -0
  17. package/dist/es2019/rules/use-compiled/index.js +182 -0
  18. package/dist/es2019/rules/use-compiled/is-safe.js +118 -0
  19. package/dist/es2019/rules/use-compiled/utils.js +15 -0
  20. package/dist/esm/presets/all.codegen.js +3 -1
  21. package/dist/esm/presets/recommended.codegen.js +3 -1
  22. package/dist/esm/rules/index.codegen.js +6 -2
  23. package/dist/esm/rules/no-global-styles/index.js +94 -0
  24. package/dist/esm/rules/use-compiled/index.js +250 -0
  25. package/dist/esm/rules/use-compiled/is-safe.js +121 -0
  26. package/dist/esm/rules/use-compiled/utils.js +17 -0
  27. package/dist/types/index.codegen.d.ts +4 -0
  28. package/dist/types/presets/all.codegen.d.ts +3 -1
  29. package/dist/types/presets/recommended.codegen.d.ts +3 -1
  30. package/dist/types/rules/index.codegen.d.ts +2 -0
  31. package/dist/types/rules/no-global-styles/index.d.ts +2 -0
  32. package/dist/types/rules/use-compiled/index.d.ts +3 -0
  33. package/dist/types/rules/use-compiled/is-safe.d.ts +10 -0
  34. package/dist/types/rules/use-compiled/utils.d.ts +6 -0
  35. package/dist/types-ts4.5/index.codegen.d.ts +4 -0
  36. package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
  37. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
  38. package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
  39. package/dist/types-ts4.5/rules/no-global-styles/index.d.ts +2 -0
  40. package/dist/types-ts4.5/rules/use-compiled/index.d.ts +3 -0
  41. package/dist/types-ts4.5/rules/use-compiled/is-safe.d.ts +10 -0
  42. package/dist/types-ts4.5/rules/use-compiled/utils.d.ts +6 -0
  43. package/package.json +3 -2
  44. package/src/presets/all.codegen.tsx +3 -1
  45. package/src/presets/recommended.codegen.tsx +3 -1
  46. package/src/rules/index.codegen.tsx +5 -1
  47. package/src/rules/no-global-styles/README.md +56 -0
  48. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +33 -0
  49. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +46 -0
  50. package/src/rules/no-global-styles/index.tsx +85 -0
  51. package/src/rules/use-compiled/README.md +49 -0
  52. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +73 -0
  53. package/src/rules/use-compiled/__tests__/css.test.tsx +126 -0
  54. package/src/rules/use-compiled/__tests__/imports.test.tsx +163 -0
  55. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +140 -0
  56. package/src/rules/use-compiled/__tests__/pragma.test.tsx +108 -0
  57. package/src/rules/use-compiled/__tests__/styled.test.tsx +114 -0
  58. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +15 -0
  59. package/src/rules/use-compiled/index.tsx +275 -0
  60. package/src/rules/use-compiled/is-safe.tsx +163 -0
  61. package/src/rules/use-compiled/utils.tsx +37 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#99544](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/99544)
8
+ [`391edf6de450`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/391edf6de450) -
9
+ Added `no-global-styles` rule which prevents:
10
+
11
+ - The `Global` API from `@emotion`
12
+ - The `createGlobalStyle` and `injectGlobal` APIs from `styled-components`
13
+ - `<style>` elements in JSX
14
+ - CSS imports (including SASS and LESS)
15
+
16
+ ## 0.12.0
17
+
18
+ ### Minor Changes
19
+
20
+ - [#96162](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/96162)
21
+ [`d68864c620d0`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/d68864c620d0) -
22
+ Add `use-compiled` rule to enforce usage of `@compiled/react` instead of other CSS-in-JS
23
+ libraries
24
+
3
25
  ## 0.11.6
4
26
 
5
27
  ### Patch Changes
package/README.md CHANGED
@@ -51,10 +51,12 @@ module.exports = {
51
51
  | <a href="./src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
52
52
  | <a href="./src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
53
53
  | <a href="./src/rules/no-exported-styles/README.md">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
54
+ | <a href="./src/rules/no-global-styles/README.md">no-global-styles</a> | Prevents global styles through CSS-in-JS or CSS module imports | Yes | | |
54
55
  | <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
55
56
  | <a href="./src/rules/no-imported-style-values/README.md">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
56
57
  | <a href="./src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
57
58
  | <a href="./src/rules/no-unsafe-values/README.md">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
59
+ | <a href="./src/rules/use-compiled/README.md">use-compiled</a> | Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries | Yes | Yes | |
58
60
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
59
61
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-css-tagged-template-expression/usage">@atlaskit/design-system/no-css-tagged-template-expression</a> | Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
60
62
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-keyframes-tagged-template-expression/usage">@atlaskit/design-system/no-keyframes-tagged-template-expression</a> | Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
@@ -20,10 +20,12 @@ but more are planned to support the standard over time.
20
20
  | <a href="no-container-queries/usage">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
21
21
  | <a href="no-dynamic-styles/usage">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
22
22
  | <a href="no-exported-styles/usage">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
23
+ | <a href="no-global-styles/usage">no-global-styles</a> | Prevents global styles through CSS-in-JS or CSS module imports | Yes | | |
23
24
  | <a href="no-important-styles/usage">no-important-styles</a> | Disallows important style declarations | Yes | | |
24
25
  | <a href="no-imported-style-values/usage">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
25
26
  | <a href="no-nested-selectors/usage">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
26
27
  | <a href="no-unsafe-values/usage">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
28
+ | <a href="use-compiled/usage">use-compiled</a> | Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries | Yes | Yes | |
27
29
  | <a href="consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
28
30
  | <a href="no-css-tagged-template-expression/usage">@atlaskit/design-system/no-css-tagged-template-expression</a> | Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
29
31
  | <a href="no-keyframes-tagged-template-expression/usage">@atlaskit/design-system/no-keyframes-tagged-template-expression</a> | Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
@@ -153,7 +155,7 @@ export const Component = ({
153
155
  Every element should be clear in isolation how it's styled, using locally and statically available
154
156
  context so both humans and tooling can read this component in a performant and deterministic manner.
155
157
 
156
- 1. Use `@compiled/…` wherever possible!
158
+ 1. Use `@compiled/react` or [Primitives](/components/primitives/overview) wherever possible!
157
159
  2. Instead of dynamic styles:
158
160
  1. Use static `css()` calls everywhere.
159
161
  2. Compose styles together, turning styles on or off.
@@ -0,0 +1,59 @@
1
+ # no-global-styles
2
+
3
+ This rule prevents global styles through CSS-in-JS or CSS module imports.
4
+
5
+ Requiring local definition of styles helps to ensure that style dependencies are statically
6
+ resolvable.
7
+
8
+ ## Examples
9
+
10
+ ### Incorrect
11
+
12
+ ```tsx
13
+ import { Global } from '@emotion/react';
14
+
15
+ <Global
16
+ styles={{
17
+ '.some-class': {
18
+ fontSize: 50,
19
+ textAlign: 'center',
20
+ },
21
+ }}
22
+ />;
23
+ ```
24
+
25
+ ```tsx
26
+ import { createGlobalStyle } from 'styled-components';
27
+
28
+ const GlobalStyle = createGlobalStyle({
29
+ body: {
30
+ margin: 0,
31
+ },
32
+ });
33
+ ```
34
+
35
+ ```tsx
36
+ import { injectGlobal } from 'styled-components';
37
+
38
+ injectGlobal({
39
+ body: {
40
+ margin: 0,
41
+ },
42
+ });
43
+ ```
44
+
45
+ ```tsx
46
+ <style>
47
+ {`
48
+ .some-class {
49
+ color: red;
50
+ }
51
+ `}
52
+ </style>
53
+ ```
54
+
55
+ ### Correct
56
+
57
+ ```tsx
58
+ import '@atlaskit/css-reset';
59
+ ```
@@ -0,0 +1,54 @@
1
+ # use-compiled
2
+
3
+ Ensures usage of `@compiled/react` over other CSS-in-JS libraries like Emotion or styled-components.
4
+
5
+ **WARNING** It may be unsafe to mix usages of `@compiled/react` with other CSS-in-JS libraries on
6
+ the same component. When converting to `@compiled/react` verify ALL changes. For this reason, the
7
+ auto-fixer has been disabled by default, but it may be useful to enable to empower a migration.
8
+
9
+ ## Examples
10
+
11
+ ### Incorrect
12
+
13
+ ```js
14
+ /** @jsx jsx */
15
+ import { css } from '@emotion/core';
16
+ import { jsx } from '@emotion/react';
17
+ import styled from '@emotion/styled';
18
+ ```
19
+
20
+ ```js
21
+ import styled, { css } from 'styled-components';
22
+ ```
23
+
24
+ ### Correct
25
+
26
+ We expect usage of `@atlaskit/primitives.xcss` when working with Primitives and
27
+ `@compiled/react.css` when working with custom or native code.
28
+
29
+ ```js
30
+ /** @jsx jsx */
31
+ import { jsx, css, styled } from '@compiled/react';
32
+
33
+ const styles = css({ color: 'var(--ds-color-text)' });
34
+ export default () => <div css={styles}>…</div>;
35
+ ```
36
+
37
+ ```js
38
+ import { xcss, Box } from '@atlaskit/primitives';
39
+
40
+ const styles = xcss({ color: 'color.text' });
41
+ export default () => <Box xcss={styles}>…</Box>;
42
+ ```
43
+
44
+ ## Options
45
+
46
+ ### `canAutoFix: boolean`
47
+
48
+ Determines whether or not the auto-fixer is enabled.
49
+
50
+ Defaults to `false` due to safety concerns when mixing Compiled and other CSS-in-JS libraries on the
51
+ same component.
52
+
53
+ Even when enabled, the auto-fixer will only convert usages that are deemed safe. Currently this is
54
+ limited to purely static styles where all keys and values are simple literals.
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
9
+ * @codegen <<SignedSource::9ac05c3cbd30f6209164b7a24f7a7a19>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -21,10 +21,12 @@ var _default = exports.default = {
21
21
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
23
23
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
24
+ '@atlaskit/ui-styling-standard/no-global-styles': 'warn',
24
25
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
25
26
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
26
27
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
27
28
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
29
+ '@atlaskit/ui-styling-standard/use-compiled': 'warn',
28
30
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
29
31
  excludeReactComponents: true
30
32
  }],
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
9
+ * @codegen <<SignedSource::9ac05c3cbd30f6209164b7a24f7a7a19>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -21,10 +21,12 @@ var _default = exports.default = {
21
21
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
23
23
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
24
+ '@atlaskit/ui-styling-standard/no-global-styles': 'warn',
24
25
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
25
26
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
26
27
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
27
28
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
29
+ '@atlaskit/ui-styling-standard/use-compiled': 'warn',
28
30
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
29
31
  excludeReactComponents: true
30
32
  }],
@@ -14,13 +14,15 @@ var _noClassnameProp = _interopRequireDefault(require("./no-classname-prop"));
14
14
  var _noContainerQueries = _interopRequireDefault(require("./no-container-queries"));
15
15
  var _noDynamicStyles = _interopRequireDefault(require("./no-dynamic-styles"));
16
16
  var _noExportedStyles = _interopRequireDefault(require("./no-exported-styles"));
17
+ var _noGlobalStyles = _interopRequireDefault(require("./no-global-styles"));
17
18
  var _noImportantStyles = _interopRequireDefault(require("./no-important-styles"));
18
19
  var _noImportedStyleValues = _interopRequireDefault(require("./no-imported-style-values"));
19
20
  var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
20
21
  var _noUnsafeValues = _interopRequireDefault(require("./no-unsafe-values"));
22
+ var _useCompiled = _interopRequireDefault(require("./use-compiled"));
21
23
  /**
22
24
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
23
- * @codegen <<SignedSource::78ffc29d20fd2fe3727a620cb4ddf192>>
25
+ * @codegen <<SignedSource::3cea683df6e31f816db56abf1fd65c1e>>
24
26
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
25
27
  */
26
28
  var _default = exports.default = {
@@ -33,8 +35,10 @@ var _default = exports.default = {
33
35
  'no-container-queries': _noContainerQueries.default,
34
36
  'no-dynamic-styles': _noDynamicStyles.default,
35
37
  'no-exported-styles': _noExportedStyles.default,
38
+ 'no-global-styles': _noGlobalStyles.default,
36
39
  'no-important-styles': _noImportantStyles.default,
37
40
  'no-imported-style-values': _noImportedStyleValues.default,
38
41
  'no-nested-selectors': _noNestedSelectors.default,
39
- 'no-unsafe-values': _noUnsafeValues.default
42
+ 'no-unsafe-values': _noUnsafeValues.default,
43
+ 'use-compiled': _useCompiled.default
40
44
  };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.rule = exports.default = void 0;
7
+ var _createRule = require("../utils/create-rule");
8
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
9
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
10
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
11
+ var restrictedFileTypes = ['.css', '.less', '.sass', '.scss'];
12
+ var restrictedImports = {
13
+ '@emotion/core': ['Global'],
14
+ '@emotion/react': ['Global'],
15
+ 'styled-components': ['createGlobalStyle', 'injectGlobal']
16
+ };
17
+ function isRestrictedImportSource(importSource) {
18
+ return importSource in restrictedImports;
19
+ }
20
+ var rule = exports.rule = (0, _createRule.createLintRule)({
21
+ meta: {
22
+ name: 'no-global-styles',
23
+ docs: {
24
+ description: 'Prevents global styles through CSS-in-JS or CSS module imports',
25
+ recommended: true,
26
+ severity: 'warn'
27
+ },
28
+ messages: {
29
+ 'no-global-styles': 'Global styles are not allowed.',
30
+ 'no-css-modules': 'Importing CSS files is not allowed.'
31
+ },
32
+ type: 'problem'
33
+ },
34
+ create: function create(context) {
35
+ function checkFileType(node) {
36
+ var importSource = node.source.value;
37
+ if (restrictedFileTypes.some(function (suffix) {
38
+ return importSource.endsWith(suffix);
39
+ })) {
40
+ context.report({
41
+ node: node.source,
42
+ messageId: 'no-css-modules'
43
+ });
44
+ }
45
+ }
46
+ function checkImports(node) {
47
+ var importSource = node.source.value;
48
+ if (!isRestrictedImportSource(importSource)) {
49
+ return;
50
+ }
51
+ var _iterator = _createForOfIteratorHelper(restrictedImports[importSource]),
52
+ _step;
53
+ try {
54
+ var _loop = function _loop() {
55
+ var importName = _step.value;
56
+ var specifier = node.specifiers.filter(isImportSpecifier).find(function (specifier) {
57
+ return specifier.imported.name === importName;
58
+ });
59
+ if (specifier) {
60
+ context.report({
61
+ node: specifier,
62
+ messageId: 'no-global-styles'
63
+ });
64
+ }
65
+ };
66
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
67
+ _loop();
68
+ }
69
+ } catch (err) {
70
+ _iterator.e(err);
71
+ } finally {
72
+ _iterator.f();
73
+ }
74
+ }
75
+ return {
76
+ ImportDeclaration: function (_ImportDeclaration) {
77
+ function ImportDeclaration(_x) {
78
+ return _ImportDeclaration.apply(this, arguments);
79
+ }
80
+ ImportDeclaration.toString = function () {
81
+ return _ImportDeclaration.toString();
82
+ };
83
+ return ImportDeclaration;
84
+ }(function (node) {
85
+ checkFileType(node);
86
+ checkImports(node);
87
+ }),
88
+ 'JSXOpeningElement[name.name="style"]': function JSXOpeningElementNameNameStyle(node) {
89
+ context.report({
90
+ node: node,
91
+ messageId: 'no-global-styles'
92
+ });
93
+ }
94
+ };
95
+ }
96
+ });
97
+ var _default = exports.default = rule;
98
+ function isImportSpecifier(node) {
99
+ return node.type === 'ImportSpecifier';
100
+ }
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.rule = exports.default = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
12
+ var _findVariable = require("@atlaskit/eslint-utils/find-variable");
13
+ var _createRule = require("../utils/create-rule");
14
+ var _utils = require("./utils");
15
+ var _isSafe = require("./is-safe");
16
+ var SUPPORTED_EMOTION_IMPORTS = new Set(['css', 'keyframes', 'ClassNames', 'jsx']);
17
+ var SUPPORTED_IMPORT_MAP = {
18
+ '@emotion/core': SUPPORTED_EMOTION_IMPORTS,
19
+ '@emotion/react': SUPPORTED_EMOTION_IMPORTS,
20
+ '@emotion/styled': new Set(['default']),
21
+ 'styled-components': new Set(['default', 'css', 'keyframes'])
22
+ };
23
+ function isSupportedImportSource(importSource) {
24
+ return importSource in SUPPORTED_IMPORT_MAP;
25
+ }
26
+ var schema = {
27
+ type: 'array',
28
+ items: [{
29
+ type: 'object',
30
+ properties: {
31
+ canAutoFix: {
32
+ type: 'boolean'
33
+ }
34
+ },
35
+ additionalProperties: false
36
+ }]
37
+ };
38
+ var defaultConfig = {
39
+ canAutoFix: false
40
+ };
41
+ function readConfig(context) {
42
+ var _ref = context.options,
43
+ _ref2 = (0, _slicedToArray2.default)(_ref, 1),
44
+ config = _ref2[0];
45
+ return Object.assign({}, defaultConfig, config);
46
+ }
47
+ var rule = exports.rule = (0, _createRule.createLintRule)({
48
+ meta: {
49
+ name: 'use-compiled',
50
+ type: 'problem',
51
+ fixable: 'code',
52
+ docs: {
53
+ description: 'Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries',
54
+ recommended: true,
55
+ severity: 'warn'
56
+ },
57
+ messages: {
58
+ 'use-compiled': "Use '@compiled/react' instead of '{{importSource}}'"
59
+ },
60
+ schema: schema
61
+ },
62
+ create: function create(context) {
63
+ var config = readConfig(context);
64
+ return {
65
+ Program: function Program() {
66
+ var pragma = context.getSourceCode().getAllComments().find(function (n) {
67
+ return n.value.includes('@jsxImportSource @emotion/react');
68
+ });
69
+ if (!pragma) {
70
+ return;
71
+ }
72
+ context.report({
73
+ messageId: 'use-compiled',
74
+ data: {
75
+ importSource: '@emotion/react'
76
+ },
77
+ loc: pragma.loc,
78
+ fix: /*#__PURE__*/_regenerator.default.mark(function fix(fixer) {
79
+ var importDeclarations, hasEmotionImport, hasCompiledImport;
80
+ return _regenerator.default.wrap(function fix$(_context) {
81
+ while (1) switch (_context.prev = _context.next) {
82
+ case 0:
83
+ if (config.canAutoFix) {
84
+ _context.next = 2;
85
+ break;
86
+ }
87
+ return _context.abrupt("return");
88
+ case 2:
89
+ importDeclarations = context.getSourceCode().ast.body.filter(function (node) {
90
+ return node.type === 'ImportDeclaration';
91
+ });
92
+ hasEmotionImport = importDeclarations.some(function (importDeclaration) {
93
+ var importSource = importDeclaration.source.value;
94
+ return importSource.startsWith('@emotion/');
95
+ });
96
+ hasCompiledImport = importDeclarations.some(function (importDeclaration) {
97
+ return importDeclaration.source.value === '@compiled/react';
98
+ });
99
+ if (!(hasEmotionImport || !hasCompiledImport)) {
100
+ _context.next = 7;
101
+ break;
102
+ }
103
+ return _context.abrupt("return");
104
+ case 7:
105
+ _context.next = 9;
106
+ return fixer.replaceText(
107
+ // @ts-expect-error - types don't accept comment nodes
108
+ pragma, "/** @jsxImportSource @compiled/react */");
109
+ case 9:
110
+ case "end":
111
+ return _context.stop();
112
+ }
113
+ }, fix);
114
+ })
115
+ });
116
+ },
117
+ ImportDeclaration: function (_ImportDeclaration) {
118
+ function ImportDeclaration(_x) {
119
+ return _ImportDeclaration.apply(this, arguments);
120
+ }
121
+ ImportDeclaration.toString = function () {
122
+ return _ImportDeclaration.toString();
123
+ };
124
+ return ImportDeclaration;
125
+ }(function (node) {
126
+ var importSource = node.source.value;
127
+ if (!isSupportedImportSource(importSource)) {
128
+ return;
129
+ }
130
+ context.report({
131
+ node: node.source,
132
+ messageId: 'use-compiled',
133
+ data: {
134
+ importSource: importSource
135
+ },
136
+ fix: /*#__PURE__*/_regenerator.default.mark(function fix(fixer) {
137
+ var specifiers;
138
+ return _regenerator.default.wrap(function fix$(_context2) {
139
+ while (1) switch (_context2.prev = _context2.next) {
140
+ case 0:
141
+ if (config.canAutoFix) {
142
+ _context2.next = 2;
143
+ break;
144
+ }
145
+ return _context2.abrupt("return");
146
+ case 2:
147
+ specifiers = getSpecifiers({
148
+ context: context,
149
+ node: node,
150
+ importSource: importSource
151
+ });
152
+ if (specifiers) {
153
+ _context2.next = 5;
154
+ break;
155
+ }
156
+ return _context2.abrupt("return");
157
+ case 5:
158
+ return _context2.delegateYield(fixImport({
159
+ context: context,
160
+ fixer: fixer,
161
+ node: node,
162
+ specifiers: specifiers
163
+ }), "t0", 6);
164
+ case 6:
165
+ case "end":
166
+ return _context2.stop();
167
+ }
168
+ }, fix);
169
+ })
170
+ });
171
+ })
172
+ };
173
+ }
174
+ });
175
+ var _default = exports.default = rule;
176
+ function getSpecifiers(_ref3) {
177
+ var context = _ref3.context,
178
+ node = _ref3.node,
179
+ importSource = _ref3.importSource;
180
+ var supportedImports = SUPPORTED_IMPORT_MAP[importSource];
181
+
182
+ // We aren't supporting namespace imports
183
+ if (node.specifiers.some(_utils.isImportNamespaceSpecifier)) {
184
+ return;
185
+ }
186
+ var defaultSpecifier = node.specifiers.find(_utils.isImportDefaultSpecifier);
187
+ if (defaultSpecifier) {
188
+ if (!supportedImports.has('default')) {
189
+ return;
190
+ }
191
+ var styled = (0, _findVariable.findVariable)({
192
+ identifier: defaultSpecifier.local,
193
+ sourceCode: context.getSourceCode()
194
+ });
195
+ if (!styled || !(0, _isSafe.isSafeStyled)(styled)) {
196
+ return;
197
+ }
198
+ }
199
+ var namedSpecifiers = node.specifiers.filter(_utils.isImportSpecifier);
200
+ if (!namedSpecifiers.every(function (specifier) {
201
+ return (0, _isSafe.isSafeUsage)({
202
+ context: context,
203
+ specifier: specifier,
204
+ supportedImports: supportedImports
205
+ });
206
+ })) {
207
+ return;
208
+ }
209
+ if (defaultSpecifier) {
210
+ return [].concat((0, _toConsumableArray2.default)(namedSpecifiers), [
211
+ // ASSUMPTION: the default import, if allowed, is for `styled`
212
+ (0, _eslintCodemodUtils.importSpecifier)({
213
+ imported: (0, _eslintCodemodUtils.identifier)('styled'),
214
+ local: node.specifiers[0].local
215
+ })]);
216
+ }
217
+ return namedSpecifiers;
218
+ }
219
+ function fixImport(_ref4) {
220
+ var context = _ref4.context,
221
+ fixer = _ref4.fixer,
222
+ node = _ref4.node,
223
+ specifiers = _ref4.specifiers;
224
+ return /*#__PURE__*/_regenerator.default.mark(function _callee() {
225
+ var compiledImport;
226
+ return _regenerator.default.wrap(function _callee$(_context3) {
227
+ while (1) switch (_context3.prev = _context3.next) {
228
+ case 0:
229
+ compiledImport = (0, _utils.getFirstImportFromSource)(context, '@compiled/react');
230
+ if (!compiledImport) {
231
+ _context3.next = 8;
232
+ break;
233
+ }
234
+ _context3.next = 4;
235
+ return fixer.remove(node);
236
+ case 4:
237
+ _context3.next = 6;
238
+ return fixer.replaceText(compiledImport, buildImportDeclaration(compiledImport.specifiers.concat(specifiers), '@compiled/react'));
239
+ case 6:
240
+ _context3.next = 10;
241
+ break;
242
+ case 8:
243
+ _context3.next = 10;
244
+ return fixer.replaceText(node, buildImportDeclaration(specifiers, '@compiled/react'));
245
+ case 10:
246
+ case "end":
247
+ return _context3.stop();
248
+ }
249
+ }, _callee);
250
+ })();
251
+ }
252
+ function buildImportDeclaration(specifiers, source) {
253
+ return "".concat((0, _eslintCodemodUtils.importDeclaration)({
254
+ specifiers: specifiers,
255
+ source: (0, _eslintCodemodUtils.literal)(source)
256
+ }), ";");
257
+ }