@atlaskit/eslint-plugin-ui-styling-standard 0.15.1 → 0.16.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 (99) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/constellation/index/migration-guide.mdx +309 -0
  3. package/constellation/index/overview.mdx +4 -0
  4. package/constellation/index/props.mdx +9 -5
  5. package/dist/cjs/rules/no-nested-selectors/index.js +68 -49
  6. package/dist/es2019/rules/no-nested-selectors/index.js +63 -45
  7. package/dist/esm/rules/no-nested-selectors/index.js +64 -49
  8. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
  9. package/index.js +9 -9
  10. package/package.json +3 -3
  11. package/scripts/codegen.tsx +223 -284
  12. package/src/rules/__tests__/utils/_tester.tsx +24 -24
  13. package/src/rules/atlaskit-theme/README.md +21 -19
  14. package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
  15. package/src/rules/atlaskit-theme/index.ts +107 -115
  16. package/src/rules/convert-props-syntax/README.md +32 -22
  17. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
  18. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
  19. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
  20. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
  21. package/src/rules/convert-props-syntax/index.tsx +356 -386
  22. package/src/rules/enforce-style-prop/README.md +21 -18
  23. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
  24. package/src/rules/enforce-style-prop/index.tsx +144 -160
  25. package/src/rules/external-rules.tsx +163 -164
  26. package/src/rules/local-cx-xcss/README.md +10 -6
  27. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
  28. package/src/rules/local-cx-xcss/index.tsx +46 -56
  29. package/src/rules/no-array-arguments/README.md +2 -1
  30. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
  31. package/src/rules/no-array-arguments/index.tsx +68 -69
  32. package/src/rules/no-classname-prop/README.md +7 -4
  33. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
  34. package/src/rules/no-classname-prop/index.tsx +25 -25
  35. package/src/rules/no-container-queries/README.md +11 -9
  36. package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
  37. package/src/rules/no-container-queries/index.tsx +75 -81
  38. package/src/rules/no-dynamic-styles/README.md +16 -19
  39. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
  40. package/src/rules/no-dynamic-styles/index.tsx +45 -48
  41. package/src/rules/no-exported-styles/README.md +17 -12
  42. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
  43. package/src/rules/no-exported-styles/index.tsx +36 -36
  44. package/src/rules/no-global-styles/README.md +15 -14
  45. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
  46. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
  47. package/src/rules/no-global-styles/index.tsx +59 -62
  48. package/src/rules/no-important-styles/README.md +5 -3
  49. package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
  50. package/src/rules/no-important-styles/index.tsx +49 -49
  51. package/src/rules/no-imported-style-values/README.md +23 -15
  52. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
  53. package/src/rules/no-imported-style-values/index.tsx +116 -120
  54. package/src/rules/no-nested-selectors/README.md +15 -13
  55. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +186 -149
  56. package/src/rules/no-nested-selectors/index.tsx +92 -105
  57. package/src/rules/no-styled/README.md +14 -9
  58. package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
  59. package/src/rules/no-styled/index.tsx +34 -41
  60. package/src/rules/no-unsafe-selectors/README.md +103 -93
  61. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
  62. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
  63. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
  64. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
  65. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
  66. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
  67. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
  68. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
  69. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
  70. package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
  71. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
  72. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
  73. package/src/rules/no-unsafe-values/README.md +34 -40
  74. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
  75. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
  76. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
  77. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
  78. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
  79. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
  80. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
  81. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
  82. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
  83. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
  84. package/src/rules/no-unsafe-values/index.tsx +76 -78
  85. package/src/rules/no-unsafe-values/messages.tsx +12 -12
  86. package/src/rules/no-unsafe-values/utils.tsx +370 -382
  87. package/src/rules/use-compiled/README.md +11 -8
  88. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
  89. package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
  90. package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
  91. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
  92. package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
  93. package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
  94. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
  95. package/src/rules/use-compiled/index.tsx +203 -226
  96. package/src/rules/use-compiled/is-safe.tsx +113 -117
  97. package/src/rules/use-compiled/utils.tsx +14 -18
  98. package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
  99. package/src/rules/utils/create-rule.tsx +3 -6
@@ -1,112 +1,99 @@
1
1
  import type { Property } from 'estree';
2
+ import cssSelectorParser, { type Selector, isNesting, isPseudo } from 'postcss-selector-parser';
2
3
 
3
- import {
4
- isStyled,
5
- isCss,
6
- isXcss,
7
- getImportSources,
8
- } from '@atlaskit/eslint-utils/is-supported-import';
9
- import { createLintRule } from '../utils/create-rule';
10
- import { type JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
11
-
12
- const schema: JSONSchema4 = [
13
- {
14
- type: 'object',
15
- properties: {
16
- importSources: {
17
- type: 'array',
18
- items: { type: 'string' },
19
- uniqueItems: true,
20
- },
21
- },
22
- },
23
- ];
24
-
25
- const getCssSelector = (node: Property): string | null => {
26
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
27
- return node.key.value;
28
- }
29
-
30
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
31
- return node.key.name;
32
- }
33
-
34
- if (node.key.type === 'TemplateLiteral') {
35
- return node.key.quasis.map((quasi) => quasi.value.raw).join(' ');
36
- }
37
-
38
- return null;
4
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
5
+ import { importSources } from '@atlaskit/eslint-utils/schema';
6
+
7
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
8
+
9
+ const getCssSelector = (key: Property['key']): string | null => {
10
+ if (key.type === 'Literal' && typeof key.value === 'string') {
11
+ return key.value;
12
+ }
13
+
14
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
15
+ return key.name;
16
+ }
17
+
18
+ if (key.type === 'TemplateLiteral') {
19
+ return key.quasis.map((quasi) => quasi.value.raw).join(' ');
20
+ }
21
+
22
+ return null;
39
23
  };
40
24
 
41
- export const rule = createLintRule({
42
- meta: {
43
- name: 'no-nested-selectors',
44
- docs: {
45
- description: 'Prevents usage of nested selectors within css styling',
46
- recommended: true,
47
- severity: 'warn',
48
- },
49
- messages: {
50
- 'no-nested-selectors':
51
- 'Please avoid setting styles for child elements or elements that require context from other elements.',
52
- },
53
- type: 'problem',
54
- schema,
55
- },
56
- create(context) {
57
- const importSources = getImportSources(context);
58
- return {
59
- 'CallExpression Property': (node: Property): void => {
60
- const { references } = context.getScope();
61
- const ancestors = context.getAncestors();
62
-
63
- if (
64
- ancestors.every(
65
- (ancestor) =>
66
- ancestor.type !== 'CallExpression' ||
67
- !ancestor?.callee ||
68
- !(
69
- isCss(ancestor.callee, references, importSources) ||
70
- isStyled(ancestor.callee, references, importSources) ||
71
- isXcss(ancestor.callee, references, importSources)
72
- ),
73
- )
74
- ) {
75
- return;
76
- }
77
-
78
- if (node.value.type !== 'ObjectExpression') {
79
- // If the value is a CSS object, safe to assume we're at a CSS selector
80
- return;
81
- }
82
-
83
- const cssSelector = getCssSelector(node);
84
- if (cssSelector === null) {
85
- return;
86
- }
87
-
88
- const tokens = cssSelector.split(/[, ]+/);
89
-
90
- if (
91
- !cssSelector.includes('@') &&
92
- !tokens.every(
93
- (token) =>
94
- token.length === 0 ||
95
- token.startsWith('&') ||
96
- token.startsWith(':') ||
97
- token.includes('+') ||
98
- token.includes('~') ||
99
- token.endsWith('&'),
100
- )
101
- ) {
102
- context.report({
103
- messageId: 'no-nested-selectors',
104
- node: node.key,
105
- });
106
- }
107
- },
108
- };
109
- },
25
+ function isAllowedSelector(selector: Selector): boolean {
26
+ const [head, ...tail] = selector.nodes;
27
+
28
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
29
+ const isHeadAllowed = isNesting(head) || isPseudo(head);
30
+
31
+ // All remaining nodes must be pseudo selectors.
32
+ const isTailAllowed = tail.every(isPseudo);
33
+
34
+ return isHeadAllowed && isTailAllowed;
35
+ }
36
+
37
+ const cssSelectorProcessor = cssSelectorParser();
38
+
39
+ export const rule = createLintRuleWithTypedConfig({
40
+ meta: {
41
+ name: 'no-nested-selectors',
42
+ docs: {
43
+ description: 'Prevents usage of nested selectors within css styling',
44
+ recommended: true,
45
+ severity: 'warn',
46
+ },
47
+ messages: {
48
+ 'no-nested-selectors':
49
+ 'Please avoid setting styles for child elements or elements that require context from other elements.',
50
+ },
51
+ type: 'problem',
52
+ schema: {
53
+ type: 'object',
54
+ properties: {
55
+ importSources,
56
+ },
57
+ },
58
+ },
59
+ create(context, { importSources }) {
60
+ return {
61
+ CallExpression(node) {
62
+ const { references } = context.getScope();
63
+
64
+ walkStyleProperties(node, references, importSources, ({ key, value }) => {
65
+ if (value.type !== 'ObjectExpression') {
66
+ // If the value is a CSS object, safe to assume we're at a CSS selector
67
+ return;
68
+ }
69
+
70
+ const selectorText = getCssSelector(key);
71
+ if (selectorText === null) {
72
+ return;
73
+ }
74
+
75
+ // Ignore at-rules
76
+ if (selectorText.includes('@')) {
77
+ return;
78
+ }
79
+
80
+ try {
81
+ const selectorList = cssSelectorProcessor.astSync(selectorText);
82
+
83
+ if (!selectorList.nodes.every(isAllowedSelector)) {
84
+ context.report({
85
+ messageId: 'no-nested-selectors',
86
+ node: key,
87
+ });
88
+ }
89
+ } catch (_) {
90
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
91
+ // we can ignore it here.
92
+ }
93
+ });
94
+ },
95
+ };
96
+ },
110
97
  });
111
98
 
112
99
  export default rule;
@@ -10,7 +10,7 @@ Avoid usages of styled, eg. `styled.div`, `styled('div')`, `styled(Component)`,
10
10
  import styled from 'styled-components';
11
11
 
12
12
  const Component = styled.div`
13
- color: red;
13
+ color: red;
14
14
  `;
15
15
  export default styled.div({ color: 'red' });
16
16
  ```
@@ -33,7 +33,7 @@ export const ComponentTwo = styled3(Component)({ … });
33
33
  import styled from 'styled-components';
34
34
 
35
35
  export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))({
36
- color: 'red',
36
+ color: 'red',
37
37
  });
38
38
  ```
39
39
 
@@ -41,7 +41,7 @@ export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))({
41
41
  import styled from 'styled-components';
42
42
 
43
43
  export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))`
44
- color: red;
44
+ color: red;
45
45
  `;
46
46
  ```
47
47
 
@@ -53,7 +53,7 @@ import { css, jsx } from '@emotion/react';
53
53
  const styles = css({ color: 'red' });
54
54
 
55
55
  const Component = ({ children }) => {
56
- return <div css={styles}>{children}</div>;
56
+ return <div css={styles}>{children}</div>;
57
57
  };
58
58
  ```
59
59
 
@@ -62,11 +62,11 @@ const Component = ({ children }) => {
62
62
  import { Box, xcss } from '@atlaskit/primitives';
63
63
 
64
64
  const styles = xcss({
65
- color: 'color.text.subtlest',
65
+ color: 'color.text.subtlest',
66
66
  });
67
67
 
68
68
  const Component = ({ children }) => {
69
- return <Box xcss={styles}>{children}</Box>;
69
+ return <Box xcss={styles}>{children}</Box>;
70
70
  };
71
71
  ```
72
72
 
@@ -74,9 +74,14 @@ const Component = ({ children }) => {
74
74
 
75
75
  ### Why avoid using `styled` imports?
76
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.
77
+ Using `styled` imports impacts file readability, has TypeScript implications, adds linting
78
+ complexity, promotes exported styles, and potential refactoring efforts. Using `css()` with the
79
+ `<div css={…}>` syntax is more performant and clear.
78
80
 
79
81
  ### How will I extend like `styled(Button)`?
80
82
 
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.
83
+ Primarily, we would suggest you don't extend styles of a component you don't own unless they have an
84
+ explicit bounded interface for you to customize against. The Atlassian Design System for example has
85
+ removed `styled(Button)` interactivity in our latest variants because they are unsafe for us to
86
+ evolve the system with. Instead, make use of reusable component patterns to pass props around which
87
+ impact the styles of that component.
@@ -2,9 +2,9 @@ import { tester } from '../../__tests__/utils/_tester';
2
2
  import rule from '../index';
3
3
 
4
4
  tester.run('no-styled', rule, {
5
- valid: [
6
- {
7
- code: `
5
+ valid: [
6
+ {
7
+ code: `
8
8
  import { css, jsx } from '@emotion/react';
9
9
 
10
10
  const styles = css({ color: 'red' });
@@ -13,9 +13,9 @@ tester.run('no-styled', rule, {
13
13
  return <div css={styles}>{children}</div>;
14
14
  };
15
15
  `,
16
- },
17
- {
18
- code: `
16
+ },
17
+ {
18
+ code: `
19
19
  import { Box, xcss } from '@atlaskit/primitives';
20
20
 
21
21
  const styles = xcss({
@@ -25,49 +25,49 @@ tester.run('no-styled', rule, {
25
25
  const Component = ({ children }) => {
26
26
  return <Box xcss={styles}>{children}</Box>;
27
27
  };
28
- `
29
- }
30
- ],
31
- invalid: [
32
- {
33
- code: `
28
+ `,
29
+ },
30
+ ],
31
+ invalid: [
32
+ {
33
+ code: `
34
34
  import styled from 'styled-components';
35
35
 
36
36
  const Component = styled.div\`color: red;\`
37
37
  `,
38
- errors: [{ messageId: 'no-styled' }],
39
- },
40
- {
41
- code: `
38
+ errors: [{ messageId: 'no-styled' }],
39
+ },
40
+ {
41
+ code: `
42
42
  import styled from 'styled-components';
43
43
 
44
44
  export default styled.div({ color: 'red' });
45
45
  `,
46
- errors: [{ messageId: 'no-styled' }],
47
- },
48
- {
49
- code: `
46
+ errors: [{ messageId: 'no-styled' }],
47
+ },
48
+ {
49
+ code: `
50
50
  import { styled as styled3 } from '@compiled/react';
51
51
 
52
52
  export const ComponentTwo = styled3(Component)({ color: 'blue' });
53
53
  `,
54
- errors: [{ messageId: 'no-styled' }],
55
- },
56
- {
57
- code: `
54
+ errors: [{ messageId: 'no-styled' }],
55
+ },
56
+ {
57
+ code: `
58
58
  import styled from 'styled-components';
59
59
 
60
60
  export default styled.div.attrs(props => ({ 'data-testid': props.testId }))({ color: 'red' });
61
61
  `,
62
- errors: [{ messageId: 'no-styled' }],
63
- },
64
- {
65
- code: `
62
+ errors: [{ messageId: 'no-styled' }],
63
+ },
64
+ {
65
+ code: `
66
66
  import styled from 'styled-components';
67
67
 
68
68
  export default styled.div.attrs(props => ({ 'data-testid': props.testId }))\`color: red\`;
69
69
  `,
70
- errors: [{ messageId: 'no-styled' }],
71
- }
72
- ],
70
+ errors: [{ messageId: 'no-styled' }],
71
+ },
72
+ ],
73
73
  });
@@ -1,52 +1,45 @@
1
1
  import { type MemberExpression, type CallExpression } from 'eslint-codemod-utils';
2
2
  import { isStyled, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
3
3
 
4
-
5
4
  import { createLintRule } from '../utils/create-rule';
6
5
 
7
6
  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
- }
7
+ meta: {
8
+ name: 'no-styled',
9
+ docs: {
10
+ description: 'Disallows usage of the `styled` imports',
11
+ recommended: true,
12
+ severity: 'warn',
13
+ },
14
+ messages: {
15
+ 'no-styled': "Avoid usages of styled, eg. styled.div, styled('div'), styled(Component), etc.",
16
+ },
17
+ type: 'problem',
18
+ },
19
+ create(context) {
20
+ return {
21
+ CallExpression: (node: CallExpression) => {
22
+ const references = context.getScope().references;
23
+ const importSources = getImportSources(context);
31
24
 
32
- },
33
- MemberExpression: (node: MemberExpression) => {
34
- const references = context.getScope().references;
35
- const importSources = getImportSources(context);
25
+ if (isStyled(node, references, importSources)) {
26
+ context.report({ node, messageId: 'no-styled' });
27
+ }
28
+ },
29
+ MemberExpression: (node: MemberExpression) => {
30
+ const references = context.getScope().references;
31
+ const importSources = getImportSources(context);
36
32
 
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
- },
33
+ if (node.object.type === 'Identifier' && isStyled(node.object, references, importSources)) {
34
+ const parent = context.getAncestors().at(-1);
35
+ if (parent && (parent.type === 'CallExpression' || parent.type === 'MemberExpression')) {
36
+ return;
37
+ }
38
+ context.report({ node, messageId: 'no-styled' });
39
+ }
40
+ },
41
+ };
42
+ },
50
43
  });
51
44
 
52
45
  export default rule;