@atlaskit/eslint-plugin-ui-styling-standard 0.3.0 → 0.4.1

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 +12 -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 +29 -11
  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 +5 -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 +39 -0
  19. package/dist/cjs/presets/recommended.codegen.js +39 -0
  20. package/dist/cjs/rules/external-rules.js +169 -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 +33 -0
  28. package/dist/es2019/presets/recommended.codegen.js +33 -0
  29. package/dist/es2019/rules/external-rules.js +164 -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 +33 -0
  37. package/dist/esm/presets/recommended.codegen.js +33 -0
  38. package/dist/esm/rules/external-rules.js +164 -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 +59 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist/types/presets/all.codegen.d.ts +34 -0
  46. package/dist/types/presets/recommended.codegen.d.ts +34 -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 +59 -0
  53. package/dist/types-ts4.5/index.d.ts +1 -0
  54. package/dist/types-ts4.5/presets/all.codegen.d.ts +34 -0
  55. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +34 -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 +78 -0
  66. package/src/presets/recommended.codegen.tsx +78 -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 +205 -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
@@ -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,205 @@
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
+ /**
21
+ * Same as importSources, but excludes `styled-components` because the type-checking
22
+ * for `styled-components` does not support the `no-styled-tagged-template-expression`
23
+ * lint output for this:
24
+ *
25
+ * export const Container = styled(Section)`
26
+ * max-height: ${gridSize * 46}px;
27
+ * overflow: auto;
28
+ * `;
29
+ *
30
+ * export const ErrorStateContainer = styled(Container)`
31
+ * margin: ${
32
+ * (props: any) =>
33
+ * props.canManageAutomations // Use negative margin to reduce whitespace when the Footer component is shown
34
+ * ? `${gridSize * -4}px ${gridSize * 2}px`
35
+ * : `0 ${gridSize * 2}px`
36
+ * };
37
+ * `;
38
+ */
39
+ const noStyledTaggedTemplateExpressionImportSources: readonly string[] = [
40
+ '@compiled/react',
41
+ '@atlaskit/css', // doesn't do anything because @atlaskit/css doesn't expose a styled API
42
+ '@emotion/core',
43
+ '@emotion/react',
44
+ '@emotion/styled',
45
+ ];
46
+
47
+ export const externalRules: ExternalRuleMeta[] = [
48
+ {
49
+ name: '@atlaskit/design-system/consistent-css-prop-usage',
50
+ displayName: 'consistent-css-prop-usage',
51
+ isExternal: true,
52
+ fixable: 'code',
53
+ docs: {
54
+ description: 'Ensures consistency with `css` and `xcss` prop usages',
55
+ url: getRuleUrl('consistent-css-prop-usage'),
56
+ externalUrl:
57
+ 'https://atlassian.design/components/eslint-plugin-design-system/consistent-css-prop-usage/usage',
58
+ recommended: true,
59
+ severity: 'warn',
60
+ pluginConfig: {
61
+ // When passing a css prop to a React component, we don't know whether the component
62
+ // is Emotion or Compiled. Hence it is not safe to run autofixes like wrapping it in
63
+ // a `css` function call.
64
+ excludeReactComponents: true,
65
+ },
66
+ },
67
+ },
68
+ {
69
+ name: '@atlaskit/design-system/no-css-tagged-template-expression',
70
+ displayName: 'no-css-tagged-template-expression',
71
+ isExternal: true,
72
+ fixable: 'code',
73
+ docs: {
74
+ description:
75
+ 'Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled',
76
+ url: getRuleUrl('no-css-tagged-template-expression'),
77
+ externalUrl:
78
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-css-tagged-template-expression/usage',
79
+ recommended: true,
80
+ severity: 'warn',
81
+ },
82
+ },
83
+ {
84
+ name: '@atlaskit/design-system/no-keyframes-tagged-template-expression',
85
+ displayName: 'no-keyframes-tagged-template-expression',
86
+ isExternal: true,
87
+ fixable: 'code',
88
+ docs: {
89
+ description:
90
+ 'Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled',
91
+ url: getRuleUrl('no-keyframes-tagged-template-expression'),
92
+ externalUrl:
93
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-keyframes-tagged-template-expression/usage',
94
+ recommended: true,
95
+ severity: 'warn',
96
+ },
97
+ },
98
+ {
99
+ name: '@atlaskit/design-system/no-styled-tagged-template-expression',
100
+ displayName: 'no-styled-tagged-template-expression',
101
+ isExternal: true,
102
+ fixable: 'code',
103
+ docs: {
104
+ description:
105
+ 'Disallows any `styled` tagged template expressions that originate from Emotion, Styled Components or Compiled',
106
+ url: getRuleUrl('no-styled-tagged-template-expression'),
107
+ externalUrl:
108
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-styled-tagged-template-expression/usage',
109
+ recommended: true,
110
+ severity: 'warn',
111
+ pluginConfig: {
112
+ importSources: noStyledTaggedTemplateExpressionImportSources,
113
+ },
114
+ },
115
+ },
116
+ {
117
+ name: '@compiled/no-suppress-xcss',
118
+ displayName: 'no-suppress-xcss',
119
+ isExternal: true,
120
+ docs: {
121
+ description:
122
+ 'Disallows supressing type violations when using the xcss prop.',
123
+ url: getRuleUrl('no-suppress-xcss'),
124
+ externalUrl:
125
+ 'https://github.com/atlassian-labs/compiled/tree/master/packages/eslint-plugin/src/rules/no-suppress-xcss',
126
+ recommended: true,
127
+ severity: 'warn',
128
+ },
129
+ },
130
+ {
131
+ name: '@compiled/no-js-xcss',
132
+ displayName: 'no-js-xcss',
133
+ isExternal: true,
134
+ docs: {
135
+ description: 'Disallows using xcss prop inside JavaScript files.',
136
+ url: getRuleUrl('no-js-xcss'),
137
+ externalUrl:
138
+ 'https://github.com/atlassian-labs/compiled/tree/master/packages/eslint-plugin/src/rules/no-js-xcss',
139
+ recommended: true,
140
+ severity: 'warn',
141
+ },
142
+ },
143
+ {
144
+ name: '@atlaskit/design-system/no-empty-styled-expression',
145
+ displayName: 'no-empty-styled-expression',
146
+ isExternal: true,
147
+ docs: {
148
+ description:
149
+ 'Forbids any styled expression to be used when passing empty arguments to styled.div() (or other JSX elements).',
150
+ url: getRuleUrl('no-empty-styled-expression'),
151
+ externalUrl:
152
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-empty-styled-expression/usage',
153
+ recommended: true,
154
+ severity: 'warn',
155
+ pluginConfig: { importSources },
156
+ },
157
+ },
158
+ {
159
+ name: '@atlaskit/design-system/no-exported-css',
160
+ displayName: 'no-exported-css',
161
+ isExternal: true,
162
+ docs: {
163
+ description:
164
+ 'Forbid exporting `css` function calls. Exporting `css` function calls can result in unexpected behaviour at runtime, and is not statically analysable.',
165
+ url: getRuleUrl('no-exported-css'),
166
+ externalUrl:
167
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-exported-css/usage',
168
+ recommended: true,
169
+ severity: 'warn',
170
+ pluginConfig: { importSources },
171
+ },
172
+ },
173
+ {
174
+ name: '@atlaskit/design-system/no-exported-keyframes',
175
+ displayName: 'no-exported-keyframes',
176
+ isExternal: true,
177
+ docs: {
178
+ description:
179
+ 'Forbid exporting `keyframes` function calls. Exporting `css` function calls can result in unexpected behaviour at runtime, and is not statically analysable.',
180
+ url: getRuleUrl('no-exported-keyframes'),
181
+ externalUrl:
182
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-exported-keyframes/usage',
183
+ recommended: true,
184
+ severity: 'warn',
185
+ pluginConfig: { importSources },
186
+ },
187
+ },
188
+ {
189
+ name: '@atlaskit/design-system/no-invalid-css-map',
190
+ displayName: 'no-invalid-css-map',
191
+ isExternal: true,
192
+ docs: {
193
+ description:
194
+ "Checks the validity of a CSS map created through cssMap. This is intended to be used alongside TypeScript's type-checking.",
195
+ url: getRuleUrl('no-invalid-css-map'),
196
+ externalUrl:
197
+ 'https://atlassian.design/components/eslint-plugin-design-system/no-invalid-css-map/usage',
198
+ recommended: true,
199
+ severity: 'warn',
200
+ pluginConfig: {
201
+ allowedFunctionCalls: [['@atlaskit/tokens', 'token']],
202
+ },
203
+ },
204
+ },
205
+ ];
@@ -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;
@@ -0,0 +1,80 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import type { Rule } from 'eslint';
4
+
5
+ /**
6
+ * We are moving to our own small abstraction to create a lint rule that we have the power
7
+ * to change and mold to our own needs.
8
+ *
9
+ * @see createLintRule
10
+ *
11
+ * @private
12
+ * @deprecated
13
+ */
14
+ export const createRule = ESLintUtils.RuleCreator((name) => getRuleUrl(name));
15
+
16
+ export interface LintRule extends Omit<Rule.RuleModule, 'meta'> {
17
+ /**
18
+ * Including this for backwards compat moving from the typescript-eslint util.
19
+ */
20
+ name?: never;
21
+ meta: LintRuleMeta;
22
+ }
23
+
24
+ export interface LintRuleMeta extends Omit<Rule.RuleMetaData, 'docs'> {
25
+ /**
26
+ * Name of the rule.
27
+ * Must match the folder it is in exactly else build will throw.
28
+ * Add an exception in codegen.tsx for nested rules.
29
+ */
30
+ name: string;
31
+ docs: {
32
+ /**
33
+ * Short description of what this rule does.
34
+ */
35
+ description: string;
36
+ /**
37
+ * Specifies the URL at which the full documentation can be accessed.
38
+ */
39
+ url?: string | undefined;
40
+ /**
41
+ * If this rule should be in the recommended preset or not.
42
+ * Set to `false` to exclude it from the recommended preset.
43
+ */
44
+ recommended: boolean;
45
+ /**
46
+ * The severity level to be applied to this rule.
47
+ * When setting to `"error"` it will mean releasing a breaking change.
48
+ */
49
+ severity: 'error' | 'warn';
50
+ /**
51
+ * This is configuration passed into the plugin to configure the rule.
52
+ *
53
+ * Eg. `{ excludeReactComponents: true }` would be passed into the plugin config
54
+ */
55
+ pluginConfig?: Record<string, any>;
56
+ };
57
+ }
58
+
59
+ /**
60
+ * Tiny wrapped over the ESLint rule module type that ensures
61
+ * there is a docs link to our ESLint plugin documentation page,
62
+ * as well as improving type support.
63
+ */
64
+ export const createLintRule = (rule: LintRule) => {
65
+ (rule.meta.docs as Record<string, unknown>).url = getRuleUrl(rule.meta.name);
66
+
67
+ return rule as Rule.RuleModule;
68
+ };
69
+
70
+ export function getRuleUrl(ruleName: string) {
71
+ const name = getPathSafeName(ruleName);
72
+ return `https://atlassian.design/components/eslint-plugin-ui-styling-standard/${name}/usage`;
73
+ }
74
+
75
+ /**
76
+ * If it's a nested rule, ensure the url is clean and safe for urls, file paths, etc.
77
+ */
78
+ export function getPathSafeName(ruleName: string) {
79
+ return ruleName.replace('/', '-');
80
+ }
@@ -0,0 +1,130 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import type { Rule, Scope } from 'eslint';
3
+ import type { CallExpression } from 'estree';
4
+
5
+ type Definition = Scope.Definition;
6
+ type Callee = CallExpression['callee'];
7
+ type Reference = Scope.Reference;
8
+
9
+ export const CSS_IN_JS_IMPORTS = {
10
+ compiled: '@compiled/react',
11
+ emotionReact: '@emotion/react',
12
+ emotionCore: '@emotion/core',
13
+ styledComponents: 'styled-components',
14
+ atlaskitCss: '@atlaskit/css',
15
+ atlaskitPrimitives: '@atlaskit/primitives',
16
+ } as const;
17
+
18
+ // A CSS-in-JS library an import of a valid css, cx, cssMap, etc.
19
+ // function might originate from, e.g. @compiled/react, @emotion/core.
20
+ export type ImportSource = string;
21
+
22
+ export type SupportedNameChecker = (
23
+ nodeToCheck: Callee,
24
+ referencesInScope: Reference[],
25
+ importSources: ImportSource[],
26
+ ) => boolean;
27
+
28
+ // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
29
+ export const DEFAULT_IMPORT_SOURCES: ImportSource[] = [
30
+ CSS_IN_JS_IMPORTS.compiled,
31
+ CSS_IN_JS_IMPORTS.atlaskitCss,
32
+ ];
33
+
34
+ /**
35
+ * Given the ESLint rule context, extract and parse the value of the importSources rule option.
36
+ * The importSources option is used to define additional libraries for which an ESLint rule
37
+ * should apply to.
38
+ *
39
+ * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
40
+ * of what importSources is configured to by the user.
41
+ *
42
+ * @param context The rule context.
43
+ * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
44
+ * on the rule options configuration.
45
+ */
46
+ export const getImportSources = (context: Rule.RuleContext): ImportSource[] => {
47
+ const options = context.options;
48
+ if (!options.length) {
49
+ return DEFAULT_IMPORT_SOURCES;
50
+ }
51
+
52
+ if (options[0].importSources && Array.isArray(options[0].importSources)) {
53
+ return [...DEFAULT_IMPORT_SOURCES, ...options[0].importSources];
54
+ }
55
+
56
+ return DEFAULT_IMPORT_SOURCES;
57
+ };
58
+
59
+ const isSupportedImportWrapper = (
60
+ functionName: string,
61
+ ): SupportedNameChecker => {
62
+ const checkDefinitionHasImport = (
63
+ def: Definition,
64
+ importSources: ImportSource[],
65
+ ) => {
66
+ if (def.type !== 'ImportBinding') {
67
+ return false;
68
+ }
69
+
70
+ if (
71
+ !def.parent ||
72
+ !importSources.includes(def.parent.source.value as ImportSource)
73
+ ) {
74
+ return false;
75
+ }
76
+
77
+ return (
78
+ // import { functionName } from 'import-source';
79
+ (def.node.type === 'ImportSpecifier' &&
80
+ def.node.imported.name === functionName) ||
81
+ // import functionName from 'import-source';
82
+ (def.node.type === 'ImportDefaultSpecifier' &&
83
+ def.node.local.name === functionName)
84
+ );
85
+ };
86
+
87
+ /**
88
+ * Checks whether:
89
+ *
90
+ * 1. A function name `nodeToCheck` matches the name of the function we
91
+ * want to check for (e.g. `cx`, `css`, `cssMap`, or `keyframes`), and
92
+ * 2. Whether `nodeToCheck` originates from one of the libraries listed
93
+ * in `importSources`.
94
+ *
95
+ * @param nodeToCheck The function callee we are checking (e.g. The `css` in `css()`).
96
+ * @param referencesInScope List of references that are in scope. We'll use this
97
+ * to check where the function callee is imported from.
98
+ * @param importSources List of libraries that we want to ensure `nodeToCheck`
99
+ * comes from.
100
+ *
101
+ * @returns Whether the above conditions are true.
102
+ */
103
+ const isSupportedImport = (
104
+ nodeToCheck: Callee,
105
+ referencesInScope: Reference[],
106
+ importSources: ImportSource[],
107
+ ): boolean => {
108
+ return (
109
+ nodeToCheck.type === 'Identifier' &&
110
+ referencesInScope.some(
111
+ (reference) =>
112
+ reference.identifier === nodeToCheck &&
113
+ reference.resolved?.defs.some((def) =>
114
+ checkDefinitionHasImport(def, importSources),
115
+ ),
116
+ )
117
+ );
118
+ };
119
+
120
+ return isSupportedImport;
121
+ };
122
+
123
+ // Unused functions have been commented out until we implement corresponding
124
+ // eslint rules which use them
125
+ //
126
+ export const isCss = isSupportedImportWrapper('css');
127
+ export const isCxFunction = isSupportedImportWrapper('cx');
128
+ export const isCssMap = isSupportedImportWrapper('cssMap');
129
+ export const isKeyframes = isSupportedImportWrapper('keyframes');
130
+ export const isStyled = isSupportedImportWrapper('styled');
package/tsconfig.app.json CHANGED
@@ -32,6 +32,10 @@
32
32
  "composite": true,
33
33
  "outDir": "../../../tsDist/@atlaskit__eslint-plugin-ui-styling-standard/app"
34
34
  },
35
- "references": [],
35
+ "references": [
36
+ {
37
+ "path": "../codegen/tsconfig.app.json"
38
+ }
39
+ ],
36
40
  "files": []
37
41
  }