@atlaskit/eslint-plugin-ui-styling-standard 0.16.3 → 0.16.5

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 (41) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/constellation/atlaskit-theme/usage.mdx +74 -55
  3. package/constellation/convert-props-syntax/usage.mdx +29 -24
  4. package/constellation/enforce-style-prop/usage.mdx +32 -26
  5. package/constellation/local-cx-xcss/usage.mdx +27 -15
  6. package/constellation/no-array-arguments/usage.mdx +12 -9
  7. package/constellation/no-classname-prop/usage.mdx +13 -5
  8. package/constellation/no-container-queries/usage.mdx +65 -16
  9. package/constellation/no-dynamic-styles/usage.mdx +16 -11
  10. package/constellation/no-exported-styles/usage.mdx +25 -30
  11. package/constellation/no-global-styles/usage.mdx +4 -3
  12. package/constellation/no-important-styles/usage.mdx +5 -3
  13. package/constellation/no-imported-style-values/usage.mdx +30 -27
  14. package/constellation/no-nested-selectors/usage.mdx +51 -18
  15. package/constellation/no-styled/usage.mdx +17 -14
  16. package/constellation/no-unsafe-selectors/usage.mdx +184 -58
  17. package/constellation/no-unsafe-values/usage.mdx +150 -48
  18. package/constellation/use-compiled/usage.mdx +8 -8
  19. package/dist/cjs/rules/no-imported-style-values/index.js +50 -2
  20. package/dist/es2019/rules/no-imported-style-values/index.js +49 -2
  21. package/dist/esm/rules/no-imported-style-values/index.js +50 -2
  22. package/package.json +1 -1
  23. package/src/rules/atlaskit-theme/README.md +74 -55
  24. package/src/rules/convert-props-syntax/README.md +29 -24
  25. package/src/rules/enforce-style-prop/README.md +32 -26
  26. package/src/rules/local-cx-xcss/README.md +27 -15
  27. package/src/rules/no-array-arguments/README.md +12 -9
  28. package/src/rules/no-classname-prop/README.md +13 -5
  29. package/src/rules/no-container-queries/README.md +65 -16
  30. package/src/rules/no-dynamic-styles/README.md +16 -11
  31. package/src/rules/no-exported-styles/README.md +25 -30
  32. package/src/rules/no-global-styles/README.md +4 -3
  33. package/src/rules/no-important-styles/README.md +5 -3
  34. package/src/rules/no-imported-style-values/README.md +30 -27
  35. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +208 -0
  36. package/src/rules/no-imported-style-values/index.tsx +51 -4
  37. package/src/rules/no-nested-selectors/README.md +51 -18
  38. package/src/rules/no-styled/README.md +17 -14
  39. package/src/rules/no-unsafe-selectors/README.md +184 -58
  40. package/src/rules/no-unsafe-values/README.md +150 -48
  41. package/src/rules/use-compiled/README.md +8 -8
@@ -141,6 +141,90 @@ typescriptEslintTester.run(
141
141
  `,
142
142
  options: [],
143
143
  },
144
+ {
145
+ name: 'Imported value in LHS of conditional expression in css prop',
146
+ code: `
147
+ import { css } from '@compiled/react';
148
+ import { ff } from '@atlassian/jira-feature-flagging';
149
+ import { IS_FF_ENABLED } from '../shared';
150
+
151
+ const baseStyles = css({ margin: 0 });
152
+ const styles = css({ color: token('color.text') });
153
+ const stylesFF = css({ color: token('color.text.danger') });
154
+
155
+ const Component = () => (
156
+ <>
157
+ <div css={IS_FF_ENABLED ? stylesFF : styles} />
158
+ <div css={[baseStyles, IS_FF_ENABLED ? stylesFF : styles]} />
159
+ <div css={ff('my-feature-flag') ? stylesFF : styles} />
160
+ <div css={[baseStyles, ff('my-feature-flag') ? stylesFF : styles]} />
161
+ </>
162
+ );
163
+ `,
164
+ },
165
+ {
166
+ name: 'Imported value as entire LHS of logical expression in css prop',
167
+ code: `
168
+ import { css } from '@compiled/react';
169
+ import { ff } from '@atlassian/jira-feature-flagging';
170
+ import { IS_FF_ENABLED } from '../shared';
171
+
172
+ const baseStyles = css({ margin: 0 });
173
+ const stylesFF = css({ color: token('color.text.danger') });
174
+
175
+ const Component = () => (
176
+ <>
177
+ <div css={IS_FF_ENABLED && stylesFF} />
178
+ <div css={[baseStyles, IS_FF_ENABLED && stylesFF]} />
179
+ <div css={ff('my-feature-flag') && stylesFF} />
180
+ <div css={[baseStyles, ff('my-feature-flag') && stylesFF]} />
181
+ </>
182
+ );
183
+ `,
184
+ },
185
+ {
186
+ name: 'Imported value in LHS of conditional expression in style prop',
187
+ code: `
188
+ import { css } from '@compiled/react';
189
+ import { ff } from '@atlassian/jira-feature-flagging';
190
+ import { IS_FF_ENABLED } from '../shared';
191
+
192
+ const Component = (props: { width: number }) => (
193
+ <>
194
+ <div style={{ width: IS_FF_ENABLED ? Math.abs(width) : width }} />
195
+ <div style={{ width: ff('my-feature-flag') ? Math.abs(width) : width }} />
196
+ </>
197
+ );
198
+ `,
199
+ },
200
+ {
201
+ name: 'Non-computed member access that collides with imported name',
202
+ code: `
203
+ import { abs } from '../shared';
204
+
205
+ const Component = (props: { width: number }) => (
206
+ <div style={{ width: Math.abs(width) }} />
207
+ );
208
+ `,
209
+ },
210
+ {
211
+ name: 'Non-computed property key that collides with imported name',
212
+ code: `
213
+ import { css } from '@compiled/react';
214
+ import { color } from '../shared';
215
+
216
+ const styles = css({
217
+ color: token('color.text'),
218
+ });
219
+
220
+ const Component = (props: { color: string }) => (
221
+ <div
222
+ css={styles}
223
+ style={{ color: props.color }}
224
+ />
225
+ );
226
+ `,
227
+ },
144
228
  ],
145
229
  invalid: [
146
230
  {
@@ -278,6 +362,130 @@ typescriptEslintTester.run(
278
362
  `,
279
363
  errors: [{ messageId: 'no-imported-style-values' }],
280
364
  },
365
+ {
366
+ name: 'Imported value in LHS of conditional expression in style declaration',
367
+ code: `
368
+ import { css } from '@compiled/react';
369
+ import { ff } from '@atlassian/jira-feature-flagging';
370
+ import { IS_FF_ENABLED } from '../shared';
371
+
372
+ const stylesA = css({
373
+ color: IS_FF_ENABLED ? token('color.text.danger'): token('color.text'),
374
+ });
375
+
376
+ const stylesB = css({
377
+ color: ff('my-feature-flag') ? token('color.text.danger') : token('color.text'),
378
+ });
379
+ `,
380
+ errors: [
381
+ { messageId: 'no-imported-style-values', line: 7 },
382
+ { messageId: 'no-imported-style-values', line: 11 },
383
+ ],
384
+ },
385
+ {
386
+ name: 'Imported value in RHS of conditional expression in css prop',
387
+ code: `
388
+ import { css } from '@compiled/react';
389
+ import { ff } from '@atlassian/jira-feature-flagging';
390
+ import { IS_FF_ENABLED, stylesFF } from '../shared';
391
+
392
+ const baseStyles = css({ margin: 0 });
393
+ const styles = css({ color: token('color.text' )});
394
+
395
+ const Component = () => (
396
+ <>
397
+ <div css={IS_FF_ENABLED ? stylesFF : styles} />
398
+ <div css={[baseStyles, IS_FF_ENABLED ? stylesFF : styles]} />
399
+ <div css={ff('my-feature-flag') ? stylesFF : styles} />
400
+ <div css={[baseStyles, ff('my-feature-flag') ? stylesFF : styles]} />
401
+ </>
402
+ );
403
+ `,
404
+ errors: [
405
+ { messageId: 'no-imported-style-values', line: 11 },
406
+ { messageId: 'no-imported-style-values', line: 12 },
407
+ { messageId: 'no-imported-style-values', line: 13 },
408
+ { messageId: 'no-imported-style-values', line: 14 },
409
+ ],
410
+ },
411
+ {
412
+ name: 'Imported value in RHS of logical expression in css prop',
413
+ code: `
414
+ import { css } from '@compiled/react';
415
+ import { ff } from '@atlassian/jira-feature-flagging';
416
+ import { IS_FF_ENABLED, stylesFF } from '../shared';
417
+
418
+ const baseStyles = css({ margin: 0 });
419
+ const styles = css({ color: token('color.text' )});
420
+
421
+ const Component = () => (
422
+ <>
423
+ <div css={IS_FF_ENABLED && stylesFF} />
424
+ <div css={[baseStyles, IS_FF_ENABLED && stylesFF]} />
425
+ <div css={ff('my-feature-flag') && stylesFF} />
426
+ <div css={[baseStyles, ff('my-feature-flag') && stylesFF]} />
427
+ </>
428
+ );
429
+ `,
430
+ errors: [
431
+ { messageId: 'no-imported-style-values', line: 11 },
432
+ { messageId: 'no-imported-style-values', line: 12 },
433
+ { messageId: 'no-imported-style-values', line: 13 },
434
+ { messageId: 'no-imported-style-values', line: 14 },
435
+ ],
436
+ },
437
+ {
438
+ name: 'Imported value in RHS of conditional expression in style prop',
439
+ code: `
440
+ import { css } from '@compiled/react';
441
+ import { ff } from '@atlassian/jira-feature-flagging';
442
+ import { IS_FF_ENABLED, abs } from '../shared';
443
+
444
+ const Component = (props: { width: number }) => (
445
+ <>
446
+ <div style={{ width: IS_FF_ENABLED ? abs(width) : width }} />
447
+ <div style={{ width: ff('my-feature-flag') ? abs(width) : width }} />
448
+ </>
449
+ );
450
+ `,
451
+ errors: [
452
+ { messageId: 'no-imported-style-values', line: 8 },
453
+ { messageId: 'no-imported-style-values', line: 9 },
454
+ ],
455
+ },
456
+ {
457
+ name: 'Computed member access with imported name',
458
+ code: `
459
+ import { abs } from '../shared';
460
+
461
+ const Component = (props: { width: number }) => (
462
+ <div style={{ width: Math[abs](width) }} />
463
+ );
464
+ `,
465
+ errors: [{ messageId: 'no-imported-style-values' }],
466
+ },
467
+ {
468
+ name: 'Computed property key with imported name',
469
+ code: `
470
+ import { css } from '@compiled/react';
471
+ import { color } from '../shared';
472
+
473
+ const styles = css({
474
+ [color]: token('color.text'),
475
+ });
476
+
477
+ const Component = (props: { color: string }) => (
478
+ <div
479
+ css={styles}
480
+ style={{ [color]: props.color }}
481
+ />
482
+ );
483
+ `,
484
+ errors: [
485
+ { messageId: 'no-imported-style-values' },
486
+ { messageId: 'no-imported-style-values' },
487
+ ],
488
+ },
281
489
  ],
282
490
  },
283
491
  );
@@ -4,6 +4,7 @@ import {
4
4
  hasStyleObjectArguments,
5
5
  } from '@atlaskit/eslint-utils/is-supported-import';
6
6
  import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
7
+ import type { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
7
8
  import { findVariable } from '@atlaskit/eslint-utils/find-variable';
8
9
  import type { Rule } from 'eslint';
9
10
  import {
@@ -14,6 +15,7 @@ import {
14
15
  } from '@atlaskit/eslint-utils/allowed-function-calls';
15
16
  import type * as ESTree from 'eslint-codemod-utils';
16
17
  import esquery from 'esquery';
18
+ import estraverse from 'estraverse';
17
19
 
18
20
  const schema: JSONSchema4 = [
19
21
  {
@@ -81,6 +83,22 @@ const checkIdentifier = (
81
83
  functionAllowList: AllowList,
82
84
  keysAllowList: AllowList,
83
85
  ) => {
86
+ const { parent } = identifier as Rule.Node;
87
+
88
+ // Even though `member` in `object.member` is syntactically an identifier,
89
+ // it should be ignored as `.member` is syntax sugar for `['member']` here.
90
+ // We should still lint `object[member]` (when `parent.computed` === true)
91
+ if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
92
+ return;
93
+ }
94
+
95
+ // Even though `key` in `{ key: 'value' }` is syntactically an identifier,
96
+ // it should be ignored as `key` is syntax sugar for `'key'` here.
97
+ // We should still lint `[key]` (when `parent.computed` === true)
98
+ if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
99
+ return;
100
+ }
101
+
84
102
  if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
85
103
  context.report({
86
104
  node: identifier,
@@ -129,10 +147,39 @@ export const rule = createLintRule({
129
147
  }
130
148
  node.arguments.forEach(checkForIdentifiers);
131
149
  },
132
- 'JSXAttribute[name.name=/^style|css$/] Identifier:not(TSTypeReference Identifier)'(
133
- node: ESTree.Identifier,
134
- ) {
135
- checkIdentifier(node, context, functionAllowList, keysAllowList);
150
+ 'JSXAttribute[name.name=/^style|css$/]'(jsxAttribute: ESTree.Node) {
151
+ estraverse.traverse(jsxAttribute, {
152
+ fallback(node) {
153
+ // estraverse does not know about nodes not in the ESTree standard.
154
+ // This includes JSX nodes, so we must provide a fallback method to handle them.
155
+ // Using `fallback: 'iteration'` can cause cycles because it will iterate
156
+ // over the `parent` attribute on JSX nodes.
157
+ return Object.keys(node).filter((key) => key !== 'parent');
158
+ },
159
+ enter(node, parent) {
160
+ // Ignore type contexts
161
+ // Cast required because ESTree is not aware of TypeScript nodes
162
+ if ((node.type as AST_NODE_TYPES) === 'TSTypeReference') {
163
+ return estraverse.VisitorOption.Skip;
164
+ }
165
+
166
+ // Ignore conditions of ternary expressions
167
+ if (parent?.type === 'ConditionalExpression' && node === parent.test) {
168
+ return estraverse.VisitorOption.Skip;
169
+ }
170
+
171
+ // Ignore LHS of logical expressions (&&, ||)
172
+ if (parent?.type === 'LogicalExpression' && node === parent.left) {
173
+ return estraverse.VisitorOption.Skip;
174
+ }
175
+
176
+ if (node.type !== 'Identifier') {
177
+ return;
178
+ }
179
+
180
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
181
+ },
182
+ });
136
183
  },
137
184
  };
138
185
  },
@@ -1,52 +1,86 @@
1
- This rule prevents the usage of the nested selectors in style declarations. Avoid setting styles for
2
- any child elements or elements that require context from other elements.
1
+ Blocks nested selectors in style declarations.
2
+
3
+ Use styles which do not require context from other elements.
3
4
 
4
5
  ## Examples
5
6
 
6
7
  ### Incorrect
7
8
 
8
- ```js
9
+ ```tsx
9
10
  import { css } from '@compiled/react';
11
+ import { token } from '@atlaskit/tokens';
12
+
10
13
  const styles = css({
11
14
  h2: {
12
- fontSize: '1.5rem',
15
+ font: token('font.heading.large'),
13
16
  },
14
17
  });
18
+
19
+ const Component = () => (
20
+ <div css={styles}>
21
+ <h2>Heading</h2>
22
+ </div>
23
+ );
15
24
  ```
16
25
 
17
- ```js
26
+ ```tsx
18
27
  import { styled } from '@compiled/react';
19
- const Component = styled.div({
20
- '*': {
21
- padding: 0,
28
+ import { token } from '@atlaskit/tokens';
29
+
30
+ const Table = styled.table({
31
+ border: `1px solid ${token('color.border')}`,
32
+ td: {
33
+ padding: token('space.100'),
22
34
  },
23
35
  });
24
36
  ```
25
37
 
26
- ```js
38
+ ```tsx
27
39
  import { styled } from '@compiled/react';
40
+ import { token } from '@atlaskit/tokens';
41
+
28
42
  const Component = styled.div({
29
43
  '.myclass': {
30
- padding: 0,
44
+ padding: token('space.100'),
31
45
  },
32
46
  });
33
47
  ```
34
48
 
35
49
  ### Correct
36
50
 
37
- ```js
51
+ ```tsx
38
52
  import { css } from '@compiled/react';
39
- const styles = css({
40
- display: 'flex',
41
- flexDirection: 'column',
53
+ import { token } from '@atlaskit/tokens';
54
+
55
+ const headingStyles = css({
56
+ font: token('font.heading.large'),
57
+ });
58
+
59
+ const Component = () => (
60
+ <div>
61
+ <h2 css={headingStyles}>Heading</h2>
62
+ </div>
63
+ );
64
+ ```
65
+
66
+ ```tsx
67
+ import { css } from '@compiled/react';
68
+ import { token } from '@atlaskit/tokens';
69
+
70
+ const tableStyles = css({
71
+ border: `1px solid ${token('color.border')}`,
72
+ });
73
+
74
+ const tdStyles = css({
75
+ padding: token('space.100'),
42
76
  });
43
77
  ```
44
78
 
45
79
  ## Options
46
80
 
47
- ### importSources
81
+ ### `importSources: string[]`
48
82
 
49
- By default, this rule will check `css` usages from:
83
+ By default, this rule will check styles using:
50
84
 
51
85
  - `@atlaskit/css`
52
86
  - `@atlaskit/primitives`
@@ -56,5 +90,4 @@ By default, this rule will check `css` usages from:
56
90
  - `@emotion/styled`
57
91
  - `styled-components`
58
92
 
59
- To change this list of libraries, you can define a custom set of `importSources`, which accepts an
60
- array of package names (strings).
93
+ Override this list with the `importSources` option, which accepts an array of package names.
@@ -1,6 +1,13 @@
1
- This rule prevents usage of `styled` imports'.
1
+ Blocks the `styled` API, which creates unnecessary indirection.
2
2
 
3
- Avoid usages of styled, eg. `styled.div`, `styled('div')`, `styled(Component)`, etc.
3
+ This indirection:
4
+
5
+ - obfuscates which tag is being rendered
6
+ - adds linting complexity
7
+ - promotes exported styles
8
+ - can complicate refactoring
9
+
10
+ Use the `css` API instead. It has better performance and clarity.
4
11
 
5
12
  ## Examples
6
13
 
@@ -49,8 +56,9 @@ export default styled.div.attrs((props) => ({ 'data-testid': props.testId }))`
49
56
 
50
57
  ```tsx
51
58
  import { css, jsx } from '@emotion/react';
59
+ import { token } from '@atlaskit/tokens';
52
60
 
53
- const styles = css({ color: 'red' });
61
+ const styles = css({ color: token('color.text.subtlest') });
54
62
 
55
63
  const Component = ({ children }) => {
56
64
  return <div css={styles}>{children}</div>;
@@ -58,7 +66,6 @@ const Component = ({ children }) => {
58
66
  ```
59
67
 
60
68
  ```tsx
61
- // Using Primitives
62
69
  import { Box, xcss } from '@atlaskit/primitives';
63
70
 
64
71
  const styles = xcss({
@@ -72,16 +79,12 @@ const Component = ({ children }) => {
72
79
 
73
80
  ## FAQ
74
81
 
75
- ### Why avoid using `styled` imports?
82
+ ### How will I extend like `styled(Button)`?
76
83
 
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.
84
+ Don't modify the styles of components you don't own, unless they provide an explicit bounded
85
+ interface for doing so — such as the `xcss` prop.
80
86
 
81
- ### How will I extend like `styled(Button)`?
87
+ Use props (excluding `className` which is prohibited) to modify component styles instead.
82
88
 
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.
89
+ The Atlassian Design System, for example, no longer supports `styled(Button)` because it is unsafe
90
+ for us to evolve the system with.