@atlaskit/eslint-plugin-ui-styling-standard 0.3.0 → 0.4.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 (80) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +18 -5
  3. package/build/tsconfig.json +2 -4
  4. package/constellation/consistent-css-prop-usage/usage.mdx +5 -0
  5. package/constellation/index/overview.mdx +21 -0
  6. package/constellation/local-cx-xcss/usage.mdx +37 -0
  7. package/constellation/no-css-tagged-template-expression/usage.mdx +5 -0
  8. package/constellation/no-empty-styled-expression/usage.mdx +5 -0
  9. package/constellation/no-exported-css/usage.mdx +5 -0
  10. package/constellation/no-exported-keyframes/usage.mdx +5 -0
  11. package/constellation/no-invalid-css-map/usage.mdx +5 -0
  12. package/constellation/no-js-xcss/usage.mdx +23 -0
  13. package/constellation/no-keyframes-tagged-template-expression/usage.mdx +5 -0
  14. package/constellation/no-styled-tagged-template-expression/usage.mdx +5 -0
  15. package/constellation/no-suppress-xcss/usage.mdx +25 -0
  16. package/dist/cjs/index.codegen.js +26 -0
  17. package/dist/cjs/index.js +12 -33
  18. package/dist/cjs/presets/all.codegen.js +37 -0
  19. package/dist/cjs/presets/recommended.codegen.js +37 -0
  20. package/dist/cjs/rules/external-rules.js +143 -0
  21. package/dist/cjs/rules/index.codegen.js +16 -0
  22. package/dist/cjs/rules/local-cx-xcss/index.js +51 -0
  23. package/dist/cjs/rules/utils/create-rule.js +43 -0
  24. package/dist/cjs/rules/utils/is-supported-import.js +98 -0
  25. package/dist/es2019/index.codegen.js +12 -0
  26. package/dist/es2019/index.js +1 -33
  27. package/dist/es2019/presets/all.codegen.js +31 -0
  28. package/dist/es2019/presets/recommended.codegen.js +31 -0
  29. package/dist/es2019/rules/external-rules.js +138 -0
  30. package/dist/es2019/rules/index.codegen.js +9 -0
  31. package/dist/es2019/rules/local-cx-xcss/index.js +45 -0
  32. package/dist/es2019/rules/utils/create-rule.js +33 -0
  33. package/dist/es2019/rules/utils/is-supported-import.js +87 -0
  34. package/dist/esm/index.codegen.js +12 -0
  35. package/dist/esm/index.js +1 -33
  36. package/dist/esm/presets/all.codegen.js +31 -0
  37. package/dist/esm/presets/recommended.codegen.js +31 -0
  38. package/dist/esm/rules/external-rules.js +138 -0
  39. package/dist/esm/rules/index.codegen.js +9 -0
  40. package/dist/esm/rules/local-cx-xcss/index.js +45 -0
  41. package/dist/esm/rules/utils/create-rule.js +35 -0
  42. package/dist/esm/rules/utils/is-supported-import.js +90 -0
  43. package/dist/types/index.codegen.d.ts +55 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist/types/presets/all.codegen.d.ts +32 -0
  46. package/dist/types/presets/recommended.codegen.d.ts +32 -0
  47. package/dist/types/rules/external-rules.d.ts +11 -0
  48. package/dist/types/rules/index.codegen.d.ts +4 -0
  49. package/dist/types/rules/local-cx-xcss/index.d.ts +3 -0
  50. package/dist/types/rules/utils/create-rule.d.ts +64 -0
  51. package/dist/types/rules/utils/is-supported-import.d.ts +34 -0
  52. package/dist/types-ts4.5/index.codegen.d.ts +55 -0
  53. package/dist/types-ts4.5/index.d.ts +1 -0
  54. package/dist/types-ts4.5/presets/all.codegen.d.ts +32 -0
  55. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +32 -0
  56. package/dist/types-ts4.5/rules/external-rules.d.ts +11 -0
  57. package/dist/types-ts4.5/rules/index.codegen.d.ts +4 -0
  58. package/dist/types-ts4.5/rules/local-cx-xcss/index.d.ts +3 -0
  59. package/dist/types-ts4.5/rules/utils/create-rule.d.ts +64 -0
  60. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +34 -0
  61. package/package.json +13 -4
  62. package/scripts/codegen.tsx +439 -0
  63. package/src/index.codegen.tsx +14 -0
  64. package/src/index.tsx +1 -51
  65. package/src/presets/all.codegen.tsx +67 -0
  66. package/src/presets/recommended.codegen.tsx +67 -0
  67. package/src/rules/TEMPLATE.md +29 -0
  68. package/src/rules/__tests__/utils/_tester.tsx +40 -0
  69. package/src/rules/external-rules.tsx +175 -0
  70. package/src/rules/index.codegen.tsx +10 -0
  71. package/src/rules/local-cx-xcss/README.md +35 -0
  72. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +102 -0
  73. package/src/rules/local-cx-xcss/index.tsx +69 -0
  74. package/src/rules/utils/create-rule.tsx +80 -0
  75. package/src/rules/utils/is-supported-import.tsx +130 -0
  76. package/tsconfig.app.json +5 -1
  77. package/tsconfig.dev.json +5 -1
  78. package/tsconfig.json +7 -6
  79. package/dist/types/src/index.d.ts +0 -29
  80. package/dist/types-ts4.5/src/index.d.ts +0 -29
package/src/index.tsx CHANGED
@@ -1,51 +1 @@
1
- export const rules = {};
2
-
3
- const importSources: readonly string[] = [
4
- '@compiled/react',
5
- '@atlaskit/css',
6
- '@emotion/core',
7
- '@emotion/styled',
8
- 'styled-components',
9
- ];
10
-
11
- export const configs = {
12
- recommended: {
13
- plugins: [
14
- '@atlaskit/ui-styling-standard',
15
- '@atlaskit/design-system',
16
- '@compiled',
17
- ],
18
- rules: {
19
- '@atlaskit/design-system/consistent-css-prop-usage': [
20
- 'warn',
21
- {
22
- // When passing a css prop to a React component, we don't know whether the component
23
- // is Emotion or Compiled. Hence it is not safe to run autofixes like wrapping it in
24
- // a `css` function call.
25
- excludeReactComponents: true,
26
- },
27
- ],
28
- '@atlaskit/design-system/local-cx-xcss': 'warn',
29
- '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
30
- '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
31
- '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
32
- '@compiled/no-suppress-xcss': 'error',
33
- '@compiled/no-js-xcss': 'error',
34
- '@atlaskit/design-system/no-empty-styled-expression': [
35
- 'warn',
36
- { importSources },
37
- ],
38
- '@atlaskit/design-system/no-exported-css': ['warn', { importSources }],
39
- '@atlaskit/design-system/no-exported-keyframes': [
40
- 'warn',
41
- { importSources },
42
- ],
43
- '@atlaskit/design-system/no-invalid-css-map': [
44
- 'warn',
45
- {
46
- allowedFunctionCalls: [['@atlaskit/tokens', 'token']],
47
- },
48
- ],
49
- },
50
- },
51
- };
1
+ export { configs, rules } from './index.codegen';
@@ -0,0 +1,67 @@
1
+ /**
2
+ * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
+ * @codegen <<SignedSource::6b1418e07df6af9ad4cae4baf66739c1>>
4
+ * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
+ */
6
+ export default {
7
+ plugins: [
8
+ '@atlaskit/ui-styling-standard',
9
+ '@atlaskit/design-system',
10
+ '@compiled',
11
+ ],
12
+ rules: {
13
+ '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
14
+ '@atlaskit/design-system/consistent-css-prop-usage': [
15
+ 'warn',
16
+ { excludeReactComponents: true },
17
+ ],
18
+ '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
19
+ '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
20
+ '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
21
+ '@compiled/no-suppress-xcss': 'warn',
22
+ '@compiled/no-js-xcss': 'warn',
23
+ '@atlaskit/design-system/no-empty-styled-expression': [
24
+ 'warn',
25
+ {
26
+ importSources: [
27
+ '@compiled/react',
28
+ '@atlaskit/css',
29
+ '@emotion/core',
30
+ '@emotion/react',
31
+ '@emotion/styled',
32
+ 'styled-components',
33
+ ],
34
+ },
35
+ ],
36
+ '@atlaskit/design-system/no-exported-css': [
37
+ 'warn',
38
+ {
39
+ importSources: [
40
+ '@compiled/react',
41
+ '@atlaskit/css',
42
+ '@emotion/core',
43
+ '@emotion/react',
44
+ '@emotion/styled',
45
+ 'styled-components',
46
+ ],
47
+ },
48
+ ],
49
+ '@atlaskit/design-system/no-exported-keyframes': [
50
+ 'warn',
51
+ {
52
+ importSources: [
53
+ '@compiled/react',
54
+ '@atlaskit/css',
55
+ '@emotion/core',
56
+ '@emotion/react',
57
+ '@emotion/styled',
58
+ 'styled-components',
59
+ ],
60
+ },
61
+ ],
62
+ '@atlaskit/design-system/no-invalid-css-map': [
63
+ 'warn',
64
+ { allowedFunctionCalls: [['@atlaskit/tokens', 'token']] },
65
+ ],
66
+ },
67
+ };
@@ -0,0 +1,67 @@
1
+ /**
2
+ * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
+ * @codegen <<SignedSource::6b1418e07df6af9ad4cae4baf66739c1>>
4
+ * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
+ */
6
+ export default {
7
+ plugins: [
8
+ '@atlaskit/ui-styling-standard',
9
+ '@atlaskit/design-system',
10
+ '@compiled',
11
+ ],
12
+ rules: {
13
+ '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
14
+ '@atlaskit/design-system/consistent-css-prop-usage': [
15
+ 'warn',
16
+ { excludeReactComponents: true },
17
+ ],
18
+ '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
19
+ '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
20
+ '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
21
+ '@compiled/no-suppress-xcss': 'warn',
22
+ '@compiled/no-js-xcss': 'warn',
23
+ '@atlaskit/design-system/no-empty-styled-expression': [
24
+ 'warn',
25
+ {
26
+ importSources: [
27
+ '@compiled/react',
28
+ '@atlaskit/css',
29
+ '@emotion/core',
30
+ '@emotion/react',
31
+ '@emotion/styled',
32
+ 'styled-components',
33
+ ],
34
+ },
35
+ ],
36
+ '@atlaskit/design-system/no-exported-css': [
37
+ 'warn',
38
+ {
39
+ importSources: [
40
+ '@compiled/react',
41
+ '@atlaskit/css',
42
+ '@emotion/core',
43
+ '@emotion/react',
44
+ '@emotion/styled',
45
+ 'styled-components',
46
+ ],
47
+ },
48
+ ],
49
+ '@atlaskit/design-system/no-exported-keyframes': [
50
+ 'warn',
51
+ {
52
+ importSources: [
53
+ '@compiled/react',
54
+ '@atlaskit/css',
55
+ '@emotion/core',
56
+ '@emotion/react',
57
+ '@emotion/styled',
58
+ 'styled-components',
59
+ ],
60
+ },
61
+ ],
62
+ '@atlaskit/design-system/no-invalid-css-map': [
63
+ 'warn',
64
+ { allowedFunctionCalls: [['@atlaskit/tokens', 'token']] },
65
+ ],
66
+ },
67
+ };
@@ -0,0 +1,29 @@
1
+ <!-- To fill out -- tell us why this rule exists and any relevant context. -->
2
+
3
+ ## Examples
4
+
5
+ <!-- To fill out -- tell us when this rule will mark violations. -->
6
+
7
+ ### Incorrect
8
+
9
+ ```tsx
10
+ // To fill out: incorrect code
11
+ // Have one code block for all incorrect examples
12
+ ```
13
+
14
+ ### Correct
15
+
16
+ ```tsx
17
+ // To fill out: correct code
18
+ // Have one code block for all correct examples
19
+ ```
20
+
21
+ ## Options
22
+
23
+ <!-- To fill out -- delete section if rule doesn't take any options. -->
24
+
25
+ This rule accepts...
26
+
27
+ ## When not to use it
28
+
29
+ <!-- To fill out -- delete section if this should should always be used. -->
@@ -0,0 +1,40 @@
1
+ /* eslint-disable @repo/internal/fs/filename-pattern-match */
2
+ /* eslint-disable no-undef */
3
+ import { ESLintUtils } from '@typescript-eslint/utils';
4
+ import { RuleTester } from 'eslint';
5
+
6
+ import __noop from '@atlaskit/ds-lib/noop';
7
+
8
+ (RuleTester as any).describe = (text: string, method: Function) => {
9
+ const origHasAssertions = expect.hasAssertions;
10
+ describe(text, () => {
11
+ beforeAll(() => {
12
+ // Stub out expect.hasAssertions beforeEach from jest-presetup.js
13
+ expect.hasAssertions = __noop;
14
+ });
15
+ afterAll(() => {
16
+ expect.hasAssertions = origHasAssertions;
17
+ });
18
+
19
+ method();
20
+ });
21
+ };
22
+
23
+ export const typescriptEslintTester = new ESLintUtils.RuleTester({
24
+ parser: '@typescript-eslint/parser',
25
+ parserOptions: {
26
+ ecmaVersion: 2020,
27
+ sourceType: 'module',
28
+ ecmaFeatures: {
29
+ jsx: true,
30
+ },
31
+ },
32
+ });
33
+
34
+ export const tester = new RuleTester({
35
+ parser: require.resolve('babel-eslint'),
36
+ parserOptions: {
37
+ ecmaVersion: 6,
38
+ sourceType: 'module',
39
+ },
40
+ });
@@ -0,0 +1,175 @@
1
+ import { getRuleUrl, type LintRuleMeta } from './utils/create-rule';
2
+
3
+ /** External rules must be scoped, have a display name, and external urls. */
4
+ export type ExternalRuleMeta = LintRuleMeta & {
5
+ isExternal: true;
6
+ name: `@${string}/${string}`;
7
+ displayName: string;
8
+ docs: { externalUrl: `https://${string}` };
9
+ };
10
+
11
+ const importSources: readonly string[] = [
12
+ '@compiled/react',
13
+ '@atlaskit/css',
14
+ '@emotion/core',
15
+ '@emotion/react',
16
+ '@emotion/styled',
17
+ 'styled-components',
18
+ ];
19
+
20
+ export const externalRules: ExternalRuleMeta[] = [
21
+ {
22
+ name: '@atlaskit/design-system/consistent-css-prop-usage',
23
+ displayName: 'consistent-css-prop-usage',
24
+ isExternal: true,
25
+ fixable: 'code',
26
+ docs: {
27
+ description: 'Ensures consistency with `css` and `xcss` prop usages',
28
+ url: getRuleUrl('consistent-css-prop-usage'),
29
+ externalUrl:
30
+ 'https://atlassian.design/components/eslint-plugin-design-system/consistent-css-prop-usage/usage',
31
+ recommended: true,
32
+ severity: 'warn',
33
+ pluginConfig: {
34
+ // When passing a css prop to a React component, we don't know whether the component
35
+ // is Emotion or Compiled. Hence it is not safe to run autofixes like wrapping it in
36
+ // a `css` function call.
37
+ excludeReactComponents: true,
38
+ },
39
+ },
40
+ },
41
+ {
42
+ name: '@atlaskit/design-system/no-css-tagged-template-expression',
43
+ displayName: 'no-css-tagged-template-expression',
44
+ isExternal: true,
45
+ fixable: 'code',
46
+ docs: {
47
+ description:
48
+ 'Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled',
49
+ url: getRuleUrl('no-css-tagged-template-expression'),
50
+ externalUrl:
51
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-css-tagged-template-expression/usage',
52
+ recommended: true,
53
+ severity: 'warn',
54
+ },
55
+ },
56
+ {
57
+ name: '@atlaskit/design-system/no-keyframes-tagged-template-expression',
58
+ displayName: 'no-keyframes-tagged-template-expression',
59
+ isExternal: true,
60
+ fixable: 'code',
61
+ docs: {
62
+ description:
63
+ 'Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled',
64
+ url: getRuleUrl('no-keyframes-tagged-template-expression'),
65
+ externalUrl:
66
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-keyframes-tagged-template-expression/usage',
67
+ recommended: true,
68
+ severity: 'warn',
69
+ },
70
+ },
71
+ {
72
+ name: '@atlaskit/design-system/no-styled-tagged-template-expression',
73
+ displayName: 'no-styled-tagged-template-expression',
74
+ isExternal: true,
75
+ fixable: 'code',
76
+ docs: {
77
+ description:
78
+ 'Disallows any `styled` tagged template expressions that originate from Emotion, Styled Components or Compiled',
79
+ url: getRuleUrl('no-styled-tagged-template-expression'),
80
+ externalUrl:
81
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-styled-tagged-template-expression/usage',
82
+ recommended: true,
83
+ severity: 'warn',
84
+ },
85
+ },
86
+ {
87
+ name: '@compiled/no-suppress-xcss',
88
+ displayName: 'no-suppress-xcss',
89
+ isExternal: true,
90
+ docs: {
91
+ description:
92
+ 'Disallows supressing type violations when using the xcss prop.',
93
+ url: getRuleUrl('no-suppress-xcss'),
94
+ externalUrl:
95
+ 'https://github.com/atlassian-labs/compiled/tree/master/packages/eslint-plugin/src/rules/no-suppress-xcss',
96
+ recommended: true,
97
+ severity: 'warn',
98
+ },
99
+ },
100
+ {
101
+ name: '@compiled/no-js-xcss',
102
+ displayName: 'no-js-xcss',
103
+ isExternal: true,
104
+ docs: {
105
+ description: 'Disallows using xcss prop inside JavaScript files.',
106
+ url: getRuleUrl('no-js-xcss'),
107
+ externalUrl:
108
+ 'https://github.com/atlassian-labs/compiled/tree/master/packages/eslint-plugin/src/rules/no-js-xcss',
109
+ recommended: true,
110
+ severity: 'warn',
111
+ },
112
+ },
113
+ {
114
+ name: '@atlaskit/design-system/no-empty-styled-expression',
115
+ displayName: 'no-empty-styled-expression',
116
+ isExternal: true,
117
+ docs: {
118
+ description:
119
+ 'Forbids any styled expression to be used when passing empty arguments to styled.div() (or other JSX elements).',
120
+ url: getRuleUrl('no-empty-styled-expression'),
121
+ externalUrl:
122
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-empty-styled-expression/usage',
123
+ recommended: true,
124
+ severity: 'warn',
125
+ pluginConfig: { importSources },
126
+ },
127
+ },
128
+ {
129
+ name: '@atlaskit/design-system/no-exported-css',
130
+ displayName: 'no-exported-css',
131
+ isExternal: true,
132
+ docs: {
133
+ description:
134
+ 'Forbid exporting `css` function calls. Exporting `css` function calls can result in unexpected behaviour at runtime, and is not statically analysable.',
135
+ url: getRuleUrl('no-exported-css'),
136
+ externalUrl:
137
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-exported-css/usage',
138
+ recommended: true,
139
+ severity: 'warn',
140
+ pluginConfig: { importSources },
141
+ },
142
+ },
143
+ {
144
+ name: '@atlaskit/design-system/no-exported-keyframes',
145
+ displayName: 'no-exported-keyframes',
146
+ isExternal: true,
147
+ docs: {
148
+ description:
149
+ 'Forbid exporting `keyframes` function calls. Exporting `css` function calls can result in unexpected behaviour at runtime, and is not statically analysable.',
150
+ url: getRuleUrl('no-exported-keyframes'),
151
+ externalUrl:
152
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-exported-keyframes/usage',
153
+ recommended: true,
154
+ severity: 'warn',
155
+ pluginConfig: { importSources },
156
+ },
157
+ },
158
+ {
159
+ name: '@atlaskit/design-system/no-invalid-css-map',
160
+ displayName: 'no-invalid-css-map',
161
+ isExternal: true,
162
+ docs: {
163
+ description:
164
+ "Checks the validity of a CSS map created through cssMap. This is intended to be used alongside TypeScript's type-checking.",
165
+ url: getRuleUrl('no-invalid-css-map'),
166
+ externalUrl:
167
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-invalid-css-map/usage',
168
+ recommended: true,
169
+ severity: 'warn',
170
+ pluginConfig: {
171
+ allowedFunctionCalls: [['@atlaskit/tokens', 'token']],
172
+ },
173
+ },
174
+ },
175
+ ];
@@ -0,0 +1,10 @@
1
+ /**
2
+ * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
+ * @codegen <<SignedSource::f907e3f7ab0b7145dcb8318732ae1fec>>
4
+ * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
+ */
6
+ import localCxXcss from './local-cx-xcss';
7
+
8
+ export default {
9
+ 'local-cx-xcss': localCxXcss,
10
+ };
@@ -0,0 +1,35 @@
1
+ This rule ensures the `cx()` function is only used within the `xcss` prop. This aids tracking what styles are applied to a jsx element.
2
+
3
+ The `cx` function is checked only if it is imported from `@compiled/react` or `@atlaskit/css`.
4
+
5
+ Passing arguments to the `cx()` function is how you compose styles (combine more than one set of styles together) with XCSS. This is a workaround for the more conventional array syntax (e.g. [in Emotion](https://emotion.sh/docs/composition)) `<div xcss={[style1, style2]} />` not giving robust enough type checking.
6
+
7
+ ## Examples
8
+
9
+ ### Incorrect
10
+
11
+ ```js
12
+ import { cx, cssMap } from '@compiled/react';
13
+
14
+ const styles = cssMap({
15
+ text: { color: 'red' },
16
+ bg: { background: 'blue' },
17
+ });
18
+
19
+ const joinedStyles = cx(styles.text, styles.bg);
20
+
21
+ <Button xcss={joinedStyles} />;
22
+ ```
23
+
24
+ ### Correct
25
+
26
+ ```js
27
+ import { cx, cssMap } from '@compiled/react';
28
+
29
+ const styles = cssMap({
30
+ text: { color: 'red' },
31
+ bg: { background: 'blue' },
32
+ });
33
+
34
+ <Button xcss={cx(styles.text, styles.bg)} />;
35
+ ```
@@ -0,0 +1,102 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('local-cx-xcss', rule, {
5
+ valid: [
6
+ `
7
+ import { cx } from '@compiled/react';
8
+
9
+ <Component xcss={cx({})} />
10
+ `,
11
+ `
12
+ import { cx } from '@compiled/react';
13
+
14
+ <Component innerXcss={cx({})} />
15
+ `,
16
+ `
17
+ import { cx, cssMap } from '@compiled/react';
18
+
19
+ const styles = cssMap({
20
+ text: { color: 'red' },
21
+ bg: { background: 'blue' },
22
+ });
23
+
24
+ <Button innerXcss={cx(styles.text, styles.bg)} />;
25
+ `,
26
+ `
27
+ // Ignore cx usage not from compiled
28
+ const styles = cssMap({
29
+ text: { color: 'red' },
30
+ bg: { background: 'blue' },
31
+ });
32
+
33
+ const joinedStyles = cx(styles.text, styles.bg);
34
+
35
+ <Button xcss={joinedStyles} />;
36
+ `,
37
+ `
38
+ // Ignore cx usage not from compiled
39
+ const styles = cx({});
40
+
41
+ <Component xcss={styles} />
42
+ `,
43
+ `
44
+ // Ignore cx usage where
45
+ // the library is not specified in additionalCompiledLibraries
46
+ import { cx } from 'custom-package';
47
+ const styles = cx({});
48
+
49
+ <Component xcss={styles} />
50
+ `,
51
+ ],
52
+ invalid: [
53
+ {
54
+ code: `
55
+ import { cx } from '@compiled/react';
56
+ const styles = cx({});
57
+
58
+ <Component xcss={styles} />
59
+ `,
60
+ errors: [{ messageId: 'local-cx-xcss' }],
61
+ },
62
+ {
63
+ code: `
64
+ import { cx, cssMap } from '@compiled/react';
65
+
66
+ const styles = cssMap({
67
+ text: { color: 'red' },
68
+ bg: { background: 'blue' },
69
+ });
70
+
71
+ const joinedStyles = cx(styles.text, styles.bg);
72
+
73
+ <Button xcss={joinedStyles} />;
74
+ `,
75
+ errors: [{ messageId: 'local-cx-xcss' }],
76
+ },
77
+ {
78
+ code: `
79
+ import { cx } from '@compiled/react';
80
+ const styles = cx({});
81
+
82
+ <Component innerXcss={styles} />
83
+ `,
84
+ errors: [{ messageId: 'local-cx-xcss' }],
85
+ },
86
+ {
87
+ code: `
88
+ import { cx, cssMap } from '@compiled/react';
89
+
90
+ const styles = cssMap({
91
+ text: { color: 'red' },
92
+ bg: { background: 'blue' },
93
+ });
94
+
95
+ const joinedStyles = cx(styles.text, styles.bg);
96
+
97
+ <Button innerXcss={joinedStyles} />;
98
+ `,
99
+ errors: [{ messageId: 'local-cx-xcss' }],
100
+ },
101
+ ],
102
+ });
@@ -0,0 +1,69 @@
1
+ import type { Rule } from 'eslint';
2
+ import type { JSXAttribute } from 'estree-jsx';
3
+
4
+ import { createLintRule } from '../utils/create-rule';
5
+ import { CSS_IN_JS_IMPORTS, isCxFunction } from '../utils/is-supported-import';
6
+
7
+ const IMPORT_SOURCES = [
8
+ CSS_IN_JS_IMPORTS.compiled,
9
+ CSS_IN_JS_IMPORTS.atlaskitCss,
10
+ ];
11
+
12
+ function getParentJSXAttribute(node: Rule.Node): JSXAttribute | null {
13
+ let parent: Rule.Node | null = node.parent;
14
+
15
+ while (parent && parent.type !== 'JSXAttribute') {
16
+ parent = parent.parent;
17
+ }
18
+
19
+ if (parent && parent.type === 'JSXAttribute') {
20
+ return parent;
21
+ }
22
+
23
+ return null;
24
+ }
25
+
26
+ export const rule = createLintRule({
27
+ meta: {
28
+ name: 'local-cx-xcss',
29
+ docs: {
30
+ description:
31
+ '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.',
32
+ recommended: true,
33
+ severity: 'warn',
34
+ },
35
+ messages: {
36
+ 'local-cx-xcss':
37
+ 'The cx function should only be declared inside the xcss prop to simplify tracking styles that are applied to a jsx element.',
38
+ },
39
+ type: 'problem',
40
+ },
41
+ create(context) {
42
+ return {
43
+ 'CallExpression[callee.name="cx"]': (node: Rule.Node) => {
44
+ if (
45
+ node.type === 'CallExpression' &&
46
+ isCxFunction(
47
+ node.callee,
48
+ context.getScope().references,
49
+ IMPORT_SOURCES,
50
+ )
51
+ ) {
52
+ const parentJSXAttribute = getParentJSXAttribute(node);
53
+ const propName = parentJSXAttribute?.name.name.toString();
54
+
55
+ if (propName && /[xX]css$/.test(propName)) {
56
+ return;
57
+ }
58
+
59
+ context.report({
60
+ node,
61
+ messageId: 'local-cx-xcss',
62
+ });
63
+ }
64
+ },
65
+ };
66
+ },
67
+ });
68
+
69
+ export default rule;