@atlaskit/eslint-plugin-ui-styling-standard 0.6.0 → 0.6.2
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 +13 -0
- package/README.md +16 -15
- package/constellation/convert-props-syntax/usage.mdx +115 -0
- package/constellation/index/overview.mdx +16 -15
- package/dist/cjs/presets/all.codegen.js +2 -1
- package/dist/cjs/presets/recommended.codegen.js +2 -1
- package/dist/cjs/rules/convert-props-syntax/index.js +396 -0
- package/dist/cjs/rules/index.codegen.js +3 -1
- package/dist/cjs/rules/utils/is-supported-import.js +7 -0
- package/dist/es2019/presets/all.codegen.js +2 -1
- package/dist/es2019/presets/recommended.codegen.js +2 -1
- package/dist/es2019/rules/convert-props-syntax/index.js +291 -0
- package/dist/es2019/rules/index.codegen.js +3 -1
- package/dist/es2019/rules/utils/is-supported-import.js +7 -0
- package/dist/esm/presets/all.codegen.js +2 -1
- package/dist/esm/presets/recommended.codegen.js +2 -1
- package/dist/esm/rules/convert-props-syntax/index.js +389 -0
- package/dist/esm/rules/index.codegen.js +3 -1
- package/dist/esm/rules/utils/is-supported-import.js +7 -0
- package/dist/types/index.codegen.d.ts +2 -0
- package/dist/types/presets/all.codegen.d.ts +2 -1
- package/dist/types/presets/recommended.codegen.d.ts +2 -1
- package/dist/types/rules/convert-props-syntax/index.d.ts +3 -0
- package/dist/types/rules/index.codegen.d.ts +1 -0
- package/dist/types/rules/utils/create-rule.d.ts +1 -1
- package/dist/types-ts4.5/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
- package/dist/types-ts4.5/rules/convert-props-syntax/index.d.ts +3 -0
- package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
- package/dist/types-ts4.5/rules/utils/create-rule.d.ts +1 -1
- package/package.json +6 -4
- package/src/presets/all.codegen.tsx +2 -1
- package/src/presets/recommended.codegen.tsx +2 -1
- package/src/rules/convert-props-syntax/README.md +113 -0
- package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +76 -0
- package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +422 -0
- package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +140 -0
- package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +69 -0
- package/src/rules/convert-props-syntax/index.tsx +414 -0
- package/src/rules/index.codegen.tsx +3 -1
- package/src/rules/utils/README.md +1 -0
- package/src/rules/utils/is-supported-import.tsx +7 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { analyze } from 'eslint-scope';
|
|
2
|
+
import { createLintRule } from '../utils/create-rule';
|
|
3
|
+
import { CSS_IN_JS_IMPORTS, isStyledComponents } from '../utils/is-supported-import';
|
|
4
|
+
import ESTraverse from 'estraverse';
|
|
5
|
+
const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.styledComponents];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Represents some information about single line that contains some props, e.g.
|
|
9
|
+
* the arrow function in
|
|
10
|
+
*
|
|
11
|
+
* color: (props) => `mix(${props.myColor}, #fff)`
|
|
12
|
+
*
|
|
13
|
+
* or about a destructuring assignment, e.g. the arrow function in
|
|
14
|
+
*
|
|
15
|
+
* color: ({ myColor, otherColor }) => someFunction(myColor, otherColor)
|
|
16
|
+
*
|
|
17
|
+
* or something that we don't recognise, or that we don't think is valid
|
|
18
|
+
*
|
|
19
|
+
* color: (props, someOtherVariable) => someOtherVariable
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const standardizePropExpression = (sourceCode, expression) => {
|
|
23
|
+
const propsObjectName = 'props';
|
|
24
|
+
if (expression.type === 'Identifier') {
|
|
25
|
+
return `${propsObjectName}.${expression.name}`;
|
|
26
|
+
}
|
|
27
|
+
if (expression.type === 'MemberExpression' && expression.property.type === 'Identifier') {
|
|
28
|
+
const propertyName = sourceCode.getText(expression.property);
|
|
29
|
+
return `${propsObjectName}.${propertyName}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Cannot safely process the expression, bail out!
|
|
33
|
+
return undefined;
|
|
34
|
+
};
|
|
35
|
+
const parseArrowFunctionExpression = (expression, sourceCode) => {
|
|
36
|
+
if (expression.params.length !== 1) {
|
|
37
|
+
return {
|
|
38
|
+
valid: false
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const scopeManager = analyze(expression, {});
|
|
42
|
+
const currentScope = scopeManager.acquire(expression);
|
|
43
|
+
if (!currentScope) {
|
|
44
|
+
return {
|
|
45
|
+
valid: false
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const parameter = expression.params[0];
|
|
49
|
+
const props = [];
|
|
50
|
+
|
|
51
|
+
// Don't bother handling type annotations that appear in the parameters of arrow functions.
|
|
52
|
+
//
|
|
53
|
+
// 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.
|
|
54
|
+
if ('typeAnnotation' in parameter && parameter.typeAnnotation) {
|
|
55
|
+
return {
|
|
56
|
+
valid: false
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (parameter.type === 'ObjectPattern') {
|
|
60
|
+
if (parameter.properties.some(value => value.type === 'RestElement')) {
|
|
61
|
+
return {
|
|
62
|
+
valid: false
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const properties = parameter.properties;
|
|
66
|
+
for (const property of properties) {
|
|
67
|
+
const propertyValue = property.value;
|
|
68
|
+
// Don't bother supporting other patterns like rest elements, default values...
|
|
69
|
+
if (propertyValue.type !== 'Identifier') {
|
|
70
|
+
return {
|
|
71
|
+
valid: false
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const variable = currentScope.variables.find(varb => varb.name === propertyValue.name);
|
|
75
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
76
|
+
// Something went wrong, best to just bail out.
|
|
77
|
+
return {
|
|
78
|
+
valid: false
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const propName = standardizePropExpression(sourceCode, propertyValue);
|
|
82
|
+
if (!propName) {
|
|
83
|
+
return {
|
|
84
|
+
valid: false
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
props.push({
|
|
88
|
+
propName,
|
|
89
|
+
usages: variable.references.map(reference => reference.identifier)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
} else if (parameter.type === 'Identifier') {
|
|
93
|
+
const variable = currentScope.variables.find(varb => varb.name === parameter.name);
|
|
94
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
95
|
+
// Something went wrong, best to just bail out.
|
|
96
|
+
return {
|
|
97
|
+
valid: false
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
for (const reference of variable.references) {
|
|
101
|
+
const memberExpression = reference.identifier.parent;
|
|
102
|
+
if (memberExpression.type !== 'MemberExpression') {
|
|
103
|
+
// If reference.identifier.parent is not a member expression, that indicates
|
|
104
|
+
// that we have a usage of the props argument that does NOT look like
|
|
105
|
+
//
|
|
106
|
+
// (props) => props.color
|
|
107
|
+
//
|
|
108
|
+
// e.g. we might have (props) => props['...'] or (props) => `${props}px` perhaps...
|
|
109
|
+
//
|
|
110
|
+
// We have no choice but to bail out because the syntax is not what we expect.
|
|
111
|
+
return {
|
|
112
|
+
valid: false
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const propName = standardizePropExpression(sourceCode, memberExpression);
|
|
116
|
+
if (!propName) {
|
|
117
|
+
return {
|
|
118
|
+
valid: false
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
props.push({
|
|
122
|
+
propName,
|
|
123
|
+
usages: [memberExpression]
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
// Bail out for any syntax we don't recognise.
|
|
128
|
+
return {
|
|
129
|
+
valid: false
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
arrowFunctionExpression: expression,
|
|
134
|
+
props,
|
|
135
|
+
valid: true
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
const lintArguments = (context, args) => {
|
|
139
|
+
for (const argument of args) {
|
|
140
|
+
if (argument.type === 'ArrowFunctionExpression') {
|
|
141
|
+
// styled.div((props) => ({ ... }))
|
|
142
|
+
// styled.div(({ color, height }) => ({ ... }))
|
|
143
|
+
//
|
|
144
|
+
// Already in the form we want.
|
|
145
|
+
continue;
|
|
146
|
+
} else if (argument.type === 'ObjectExpression') {
|
|
147
|
+
const allProps = [];
|
|
148
|
+
|
|
149
|
+
// vvvvvvv
|
|
150
|
+
// styled.div({ ... })
|
|
151
|
+
for (const property of argument.properties) {
|
|
152
|
+
if (property.type === 'SpreadElement') {
|
|
153
|
+
// e.g.
|
|
154
|
+
// styled.div({ color: 'blue', ...someStyles })
|
|
155
|
+
//
|
|
156
|
+
// If we have a spread element, we can no longer safely
|
|
157
|
+
// transform this styled/css call without a lot more effort.
|
|
158
|
+
//
|
|
159
|
+
// Best to give up...
|
|
160
|
+
return;
|
|
161
|
+
} else {
|
|
162
|
+
ESTraverse.traverse(property.value, {
|
|
163
|
+
enter: function (node, _parent) {
|
|
164
|
+
if (node.type === 'ArrowFunctionExpression') {
|
|
165
|
+
const propNames = parseArrowFunctionExpression(node, context.getSourceCode());
|
|
166
|
+
if (!propNames) {
|
|
167
|
+
// We've encountered a CSS value we don't recognise, skip.
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
allProps.push(propNames);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!allProps.length) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (allProps.some(propUsage => !propUsage.valid)) {
|
|
180
|
+
context.report({
|
|
181
|
+
node: argument,
|
|
182
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer'
|
|
183
|
+
});
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
context.report({
|
|
187
|
+
node: argument,
|
|
188
|
+
messageId: 'unsupported-styled-components-prop-syntax',
|
|
189
|
+
fix(fixer) {
|
|
190
|
+
const sourceCode = context.getSourceCode();
|
|
191
|
+
return fixPropsObjectUsages(fixer, sourceCode, argument,
|
|
192
|
+
// The
|
|
193
|
+
// allProps.some((propUsage) => !propUsage.valid)
|
|
194
|
+
// condition above should have made this type assertion unnecessary...
|
|
195
|
+
allProps);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const convertPropUsage = (propUsage, sourceCode) => {
|
|
202
|
+
const arrowFunctionOffset = propUsage.arrowFunctionExpression.body.loc ? {
|
|
203
|
+
startIndex: sourceCode.getIndexFromLoc(propUsage.arrowFunctionExpression.body.loc.start),
|
|
204
|
+
endIndex: sourceCode.getIndexFromLoc(propUsage.arrowFunctionExpression.body.loc.end)
|
|
205
|
+
} : undefined;
|
|
206
|
+
if (!arrowFunctionOffset) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const sectionsToUpdate = [];
|
|
210
|
+
const newCode = [];
|
|
211
|
+
for (const prop of propUsage.props) {
|
|
212
|
+
for (const usage of prop.usages) {
|
|
213
|
+
if (!usage.loc) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
sectionsToUpdate.push({
|
|
217
|
+
identifier: usage,
|
|
218
|
+
newCode: prop.propName,
|
|
219
|
+
startIndex: sourceCode.getIndexFromLoc(usage.loc.start),
|
|
220
|
+
endIndex: sourceCode.getIndexFromLoc(usage.loc.end)
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
sectionsToUpdate.sort((a, b) => {
|
|
225
|
+
if (!a.identifier.loc || !b.identifier.loc) {
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
return a.identifier.loc.start.line - b.identifier.loc.start.line || a.identifier.loc.start.column - b.identifier.loc.start.column;
|
|
229
|
+
});
|
|
230
|
+
let prevEndIndex = arrowFunctionOffset.startIndex;
|
|
231
|
+
for (const section of sectionsToUpdate) {
|
|
232
|
+
if (prevEndIndex > section.startIndex || prevEndIndex >= section.endIndex) {
|
|
233
|
+
// Props overlap! This is unexpected, bail out.
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
newCode.push(sourceCode.getText().slice(prevEndIndex, section.startIndex));
|
|
237
|
+
newCode.push(section.newCode);
|
|
238
|
+
prevEndIndex = section.endIndex;
|
|
239
|
+
}
|
|
240
|
+
newCode.push(sourceCode.getText().slice(prevEndIndex, arrowFunctionOffset.endIndex));
|
|
241
|
+
return newCode.join('');
|
|
242
|
+
};
|
|
243
|
+
const fixPropsObjectUsages = (fixer, sourceCode, expression, allProps) => {
|
|
244
|
+
const fixers = [
|
|
245
|
+
// Wrap the object (containing the CSS properties and values) in an arrow function,
|
|
246
|
+
// so it becomes something like
|
|
247
|
+
//
|
|
248
|
+
// styled.div(
|
|
249
|
+
// (props) => ({
|
|
250
|
+
// color: ...
|
|
251
|
+
// })
|
|
252
|
+
// );
|
|
253
|
+
fixer.insertTextBefore(expression, '(props) => ('), fixer.insertTextAfter(expression, ')')];
|
|
254
|
+
for (const propUsage of allProps) {
|
|
255
|
+
const arrowFunctionExpression = propUsage.arrowFunctionExpression;
|
|
256
|
+
const newCode = convertPropUsage(propUsage, sourceCode);
|
|
257
|
+
if (newCode) {
|
|
258
|
+
fixers.push(fixer.replaceText(arrowFunctionExpression, newCode));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return fixers;
|
|
262
|
+
};
|
|
263
|
+
export const rule = createLintRule({
|
|
264
|
+
meta: {
|
|
265
|
+
name: 'convert-props-syntax',
|
|
266
|
+
docs: {
|
|
267
|
+
description: 'Convert props syntax that is unsupported by `styled-components` <4.x 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 `styled-components`.',
|
|
268
|
+
recommended: true,
|
|
269
|
+
severity: 'error'
|
|
270
|
+
},
|
|
271
|
+
fixable: 'code',
|
|
272
|
+
messages: {
|
|
273
|
+
'unsupported-styled-components-prop-syntax': 'This syntax is not supported by styled-components <4.x 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.',
|
|
274
|
+
'unsupported-styled-components-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components <4.x 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.'
|
|
275
|
+
},
|
|
276
|
+
type: 'problem'
|
|
277
|
+
},
|
|
278
|
+
create(context) {
|
|
279
|
+
return {
|
|
280
|
+
'CallExpression[callee.type="MemberExpression"]': node => {
|
|
281
|
+
const references = context.getScope().references;
|
|
282
|
+
const callee = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
|
|
283
|
+
if (!isStyledComponents(callee, references, IMPORT_SOURCES)) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
lintArguments(context, node.arguments);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
export default rule;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::7c8793f6c12f32e915fd9b4dc7a6edef>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
|
+
import convertPropsSyntax from './convert-props-syntax';
|
|
6
7
|
import localCxXcss from './local-cx-xcss';
|
|
7
8
|
import noClassnameProp from './no-classname-prop';
|
|
8
9
|
import noImportantStyles from './no-important-styles';
|
|
9
10
|
export default {
|
|
11
|
+
'convert-props-syntax': convertPropsSyntax,
|
|
10
12
|
'local-cx-xcss': localCxXcss,
|
|
11
13
|
'no-classname-prop': noClassnameProp,
|
|
12
14
|
'no-important-styles': noImportantStyles
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// This should be kept in sync with
|
|
2
|
+
// packages/design-system/eslint-plugin/src/rules/utils/is-supported-import.tsx
|
|
3
|
+
// whenever possible.
|
|
4
|
+
//
|
|
5
|
+
// TODO: would having an @atlassian/eslint-plugin-design-system-common
|
|
6
|
+
// package be useful?
|
|
7
|
+
|
|
1
8
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
2
9
|
|
|
3
10
|
export const CSS_IN_JS_IMPORTS = {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::c4a9648d888d86d752ea2b3442d395ba>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
7
7
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
8
8
|
rules: {
|
|
9
|
+
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
9
10
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
10
11
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
11
12
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::c4a9648d888d86d752ea2b3442d395ba>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
7
7
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
8
8
|
rules: {
|
|
9
|
+
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
9
10
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
10
11
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
11
12
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|