@atlaskit/eslint-plugin-ui-styling-standard 0.12.0 → 0.14.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 (46) hide show
  1. package/CHANGELOG.md +21 -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/no-styled/usage.mdx +89 -0
  6. package/constellation/use-compiled/usage.mdx +20 -17
  7. package/dist/cjs/presets/all.codegen.js +3 -1
  8. package/dist/cjs/presets/recommended.codegen.js +3 -1
  9. package/dist/cjs/rules/index.codegen.js +5 -1
  10. package/dist/cjs/rules/no-global-styles/index.js +100 -0
  11. package/dist/cjs/rules/no-styled/index.js +67 -0
  12. package/dist/es2019/presets/all.codegen.js +3 -1
  13. package/dist/es2019/presets/recommended.codegen.js +3 -1
  14. package/dist/es2019/rules/index.codegen.js +5 -1
  15. package/dist/es2019/rules/no-global-styles/index.js +67 -0
  16. package/dist/es2019/rules/no-styled/index.js +45 -0
  17. package/dist/esm/presets/all.codegen.js +3 -1
  18. package/dist/esm/presets/recommended.codegen.js +3 -1
  19. package/dist/esm/rules/index.codegen.js +5 -1
  20. package/dist/esm/rules/no-global-styles/index.js +94 -0
  21. package/dist/esm/rules/no-styled/index.js +61 -0
  22. package/dist/types/index.codegen.d.ts +4 -0
  23. package/dist/types/presets/all.codegen.d.ts +3 -1
  24. package/dist/types/presets/recommended.codegen.d.ts +3 -1
  25. package/dist/types/rules/index.codegen.d.ts +2 -0
  26. package/dist/types/rules/no-global-styles/index.d.ts +2 -0
  27. package/dist/types/rules/no-styled/index.d.ts +2 -0
  28. package/dist/types-ts4.5/index.codegen.d.ts +4 -0
  29. package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
  30. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
  31. package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
  32. package/dist/types-ts4.5/rules/no-global-styles/index.d.ts +2 -0
  33. package/dist/types-ts4.5/rules/no-styled/index.d.ts +2 -0
  34. package/package.json +2 -2
  35. package/src/presets/all.codegen.tsx +3 -1
  36. package/src/presets/recommended.codegen.tsx +3 -1
  37. package/src/rules/index.codegen.tsx +5 -1
  38. package/src/rules/no-global-styles/README.md +56 -0
  39. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +33 -0
  40. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +46 -0
  41. package/src/rules/no-global-styles/index.tsx +85 -0
  42. package/src/rules/no-styled/README.md +82 -0
  43. package/src/rules/no-styled/__tests__/rule.test.tsx +73 -0
  44. package/src/rules/no-styled/index.tsx +52 -0
  45. package/src/rules/use-compiled/README.md +16 -16
  46. package/src/rules/use-compiled/__tests__/imports.test.tsx +24 -0
@@ -0,0 +1,56 @@
1
+ This rule prevents global styles through CSS-in-JS or CSS module imports.
2
+
3
+ Requiring local definition of styles helps to ensure that style dependencies are statically resolvable.
4
+
5
+ ## Examples
6
+
7
+ ### Incorrect
8
+
9
+ ```tsx
10
+ import { Global } from '@emotion/react';
11
+
12
+ <Global
13
+ styles={{
14
+ '.some-class': {
15
+ fontSize: 50,
16
+ textAlign: 'center',
17
+ },
18
+ }}
19
+ />;
20
+ ```
21
+
22
+ ```tsx
23
+ import { createGlobalStyle } from 'styled-components';
24
+
25
+ const GlobalStyle = createGlobalStyle({
26
+ body: {
27
+ margin: 0,
28
+ },
29
+ });
30
+ ```
31
+
32
+ ```tsx
33
+ import { injectGlobal } from 'styled-components';
34
+
35
+ injectGlobal({
36
+ body: {
37
+ margin: 0,
38
+ },
39
+ });
40
+ ```
41
+
42
+ ```tsx
43
+ <style>
44
+ {`
45
+ .some-class {
46
+ color: red;
47
+ }
48
+ `}
49
+ </style>
50
+ ```
51
+
52
+ ### Correct
53
+
54
+ ```tsx
55
+ import '@atlaskit/css-reset';
56
+ ```
@@ -0,0 +1,33 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('no-css-modules', rule, {
5
+ valid: [
6
+ "import 'styles-css';",
7
+ "import 'less';",
8
+ "import 'sass-module';",
9
+ "import 'something-scss-something';",
10
+ ],
11
+ invalid: [
12
+ {
13
+ name: '.css import',
14
+ code: "import 'styles.css';",
15
+ errors: [{ messageId: 'no-css-modules' }],
16
+ },
17
+ {
18
+ name: '.less import',
19
+ code: "import 'styles.less';",
20
+ errors: [{ messageId: 'no-css-modules' }],
21
+ },
22
+ {
23
+ name: '.sass import',
24
+ code: "import 'styles.sass';",
25
+ errors: [{ messageId: 'no-css-modules' }],
26
+ },
27
+ {
28
+ name: '.scss import',
29
+ code: "import 'styles.scss';",
30
+ errors: [{ messageId: 'no-css-modules' }],
31
+ },
32
+ ],
33
+ });
@@ -0,0 +1,46 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('no-global-styles', rule, {
5
+ valid: [
6
+ "import { css } from '@emotion/react';",
7
+ "import { keyframes } from '@emotion/core';",
8
+ "import styled from 'styled-components';",
9
+ ],
10
+ invalid: [
11
+ {
12
+ name: '[@emotion/react] Global',
13
+ code: "import { Global } from '@emotion/react';",
14
+ errors: [{ messageId: 'no-global-styles' }],
15
+ },
16
+ {
17
+ name: '[@emotion/core] Global',
18
+ code: "import { Global } from '@emotion/core';",
19
+ errors: [{ messageId: 'no-global-styles' }],
20
+ },
21
+ {
22
+ name: '[styled-components] injectGlobal',
23
+ code: "import { injectGlobal } from 'styled-components';",
24
+ errors: [{ messageId: 'no-global-styles' }],
25
+ },
26
+ {
27
+ name: '[styled-components] createGlobalStyle',
28
+ code: "import { createGlobalStyle } from 'styled-components';",
29
+ errors: [{ messageId: 'no-global-styles' }],
30
+ },
31
+ {
32
+ name: 'aliasing',
33
+ code: "import { Global as EmotionGlobal } from '@emotion/react';",
34
+ errors: [{ messageId: 'no-global-styles' }],
35
+ },
36
+ {
37
+ name: 'style tag',
38
+ code: `
39
+ <style>
40
+ {'.my-class { color: red; }'}
41
+ </style>
42
+ `,
43
+ errors: [{ messageId: 'no-global-styles' }],
44
+ },
45
+ ],
46
+ });
@@ -0,0 +1,85 @@
1
+ import type * as ESTree from 'eslint-codemod-utils';
2
+
3
+ import { createLintRule } from '../utils/create-rule';
4
+
5
+ const restrictedFileTypes = ['.css', '.less', '.sass', '.scss'];
6
+
7
+ const restrictedImports = {
8
+ '@emotion/core': ['Global'],
9
+ '@emotion/react': ['Global'],
10
+ 'styled-components': ['createGlobalStyle', 'injectGlobal'],
11
+ };
12
+
13
+ type RestrictedImportSource = keyof typeof restrictedImports;
14
+ function isRestrictedImportSource(
15
+ importSource: string,
16
+ ): importSource is RestrictedImportSource {
17
+ return importSource in restrictedImports;
18
+ }
19
+
20
+ export const rule = createLintRule({
21
+ meta: {
22
+ name: 'no-global-styles',
23
+ docs: {
24
+ description:
25
+ 'Prevents global styles through CSS-in-JS or CSS module imports',
26
+ recommended: true,
27
+ severity: 'warn',
28
+ },
29
+ messages: {
30
+ 'no-global-styles': 'Global styles are not allowed.',
31
+ 'no-css-modules': 'Importing CSS files is not allowed.',
32
+ },
33
+ type: 'problem',
34
+ },
35
+ create(context) {
36
+ function checkFileType(node: ESTree.ImportDeclaration) {
37
+ const importSource = node.source.value as string;
38
+ if (restrictedFileTypes.some((suffix) => importSource.endsWith(suffix))) {
39
+ context.report({
40
+ node: node.source,
41
+ messageId: 'no-css-modules',
42
+ });
43
+ }
44
+ }
45
+
46
+ function checkImports(node: ESTree.ImportDeclaration) {
47
+ const importSource = node.source.value as string;
48
+ if (!isRestrictedImportSource(importSource)) {
49
+ return;
50
+ }
51
+
52
+ for (const importName of restrictedImports[importSource]) {
53
+ const specifier = node.specifiers
54
+ .filter(isImportSpecifier)
55
+ .find((specifier) => specifier.imported.name === importName);
56
+
57
+ if (specifier) {
58
+ context.report({
59
+ node: specifier,
60
+ messageId: 'no-global-styles',
61
+ });
62
+ }
63
+ }
64
+ }
65
+
66
+ return {
67
+ ImportDeclaration(node: ESTree.ImportDeclaration) {
68
+ checkFileType(node);
69
+ checkImports(node);
70
+ },
71
+ 'JSXOpeningElement[name.name="style"]'(node: ESTree.Node) {
72
+ context.report({
73
+ node,
74
+ messageId: 'no-global-styles',
75
+ });
76
+ },
77
+ };
78
+ },
79
+ });
80
+
81
+ export default rule;
82
+
83
+ function isImportSpecifier(node: ESTree.Node): node is ESTree.ImportSpecifier {
84
+ return node.type === 'ImportSpecifier';
85
+ }
@@ -0,0 +1,82 @@
1
+ This rule prevents usage of `styled` imports'.
2
+
3
+ Avoid usages of styled, eg. `styled.div`, `styled('div')`, `styled(Component)`, etc.
4
+
5
+ ## Examples
6
+
7
+ ### Incorrect
8
+
9
+ ```tsx
10
+ import styled from 'styled-components';
11
+
12
+ const Component = styled.div`
13
+ color: red;
14
+ `;
15
+ export default styled.div({ color: 'red' });
16
+ ```
17
+
18
+ ```tsx
19
+ import styled2 from '@emotion/styled';
20
+
21
+ export const Component = styled2('div')`…`;
22
+ ```
23
+
24
+ ```tsx
25
+ import styled from 'styled-components';
26
+ import { styled as styled3 } from '@compiled/react';
27
+
28
+ const Component = styled.div`color: red;`
29
+ export const ComponentTwo = styled3(Component)({ … });
30
+ ```
31
+
32
+ ```tsx
33
+ import styled from 'styled-components';
34
+
35
+ export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))({
36
+ color: 'red',
37
+ });
38
+ ```
39
+
40
+ ```tsx
41
+ import styled from 'styled-components';
42
+
43
+ export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))`
44
+ color: red;
45
+ `;
46
+ ```
47
+
48
+ ### Correct
49
+
50
+ ```tsx
51
+ import { css, jsx } from '@emotion/react';
52
+
53
+ const styles = css({ color: 'red' });
54
+
55
+ const Component = ({ children }) => {
56
+ return <div css={styles}>{children}</div>;
57
+ };
58
+ ```
59
+
60
+ ```tsx
61
+ // Using Primitives
62
+ import { Box, xcss } from '@atlaskit/primitives';
63
+
64
+ const styles = xcss({
65
+ color: 'color.text.subtlest',
66
+ });
67
+
68
+ const Component = ({ children }) => {
69
+ return <Box xcss={styles}>{children}</Box>;
70
+ };
71
+ ```
72
+
73
+ ## FAQ
74
+
75
+ ### Why avoid using `styled` imports?
76
+
77
+ Using `styled` imports impacts file readability, has TypeScript implications, adds linting complexity, promotes exported styles, and potential refactoring efforts. Using `css()` with the `<div css={…}>` syntax is more performant and clear.
78
+
79
+ ### How will I extend like `styled(Button)`?
80
+
81
+ Primarily, we would suggest you don't extend styles of a component you don't own unless they have an explicit bounded interface for you to customize against. The Atlassian Design System for example has removed `styled(Button)` interactivity in our latest variants because they are unsafe for us to evolve the system with.
82
+ Instead, make use of reusable component patterns to pass props around which impact the styles of that component.
@@ -0,0 +1,73 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('no-styled', rule, {
5
+ valid: [
6
+ {
7
+ code: `
8
+ import { css, jsx } from '@emotion/react';
9
+
10
+ const styles = css({ color: 'red' });
11
+
12
+ const Component = ({ children }) => {
13
+ return <div css={styles}>{children}</div>;
14
+ };
15
+ `,
16
+ },
17
+ {
18
+ code: `
19
+ import { Box, xcss } from '@atlaskit/primitives';
20
+
21
+ const styles = xcss({
22
+ color: 'color.text.subtlest',
23
+ });
24
+
25
+ const Component = ({ children }) => {
26
+ return <Box xcss={styles}>{children}</Box>;
27
+ };
28
+ `
29
+ }
30
+ ],
31
+ invalid: [
32
+ {
33
+ code: `
34
+ import styled from 'styled-components';
35
+
36
+ const Component = styled.div\`color: red;\`
37
+ `,
38
+ errors: [{ messageId: 'no-styled' }],
39
+ },
40
+ {
41
+ code: `
42
+ import styled from 'styled-components';
43
+
44
+ export default styled.div({ color: 'red' });
45
+ `,
46
+ errors: [{ messageId: 'no-styled' }],
47
+ },
48
+ {
49
+ code: `
50
+ import { styled as styled3 } from '@compiled/react';
51
+
52
+ export const ComponentTwo = styled3(Component)({ color: 'blue' });
53
+ `,
54
+ errors: [{ messageId: 'no-styled' }],
55
+ },
56
+ {
57
+ code: `
58
+ import styled from 'styled-components';
59
+
60
+ export default styled.div.attrs(props => ({ 'data-testid': props.testId }))({ color: 'red' });
61
+ `,
62
+ errors: [{ messageId: 'no-styled' }],
63
+ },
64
+ {
65
+ code: `
66
+ import styled from 'styled-components';
67
+
68
+ export default styled.div.attrs(props => ({ 'data-testid': props.testId }))\`color: red\`;
69
+ `,
70
+ errors: [{ messageId: 'no-styled' }],
71
+ }
72
+ ],
73
+ });
@@ -0,0 +1,52 @@
1
+ import { type MemberExpression, type CallExpression } from 'eslint-codemod-utils';
2
+ import { isStyled, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
3
+
4
+
5
+ import { createLintRule } from '../utils/create-rule';
6
+
7
+ export const rule = createLintRule({
8
+ meta: {
9
+ name: 'no-styled',
10
+ docs: {
11
+ description: 'Disallows usage of the `styled` imports',
12
+ recommended: true,
13
+ severity: 'warn',
14
+ },
15
+ messages: {
16
+ 'no-styled': "Avoid usages of styled, eg. styled.div, styled('div'), styled(Component), etc.",
17
+ },
18
+ type: 'problem',
19
+ },
20
+ create(context) {
21
+ return {
22
+ CallExpression: (node: CallExpression) => {
23
+ const references = context.getScope().references;
24
+ const importSources = getImportSources(context);
25
+
26
+ if (
27
+ isStyled(node, references, importSources)
28
+ ) {
29
+ context.report({ node, messageId: 'no-styled' });
30
+ }
31
+
32
+ },
33
+ MemberExpression: (node: MemberExpression) => {
34
+ const references = context.getScope().references;
35
+ const importSources = getImportSources(context);
36
+
37
+ if (
38
+ node.object.type === 'Identifier' &&
39
+ isStyled(node.object, references, importSources)
40
+ ) {
41
+ const parent = context.getAncestors().at(-1);
42
+ if (parent && (parent.type === 'CallExpression' || parent.type === 'MemberExpression')) {
43
+ return;
44
+ }
45
+ context.report({ node, messageId: 'no-styled' });
46
+ }
47
+ },
48
+ };
49
+ },
50
+ });
51
+
52
+ export default rule;
@@ -1,25 +1,17 @@
1
- Ensures usage of `@compiled/react` over other CSS-in-JS libraries.
1
+ Ensures usage of `@compiled/react` over other CSS-in-JS libraries like Emotion or styled-components.
2
2
 
3
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.
4
+ It may be unsafe to mix usages of `@compiled/react` with other CSS-in-JS libraries on the same component. When converting to `@compiled/react` verify ALL changes. For this reason, the auto-fixer has been disabled by default, but it may be useful to enable to empower a migration.
10
5
 
11
6
  ## Examples
12
7
 
13
8
  ### Incorrect
14
9
 
15
- ```js
16
- import { css } from '@emotion/core';
17
- import styled from '@emotion/styled';
18
- ```
19
-
20
10
  ```js
21
11
  /** @jsx jsx */
12
+ import { css } from '@emotion/core';
22
13
  import { jsx } from '@emotion/react';
14
+ import styled from '@emotion/styled';
23
15
  ```
24
16
 
25
17
  ```js
@@ -28,13 +20,21 @@ import styled, { css } from 'styled-components';
28
20
 
29
21
  ### Correct
30
22
 
23
+ We expect usage of `xcss` with `@atlaskit/primitives` when working with Primitives, and `css` with `@compiled/react` when working with custom or native code. 
24
+
31
25
  ```js
32
- import { css, styled } from '@compiled/react';
26
+ /** @jsx jsx */
27
+ import { jsx, css, styled } from '@compiled/react';
28
+
29
+ const styles = css({ color: 'var(--ds-color-text)' });
30
+ export default () => <div css={styles}>…</div>
33
31
  ```
34
32
 
35
33
  ```js
36
- /** @jsx jsx */
37
- import { jsx } from '@compiled/react';
34
+ import { xcss, Box } from '@atlaskit/primitives';
35
+
36
+ const styles = xcss({ color: 'color.text' });
37
+ export default () => <Box xcss={styles}>…</Box>
38
38
  ```
39
39
 
40
40
  ## Options
@@ -43,7 +43,7 @@ import { jsx } from '@compiled/react';
43
43
 
44
44
  Determines whether or not the auto-fixer is enabled.
45
45
 
46
- Defaults to `false` due to safety concerns when mixing Compiled and other CSS-in-JS libraries.
46
+ Defaults to `false` due to safety concerns when mixing Compiled and other CSS-in-JS libraries on the same component.
47
47
 
48
48
  Even when enabled, the auto-fixer will only convert usages that are deemed safe.
49
49
  Currently this is limited to purely static styles where all keys and values are simple literals.
@@ -10,6 +10,30 @@ const importSources = [
10
10
  'styled-components',
11
11
  ];
12
12
 
13
+ tester.run('imports - ignored', rule, {
14
+ valid: [
15
+ {
16
+ name: '[@atlaskit/primitives.xcss] - is ignored (it\'s okay to use, for now)',
17
+ code: `
18
+ import { xcss, Box } from '@atlaskit/primitives';
19
+ `,
20
+ },
21
+ {
22
+ name: '[@atlaskit/css] - is ignored (it\'s okay to use)',
23
+ code: `
24
+ import { cssMap, keyframes } from '@atlaskit/css';
25
+ `,
26
+ },
27
+ {
28
+ name: '[@stylexjs/stylex] - is ignored (we don\'t maintain a list of all CSS-in-JS libraries, but we could)',
29
+ code: `
30
+ import * as stylex from '@stylexjs/stylex';
31
+ `,
32
+ }
33
+ ],
34
+ invalid: [],
35
+ });
36
+
13
37
  tester.run('imports - fixable', rule, {
14
38
  valid: [],
15
39
  invalid: [