@atlaskit/eslint-plugin-ui-styling-standard 0.9.0 → 0.10.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 +12 -0
- package/README.md +2 -0
- package/constellation/enforce-style-prop/usage.mdx +38 -0
- package/constellation/index/overview.mdx +2 -0
- package/constellation/no-dynamic-styles/usage.mdx +128 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/enforce-style-prop/index.js +141 -0
- package/dist/cjs/rules/index.codegen.js +5 -1
- package/dist/cjs/rules/no-dynamic-styles/index.js +69 -0
- package/dist/es2019/presets/all.codegen.js +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/enforce-style-prop/index.js +133 -0
- package/dist/es2019/rules/index.codegen.js +5 -1
- package/dist/es2019/rules/no-dynamic-styles/index.js +51 -0
- package/dist/esm/presets/all.codegen.js +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/enforce-style-prop/index.js +135 -0
- package/dist/esm/rules/index.codegen.js +5 -1
- package/dist/esm/rules/no-dynamic-styles/index.js +62 -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/enforce-style-prop/index.d.ts +3 -0
- 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-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/enforce-style-prop/index.d.ts +3 -0
- 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/package.json +3 -2
- package/src/presets/all.codegen.tsx +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/enforce-style-prop/README.md +36 -0
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +231 -0
- package/src/rules/enforce-style-prop/index.tsx +169 -0
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/eslint-plugin-ui-styling-standard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|
|
@@ -44,11 +44,12 @@
|
|
|
44
44
|
"@typescript-eslint/utils": "^5.48.1",
|
|
45
45
|
"eslint-codemod-utils": "^1.8.6",
|
|
46
46
|
"eslint-scope": "^8.0.0",
|
|
47
|
+
"esquery": "^1.5.0",
|
|
47
48
|
"estraverse": "^5.3.0"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"@atlaskit/ds-lib": "^2.3.0",
|
|
51
|
-
"@atlaskit/eslint-plugin-design-system": "^9.
|
|
52
|
+
"@atlaskit/eslint-plugin-design-system": "^9.5.0",
|
|
52
53
|
"@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
|
|
53
54
|
"@atlassian/codegen": "*",
|
|
54
55
|
"@compiled/eslint-plugin": "^0.13.6",
|
|
@@ -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::8859bbdd2c4bf68ed4a1780f0e776e47>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -11,10 +11,12 @@ export default {
|
|
|
11
11
|
],
|
|
12
12
|
rules: {
|
|
13
13
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
14
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
14
15
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
15
16
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
16
17
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
17
18
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
|
+
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
18
20
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
20
22
|
'@atlaskit/design-system/consistent-css-prop-usage': [
|
|
@@ -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::8859bbdd2c4bf68ed4a1780f0e776e47>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -11,10 +11,12 @@ export default {
|
|
|
11
11
|
],
|
|
12
12
|
rules: {
|
|
13
13
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
14
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
14
15
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
15
16
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
16
17
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
17
18
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
|
+
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
18
20
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
20
22
|
'@atlaskit/design-system/consistent-css-prop-usage': [
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
This rule enforces passing dynamic CSS values to the `style` prop in JSX components.
|
|
2
|
+
|
|
3
|
+
We only want `props.style` to be used for dynamic values that cannot be easily determined at build time, e.g. `<div style={{ width: props.width }}>`. No statics values such as numbers, strings or any other statically analysable values should be passed to the `style` attribute. Instead, static CSS values should be passed into the `css` prop in JSX components.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
### Incorrect
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
function Component() {
|
|
11
|
+
return (
|
|
12
|
+
<div
|
|
13
|
+
style={{
|
|
14
|
+
margin: 0,
|
|
15
|
+
color: 'red',
|
|
16
|
+
}}
|
|
17
|
+
/>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Correct
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
function Component(props) {
|
|
26
|
+
return (
|
|
27
|
+
<div
|
|
28
|
+
style={{
|
|
29
|
+
width: props.width,
|
|
30
|
+
'--my-nested-width': props.width,
|
|
31
|
+
}}
|
|
32
|
+
css={css({ margin: 0, color: 'red' })}
|
|
33
|
+
/>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { tester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
tester.run('enforce-style-prop', rule, {
|
|
5
|
+
valid: [
|
|
6
|
+
{
|
|
7
|
+
name: 'No style attribute used',
|
|
8
|
+
code: `
|
|
9
|
+
<div css={styles} />
|
|
10
|
+
`,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: 'Values from props are acceptable',
|
|
14
|
+
code: `
|
|
15
|
+
function Component(props) {
|
|
16
|
+
return <div
|
|
17
|
+
style={{
|
|
18
|
+
width: props.width,
|
|
19
|
+
'--my-nested-width': props.width,
|
|
20
|
+
}}
|
|
21
|
+
css={css({ margin: 0, color: 'red' })}
|
|
22
|
+
/>
|
|
23
|
+
}
|
|
24
|
+
`,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'Values from deconstructed props are acceptable',
|
|
28
|
+
code: `
|
|
29
|
+
function DeconstructionComponent1({ myWidth }) {
|
|
30
|
+
return <div style={{ width: myWidth }}>hello</div>;
|
|
31
|
+
}
|
|
32
|
+
`,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'Multiple values from deconstructed props are acceptable',
|
|
36
|
+
code: `
|
|
37
|
+
function DeconstructionComponent2({ myWidth, height, myColor = '#fff' }) {
|
|
38
|
+
return <div style={{ width: myWidth, height, color: myColor }}>hello</div>;
|
|
39
|
+
}
|
|
40
|
+
`,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'Skips linting @media query as it is not supported by the style prop anyways',
|
|
44
|
+
code: `
|
|
45
|
+
function Component() {
|
|
46
|
+
return <div
|
|
47
|
+
style={{
|
|
48
|
+
'@media …': { /* ... */ },
|
|
49
|
+
}}
|
|
50
|
+
/>
|
|
51
|
+
}
|
|
52
|
+
`,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'Skips linting selector as it is not supported by the style prop anyways',
|
|
56
|
+
code: `
|
|
57
|
+
function Component() {
|
|
58
|
+
return <div
|
|
59
|
+
style={{
|
|
60
|
+
'button': { /* ... */ },
|
|
61
|
+
}}
|
|
62
|
+
/>
|
|
63
|
+
}
|
|
64
|
+
`,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'Static styles are outside of style attribute',
|
|
68
|
+
code: `
|
|
69
|
+
function Component(props) {
|
|
70
|
+
return (
|
|
71
|
+
<div
|
|
72
|
+
style={{
|
|
73
|
+
width: props.width,
|
|
74
|
+
}}
|
|
75
|
+
css={css({ margin: 0, color: 'red' })}
|
|
76
|
+
/>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
`,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'Props destructured in function via object pattern',
|
|
83
|
+
code: `
|
|
84
|
+
function Component(props) {
|
|
85
|
+
const { width } = props;
|
|
86
|
+
return <div style={{ width: width }} />
|
|
87
|
+
}
|
|
88
|
+
`,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: 'Props destructured in function via member expression',
|
|
92
|
+
code: `
|
|
93
|
+
function Component(props) {
|
|
94
|
+
const width = props.width;
|
|
95
|
+
return <div style={{ width: width }} />
|
|
96
|
+
}
|
|
97
|
+
`,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'Ignore state variables',
|
|
101
|
+
code: `
|
|
102
|
+
const Component = () => {
|
|
103
|
+
const [state, setState] = useState();
|
|
104
|
+
return <div style={{ width: state.width }} />
|
|
105
|
+
};
|
|
106
|
+
`,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'Ignore variables initialised by call expression',
|
|
110
|
+
code: `
|
|
111
|
+
const Component = () => {
|
|
112
|
+
const tableWidth = calcDefaultLayoutWidthByAppearance(
|
|
113
|
+
rendererAppearance,
|
|
114
|
+
tableNode,
|
|
115
|
+
);
|
|
116
|
+
return (
|
|
117
|
+
<div style={{ width: tableWidth }} />
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
`,
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'Property from object initialisation is ok',
|
|
124
|
+
code: `
|
|
125
|
+
function Component(props) {
|
|
126
|
+
const obj = { width: 0 };
|
|
127
|
+
return <div
|
|
128
|
+
style={{
|
|
129
|
+
width: obj.width
|
|
130
|
+
}}
|
|
131
|
+
/>
|
|
132
|
+
}
|
|
133
|
+
`,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'Let variables are ok',
|
|
137
|
+
code: `
|
|
138
|
+
function Component(props) {
|
|
139
|
+
let width = 0;
|
|
140
|
+
return <div
|
|
141
|
+
style={{
|
|
142
|
+
width
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
}
|
|
146
|
+
`,
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
invalid: [
|
|
150
|
+
{
|
|
151
|
+
name: 'Basic test for static css values',
|
|
152
|
+
code: `
|
|
153
|
+
function Component(props) {
|
|
154
|
+
return <div
|
|
155
|
+
style={{
|
|
156
|
+
margin: 0,
|
|
157
|
+
color: 'red',
|
|
158
|
+
}}
|
|
159
|
+
/>
|
|
160
|
+
}
|
|
161
|
+
`,
|
|
162
|
+
errors: [
|
|
163
|
+
{ messageId: 'enforce-style-prop' },
|
|
164
|
+
{ messageId: 'enforce-style-prop' },
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: 'No constants passed to style',
|
|
169
|
+
code: `
|
|
170
|
+
function Component(props) {
|
|
171
|
+
const color = 'red';
|
|
172
|
+
return <div
|
|
173
|
+
style={{
|
|
174
|
+
color, // should be static css
|
|
175
|
+
}}
|
|
176
|
+
/>
|
|
177
|
+
}
|
|
178
|
+
`,
|
|
179
|
+
errors: [{ messageId: 'enforce-style-prop' }],
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'No spread operator within style',
|
|
183
|
+
code: `
|
|
184
|
+
import importedStyleObj from '../shared';
|
|
185
|
+
function Component(props) {
|
|
186
|
+
const color = 'red';
|
|
187
|
+
return <div
|
|
188
|
+
style={{
|
|
189
|
+
...importedStyleObj,
|
|
190
|
+
...props.style
|
|
191
|
+
}}
|
|
192
|
+
/>
|
|
193
|
+
}
|
|
194
|
+
`,
|
|
195
|
+
errors: [
|
|
196
|
+
{ messageId: 'enforce-style-prop' },
|
|
197
|
+
{ messageId: 'enforce-style-prop' },
|
|
198
|
+
],
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
name: 'No imported object passed to style',
|
|
202
|
+
code: `
|
|
203
|
+
import importedStyleObj from '../shared';
|
|
204
|
+
function AnotherComponent() {
|
|
205
|
+
return <div style={importedStyleObj} />;
|
|
206
|
+
}
|
|
207
|
+
`,
|
|
208
|
+
errors: [{ messageId: 'enforce-style-prop' }],
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: 'No rest prop',
|
|
212
|
+
code: `
|
|
213
|
+
function Component({ prop1, ...spreadElement }) {
|
|
214
|
+
return <div style={{ 'button': spreadElement }}>hello</div>
|
|
215
|
+
}
|
|
216
|
+
`,
|
|
217
|
+
errors: [{ messageId: 'enforce-style-prop' }],
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
name: 'No token calls',
|
|
221
|
+
code: `
|
|
222
|
+
import { token } from '@atlaskit/tokens';
|
|
223
|
+
|
|
224
|
+
function Component() {
|
|
225
|
+
return <div style={{ paddingBottom: token('space.150', '12px') }} />;
|
|
226
|
+
}
|
|
227
|
+
`,
|
|
228
|
+
errors: [{ messageId: 'enforce-style-prop' }],
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Identifier, isNodeOfType, Node } from 'eslint-codemod-utils';
|
|
2
|
+
import type { SpreadElement, Property } from 'estree-jsx';
|
|
3
|
+
|
|
4
|
+
import { createLintRule } from '../utils/create-rule';
|
|
5
|
+
import type { Rule, Scope } from 'eslint';
|
|
6
|
+
|
|
7
|
+
type ObjPropertyType = Property | SpreadElement;
|
|
8
|
+
type IdentifierWithParent = Scope.Reference['identifier'] &
|
|
9
|
+
Rule.NodeParentExtension;
|
|
10
|
+
|
|
11
|
+
const findVariable = (
|
|
12
|
+
context: Rule.RuleContext,
|
|
13
|
+
name: string,
|
|
14
|
+
): Scope.Variable | undefined => {
|
|
15
|
+
return context.getScope().variables.find((varb) => varb.name === name);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const isParameter = (context: Rule.RuleContext, variableName: string) => {
|
|
19
|
+
const definitions = findVariable(context, variableName)?.defs;
|
|
20
|
+
if (!definitions || !definitions.length) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const result = definitions[0];
|
|
25
|
+
return result.type === 'Parameter';
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const checkIdentifier = (
|
|
29
|
+
context: Rule.RuleContext,
|
|
30
|
+
identifier: Identifier,
|
|
31
|
+
): boolean => {
|
|
32
|
+
const val = identifier.name;
|
|
33
|
+
const variable = findVariable(context, val);
|
|
34
|
+
|
|
35
|
+
if (!variable) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const variableDefinition = variable.defs[0].node;
|
|
40
|
+
|
|
41
|
+
// If identifier is a RestElement, report warning
|
|
42
|
+
if (
|
|
43
|
+
(variable.identifiers[0] as IdentifierWithParent).parent?.type ===
|
|
44
|
+
'RestElement'
|
|
45
|
+
) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Checking if identifier has been destructured from props within function
|
|
50
|
+
if (variableDefinition.type === 'VariableDeclarator') {
|
|
51
|
+
// If it's a let variable, ignore
|
|
52
|
+
if (variableDefinition.parent.kind === 'let') {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// If it's initialised by a call expression, ignore
|
|
56
|
+
if (variableDefinition.init?.type === 'CallExpression') {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
// Destructured from member expression
|
|
60
|
+
// e.g. const width = props.width
|
|
61
|
+
if (variableDefinition.init?.type === 'MemberExpression') {
|
|
62
|
+
const memberIdentifierName = variableDefinition.init.object.name;
|
|
63
|
+
return isParameter(context, memberIdentifierName);
|
|
64
|
+
}
|
|
65
|
+
// Destructured from object pattern
|
|
66
|
+
// e.g. const { width } = props
|
|
67
|
+
const propName = variableDefinition.init?.name;
|
|
68
|
+
return isParameter(context, propName);
|
|
69
|
+
}
|
|
70
|
+
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
71
|
+
return isParameter(context, val);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
function isPropertyValueAllowed(
|
|
75
|
+
property: ObjPropertyType,
|
|
76
|
+
context: Rule.RuleContext,
|
|
77
|
+
): boolean {
|
|
78
|
+
if (property.type === 'SpreadElement') {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
// property.type === 'Property', moving on to check if value is static
|
|
82
|
+
if (property.value.type === 'Literal') {
|
|
83
|
+
return false;
|
|
84
|
+
} else if (property.value.type === 'Identifier') {
|
|
85
|
+
return checkIdentifier(context, property.value);
|
|
86
|
+
} else if (
|
|
87
|
+
property.value.type === 'CallExpression' &&
|
|
88
|
+
property.value.callee.type === 'Identifier' &&
|
|
89
|
+
property.value.callee.name === 'token'
|
|
90
|
+
) {
|
|
91
|
+
return false;
|
|
92
|
+
} else if (
|
|
93
|
+
property.value.type === 'MemberExpression' &&
|
|
94
|
+
property.value.object.type === 'Identifier'
|
|
95
|
+
) {
|
|
96
|
+
const val = property.value.object.name;
|
|
97
|
+
const definitions = findVariable(context, val)?.defs;
|
|
98
|
+
if (!definitions || !definitions.length) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
const variable = definitions[0];
|
|
102
|
+
if (!variable) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const varType = variable.type;
|
|
106
|
+
|
|
107
|
+
if (varType === 'Variable') {
|
|
108
|
+
if (!variable.node.init) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const varSource = variable.node.init.type;
|
|
112
|
+
// Variables initialised by a call expression or object expression are ok
|
|
113
|
+
if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
// Check if member expression matches with function argument
|
|
117
|
+
} else if (varType !== 'Parameter') {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const rule = createLintRule({
|
|
125
|
+
meta: {
|
|
126
|
+
name: 'enforce-style-prop',
|
|
127
|
+
docs: {
|
|
128
|
+
description:
|
|
129
|
+
'Disallows usage of static styles in the `style` attribute in components',
|
|
130
|
+
recommended: true,
|
|
131
|
+
severity: 'warn',
|
|
132
|
+
},
|
|
133
|
+
messages: {
|
|
134
|
+
'enforce-style-prop':
|
|
135
|
+
'Avoid adding static values to the style prop. Use the css prop for static styles instead.',
|
|
136
|
+
},
|
|
137
|
+
type: 'problem',
|
|
138
|
+
},
|
|
139
|
+
create(context) {
|
|
140
|
+
return {
|
|
141
|
+
'JSXAttribute[name.name="style"] > JSXExpressionContainer': (
|
|
142
|
+
node: Node,
|
|
143
|
+
) => {
|
|
144
|
+
if (!isNodeOfType(node, 'JSXExpressionContainer')) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// we've reached an attribute named style
|
|
148
|
+
|
|
149
|
+
if (node.expression.type === 'ObjectExpression') {
|
|
150
|
+
node.expression.properties.forEach((value) => {
|
|
151
|
+
if (!isPropertyValueAllowed(value, context)) {
|
|
152
|
+
context.report({
|
|
153
|
+
node: value,
|
|
154
|
+
messageId: 'enforce-style-prop',
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
context.report({
|
|
160
|
+
node: node,
|
|
161
|
+
messageId: 'enforce-style-prop',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
export default rule;
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
import convertPropsSyntax from './convert-props-syntax';
|
|
7
|
+
import enforceStyleProp from './enforce-style-prop';
|
|
7
8
|
import localCxXcss from './local-cx-xcss';
|
|
8
9
|
import noArrayArguments from './no-array-arguments';
|
|
9
10
|
import noClassnameProp from './no-classname-prop';
|
|
10
11
|
import noContainerQueries from './no-container-queries';
|
|
12
|
+
import noDynamicStyles from './no-dynamic-styles';
|
|
11
13
|
import noImportantStyles from './no-important-styles';
|
|
12
14
|
import noNestedSelectors from './no-nested-selectors';
|
|
13
15
|
|
|
14
16
|
export default {
|
|
15
17
|
'convert-props-syntax': convertPropsSyntax,
|
|
18
|
+
'enforce-style-prop': enforceStyleProp,
|
|
16
19
|
'local-cx-xcss': localCxXcss,
|
|
17
20
|
'no-array-arguments': noArrayArguments,
|
|
18
21
|
'no-classname-prop': noClassnameProp,
|
|
19
22
|
'no-container-queries': noContainerQueries,
|
|
23
|
+
'no-dynamic-styles': noDynamicStyles,
|
|
20
24
|
'no-important-styles': noImportantStyles,
|
|
21
25
|
'no-nested-selectors': noNestedSelectors,
|
|
22
26
|
};
|
|
@@ -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).
|