@atlaskit/eslint-plugin-ui-styling-standard 0.10.0 → 0.11.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 (54) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-unsafe-values/usage.mdx +222 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +4 -2
  8. package/dist/cjs/rules/no-unsafe-values/index.js +85 -0
  9. package/dist/cjs/rules/no-unsafe-values/messages.js +24 -0
  10. package/dist/cjs/rules/no-unsafe-values/utils.js +437 -0
  11. package/dist/es2019/presets/all.codegen.js +2 -1
  12. package/dist/es2019/presets/recommended.codegen.js +2 -1
  13. package/dist/es2019/rules/index.codegen.js +4 -2
  14. package/dist/es2019/rules/no-unsafe-values/index.js +80 -0
  15. package/dist/es2019/rules/no-unsafe-values/messages.js +72 -0
  16. package/dist/es2019/rules/no-unsafe-values/utils.js +350 -0
  17. package/dist/esm/presets/all.codegen.js +2 -1
  18. package/dist/esm/presets/recommended.codegen.js +2 -1
  19. package/dist/esm/rules/index.codegen.js +4 -2
  20. package/dist/esm/rules/no-unsafe-values/index.js +79 -0
  21. package/dist/esm/rules/no-unsafe-values/messages.js +17 -0
  22. package/dist/esm/rules/no-unsafe-values/utils.js +430 -0
  23. package/dist/types/index.codegen.d.ts +2 -0
  24. package/dist/types/presets/all.codegen.d.ts +2 -1
  25. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  26. package/dist/types/rules/index.codegen.d.ts +1 -0
  27. package/dist/types/rules/no-unsafe-values/index.d.ts +2 -0
  28. package/dist/types/rules/no-unsafe-values/messages.d.ts +13 -0
  29. package/dist/types/rules/no-unsafe-values/utils.d.ts +28 -0
  30. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  31. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  32. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  33. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  34. package/dist/types-ts4.5/rules/no-unsafe-values/index.d.ts +2 -0
  35. package/dist/types-ts4.5/rules/no-unsafe-values/messages.d.ts +13 -0
  36. package/dist/types-ts4.5/rules/no-unsafe-values/utils.d.ts +28 -0
  37. package/package.json +4 -4
  38. package/src/presets/all.codegen.tsx +2 -1
  39. package/src/presets/recommended.codegen.tsx +2 -1
  40. package/src/rules/index.codegen.tsx +3 -1
  41. package/src/rules/no-unsafe-values/README.md +220 -0
  42. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +27 -0
  43. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +37 -0
  44. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +229 -0
  45. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +267 -0
  46. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +52 -0
  47. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +106 -0
  48. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +44 -0
  49. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +48 -0
  50. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +75 -0
  51. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +121 -0
  52. package/src/rules/no-unsafe-values/index.tsx +96 -0
  53. package/src/rules/no-unsafe-values/messages.tsx +86 -0
  54. package/src/rules/no-unsafe-values/utils.tsx +399 -0
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
3
+ * @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -19,6 +19,7 @@ export default {
19
19
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
22
+ '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': [
23
24
  'warn',
24
25
  { excludeReactComponents: true },
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
3
+ * @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -19,6 +19,7 @@ export default {
19
19
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
22
+ '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
22
23
  '@atlaskit/design-system/consistent-css-prop-usage': [
23
24
  'warn',
24
25
  { excludeReactComponents: true },
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
3
+ * @codegen <<SignedSource::edaedcad824fed1e108f90d909287584>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -12,6 +12,7 @@ import noContainerQueries from './no-container-queries';
12
12
  import noDynamicStyles from './no-dynamic-styles';
13
13
  import noImportantStyles from './no-important-styles';
14
14
  import noNestedSelectors from './no-nested-selectors';
15
+ import noUnsafeValues from './no-unsafe-values';
15
16
 
16
17
  export default {
17
18
  'convert-props-syntax': convertPropsSyntax,
@@ -23,4 +24,5 @@ export default {
23
24
  'no-dynamic-styles': noDynamicStyles,
24
25
  'no-important-styles': noImportantStyles,
25
26
  'no-nested-selectors': noNestedSelectors,
27
+ 'no-unsafe-values': noUnsafeValues,
26
28
  };
@@ -0,0 +1,220 @@
1
+ This rule prevents the usage of styles that are difficult/impossible to statically anaylze.
2
+
3
+ ## Examples
4
+
5
+ ### Incorrect
6
+
7
+ **Function calls**
8
+
9
+ Function calls are not allowed as arguments or as values,
10
+ unless they are specified in `allowedFunctionCalls` (or in the default list).
11
+
12
+ ```tsx
13
+ import { css } from '@compiled/react';
14
+
15
+ function getStyles() {
16
+ return { width: 100 };
17
+ }
18
+
19
+ const styles = css(getStyles());
20
+ ```
21
+
22
+ ```tsx
23
+ import { css } from '@compiled/react';
24
+
25
+ function getWidth() {
26
+ return 100;
27
+ }
28
+
29
+ const styles = css({
30
+ width: getWidth(),
31
+ });
32
+ ```
33
+
34
+ **Dynamic keys**
35
+
36
+ Dynamic keys are not allowed,
37
+ unless they are specified in `allowedDynamicKeys` (or in the default list).
38
+
39
+ ```tsx
40
+ import { css } from '@compiled/react';
41
+ import { SELECTOR } from 'my-package';
42
+
43
+ const styles = css({
44
+ [SELECTOR]: {
45
+ color: 'red',
46
+ },
47
+ [`${SELECTOR} > p`]: {
48
+ width: 100,
49
+ },
50
+ });
51
+ ```
52
+
53
+ **Variables**
54
+
55
+ Variables are not allowed as values, unless they:
56
+
57
+ - Have a simple, static value
58
+ - Are defined in the same file
59
+
60
+ ```tsx
61
+ import { css } from '@compiled/react';
62
+ import { SELECTOR } from 'my-package';
63
+
64
+ function myFunction() {
65
+ return '5px';
66
+ }
67
+
68
+ const styles = css({
69
+ height: myFunction,
70
+ });
71
+ ```
72
+
73
+ **Object access**
74
+
75
+ Accessing object members is not allowed in values.
76
+
77
+ ```tsx
78
+ import { css } from '@compiled/react';
79
+ import { token } from '@atlaskit/tokens';
80
+
81
+ const colors = {
82
+ text: token('color.text'),
83
+ };
84
+
85
+ const styles = css({
86
+ color: colors.text,
87
+ });
88
+ ```
89
+
90
+ **Spread elements**
91
+
92
+ Spread elements are not allowed.
93
+
94
+ ```tsx
95
+ import { css } from '@compiled/react';
96
+
97
+ const buttonStyles = {
98
+ color: 'blue',
99
+ };
100
+
101
+ const styles = css({
102
+ ...buttonStyles,
103
+ });
104
+ ```
105
+
106
+ **Array values**
107
+
108
+ Array values are not allowed.
109
+ This syntax is not supported by most styling libraries.
110
+
111
+ ```tsx
112
+ import { css } from '@compiled/react';
113
+
114
+ const styles = css({
115
+ '::before': [{ fontWeight: 'bold' }, { color: 'red' }],
116
+ });
117
+ ```
118
+
119
+ ### Correct
120
+
121
+ Things to note about the following example:
122
+
123
+ - The `warningStyles` are conditionally applied using the `css` prop
124
+ - The dynamic `width` is applied using the `style` prop
125
+ - The `token()` calls are allowed because they are statically analyzable (and in the default allow-list)
126
+
127
+ ```tsx
128
+ import { css } from '@compiled/react';
129
+ import { token } from '@atlaskit/tokens';
130
+
131
+ const baseStyles = css({
132
+ height: 100,
133
+ });
134
+
135
+ const warningStyles = css({
136
+ color: token('color.text.warning'),
137
+ backgroundColor: token('color.background.warning'),
138
+ });
139
+
140
+ const Component = ({
141
+ isWarning,
142
+ width,
143
+ }: {
144
+ isWarning: boolean;
145
+ width: number;
146
+ }) => {
147
+ return (
148
+ <div style={{ width }} css={[baseStyles, isWarning && warningStyles]} />
149
+ );
150
+ };
151
+ ```
152
+
153
+ ## Options
154
+
155
+ ### `allowedDynamicKeys: [string, string][]`
156
+
157
+ Use this to allow specified imports as dynamic keys, in addition to the built-in allow-list.
158
+
159
+ Each value should be a two-element array. The first item is the entrypoint, and the second item is a named export.
160
+
161
+ Default imports are not supported.
162
+
163
+ ```tsx
164
+ // .eslintrc.js
165
+
166
+ // ...
167
+ rules: {
168
+ '@atlaskit/eslint-plugin-ui-styling-standard/no-unsafe-values': [
169
+ 'error',
170
+ {
171
+ allowedDynamicKeys: [
172
+ ['@atlaskit/primitives/responsive', 'media'],
173
+ ]
174
+ },
175
+ ],
176
+ // ...
177
+ },
178
+ // ...
179
+ ```
180
+
181
+ ### `allowedFunctionCalls: [string, string][]`
182
+
183
+ Use this to allow specific functions to be called, in addition to the built-in allow-list.
184
+
185
+ Each value should be a two-element array. The first item is the entrypoint, and the second item is a named export.
186
+
187
+ Default imports are not currently supported.
188
+
189
+ ```tsx
190
+ // .eslintrc.js
191
+
192
+ // ...
193
+ rules: {
194
+ '@atlaskit/eslint-plugin-ui-styling-standard/no-unsafe-values': [
195
+ 'error',
196
+ {
197
+ allowedFunctionCalls: [
198
+ ['@atlaskit/tokens', 'token'],
199
+ ]
200
+ },
201
+ ],
202
+ // ...
203
+ },
204
+ // ...
205
+ ```
206
+
207
+ ### `importSources : string[]`
208
+
209
+ By default, this rule will check `css` usages from:
210
+
211
+ - `@atlaskit/css`
212
+ - `@atlaskit/primitives`
213
+ - `@compiled/react`
214
+ - `@emotion/react`
215
+ - `@emotion/core`
216
+ - `@emotion/styled`
217
+ - `styled-components`
218
+
219
+ To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
220
+ This will override the built-in allow-list.
@@ -0,0 +1,27 @@
1
+ import { typescriptEslintTester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ typescriptEslintTester.run(
5
+ 'no-unsafe-values',
6
+ // @ts-expect-error
7
+ rule,
8
+ {
9
+ valid: [],
10
+ invalid: [
11
+ {
12
+ name: 'passing an array of values',
13
+ code: `
14
+ import { css } from '@compiled/react';
15
+
16
+ const styles = css({
17
+ selector: [
18
+ { height: 40 },
19
+ { width: 40 }
20
+ ]
21
+ });
22
+ `,
23
+ errors: [{ messageId: 'no-array-values' }],
24
+ },
25
+ ],
26
+ },
27
+ );
@@ -0,0 +1,37 @@
1
+ import { typescriptEslintTester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ typescriptEslintTester.run(
5
+ 'no-unsafe-values',
6
+ // @ts-expect-error
7
+ rule,
8
+ {
9
+ valid: [],
10
+ invalid: [
11
+ {
12
+ name: 'ternary value',
13
+ code: `
14
+ import { css } from '@compiled/react';
15
+
16
+ const isVisible = false;
17
+ const styles = css({
18
+ opacity: isVisible ? 1 : 0
19
+ });
20
+ `,
21
+ errors: [{ messageId: 'no-conditional-values' }],
22
+ },
23
+ {
24
+ name: 'boolean expression values',
25
+ code: `
26
+ import { css } from '@compiled/react';
27
+
28
+ const isVisible = false;
29
+ const styles = css({
30
+ opacity: isVisible && 1
31
+ });
32
+ `,
33
+ errors: [{ messageId: 'no-conditional-values' }],
34
+ },
35
+ ],
36
+ },
37
+ );
@@ -0,0 +1,229 @@
1
+ import { typescriptEslintTester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ typescriptEslintTester.run(
5
+ 'no-unsafe-values',
6
+ // @ts-expect-error
7
+ rule,
8
+ {
9
+ valid: [
10
+ {
11
+ name: 'key is an identifier',
12
+ code: `
13
+ import { css } from '@compiled/react';
14
+
15
+ const styles = css({
16
+ color: 'blue',
17
+ });
18
+ `,
19
+ },
20
+ {
21
+ name: 'key is a literal',
22
+ code: `
23
+ import { css } from '@compiled/react';
24
+
25
+ const styles = css({
26
+ 'color': 'blue',
27
+ });
28
+ `,
29
+ },
30
+ {
31
+ name: 'key is an allow listed value',
32
+ code: `
33
+ import { css } from '@compiled/react';
34
+ import { media } from 'my-package';
35
+
36
+ const styles = css({
37
+ [media.above.xxs]: { height: '2px' }
38
+ });
39
+ `,
40
+ options: [
41
+ {
42
+ allowedDynamicKeys: [['my-package', 'media']],
43
+ },
44
+ ],
45
+ },
46
+ {
47
+ name: 'key is a simple, statically resolvable CSS var',
48
+ code: `
49
+ import { css } from '@compiled/react';
50
+
51
+ const myVar = '--my-var';
52
+ const styles = css({
53
+ [myVar]: 'red'
54
+ });
55
+ `,
56
+ },
57
+ {
58
+ name: 'key is on the default allow list',
59
+ code: `
60
+ import { css } from '@compiled/react';
61
+ import { media } from '@atlaskit/primitives/responsive';
62
+
63
+ const styles = css({
64
+ [media.above.xxs]: { height: '2px' }
65
+ });
66
+ `,
67
+ options: [
68
+ {
69
+ allowedDynamicKeys: [],
70
+ },
71
+ ],
72
+ },
73
+ ],
74
+ invalid: [
75
+ {
76
+ name: 'key is a template literal',
77
+ code: `
78
+ import { css } from '@compiled/react';
79
+
80
+ const something = 'selector';
81
+ const styles = css({
82
+ [\`\${something} > p\`]: {
83
+ color: 'blue',
84
+ },
85
+ });
86
+ `,
87
+ errors: [{ messageId: 'no-dynamic-keys' }],
88
+ },
89
+ {
90
+ name: 'nested key is a template literal',
91
+ code: `
92
+ import { css } from '@compiled/react';
93
+
94
+ const something = 'selector';
95
+ const styles = css({
96
+ button: {
97
+ p: {
98
+ [\`\${something} > p\`]: {
99
+ color: 'blue',
100
+ },
101
+ },
102
+ },
103
+ });
104
+ `,
105
+ errors: [{ messageId: 'no-dynamic-keys' }],
106
+ },
107
+ {
108
+ name: 'key is an identifier for a string literal',
109
+ code: `
110
+ import { css } from '@compiled/react';
111
+
112
+ const something = 'selector';
113
+ const styles = css({
114
+ [something]: {
115
+ color: 'blue',
116
+ },
117
+ });
118
+ `,
119
+ errors: [{ messageId: 'no-dynamic-keys' }],
120
+ },
121
+ {
122
+ name: 'nested key is an identifier for a string literal',
123
+ code: `
124
+ import { css } from '@compiled/react';
125
+
126
+ const something = 'selector';
127
+ const styles = css({
128
+ button: {
129
+ p: {
130
+ [something]: {
131
+ color: 'blue',
132
+ },
133
+ },
134
+ },
135
+ });
136
+ `,
137
+ errors: [{ messageId: 'no-dynamic-keys' }],
138
+ },
139
+ {
140
+ name: 'key is a string literal inside square brackets',
141
+ code: `
142
+ import { css } from '@compiled/react';
143
+
144
+ const styles = css({
145
+ ['color']: 'blue',
146
+ });
147
+ `,
148
+ errors: [{ messageId: 'no-dynamic-keys' }],
149
+ },
150
+ {
151
+ name: 'key is a concatenation of string literals',
152
+ code: `
153
+ import { css } from '@compiled/react';
154
+
155
+ const styles = css({
156
+ ['col' + 'or']: 'blue',
157
+ });
158
+ `,
159
+ errors: [{ messageId: 'no-dynamic-keys' }],
160
+ },
161
+ {
162
+ name: 'nested key is a string literal inside square brackets',
163
+ code: `
164
+ import { css } from '@compiled/react';
165
+
166
+ const styles = css({
167
+ button: {
168
+ p: {
169
+ ['color']: 'blue',
170
+ },
171
+ },
172
+ });
173
+ `,
174
+ errors: [{ messageId: 'no-dynamic-keys' }],
175
+ },
176
+ {
177
+ name: 'key is an import from a non-allow-listed library',
178
+ code: `
179
+ import { css } from '@compiled/react';
180
+ import { media } from 'my-package';
181
+
182
+ const styles = css({
183
+ [media.above.xxs]: { height: '2px' }
184
+ });
185
+ `,
186
+ errors: [{ messageId: 'no-dynamic-keys' }],
187
+ options: [
188
+ {
189
+ allowedDynamicKeys: [],
190
+ },
191
+ ],
192
+ },
193
+ {
194
+ name: 'key is a non-allow-listed import from an allow-listed library',
195
+ code: `
196
+ import { css } from '@compiled/react';
197
+ import { mediaNext as media } from 'my-package';
198
+
199
+ const styles = css({
200
+ [media.above.xxs]: { height: '2px' }
201
+ });
202
+ `,
203
+ errors: [{ messageId: 'no-dynamic-keys' }],
204
+ options: [
205
+ {
206
+ allowedDynamicKeys: [['my-package', 'media']],
207
+ },
208
+ ],
209
+ },
210
+ {
211
+ name: 'key is from a nested entrypoint of an allow-listed library',
212
+ code: `
213
+ import { css } from '@compiled/react';
214
+ import { media } from 'my-package/primitives/responsive';
215
+
216
+ const styles = css({
217
+ [media.above.xxs]: { height: '2px' }
218
+ });
219
+ `,
220
+ errors: [{ messageId: 'no-dynamic-keys' }],
221
+ options: [
222
+ {
223
+ allowedDynamicKeys: [['my-package/primitives', 'media']],
224
+ },
225
+ ],
226
+ },
227
+ ],
228
+ },
229
+ );