@atlaskit/eslint-plugin-ui-styling-standard 0.11.6 → 0.12.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 (51) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/use-compiled/usage.mdx +51 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +4 -2
  8. package/dist/cjs/rules/use-compiled/index.js +257 -0
  9. package/dist/cjs/rules/use-compiled/is-safe.js +130 -0
  10. package/dist/cjs/rules/use-compiled/utils.js +26 -0
  11. package/dist/es2019/presets/all.codegen.js +2 -1
  12. package/dist/es2019/presets/recommended.codegen.js +2 -1
  13. package/dist/es2019/rules/index.codegen.js +4 -2
  14. package/dist/es2019/rules/use-compiled/index.js +182 -0
  15. package/dist/es2019/rules/use-compiled/is-safe.js +118 -0
  16. package/dist/es2019/rules/use-compiled/utils.js +15 -0
  17. package/dist/esm/presets/all.codegen.js +2 -1
  18. package/dist/esm/presets/recommended.codegen.js +2 -1
  19. package/dist/esm/rules/index.codegen.js +4 -2
  20. package/dist/esm/rules/use-compiled/index.js +250 -0
  21. package/dist/esm/rules/use-compiled/is-safe.js +121 -0
  22. package/dist/esm/rules/use-compiled/utils.js +17 -0
  23. package/dist/types/index.codegen.d.ts +2 -0
  24. package/dist/types/presets/all.codegen.d.ts +2 -1
  25. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  26. package/dist/types/rules/index.codegen.d.ts +1 -0
  27. package/dist/types/rules/use-compiled/index.d.ts +3 -0
  28. package/dist/types/rules/use-compiled/is-safe.d.ts +10 -0
  29. package/dist/types/rules/use-compiled/utils.d.ts +6 -0
  30. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  31. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  32. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  33. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  34. package/dist/types-ts4.5/rules/use-compiled/index.d.ts +3 -0
  35. package/dist/types-ts4.5/rules/use-compiled/is-safe.d.ts +10 -0
  36. package/dist/types-ts4.5/rules/use-compiled/utils.d.ts +6 -0
  37. package/package.json +3 -2
  38. package/src/presets/all.codegen.tsx +2 -1
  39. package/src/presets/recommended.codegen.tsx +2 -1
  40. package/src/rules/index.codegen.tsx +3 -1
  41. package/src/rules/use-compiled/README.md +49 -0
  42. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +73 -0
  43. package/src/rules/use-compiled/__tests__/css.test.tsx +126 -0
  44. package/src/rules/use-compiled/__tests__/imports.test.tsx +139 -0
  45. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +140 -0
  46. package/src/rules/use-compiled/__tests__/pragma.test.tsx +108 -0
  47. package/src/rules/use-compiled/__tests__/styled.test.tsx +114 -0
  48. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +15 -0
  49. package/src/rules/use-compiled/index.tsx +275 -0
  50. package/src/rules/use-compiled/is-safe.tsx +163 -0
  51. package/src/rules/use-compiled/utils.tsx +37 -0
@@ -0,0 +1,121 @@
1
+ import { closestOfType } from 'eslint-codemod-utils';
2
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
3
+ export function isSafeUsage(_ref) {
4
+ var context = _ref.context,
5
+ specifier = _ref.specifier,
6
+ supportedImports = _ref.supportedImports;
7
+ var importName = specifier.imported.name;
8
+ if (!supportedImports.has(importName)) {
9
+ return false;
10
+ }
11
+ if (importName === 'css') {
12
+ var css = findVariable({
13
+ identifier: specifier.local,
14
+ sourceCode: context.getSourceCode()
15
+ });
16
+ return css !== null && isSafeCss(css);
17
+ }
18
+ if (importName === 'keyframes') {
19
+ var keyframes = findVariable({
20
+ identifier: specifier.local,
21
+ sourceCode: context.getSourceCode()
22
+ });
23
+ return keyframes !== null && isSafeKeyframes(keyframes);
24
+ }
25
+ return true;
26
+ }
27
+ export function isSafeKeyframes(keyframes) {
28
+ return keyframes.references.every(function (ref) {
29
+ var callExpression = closestOfType(ref.identifier, 'CallExpression');
30
+ if (!callExpression) {
31
+ return false;
32
+ }
33
+ if (callExpression.callee !== ref.identifier) {
34
+ return false;
35
+ }
36
+ if (callExpression.arguments.length !== 1) {
37
+ return false;
38
+ }
39
+ var argument = callExpression.arguments[0];
40
+ if (argument.type !== 'ObjectExpression') {
41
+ return false;
42
+ }
43
+ return argument.properties.every(function (property) {
44
+ return isSafeProperty(property, isSafeKeyframePropertyValue);
45
+ });
46
+ });
47
+ }
48
+ function isSafeKeyframePropertyValue(value) {
49
+ if (value.type !== 'ObjectExpression') {
50
+ return false;
51
+ }
52
+ return isSafeStyleObject(value);
53
+ }
54
+ export function isSafeCss(css) {
55
+ return css.references.every(function (ref) {
56
+ var callExpression = closestOfType(ref.identifier, 'CallExpression');
57
+ if (!callExpression) {
58
+ return false;
59
+ }
60
+ if (callExpression.callee !== ref.identifier) {
61
+ return false;
62
+ }
63
+ return callExpression.arguments.every(isSafeStyleArgument);
64
+ });
65
+ }
66
+ export function isSafeStyled(styled) {
67
+ return styled.references.every(function (ref) {
68
+ var callExpression = closestOfType(ref.identifier, 'CallExpression');
69
+ if (!callExpression) {
70
+ return false;
71
+ }
72
+
73
+ /**
74
+ * If it's not in the form `styled.tagName()`
75
+ * then we consider it unsafe to auto-fix.
76
+ */
77
+ if (callExpression.callee.type !== 'MemberExpression' || callExpression.callee.object !== ref.identifier) {
78
+ return false;
79
+ }
80
+ return callExpression.arguments.every(isSafeStyleArgument);
81
+ });
82
+ }
83
+
84
+ /**
85
+ * Determines if a style argument is safe for auto-fix conversion.
86
+ */
87
+ function isSafeStyleArgument(arg) {
88
+ if (arg.type === 'ObjectExpression') {
89
+ return isSafeStyleObject(arg);
90
+ }
91
+ return false;
92
+ }
93
+ function isSafeStyleObject(object) {
94
+ return object.properties.every(function (property) {
95
+ return isSafeProperty(property, isSafeStyleValue);
96
+ });
97
+ }
98
+ function isSafeStyleValue(value) {
99
+ if (value.type === 'Literal') {
100
+ return true;
101
+ }
102
+ return false;
103
+ }
104
+ function isSafeProperty(property, isSafePropertyValue) {
105
+ if (property.type === 'SpreadElement') {
106
+ return false;
107
+ }
108
+ if (property.method || property.shorthand || property.kind !== 'init') {
109
+ return false;
110
+ }
111
+ if (!hasSafeKey(property)) {
112
+ return false;
113
+ }
114
+ return isSafePropertyValue(property.value);
115
+ }
116
+ function hasSafeKey(property) {
117
+ if (property.computed) {
118
+ return property.key.type === 'Literal';
119
+ }
120
+ return true;
121
+ }
@@ -0,0 +1,17 @@
1
+ export function getFirstImportFromSource(context, source) {
2
+ return context.getSourceCode().ast.body.filter(isImportDeclaration).find(function (node) {
3
+ return node.source.value === source;
4
+ });
5
+ }
6
+ function isImportDeclaration(node) {
7
+ return node.type === 'ImportDeclaration';
8
+ }
9
+ export function isImportSpecifier(specifier) {
10
+ return specifier.type === 'ImportSpecifier';
11
+ }
12
+ export function isImportDefaultSpecifier(specifier) {
13
+ return specifier.type === 'ImportDefaultSpecifier';
14
+ }
15
+ export function isImportNamespaceSpecifier(specifier) {
16
+ return specifier.type === 'ImportNamespaceSpecifier';
17
+ }
@@ -16,6 +16,7 @@ export declare const configs: {
16
16
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
17
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
18
18
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
19
+ '@atlaskit/ui-styling-standard/use-compiled': string;
19
20
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
20
21
  excludeReactComponents: boolean;
21
22
  })[];
@@ -54,6 +55,7 @@ export declare const configs: {
54
55
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
55
56
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
56
57
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
58
+ '@atlaskit/ui-styling-standard/use-compiled': string;
57
59
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
58
60
  excludeReactComponents: boolean;
59
61
  })[];
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -19,6 +19,7 @@ declare const _default: {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
22
+ '@atlaskit/ui-styling-standard/use-compiled': string;
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
23
24
  excludeReactComponents: boolean;
24
25
  })[];
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -19,6 +19,7 @@ declare const _default: {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
22
+ '@atlaskit/ui-styling-standard/use-compiled': string;
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
23
24
  excludeReactComponents: boolean;
24
25
  })[];
@@ -12,5 +12,6 @@ declare const _default: {
12
12
  'no-imported-style-values': import("eslint").Rule.RuleModule;
13
13
  'no-nested-selectors': import("eslint").Rule.RuleModule;
14
14
  'no-unsafe-values': import("eslint").Rule.RuleModule;
15
+ 'use-compiled': import("eslint").Rule.RuleModule;
15
16
  };
16
17
  export default _default;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,10 @@
1
+ import type { Rule, Scope } from 'eslint';
2
+ import type * as ESTree from 'eslint-codemod-utils';
3
+ export declare function isSafeUsage({ context, specifier, supportedImports, }: {
4
+ context: Rule.RuleContext;
5
+ specifier: ESTree.ImportSpecifier;
6
+ supportedImports: Set<string>;
7
+ }): boolean;
8
+ export declare function isSafeKeyframes(keyframes: Scope.Variable): boolean;
9
+ export declare function isSafeCss(css: Scope.Variable): boolean;
10
+ export declare function isSafeStyled(styled: Scope.Variable): boolean;
@@ -0,0 +1,6 @@
1
+ import type { Rule } from 'eslint';
2
+ import type * as ESTree from 'eslint-codemod-utils';
3
+ export declare function getFirstImportFromSource(context: Rule.RuleContext, source: string): ESTree.ImportDeclaration | undefined;
4
+ export declare function isImportSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportSpecifier;
5
+ export declare function isImportDefaultSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportDefaultSpecifier;
6
+ export declare function isImportNamespaceSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportNamespaceSpecifier;
@@ -16,6 +16,7 @@ export declare const configs: {
16
16
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
17
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
18
18
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
19
+ '@atlaskit/ui-styling-standard/use-compiled': string;
19
20
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
20
21
  excludeReactComponents: boolean;
21
22
  })[];
@@ -54,6 +55,7 @@ export declare const configs: {
54
55
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
55
56
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
56
57
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
58
+ '@atlaskit/ui-styling-standard/use-compiled': string;
57
59
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
58
60
  excludeReactComponents: boolean;
59
61
  })[];
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -19,6 +19,7 @@ declare const _default: {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
22
+ '@atlaskit/ui-styling-standard/use-compiled': string;
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
23
24
  excludeReactComponents: boolean;
24
25
  })[];
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -19,6 +19,7 @@ declare const _default: {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': string;
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': string;
22
+ '@atlaskit/ui-styling-standard/use-compiled': string;
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
23
24
  excludeReactComponents: boolean;
24
25
  })[];
@@ -12,5 +12,6 @@ declare const _default: {
12
12
  'no-imported-style-values': import("eslint").Rule.RuleModule;
13
13
  'no-nested-selectors': import("eslint").Rule.RuleModule;
14
14
  'no-unsafe-values': import("eslint").Rule.RuleModule;
15
+ 'use-compiled': import("eslint").Rule.RuleModule;
15
16
  };
16
17
  export default _default;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,10 @@
1
+ import type { Rule, Scope } from 'eslint';
2
+ import type * as ESTree from 'eslint-codemod-utils';
3
+ export declare function isSafeUsage({ context, specifier, supportedImports, }: {
4
+ context: Rule.RuleContext;
5
+ specifier: ESTree.ImportSpecifier;
6
+ supportedImports: Set<string>;
7
+ }): boolean;
8
+ export declare function isSafeKeyframes(keyframes: Scope.Variable): boolean;
9
+ export declare function isSafeCss(css: Scope.Variable): boolean;
10
+ export declare function isSafeStyled(styled: Scope.Variable): boolean;
@@ -0,0 +1,6 @@
1
+ import type { Rule } from 'eslint';
2
+ import type * as ESTree from 'eslint-codemod-utils';
3
+ export declare function getFirstImportFromSource(context: Rule.RuleContext, source: string): ESTree.ImportDeclaration | undefined;
4
+ export declare function isImportSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportSpecifier;
5
+ export declare function isImportDefaultSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportDefaultSpecifier;
6
+ export declare function isImportNamespaceSpecifier(specifier: ESTree.Node): specifier is ESTree.ImportNamespaceSpecifier;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.11.6",
3
+ "version": "0.12.0",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
6
6
  "license": "Apache-2.0",
@@ -51,13 +51,14 @@
51
51
  "devDependencies": {
52
52
  "@af/formatting": "*",
53
53
  "@atlaskit/ds-lib": "^2.3.0",
54
- "@atlaskit/eslint-plugin-design-system": "^10.0.0",
54
+ "@atlaskit/eslint-plugin-design-system": "^10.1.0",
55
55
  "@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
56
56
  "@atlassian/codegen": "*",
57
57
  "@compiled/eslint-plugin": "^0.13.6",
58
58
  "@types/eslint": "^8.56.6",
59
59
  "@types/estraverse": "^5.1.7",
60
60
  "eslint": "^8.49.0",
61
+ "json-schema-to-ts": "^3.1.0",
61
62
  "lodash": "^4.17.21",
62
63
  "ts-node": "^10.9.1",
63
64
  "tsconfig-paths": "^4.2.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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -19,6 +19,7 @@ export default {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
22
+ '@atlaskit/ui-styling-standard/use-compiled': 'warn',
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': [
23
24
  'warn',
24
25
  { excludeReactComponents: true },
@@ -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::a0c326baf3aa4e87ac809b303ba34682>>
3
+ * @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -19,6 +19,7 @@ export default {
19
19
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
22
+ '@atlaskit/ui-styling-standard/use-compiled': 'warn',
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': [
23
24
  'warn',
24
25
  { excludeReactComponents: true },
@@ -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::78ffc29d20fd2fe3727a620cb4ddf192>>
3
+ * @codegen <<SignedSource::be177a361ddc1a7a41d843680299e318>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import atlaskitTheme from './atlaskit-theme';
@@ -16,6 +16,7 @@ import noImportantStyles from './no-important-styles';
16
16
  import noImportedStyleValues from './no-imported-style-values';
17
17
  import noNestedSelectors from './no-nested-selectors';
18
18
  import noUnsafeValues from './no-unsafe-values';
19
+ import useCompiled from './use-compiled';
19
20
 
20
21
  export default {
21
22
  'atlaskit-theme': atlaskitTheme,
@@ -31,4 +32,5 @@ export default {
31
32
  'no-imported-style-values': noImportedStyleValues,
32
33
  'no-nested-selectors': noNestedSelectors,
33
34
  'no-unsafe-values': noUnsafeValues,
35
+ 'use-compiled': useCompiled,
34
36
  };
@@ -0,0 +1,49 @@
1
+ Ensures usage of `@compiled/react` over other CSS-in-JS libraries.
2
+
3
+ **WARNING**
4
+
5
+ It may be unsafe to mix usages of `@compiled/react` with other CSS-in-JS libraries.
6
+
7
+ When converting to `@compiled/react` verify ALL changes.
8
+
9
+ For this reason, the auto-fixer has been disabled by default.
10
+
11
+ ## Examples
12
+
13
+ ### Incorrect
14
+
15
+ ```js
16
+ import { css } from '@emotion/core';
17
+ import styled from '@emotion/styled';
18
+ ```
19
+
20
+ ```js
21
+ /** @jsx jsx */
22
+ import { jsx } from '@emotion/react';
23
+ ```
24
+
25
+ ```js
26
+ import styled, { css } from 'styled-components';
27
+ ```
28
+
29
+ ### Correct
30
+
31
+ ```js
32
+ import { css, styled } from '@compiled/react';
33
+ ```
34
+
35
+ ```js
36
+ /** @jsx jsx */
37
+ import { jsx } from '@compiled/react';
38
+ ```
39
+
40
+ ## Options
41
+
42
+ ### `canAutoFix: boolean`
43
+
44
+ Determines whether or not the auto-fixer is enabled.
45
+
46
+ Defaults to `false` due to safety concerns when mixing Compiled and other CSS-in-JS libraries.
47
+
48
+ Even when enabled, the auto-fixer will only convert usages that are deemed safe.
49
+ Currently this is limited to purely static styles where all keys and values are simple literals.
@@ -0,0 +1,73 @@
1
+ import type { RuleTester } from 'eslint';
2
+ import { tester } from '../../__tests__/utils/_tester';
3
+ import rule from '../index';
4
+
5
+ const invalid: RuleTester.InvalidTestCase[] = [
6
+ {
7
+ name: 'css API',
8
+ code: `
9
+ import { css } from '@emotion/react';
10
+
11
+ const Component = css({
12
+ color: 'red',
13
+ });
14
+ `,
15
+ errors: [{ messageId: 'use-compiled' }],
16
+ },
17
+ {
18
+ name: 'imports',
19
+ code: `
20
+ import { css as c, keyframes as kf } from '@emotion/core';
21
+ `,
22
+ errors: [{ messageId: 'use-compiled' }],
23
+ },
24
+ {
25
+ name: `keyframes API`,
26
+ code: `
27
+ import { keyframes } from 'styled-components';
28
+
29
+ const fadeIn = keyframes({
30
+ from: {
31
+ opacity: 0,
32
+ },
33
+ to: {
34
+ opacity: 1,
35
+ }
36
+ });
37
+ `,
38
+ errors: [{ messageId: 'use-compiled' }],
39
+ },
40
+ {
41
+ name: 'pragma fix',
42
+ code: `
43
+ /** @jsxImportSource @emotion/react */
44
+
45
+ import { css } from '@compiled/react';
46
+ `,
47
+ errors: [{ messageId: 'use-compiled' }],
48
+ },
49
+ {
50
+ name: `styles are a plain object with literal values`,
51
+ code: `
52
+ import styled from '@emotion/styled';
53
+
54
+ const Component = styled.div({
55
+ color: 'red',
56
+ });
57
+ `,
58
+ errors: [{ messageId: 'use-compiled' }],
59
+ },
60
+ ];
61
+
62
+ tester.run('canAutoFix is not provided', rule, {
63
+ valid: [],
64
+ invalid,
65
+ });
66
+
67
+ tester.run('canAutoFix is false', rule, {
68
+ valid: [],
69
+ invalid: invalid.map((testCase) => ({
70
+ ...testCase,
71
+ options: [{ canAutoFix: false }],
72
+ })),
73
+ });
@@ -0,0 +1,126 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ const importSources = ['@emotion/core', '@emotion/react', 'styled-components'];
5
+
6
+ tester.run('css API - fixable', rule, {
7
+ valid: [],
8
+ invalid: importSources.flatMap((importSource) => [
9
+ {
10
+ name: `[${importSource}] styles are a plain object with literal values`,
11
+ code: `
12
+ import { css } from '${importSource}';
13
+
14
+ const Component = css({
15
+ color: 'red',
16
+ });
17
+ `,
18
+ output: `
19
+ import { css } from '@compiled/react';
20
+
21
+ const Component = css({
22
+ color: 'red',
23
+ });
24
+ `,
25
+ errors: [{ messageId: 'use-compiled' }],
26
+ options: [{ canAutoFix: true }],
27
+ },
28
+ ]),
29
+ });
30
+
31
+ /**
32
+ * These test cases have no auto-fix because we are considering
33
+ * them unsafe to automatically convert.
34
+ */
35
+ tester.run('css API - not fixable', rule, {
36
+ valid: [],
37
+ invalid: importSources.flatMap((importSource) => [
38
+ {
39
+ name: `[${importSource}] function as style argument`,
40
+ code: `
41
+ import { css } from '${importSource}';
42
+
43
+ const Component = css(() => ({
44
+ color: 'red',
45
+ }));
46
+ `,
47
+ errors: [{ messageId: 'use-compiled' }],
48
+ options: [{ canAutoFix: true }],
49
+ },
50
+ {
51
+ name: `[${importSource}] function as style object value`,
52
+ code: `
53
+ import { css } from '${importSource}';
54
+
55
+ const Component = css({
56
+ color: () => 'red',
57
+ });
58
+ `,
59
+ errors: [{ messageId: 'use-compiled' }],
60
+ options: [{ canAutoFix: true }],
61
+ },
62
+ {
63
+ name: `[${importSource}] identifier as style object value`,
64
+ code: `
65
+ import { css } from '${importSource}';
66
+
67
+ const textColor = 'red';
68
+ const Component = css({
69
+ color: textColor,
70
+ });
71
+ `,
72
+ errors: [{ messageId: 'use-compiled' }],
73
+ options: [{ canAutoFix: true }],
74
+ },
75
+ {
76
+ /**
77
+ * We could auto-fix this safely but intentionally
78
+ * keeping this rule very simple for now.
79
+ */
80
+ name: `[${importSource}] function call as style object value`,
81
+ code: `
82
+ import { css } from '${importSource}';
83
+ import { token } from '@atlaskit/tokens';
84
+
85
+ const Component = css({
86
+ color: token('color.text'),
87
+ });
88
+ `,
89
+ errors: [{ messageId: 'use-compiled' }],
90
+ options: [{ canAutoFix: true }],
91
+ },
92
+ {
93
+ name: `[${importSource}] with unsupported named import`,
94
+ code: `
95
+ import { css, type CSSObject } from '${importSource}';
96
+
97
+ const Component = css({
98
+ color: 'red',
99
+ });
100
+ `,
101
+ errors: [{ messageId: 'use-compiled' }],
102
+ options: [{ canAutoFix: true }],
103
+ },
104
+ {
105
+ name: `[${importSource}] with unsafe usage of other import`,
106
+ code: `
107
+ import { css, keyframes } from '${importSource}';
108
+
109
+ const Component = css({
110
+ color: 'red',
111
+ });
112
+
113
+ const animation = keyframes({
114
+ from: {
115
+ opacity: () => 0
116
+ },
117
+ to: {
118
+ opacity: 1
119
+ }
120
+ })
121
+ `,
122
+ errors: [{ messageId: 'use-compiled' }],
123
+ options: [{ canAutoFix: true }],
124
+ },
125
+ ]),
126
+ });