@atlaskit/eslint-plugin-ui-styling-standard 0.9.1 → 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.
- package/CHANGELOG.md +16 -0
- package/README.md +2 -0
- package/constellation/index/overview.mdx +2 -0
- package/constellation/no-dynamic-styles/usage.mdx +128 -0
- package/constellation/no-unsafe-values/usage.mdx +222 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/index.codegen.js +6 -2
- package/dist/cjs/rules/no-dynamic-styles/index.js +69 -0
- package/dist/cjs/rules/no-unsafe-values/index.js +85 -0
- package/dist/cjs/rules/no-unsafe-values/messages.js +24 -0
- package/dist/cjs/rules/no-unsafe-values/utils.js +437 -0
- package/dist/es2019/presets/all.codegen.js +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/index.codegen.js +6 -2
- package/dist/es2019/rules/no-dynamic-styles/index.js +51 -0
- package/dist/es2019/rules/no-unsafe-values/index.js +80 -0
- package/dist/es2019/rules/no-unsafe-values/messages.js +72 -0
- package/dist/es2019/rules/no-unsafe-values/utils.js +350 -0
- package/dist/esm/presets/all.codegen.js +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/index.codegen.js +6 -2
- package/dist/esm/rules/no-dynamic-styles/index.js +62 -0
- package/dist/esm/rules/no-unsafe-values/index.js +79 -0
- package/dist/esm/rules/no-unsafe-values/messages.js +17 -0
- package/dist/esm/rules/no-unsafe-values/utils.js +430 -0
- package/dist/types/index.codegen.d.ts +4 -0
- package/dist/types/presets/all.codegen.d.ts +3 -1
- package/dist/types/presets/recommended.codegen.d.ts +3 -1
- package/dist/types/rules/index.codegen.d.ts +2 -0
- package/dist/types/rules/no-dynamic-styles/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types/rules/no-unsafe-values/utils.d.ts +28 -0
- package/dist/types-ts4.5/index.codegen.d.ts +4 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-dynamic-styles/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/utils.d.ts +28 -0
- package/package.json +5 -4
- package/src/presets/all.codegen.tsx +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/index.codegen.tsx +5 -1
- package/src/rules/no-dynamic-styles/README.md +126 -0
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +128 -0
- package/src/rules/no-dynamic-styles/index.tsx +63 -0
- package/src/rules/no-unsafe-values/README.md +220 -0
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +27 -0
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +37 -0
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +229 -0
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +267 -0
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +52 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +106 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +44 -0
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +48 -0
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +75 -0
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +121 -0
- package/src/rules/no-unsafe-values/index.tsx +96 -0
- package/src/rules/no-unsafe-values/messages.tsx +86 -0
- package/src/rules/no-unsafe-values/utils.tsx +399 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/eslint-plugin-ui-styling-standard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"author": "Atlassian Pty Ltd",
|
|
5
5
|
"description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -39,12 +39,14 @@
|
|
|
39
39
|
".": "./src/index.tsx"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@atlaskit/eslint-utils": "^1.
|
|
42
|
+
"@atlaskit/eslint-utils": "^1.3.0",
|
|
43
43
|
"@babel/runtime": "^7.0.0",
|
|
44
44
|
"@typescript-eslint/utils": "^5.48.1",
|
|
45
45
|
"eslint-codemod-utils": "^1.8.6",
|
|
46
46
|
"eslint-scope": "^8.0.0",
|
|
47
|
-
"
|
|
47
|
+
"esquery": "^1.5.0",
|
|
48
|
+
"estraverse": "^5.3.0",
|
|
49
|
+
"outdent": "^0.5.0"
|
|
48
50
|
},
|
|
49
51
|
"devDependencies": {
|
|
50
52
|
"@atlaskit/ds-lib": "^2.3.0",
|
|
@@ -56,7 +58,6 @@
|
|
|
56
58
|
"@types/estraverse": "^5.1.7",
|
|
57
59
|
"eslint": "^8.49.0",
|
|
58
60
|
"lodash": "^4.17.21",
|
|
59
|
-
"outdent": "^0.5.0",
|
|
60
61
|
"prettier": "^2.8.0",
|
|
61
62
|
"ts-node": "^10.9.1",
|
|
62
63
|
"tsconfig-paths": "^4.2.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::
|
|
3
|
+
* @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -16,8 +16,10 @@ export default {
|
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
18
18
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
|
+
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
22
|
+
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
21
23
|
'@atlaskit/design-system/consistent-css-prop-usage': [
|
|
22
24
|
'warn',
|
|
23
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::
|
|
3
|
+
* @codegen <<SignedSource::544c8b1c0eb0d6329064dcf92e18e45e>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -16,8 +16,10 @@ export default {
|
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
18
18
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
|
+
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
22
|
+
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
21
23
|
'@atlaskit/design-system/consistent-css-prop-usage': [
|
|
22
24
|
'warn',
|
|
23
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::
|
|
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';
|
|
@@ -9,8 +9,10 @@ import localCxXcss from './local-cx-xcss';
|
|
|
9
9
|
import noArrayArguments from './no-array-arguments';
|
|
10
10
|
import noClassnameProp from './no-classname-prop';
|
|
11
11
|
import noContainerQueries from './no-container-queries';
|
|
12
|
+
import noDynamicStyles from './no-dynamic-styles';
|
|
12
13
|
import noImportantStyles from './no-important-styles';
|
|
13
14
|
import noNestedSelectors from './no-nested-selectors';
|
|
15
|
+
import noUnsafeValues from './no-unsafe-values';
|
|
14
16
|
|
|
15
17
|
export default {
|
|
16
18
|
'convert-props-syntax': convertPropsSyntax,
|
|
@@ -19,6 +21,8 @@ export default {
|
|
|
19
21
|
'no-array-arguments': noArrayArguments,
|
|
20
22
|
'no-classname-prop': noClassnameProp,
|
|
21
23
|
'no-container-queries': noContainerQueries,
|
|
24
|
+
'no-dynamic-styles': noDynamicStyles,
|
|
22
25
|
'no-important-styles': noImportantStyles,
|
|
23
26
|
'no-nested-selectors': noNestedSelectors,
|
|
27
|
+
'no-unsafe-values': noUnsafeValues,
|
|
24
28
|
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
This rule prevents the use of dynamic styles (function expressions) in style declarations.
|
|
2
|
+
|
|
3
|
+
This ensures that style declarations:
|
|
4
|
+
|
|
5
|
+
- are statically analyzable
|
|
6
|
+
- can be efficiently extracted into atomic CSS
|
|
7
|
+
- are more readable, deterministic, and thus easier to maintain
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
### Incorrect
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { styled } from '@compiled/react';
|
|
15
|
+
|
|
16
|
+
const Component = styled.div<{ width: number }>((props) => ({
|
|
17
|
+
width: props.width,
|
|
18
|
+
}));
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import styled from 'styled-components';
|
|
23
|
+
|
|
24
|
+
const Component = styled.div<{ width: number }>({
|
|
25
|
+
width: (props) => props.width,
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import styled from 'styled-components';
|
|
31
|
+
|
|
32
|
+
const Container = styled.div<hasPadding>({
|
|
33
|
+
padding: ({ hasPadding }) =>
|
|
34
|
+
hasPadding ? token('space.100', '8px') : token('space.0', '0px'),
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Correct
|
|
39
|
+
|
|
40
|
+
**Flags**
|
|
41
|
+
|
|
42
|
+
If you have a style that is on/off depending on a prop,
|
|
43
|
+
use the `css` prop to conditionally apply it.
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { css } from '@compiled/react';
|
|
47
|
+
|
|
48
|
+
const baseStyles = css({ padding: token('space.0') });
|
|
49
|
+
const hasPaddingStyles = css({ padding: token('space.100') });
|
|
50
|
+
|
|
51
|
+
const Container = ({ hasPadding = false }: { hasPadding?: boolean }) => {
|
|
52
|
+
return <div css={[baseStyles, hasPadding && hasPaddingStyles]} />;
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Variants**
|
|
57
|
+
|
|
58
|
+
If you have variants which are known ahead of time,
|
|
59
|
+
use `cssMap` to define them and the `css` prop to conditionally apply it.
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import { css } from '@compiled/react';
|
|
63
|
+
|
|
64
|
+
const baseStyles = css({ width: 100 });
|
|
65
|
+
const stylesMap = cssMap({
|
|
66
|
+
primary: {
|
|
67
|
+
background: token('color.background.brand.bold'),
|
|
68
|
+
'&:hover': { background: token('color.background.brand.bold.hovered') },
|
|
69
|
+
},
|
|
70
|
+
error: {
|
|
71
|
+
background: token('color.background.warning.bold'),
|
|
72
|
+
'&:hover': { background: token('color.background.warning.bold.hovered') },
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const Wrapper = ({ appearance = 'primary' }: { appearance?: 'primary' | 'error' }) => (
|
|
77
|
+
<div css={[baseStyles, stylesMap[appearance]} />
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Dynamic values**
|
|
82
|
+
|
|
83
|
+
If you have styles which are truly dynamic,
|
|
84
|
+
use the `style` prop.
|
|
85
|
+
|
|
86
|
+
This approach should be used as a last resort.
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { css } from '@compiled/react';
|
|
90
|
+
|
|
91
|
+
const Component = ({ width }: { width: number }) => {
|
|
92
|
+
return <div style={{ width }} />;
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
import { css } from '@compiled/react';
|
|
98
|
+
|
|
99
|
+
const containerStyles = css({
|
|
100
|
+
'::before': {
|
|
101
|
+
width: 'var(--ak-example-width)',
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
function Example({ width }: { width: number }) {
|
|
106
|
+
return (
|
|
107
|
+
<div css={containerStyles} style={{ '--ak-example-width': `${width}px` }} />
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Options
|
|
113
|
+
|
|
114
|
+
### importSources
|
|
115
|
+
|
|
116
|
+
By default, this rule will check usages from:
|
|
117
|
+
|
|
118
|
+
- `@atlaskit/css`
|
|
119
|
+
- `@atlaskit/primitives`
|
|
120
|
+
- `@compiled/react`
|
|
121
|
+
- `@emotion/react`
|
|
122
|
+
- `@emotion/core`
|
|
123
|
+
- `@emotion/styled`
|
|
124
|
+
- `styled-components`
|
|
125
|
+
|
|
126
|
+
To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { tester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
tester.run('no-dynamic-styles', rule, {
|
|
5
|
+
valid: [
|
|
6
|
+
{
|
|
7
|
+
name: 'arrow function expression outside of a styling context',
|
|
8
|
+
code: `
|
|
9
|
+
const arrowFunctionExpression = (props) => ({
|
|
10
|
+
width: props.width
|
|
11
|
+
});
|
|
12
|
+
`,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'function expression outside of a styling context',
|
|
16
|
+
code: `
|
|
17
|
+
const functionExpression = function(props) {
|
|
18
|
+
return {
|
|
19
|
+
height: props.height
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
`,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'function expressions as object values outside of a styling context',
|
|
26
|
+
code: `
|
|
27
|
+
const styles = {
|
|
28
|
+
width: props => props.width,
|
|
29
|
+
height: function(props) { return props.height }
|
|
30
|
+
};
|
|
31
|
+
`,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: "importSources doesn't include the styling library",
|
|
35
|
+
code: `
|
|
36
|
+
import { css } from '@compiled/react';
|
|
37
|
+
|
|
38
|
+
const styles = css(props => ({
|
|
39
|
+
width: props.width
|
|
40
|
+
}));
|
|
41
|
+
`,
|
|
42
|
+
options: [{ importSources: [] }],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
invalid: [
|
|
46
|
+
{
|
|
47
|
+
name: 'function as argument to css',
|
|
48
|
+
code: `
|
|
49
|
+
import { css } from '@compiled/react';
|
|
50
|
+
|
|
51
|
+
const styles = css(props => ({
|
|
52
|
+
width: props.width
|
|
53
|
+
}));
|
|
54
|
+
`,
|
|
55
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'function as argument to styled',
|
|
59
|
+
code: `
|
|
60
|
+
import { styled } from '@compiled/react';
|
|
61
|
+
|
|
62
|
+
const Component = styled.div(props => ({
|
|
63
|
+
width: props.width
|
|
64
|
+
}));
|
|
65
|
+
`,
|
|
66
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'multiple functions as arguments',
|
|
70
|
+
code: `
|
|
71
|
+
import { styled } from '@compiled/react';
|
|
72
|
+
|
|
73
|
+
const Component = styled.div(
|
|
74
|
+
props => ({
|
|
75
|
+
width: props.width
|
|
76
|
+
}),
|
|
77
|
+
{ color: 'red' },
|
|
78
|
+
props => ({
|
|
79
|
+
height: props.height
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
`,
|
|
83
|
+
errors: [
|
|
84
|
+
{ messageId: 'no-dynamic-styles' },
|
|
85
|
+
{ messageId: 'no-dynamic-styles' },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'functions as values',
|
|
90
|
+
code: `
|
|
91
|
+
import { styled } from '@compiled/react';
|
|
92
|
+
|
|
93
|
+
const Component = styled.div({
|
|
94
|
+
width: props => props.width,
|
|
95
|
+
height: function(props) { return props.height; }
|
|
96
|
+
});
|
|
97
|
+
`,
|
|
98
|
+
errors: [
|
|
99
|
+
{ messageId: 'no-dynamic-styles' },
|
|
100
|
+
{ messageId: 'no-dynamic-styles' },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'importSources includes styling library not in built-in list',
|
|
105
|
+
code: `
|
|
106
|
+
import { css } from 'my-package';
|
|
107
|
+
|
|
108
|
+
const styles = css(props => ({
|
|
109
|
+
width: props.width
|
|
110
|
+
}));
|
|
111
|
+
`,
|
|
112
|
+
options: [{ importSources: ['my-package'] }],
|
|
113
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
// This fails type-checking anyway, but doesn't hurt to test
|
|
117
|
+
name: 'arrow function in template literal',
|
|
118
|
+
code: `
|
|
119
|
+
import { css } from '@compiled/react';
|
|
120
|
+
|
|
121
|
+
const styles = css({
|
|
122
|
+
height: \`\${(props) => props.height}px\`,
|
|
123
|
+
});
|
|
124
|
+
`,
|
|
125
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import esquery from 'esquery';
|
|
2
|
+
import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
getImportSources,
|
|
6
|
+
hasStyleObjectArguments,
|
|
7
|
+
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
8
|
+
|
|
9
|
+
import { createLintRule } from '../utils/create-rule';
|
|
10
|
+
|
|
11
|
+
const schema: JSONSchema4 = [
|
|
12
|
+
{
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
importSources: {
|
|
16
|
+
type: 'array',
|
|
17
|
+
items: { type: 'string' },
|
|
18
|
+
uniqueItems: true,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const rule = createLintRule({
|
|
25
|
+
meta: {
|
|
26
|
+
name: 'no-dynamic-styles',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Disallows use of dynamic styles in CSS-in-JS calls',
|
|
29
|
+
recommended: true,
|
|
30
|
+
severity: 'warn',
|
|
31
|
+
},
|
|
32
|
+
schema,
|
|
33
|
+
messages: {
|
|
34
|
+
'no-dynamic-styles':
|
|
35
|
+
'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.',
|
|
36
|
+
},
|
|
37
|
+
type: 'problem',
|
|
38
|
+
},
|
|
39
|
+
create(context) {
|
|
40
|
+
const importSources = getImportSources(context);
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
CallExpression(node) {
|
|
44
|
+
const { references } = context.sourceCode.getScope(node);
|
|
45
|
+
|
|
46
|
+
if (!hasStyleObjectArguments(node.callee, references, importSources)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const matches = esquery(
|
|
51
|
+
node,
|
|
52
|
+
'ArrowFunctionExpression, FunctionExpression',
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
for (const match of matches) {
|
|
56
|
+
context.report({ node: match, messageId: 'no-dynamic-styles' });
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export default rule;
|
|
@@ -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
|
+
);
|