@atlaskit/eslint-plugin-ui-styling-standard 0.15.2 → 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.
- package/CHANGELOG.md +8 -0
- package/constellation/index/migration-guide.mdx +309 -0
- package/constellation/index/overview.mdx +4 -0
- package/constellation/index/props.mdx +9 -5
- package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
- package/index.js +9 -9
- package/package.json +3 -3
- package/scripts/codegen.tsx +223 -284
- package/src/rules/__tests__/utils/_tester.tsx +24 -24
- package/src/rules/atlaskit-theme/README.md +21 -19
- package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
- package/src/rules/atlaskit-theme/index.ts +107 -115
- package/src/rules/convert-props-syntax/README.md +32 -22
- package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
- package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
- package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
- package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
- package/src/rules/convert-props-syntax/index.tsx +356 -386
- package/src/rules/enforce-style-prop/README.md +21 -18
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
- package/src/rules/enforce-style-prop/index.tsx +144 -160
- package/src/rules/external-rules.tsx +163 -164
- package/src/rules/local-cx-xcss/README.md +10 -6
- package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
- package/src/rules/local-cx-xcss/index.tsx +46 -56
- package/src/rules/no-array-arguments/README.md +2 -1
- package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
- package/src/rules/no-array-arguments/index.tsx +68 -69
- package/src/rules/no-classname-prop/README.md +7 -4
- package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
- package/src/rules/no-classname-prop/index.tsx +25 -25
- package/src/rules/no-container-queries/README.md +11 -9
- package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
- package/src/rules/no-container-queries/index.tsx +75 -81
- package/src/rules/no-dynamic-styles/README.md +16 -19
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
- package/src/rules/no-dynamic-styles/index.tsx +45 -48
- package/src/rules/no-exported-styles/README.md +17 -12
- package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
- package/src/rules/no-exported-styles/index.tsx +36 -36
- package/src/rules/no-global-styles/README.md +15 -14
- package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
- package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
- package/src/rules/no-global-styles/index.tsx +59 -62
- package/src/rules/no-important-styles/README.md +5 -3
- package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
- package/src/rules/no-important-styles/index.tsx +49 -49
- package/src/rules/no-imported-style-values/README.md +23 -15
- package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
- package/src/rules/no-imported-style-values/index.tsx +116 -120
- package/src/rules/no-nested-selectors/README.md +15 -13
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +128 -128
- package/src/rules/no-styled/README.md +14 -9
- package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
- package/src/rules/no-styled/index.tsx +34 -41
- package/src/rules/no-unsafe-selectors/README.md +103 -93
- package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
- package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
- package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
- package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
- package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
- package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
- package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
- package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
- package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
- package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
- package/src/rules/no-unsafe-values/README.md +34 -40
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
- package/src/rules/no-unsafe-values/index.tsx +76 -78
- package/src/rules/no-unsafe-values/messages.tsx +12 -12
- package/src/rules/no-unsafe-values/utils.tsx +370 -382
- package/src/rules/use-compiled/README.md +11 -8
- package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
- package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
- package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
- package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
- package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
- package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
- package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
- package/src/rules/use-compiled/index.tsx +203 -226
- package/src/rules/use-compiled/is-safe.tsx +113 -117
- package/src/rules/use-compiled/utils.tsx +14 -18
- package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
- package/src/rules/utils/create-rule.tsx +3 -6
|
@@ -1,37 +1,33 @@
|
|
|
1
1
|
import { analyze } from 'eslint-scope';
|
|
2
2
|
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
ArrowFunctionExpression,
|
|
4
|
+
AssignmentProperty,
|
|
5
|
+
CallExpression,
|
|
6
|
+
Identifier,
|
|
7
|
+
MemberExpression,
|
|
8
|
+
ObjectExpression,
|
|
9
9
|
} from 'estree-jsx';
|
|
10
10
|
import { createLintRule } from '../utils/create-rule';
|
|
11
|
-
import {
|
|
12
|
-
isStyledComponents,
|
|
13
|
-
isEmotion,
|
|
14
|
-
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
11
|
+
import { isStyledComponents, isEmotion } from '@atlaskit/eslint-utils/is-supported-import';
|
|
15
12
|
import type { Rule, Scope, SourceCode } from 'eslint';
|
|
16
13
|
import ESTraverse from 'estraverse';
|
|
17
14
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
18
15
|
|
|
19
16
|
type PropInfo = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Name of the prop, e.g. props, backgroundColor.
|
|
19
|
+
*/
|
|
20
|
+
propName: string;
|
|
21
|
+
usages: (IdentifierWithParent | MemberExpressionWithParent)[];
|
|
25
22
|
};
|
|
26
23
|
|
|
27
24
|
type ValidPropsFound = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
arrowFunctionExpression: ArrowFunctionExpression;
|
|
26
|
+
props: PropInfo[];
|
|
27
|
+
valid: true;
|
|
31
28
|
};
|
|
32
29
|
|
|
33
|
-
type IdentifierWithParent = Scope.Reference['identifier'] &
|
|
34
|
-
Rule.NodeParentExtension;
|
|
30
|
+
type IdentifierWithParent = Scope.Reference['identifier'] & Rule.NodeParentExtension;
|
|
35
31
|
|
|
36
32
|
type MemberExpressionWithParent = MemberExpression & Rule.NodeParentExtension;
|
|
37
33
|
|
|
@@ -50,395 +46,369 @@ type MemberExpressionWithParent = MemberExpression & Rule.NodeParentExtension;
|
|
|
50
46
|
* color: (props, someOtherVariable) => someOtherVariable
|
|
51
47
|
*/
|
|
52
48
|
type PropsFound =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
| ValidPropsFound
|
|
50
|
+
| {
|
|
51
|
+
/**
|
|
52
|
+
* Props use a syntax we don't recognise, or we don't think is valid...
|
|
53
|
+
* or perhaps something unexpected happened in ESLint and we don't feel confident enough to try and autofix anything.
|
|
54
|
+
*/
|
|
55
|
+
valid: false;
|
|
56
|
+
};
|
|
61
57
|
|
|
62
58
|
const standardizePropExpression = (
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
sourceCode: SourceCode,
|
|
60
|
+
expression: MemberExpression | Identifier,
|
|
65
61
|
): string | undefined => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
// Cannot safely process the expression, bail out!
|
|
81
|
-
return undefined;
|
|
62
|
+
const propsObjectName = 'props';
|
|
63
|
+
|
|
64
|
+
if (expression.type === 'Identifier') {
|
|
65
|
+
return `${propsObjectName}.${expression.name}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (expression.type === 'MemberExpression' && expression.property.type === 'Identifier') {
|
|
69
|
+
const propertyName = sourceCode.getText(expression.property);
|
|
70
|
+
return `${propsObjectName}.${propertyName}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Cannot safely process the expression, bail out!
|
|
74
|
+
return undefined;
|
|
82
75
|
};
|
|
83
76
|
|
|
84
77
|
const parseArrowFunctionExpression = (
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
expression: ArrowFunctionExpression,
|
|
79
|
+
sourceCode: SourceCode,
|
|
87
80
|
): PropsFound => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
// Bail out for any syntax we don't recognise.
|
|
182
|
-
return { valid: false };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return { arrowFunctionExpression: expression, props, valid: true };
|
|
81
|
+
if (expression.params.length !== 1) {
|
|
82
|
+
return { valid: false };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const scopeManager = analyze(expression, {});
|
|
86
|
+
const currentScope = scopeManager.acquire(expression);
|
|
87
|
+
|
|
88
|
+
if (!currentScope) {
|
|
89
|
+
return { valid: false };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const parameter = expression.params[0];
|
|
93
|
+
const props: PropInfo[] = [];
|
|
94
|
+
|
|
95
|
+
// Don't bother handling type annotations that appear in the parameters of arrow functions.
|
|
96
|
+
//
|
|
97
|
+
// This only applies if the ESLint rule is running under @typescript-eslint (not regular eslint), hence the roundabout way we use to check whether parameter.typeAnnotation exists or not.
|
|
98
|
+
if ('typeAnnotation' in parameter && parameter.typeAnnotation) {
|
|
99
|
+
return { valid: false };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (parameter.type === 'ObjectPattern') {
|
|
103
|
+
if (parameter.properties.some((value) => value.type === 'RestElement')) {
|
|
104
|
+
return { valid: false };
|
|
105
|
+
}
|
|
106
|
+
const properties = parameter.properties as AssignmentProperty[];
|
|
107
|
+
|
|
108
|
+
for (const property of properties) {
|
|
109
|
+
const propertyValue = property.value;
|
|
110
|
+
// Don't bother supporting other patterns like rest elements, default values...
|
|
111
|
+
if (propertyValue.type !== 'Identifier') {
|
|
112
|
+
return { valid: false };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const variable = currentScope.variables.find((varb) => varb.name === propertyValue.name);
|
|
116
|
+
|
|
117
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
118
|
+
// Something went wrong, best to just bail out.
|
|
119
|
+
return { valid: false };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const propName = standardizePropExpression(sourceCode, propertyValue);
|
|
123
|
+
|
|
124
|
+
if (!propName) {
|
|
125
|
+
return { valid: false };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
props.push({
|
|
129
|
+
propName,
|
|
130
|
+
usages: variable.references.map(
|
|
131
|
+
(reference) => reference.identifier as IdentifierWithParent,
|
|
132
|
+
),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
} else if (parameter.type === 'Identifier') {
|
|
136
|
+
const variable = currentScope.variables.find((varb) => varb.name === parameter.name);
|
|
137
|
+
|
|
138
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
139
|
+
// Something went wrong, best to just bail out.
|
|
140
|
+
return { valid: false };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const reference of variable.references) {
|
|
144
|
+
const memberExpression = (reference.identifier as IdentifierWithParent).parent;
|
|
145
|
+
if (memberExpression.type !== 'MemberExpression') {
|
|
146
|
+
// If reference.identifier.parent is not a member expression, that indicates
|
|
147
|
+
// that we have a usage of the props argument that does NOT look like
|
|
148
|
+
//
|
|
149
|
+
// (props) => props.color
|
|
150
|
+
//
|
|
151
|
+
// e.g. we might have (props) => props['...'] or (props) => `${props}px` perhaps...
|
|
152
|
+
//
|
|
153
|
+
// We have no choice but to bail out because the syntax is not what we expect.
|
|
154
|
+
return { valid: false };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const propName = standardizePropExpression(sourceCode, memberExpression);
|
|
158
|
+
|
|
159
|
+
if (!propName) {
|
|
160
|
+
return { valid: false };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
props.push({
|
|
164
|
+
propName,
|
|
165
|
+
usages: [memberExpression],
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
// Bail out for any syntax we don't recognise.
|
|
170
|
+
return { valid: false };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return { arrowFunctionExpression: expression, props, valid: true };
|
|
186
174
|
};
|
|
187
175
|
|
|
188
|
-
const lintArguments = (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
) => {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
allProps as ValidPropsFound[],
|
|
265
|
-
);
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
}
|
|
176
|
+
const lintArguments = (context: Rule.RuleContext, args: CallExpression['arguments']) => {
|
|
177
|
+
for (const argument of args) {
|
|
178
|
+
if (argument.type === 'ArrowFunctionExpression') {
|
|
179
|
+
// styled.div((props) => ({ ... }))
|
|
180
|
+
// styled.div(({ color, height }) => ({ ... }))
|
|
181
|
+
//
|
|
182
|
+
// Already in the form we want.
|
|
183
|
+
continue;
|
|
184
|
+
} else if (argument.type === 'ObjectExpression') {
|
|
185
|
+
const allProps: PropsFound[] = [];
|
|
186
|
+
|
|
187
|
+
// vvvvvvv
|
|
188
|
+
// styled.div({ ... })
|
|
189
|
+
for (const property of argument.properties) {
|
|
190
|
+
if (property.type === 'SpreadElement') {
|
|
191
|
+
// e.g.
|
|
192
|
+
// styled.div({ color: 'blue', ...someStyles })
|
|
193
|
+
//
|
|
194
|
+
// If we have a spread element, we can no longer safely
|
|
195
|
+
// transform this styled/css call without a lot more effort.
|
|
196
|
+
//
|
|
197
|
+
// Best to give up...
|
|
198
|
+
return;
|
|
199
|
+
} else {
|
|
200
|
+
ESTraverse.traverse(property.value, {
|
|
201
|
+
enter: function (node, _parent) {
|
|
202
|
+
if (node.type === 'ArrowFunctionExpression') {
|
|
203
|
+
const propNames = parseArrowFunctionExpression(node, context.getSourceCode());
|
|
204
|
+
|
|
205
|
+
if (!propNames) {
|
|
206
|
+
// We've encountered a CSS value we don't recognise, skip.
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
allProps.push(propNames);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
/**
|
|
214
|
+
* This is needed to handle unknown node types. Otherwise an error is thrown.
|
|
215
|
+
*/
|
|
216
|
+
fallback: 'iteration',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!allProps.length) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (allProps.some((propUsage) => !propUsage.valid)) {
|
|
226
|
+
context.report({
|
|
227
|
+
node: argument,
|
|
228
|
+
messageId: 'unsupported-prop-syntax-no-autofixer',
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
context.report({
|
|
235
|
+
node: argument,
|
|
236
|
+
messageId: 'unsupported-prop-syntax',
|
|
237
|
+
fix(fixer) {
|
|
238
|
+
const sourceCode = context.getSourceCode();
|
|
239
|
+
return fixPropsObjectUsages(
|
|
240
|
+
fixer,
|
|
241
|
+
sourceCode,
|
|
242
|
+
argument,
|
|
243
|
+
// The
|
|
244
|
+
// allProps.some((propUsage) => !propUsage.valid)
|
|
245
|
+
// condition above should have made this type assertion unnecessary...
|
|
246
|
+
allProps as ValidPropsFound[],
|
|
247
|
+
);
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
270
252
|
};
|
|
271
253
|
|
|
272
254
|
type SectionToUpdate = {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
255
|
+
identifier: Identifier | MemberExpression;
|
|
256
|
+
newCode: string;
|
|
257
|
+
startIndex: number;
|
|
258
|
+
endIndex: number;
|
|
277
259
|
};
|
|
278
260
|
|
|
279
261
|
const convertPropUsage = (
|
|
280
|
-
|
|
281
|
-
|
|
262
|
+
propUsage: ValidPropsFound,
|
|
263
|
+
sourceCode: SourceCode,
|
|
282
264
|
): string | undefined => {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
newCode.push(
|
|
340
|
-
sourceCode.getText().slice(prevEndIndex, arrowFunctionOffset.endIndex),
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
return newCode.join('');
|
|
265
|
+
const arrowFunctionOffset = propUsage.arrowFunctionExpression.body.loc
|
|
266
|
+
? {
|
|
267
|
+
startIndex: sourceCode.getIndexFromLoc(propUsage.arrowFunctionExpression.body.loc.start),
|
|
268
|
+
endIndex: sourceCode.getIndexFromLoc(propUsage.arrowFunctionExpression.body.loc.end),
|
|
269
|
+
}
|
|
270
|
+
: undefined;
|
|
271
|
+
|
|
272
|
+
if (!arrowFunctionOffset) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const sectionsToUpdate: SectionToUpdate[] = [];
|
|
277
|
+
const newCode: string[] = [];
|
|
278
|
+
|
|
279
|
+
for (const prop of propUsage.props) {
|
|
280
|
+
for (const usage of prop.usages) {
|
|
281
|
+
if (!usage.loc) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
sectionsToUpdate.push({
|
|
286
|
+
identifier: usage,
|
|
287
|
+
newCode: prop.propName,
|
|
288
|
+
startIndex: sourceCode.getIndexFromLoc(usage.loc.start),
|
|
289
|
+
endIndex: sourceCode.getIndexFromLoc(usage.loc.end),
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
sectionsToUpdate.sort((a, b) => {
|
|
295
|
+
if (!a.identifier.loc || !b.identifier.loc) {
|
|
296
|
+
return 0;
|
|
297
|
+
}
|
|
298
|
+
return (
|
|
299
|
+
a.identifier.loc.start.line - b.identifier.loc.start.line ||
|
|
300
|
+
a.identifier.loc.start.column - b.identifier.loc.start.column
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
let prevEndIndex = arrowFunctionOffset.startIndex;
|
|
305
|
+
for (const section of sectionsToUpdate) {
|
|
306
|
+
if (prevEndIndex > section.startIndex || prevEndIndex >= section.endIndex) {
|
|
307
|
+
// Props overlap! This is unexpected, bail out.
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
newCode.push(sourceCode.getText().slice(prevEndIndex, section.startIndex));
|
|
312
|
+
newCode.push(section.newCode);
|
|
313
|
+
|
|
314
|
+
prevEndIndex = section.endIndex;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
newCode.push(sourceCode.getText().slice(prevEndIndex, arrowFunctionOffset.endIndex));
|
|
318
|
+
|
|
319
|
+
return newCode.join('');
|
|
344
320
|
};
|
|
345
321
|
|
|
346
322
|
const fixPropsObjectUsages = (
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
323
|
+
fixer: Rule.RuleFixer,
|
|
324
|
+
sourceCode: SourceCode,
|
|
325
|
+
expression: ObjectExpression,
|
|
326
|
+
allProps: ValidPropsFound[],
|
|
351
327
|
): Rule.Fix[] => {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
328
|
+
const fixers = [
|
|
329
|
+
// Wrap the object (containing the CSS properties and values) in an arrow function,
|
|
330
|
+
// so it becomes something like
|
|
331
|
+
//
|
|
332
|
+
// styled.div(
|
|
333
|
+
// (props) => ({
|
|
334
|
+
// color: ...
|
|
335
|
+
// })
|
|
336
|
+
// );
|
|
337
|
+
fixer.insertTextBefore(expression, '(props) => ('),
|
|
338
|
+
fixer.insertTextAfter(expression, ')'),
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
for (const propUsage of allProps) {
|
|
342
|
+
const arrowFunctionExpression = propUsage.arrowFunctionExpression;
|
|
343
|
+
const newCode = convertPropUsage(propUsage, sourceCode);
|
|
344
|
+
|
|
345
|
+
if (newCode) {
|
|
346
|
+
fixers.push(fixer.replaceText(arrowFunctionExpression, newCode));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return fixers;
|
|
375
351
|
};
|
|
376
352
|
|
|
377
353
|
export const rule = createLintRule({
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
lintArguments(context, node.arguments);
|
|
439
|
-
},
|
|
440
|
-
};
|
|
441
|
-
},
|
|
354
|
+
meta: {
|
|
355
|
+
name: 'convert-props-syntax',
|
|
356
|
+
docs: {
|
|
357
|
+
description:
|
|
358
|
+
'Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries.',
|
|
359
|
+
recommended: true,
|
|
360
|
+
severity: 'error',
|
|
361
|
+
},
|
|
362
|
+
fixable: 'code',
|
|
363
|
+
messages: {
|
|
364
|
+
'unsupported-prop-syntax':
|
|
365
|
+
'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
|
|
366
|
+
|
|
367
|
+
'unsupported-prop-syntax-no-autofixer':
|
|
368
|
+
'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.',
|
|
369
|
+
},
|
|
370
|
+
type: 'problem',
|
|
371
|
+
},
|
|
372
|
+
create(context) {
|
|
373
|
+
return {
|
|
374
|
+
CallExpression: (node: CallExpression) => {
|
|
375
|
+
const references = context.getScope().references;
|
|
376
|
+
|
|
377
|
+
// Rule should ignore .attrs() calls
|
|
378
|
+
if (
|
|
379
|
+
node.callee.type === 'MemberExpression' &&
|
|
380
|
+
node.callee.property.type === 'Identifier' &&
|
|
381
|
+
node.callee.property.name === 'attrs'
|
|
382
|
+
) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const callee = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
|
|
387
|
+
|
|
388
|
+
if (!isStyledComponents(callee, references) && !isEmotion(callee, references)) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* We only want to lint the second set of arguments in the following cases:
|
|
394
|
+
*
|
|
395
|
+
* 1. styled(BaseComponent)({ ... })
|
|
396
|
+
* 2. styled(BaseComponent)``
|
|
397
|
+
*/
|
|
398
|
+
if (
|
|
399
|
+
// Using this type assertion to cast into a more useful type
|
|
400
|
+
// For some reason the estree types are missing useful things like `parent`
|
|
401
|
+
isNodeOfType(node, 'CallExpression') &&
|
|
402
|
+
(node.parent?.type === 'CallExpression' ||
|
|
403
|
+
node.parent?.type === 'TaggedTemplateExpression')
|
|
404
|
+
) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
lintArguments(context, node.arguments);
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
},
|
|
442
412
|
});
|
|
443
413
|
|
|
444
414
|
export default rule;
|