@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,140 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'convert-props-syntax',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [
|
|
10
|
+
{
|
|
11
|
+
name: 'ignores type annotation in props argument',
|
|
12
|
+
code: `
|
|
13
|
+
import styled from 'styled-components';
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
color: string;
|
|
17
|
+
myBackground: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
styled.div((props: Props) => ({
|
|
21
|
+
color: props.color,
|
|
22
|
+
backgroundColor: props.myBackground,
|
|
23
|
+
}))
|
|
24
|
+
`,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
invalid: [
|
|
28
|
+
{
|
|
29
|
+
name: 'converts styled usages with type annotation on the outside',
|
|
30
|
+
code: `
|
|
31
|
+
import styled from 'styled-components';
|
|
32
|
+
|
|
33
|
+
styled.div<{ color: string; backgroundColor: string; }>({
|
|
34
|
+
color: (props) => props.color,
|
|
35
|
+
backgroundColor: ({ backgroundColor }) => backgroundColor,
|
|
36
|
+
});
|
|
37
|
+
`,
|
|
38
|
+
output: `
|
|
39
|
+
import styled from 'styled-components';
|
|
40
|
+
|
|
41
|
+
styled.div<{ color: string; backgroundColor: string; }>((props) => ({
|
|
42
|
+
color: props.color,
|
|
43
|
+
backgroundColor: props.backgroundColor,
|
|
44
|
+
}));
|
|
45
|
+
`,
|
|
46
|
+
errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'converts styled usages with type annotation on the outside (as a variable)',
|
|
50
|
+
code: `
|
|
51
|
+
import styled from 'styled-components';
|
|
52
|
+
|
|
53
|
+
type Props = {
|
|
54
|
+
color: string; backgroundColor: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
styled.div<Props>({
|
|
58
|
+
color: (props) => props.color,
|
|
59
|
+
backgroundColor: ({ backgroundColor }) => backgroundColor,
|
|
60
|
+
});
|
|
61
|
+
`,
|
|
62
|
+
output: `
|
|
63
|
+
import styled from 'styled-components';
|
|
64
|
+
|
|
65
|
+
type Props = {
|
|
66
|
+
color: string; backgroundColor: string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
styled.div<Props>((props) => ({
|
|
70
|
+
color: props.color,
|
|
71
|
+
backgroundColor: props.backgroundColor,
|
|
72
|
+
}));
|
|
73
|
+
`,
|
|
74
|
+
errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'disables autofixer when type annotation is inside props argument',
|
|
78
|
+
code: `
|
|
79
|
+
import styled from 'styled-components';
|
|
80
|
+
|
|
81
|
+
type Props = {
|
|
82
|
+
color: string;
|
|
83
|
+
backgroundColor: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
styled.div({
|
|
87
|
+
color: (props: Props) => props.color,
|
|
88
|
+
backgroundColor: (props: Props) => props.backgroundColor,
|
|
89
|
+
});
|
|
90
|
+
`,
|
|
91
|
+
errors: [
|
|
92
|
+
{
|
|
93
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'disables autofixer when type annotation is given for destructured props',
|
|
99
|
+
code: `
|
|
100
|
+
import styled from 'styled-components';
|
|
101
|
+
|
|
102
|
+
type Props = {
|
|
103
|
+
color: string;
|
|
104
|
+
backgroundColor: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
styled.div({
|
|
108
|
+
color: ({ color }: Props) => color,
|
|
109
|
+
backgroundColor: ({ backgroundColor }: Props) => backgroundColor,
|
|
110
|
+
});
|
|
111
|
+
`,
|
|
112
|
+
errors: [
|
|
113
|
+
{
|
|
114
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: 'disables autofixer when type annotation is inside props within template literal',
|
|
120
|
+
code: `
|
|
121
|
+
import styled from 'styled-components';
|
|
122
|
+
|
|
123
|
+
interface ThumbnailProps {
|
|
124
|
+
src: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
styled.div({
|
|
128
|
+
backgroundImage: \`url(\${({ src }: ThumbnailProps) => src})\`,
|
|
129
|
+
backgroundSize: 'cover',
|
|
130
|
+
});
|
|
131
|
+
`,
|
|
132
|
+
errors: [
|
|
133
|
+
{
|
|
134
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'convert-props-syntax',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [],
|
|
10
|
+
invalid: [
|
|
11
|
+
{
|
|
12
|
+
name: 'disables autofixer for rest element in parameter',
|
|
13
|
+
// Not sure if this would ever occur in a codebase, but we deliberately ignore it.
|
|
14
|
+
code: `
|
|
15
|
+
import styled from 'styled-components';
|
|
16
|
+
|
|
17
|
+
styled.div({
|
|
18
|
+
color: ({ ...restElement }) => restElement,
|
|
19
|
+
});
|
|
20
|
+
`,
|
|
21
|
+
errors: [
|
|
22
|
+
{
|
|
23
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'disables autofixer for default value in parameter',
|
|
29
|
+
code: `
|
|
30
|
+
import styled from 'styled-components';
|
|
31
|
+
|
|
32
|
+
styled.div({
|
|
33
|
+
color: ({ myColor = '#fff' }) => myColor,
|
|
34
|
+
backgroundColor: ({ myColor = '#aaa' }) => myColor,
|
|
35
|
+
});
|
|
36
|
+
`,
|
|
37
|
+
errors: [
|
|
38
|
+
{
|
|
39
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'applies autofixer for nested selector',
|
|
45
|
+
code: `
|
|
46
|
+
import styled from 'styled-components';
|
|
47
|
+
|
|
48
|
+
styled.div({
|
|
49
|
+
img: {
|
|
50
|
+
color: (props) => props.imgColor,
|
|
51
|
+
},
|
|
52
|
+
color: (props) => props.otherColor,
|
|
53
|
+
});
|
|
54
|
+
`,
|
|
55
|
+
output: `
|
|
56
|
+
import styled from 'styled-components';
|
|
57
|
+
|
|
58
|
+
styled.div((props) => ({
|
|
59
|
+
img: {
|
|
60
|
+
color: props.imgColor,
|
|
61
|
+
},
|
|
62
|
+
color: props.otherColor,
|
|
63
|
+
}));
|
|
64
|
+
`,
|
|
65
|
+
errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
);
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { analyze } from 'eslint-scope';
|
|
2
|
+
import type {
|
|
3
|
+
ArrowFunctionExpression,
|
|
4
|
+
AssignmentProperty,
|
|
5
|
+
CallExpression,
|
|
6
|
+
Identifier,
|
|
7
|
+
MemberExpression,
|
|
8
|
+
ObjectExpression,
|
|
9
|
+
} from 'estree-jsx';
|
|
10
|
+
import { createLintRule } from '../utils/create-rule';
|
|
11
|
+
import {
|
|
12
|
+
CSS_IN_JS_IMPORTS,
|
|
13
|
+
isStyledComponents,
|
|
14
|
+
} from '../utils/is-supported-import';
|
|
15
|
+
import type { Rule, Scope, SourceCode } from 'eslint';
|
|
16
|
+
import ESTraverse from 'estraverse';
|
|
17
|
+
|
|
18
|
+
const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.styledComponents];
|
|
19
|
+
|
|
20
|
+
type PropInfo = {
|
|
21
|
+
/**
|
|
22
|
+
* Name of the prop, e.g. props, backgroundColor.
|
|
23
|
+
*/
|
|
24
|
+
propName: string;
|
|
25
|
+
usages: (IdentifierWithParent | MemberExpressionWithParent)[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type ValidPropsFound = {
|
|
29
|
+
arrowFunctionExpression: ArrowFunctionExpression;
|
|
30
|
+
props: PropInfo[];
|
|
31
|
+
valid: true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type IdentifierWithParent = Scope.Reference['identifier'] &
|
|
35
|
+
Rule.NodeParentExtension;
|
|
36
|
+
|
|
37
|
+
type MemberExpressionWithParent = MemberExpression & Rule.NodeParentExtension;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Represents some information about single line that contains some props, e.g.
|
|
41
|
+
* the arrow function in
|
|
42
|
+
*
|
|
43
|
+
* color: (props) => `mix(${props.myColor}, #fff)`
|
|
44
|
+
*
|
|
45
|
+
* or about a destructuring assignment, e.g. the arrow function in
|
|
46
|
+
*
|
|
47
|
+
* color: ({ myColor, otherColor }) => someFunction(myColor, otherColor)
|
|
48
|
+
*
|
|
49
|
+
* or something that we don't recognise, or that we don't think is valid
|
|
50
|
+
*
|
|
51
|
+
* color: (props, someOtherVariable) => someOtherVariable
|
|
52
|
+
*/
|
|
53
|
+
type PropsFound =
|
|
54
|
+
| ValidPropsFound
|
|
55
|
+
| {
|
|
56
|
+
/**
|
|
57
|
+
* Props use a syntax we don't recognise, or we don't think is valid...
|
|
58
|
+
* or perhaps something unexpected happened in ESLint and we don't feel confident enough to try and autofix anything.
|
|
59
|
+
*/
|
|
60
|
+
valid: false;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const standardizePropExpression = (
|
|
64
|
+
sourceCode: SourceCode,
|
|
65
|
+
expression: MemberExpression | Identifier,
|
|
66
|
+
): string | undefined => {
|
|
67
|
+
const propsObjectName = 'props';
|
|
68
|
+
|
|
69
|
+
if (expression.type === 'Identifier') {
|
|
70
|
+
return `${propsObjectName}.${expression.name}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
expression.type === 'MemberExpression' &&
|
|
75
|
+
expression.property.type === 'Identifier'
|
|
76
|
+
) {
|
|
77
|
+
const propertyName = sourceCode.getText(expression.property);
|
|
78
|
+
return `${propsObjectName}.${propertyName}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Cannot safely process the expression, bail out!
|
|
82
|
+
return undefined;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const parseArrowFunctionExpression = (
|
|
86
|
+
expression: ArrowFunctionExpression,
|
|
87
|
+
sourceCode: SourceCode,
|
|
88
|
+
): PropsFound => {
|
|
89
|
+
if (expression.params.length !== 1) {
|
|
90
|
+
return { valid: false };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const scopeManager = analyze(expression, {});
|
|
94
|
+
const currentScope = scopeManager.acquire(expression);
|
|
95
|
+
|
|
96
|
+
if (!currentScope) {
|
|
97
|
+
return { valid: false };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const parameter = expression.params[0];
|
|
101
|
+
const props: PropInfo[] = [];
|
|
102
|
+
|
|
103
|
+
// Don't bother handling type annotations that appear in the parameters of arrow functions.
|
|
104
|
+
//
|
|
105
|
+
// 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.
|
|
106
|
+
if ('typeAnnotation' in parameter && parameter.typeAnnotation) {
|
|
107
|
+
return { valid: false };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (parameter.type === 'ObjectPattern') {
|
|
111
|
+
if (parameter.properties.some((value) => value.type === 'RestElement')) {
|
|
112
|
+
return { valid: false };
|
|
113
|
+
}
|
|
114
|
+
const properties = parameter.properties as AssignmentProperty[];
|
|
115
|
+
|
|
116
|
+
for (const property of properties) {
|
|
117
|
+
const propertyValue = property.value;
|
|
118
|
+
// Don't bother supporting other patterns like rest elements, default values...
|
|
119
|
+
if (propertyValue.type !== 'Identifier') {
|
|
120
|
+
return { valid: false };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const variable = currentScope.variables.find(
|
|
124
|
+
(varb) => varb.name === propertyValue.name,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
128
|
+
// Something went wrong, best to just bail out.
|
|
129
|
+
return { valid: false };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const propName = standardizePropExpression(sourceCode, propertyValue);
|
|
133
|
+
|
|
134
|
+
if (!propName) {
|
|
135
|
+
return { valid: false };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
props.push({
|
|
139
|
+
propName,
|
|
140
|
+
usages: variable.references.map(
|
|
141
|
+
(reference) => reference.identifier as IdentifierWithParent,
|
|
142
|
+
),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
} else if (parameter.type === 'Identifier') {
|
|
146
|
+
const variable = currentScope.variables.find(
|
|
147
|
+
(varb) => varb.name === parameter.name,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (!variable || !variable.references || !variable.references.length) {
|
|
151
|
+
// Something went wrong, best to just bail out.
|
|
152
|
+
return { valid: false };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (const reference of variable.references) {
|
|
156
|
+
const memberExpression = (reference.identifier as IdentifierWithParent)
|
|
157
|
+
.parent;
|
|
158
|
+
if (memberExpression.type !== 'MemberExpression') {
|
|
159
|
+
// If reference.identifier.parent is not a member expression, that indicates
|
|
160
|
+
// that we have a usage of the props argument that does NOT look like
|
|
161
|
+
//
|
|
162
|
+
// (props) => props.color
|
|
163
|
+
//
|
|
164
|
+
// e.g. we might have (props) => props['...'] or (props) => `${props}px` perhaps...
|
|
165
|
+
//
|
|
166
|
+
// We have no choice but to bail out because the syntax is not what we expect.
|
|
167
|
+
return { valid: false };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const propName = standardizePropExpression(sourceCode, memberExpression);
|
|
171
|
+
|
|
172
|
+
if (!propName) {
|
|
173
|
+
return { valid: false };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
props.push({
|
|
177
|
+
propName,
|
|
178
|
+
usages: [memberExpression],
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
// Bail out for any syntax we don't recognise.
|
|
183
|
+
return { valid: false };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return { arrowFunctionExpression: expression, props, valid: true };
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const lintArguments = (
|
|
190
|
+
context: Rule.RuleContext,
|
|
191
|
+
args: CallExpression['arguments'],
|
|
192
|
+
) => {
|
|
193
|
+
for (const argument of args) {
|
|
194
|
+
if (argument.type === 'ArrowFunctionExpression') {
|
|
195
|
+
// styled.div((props) => ({ ... }))
|
|
196
|
+
// styled.div(({ color, height }) => ({ ... }))
|
|
197
|
+
//
|
|
198
|
+
// Already in the form we want.
|
|
199
|
+
continue;
|
|
200
|
+
} else if (argument.type === 'ObjectExpression') {
|
|
201
|
+
const allProps: PropsFound[] = [];
|
|
202
|
+
|
|
203
|
+
// vvvvvvv
|
|
204
|
+
// styled.div({ ... })
|
|
205
|
+
for (const property of argument.properties) {
|
|
206
|
+
if (property.type === 'SpreadElement') {
|
|
207
|
+
// e.g.
|
|
208
|
+
// styled.div({ color: 'blue', ...someStyles })
|
|
209
|
+
//
|
|
210
|
+
// If we have a spread element, we can no longer safely
|
|
211
|
+
// transform this styled/css call without a lot more effort.
|
|
212
|
+
//
|
|
213
|
+
// Best to give up...
|
|
214
|
+
return;
|
|
215
|
+
} else {
|
|
216
|
+
ESTraverse.traverse(property.value, {
|
|
217
|
+
enter: function (node, _parent) {
|
|
218
|
+
if (node.type === 'ArrowFunctionExpression') {
|
|
219
|
+
const propNames = parseArrowFunctionExpression(
|
|
220
|
+
node,
|
|
221
|
+
context.getSourceCode(),
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
if (!propNames) {
|
|
225
|
+
// We've encountered a CSS value we don't recognise, skip.
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
allProps.push(propNames);
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (!allProps.length) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (allProps.some((propUsage) => !propUsage.valid)) {
|
|
241
|
+
context.report({
|
|
242
|
+
node: argument,
|
|
243
|
+
messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
context.report({
|
|
250
|
+
node: argument,
|
|
251
|
+
messageId: 'unsupported-styled-components-prop-syntax',
|
|
252
|
+
fix(fixer) {
|
|
253
|
+
const sourceCode = context.getSourceCode();
|
|
254
|
+
return fixPropsObjectUsages(
|
|
255
|
+
fixer,
|
|
256
|
+
sourceCode,
|
|
257
|
+
argument,
|
|
258
|
+
// The
|
|
259
|
+
// allProps.some((propUsage) => !propUsage.valid)
|
|
260
|
+
// condition above should have made this type assertion unnecessary...
|
|
261
|
+
allProps as ValidPropsFound[],
|
|
262
|
+
);
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
type SectionToUpdate = {
|
|
270
|
+
identifier: Identifier | MemberExpression;
|
|
271
|
+
newCode: string;
|
|
272
|
+
startIndex: number;
|
|
273
|
+
endIndex: number;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const convertPropUsage = (
|
|
277
|
+
propUsage: ValidPropsFound,
|
|
278
|
+
sourceCode: SourceCode,
|
|
279
|
+
): string | undefined => {
|
|
280
|
+
const arrowFunctionOffset = propUsage.arrowFunctionExpression.body.loc
|
|
281
|
+
? {
|
|
282
|
+
startIndex: sourceCode.getIndexFromLoc(
|
|
283
|
+
propUsage.arrowFunctionExpression.body.loc.start,
|
|
284
|
+
),
|
|
285
|
+
endIndex: sourceCode.getIndexFromLoc(
|
|
286
|
+
propUsage.arrowFunctionExpression.body.loc.end,
|
|
287
|
+
),
|
|
288
|
+
}
|
|
289
|
+
: undefined;
|
|
290
|
+
|
|
291
|
+
if (!arrowFunctionOffset) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const sectionsToUpdate: SectionToUpdate[] = [];
|
|
296
|
+
const newCode: string[] = [];
|
|
297
|
+
|
|
298
|
+
for (const prop of propUsage.props) {
|
|
299
|
+
for (const usage of prop.usages) {
|
|
300
|
+
if (!usage.loc) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
sectionsToUpdate.push({
|
|
305
|
+
identifier: usage,
|
|
306
|
+
newCode: prop.propName,
|
|
307
|
+
startIndex: sourceCode.getIndexFromLoc(usage.loc.start),
|
|
308
|
+
endIndex: sourceCode.getIndexFromLoc(usage.loc.end),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
sectionsToUpdate.sort((a, b) => {
|
|
314
|
+
if (!a.identifier.loc || !b.identifier.loc) {
|
|
315
|
+
return 0;
|
|
316
|
+
}
|
|
317
|
+
return (
|
|
318
|
+
a.identifier.loc.start.line - b.identifier.loc.start.line ||
|
|
319
|
+
a.identifier.loc.start.column - b.identifier.loc.start.column
|
|
320
|
+
);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
let prevEndIndex = arrowFunctionOffset.startIndex;
|
|
324
|
+
for (const section of sectionsToUpdate) {
|
|
325
|
+
if (prevEndIndex > section.startIndex || prevEndIndex >= section.endIndex) {
|
|
326
|
+
// Props overlap! This is unexpected, bail out.
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
newCode.push(sourceCode.getText().slice(prevEndIndex, section.startIndex));
|
|
331
|
+
newCode.push(section.newCode);
|
|
332
|
+
|
|
333
|
+
prevEndIndex = section.endIndex;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
newCode.push(
|
|
337
|
+
sourceCode.getText().slice(prevEndIndex, arrowFunctionOffset.endIndex),
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
return newCode.join('');
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const fixPropsObjectUsages = (
|
|
344
|
+
fixer: Rule.RuleFixer,
|
|
345
|
+
sourceCode: SourceCode,
|
|
346
|
+
expression: ObjectExpression,
|
|
347
|
+
allProps: ValidPropsFound[],
|
|
348
|
+
): Rule.Fix[] => {
|
|
349
|
+
const fixers = [
|
|
350
|
+
// Wrap the object (containing the CSS properties and values) in an arrow function,
|
|
351
|
+
// so it becomes something like
|
|
352
|
+
//
|
|
353
|
+
// styled.div(
|
|
354
|
+
// (props) => ({
|
|
355
|
+
// color: ...
|
|
356
|
+
// })
|
|
357
|
+
// );
|
|
358
|
+
fixer.insertTextBefore(expression, '(props) => ('),
|
|
359
|
+
fixer.insertTextAfter(expression, ')'),
|
|
360
|
+
];
|
|
361
|
+
|
|
362
|
+
for (const propUsage of allProps) {
|
|
363
|
+
const arrowFunctionExpression = propUsage.arrowFunctionExpression;
|
|
364
|
+
const newCode = convertPropUsage(propUsage, sourceCode);
|
|
365
|
+
|
|
366
|
+
if (newCode) {
|
|
367
|
+
fixers.push(fixer.replaceText(arrowFunctionExpression, newCode));
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return fixers;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export const rule = createLintRule({
|
|
375
|
+
meta: {
|
|
376
|
+
name: 'convert-props-syntax',
|
|
377
|
+
docs: {
|
|
378
|
+
description:
|
|
379
|
+
'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`.',
|
|
380
|
+
recommended: true,
|
|
381
|
+
severity: 'error',
|
|
382
|
+
},
|
|
383
|
+
fixable: 'code',
|
|
384
|
+
messages: {
|
|
385
|
+
'unsupported-styled-components-prop-syntax':
|
|
386
|
+
'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.',
|
|
387
|
+
|
|
388
|
+
'unsupported-styled-components-prop-syntax-no-autofixer':
|
|
389
|
+
'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.',
|
|
390
|
+
},
|
|
391
|
+
type: 'problem',
|
|
392
|
+
},
|
|
393
|
+
create(context) {
|
|
394
|
+
return {
|
|
395
|
+
'CallExpression[callee.type="MemberExpression"]': (
|
|
396
|
+
node: CallExpression,
|
|
397
|
+
) => {
|
|
398
|
+
const references = context.getScope().references;
|
|
399
|
+
const callee =
|
|
400
|
+
node.callee.type === 'MemberExpression'
|
|
401
|
+
? node.callee.object
|
|
402
|
+
: node.callee;
|
|
403
|
+
|
|
404
|
+
if (!isStyledComponents(callee, references, IMPORT_SOURCES)) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
lintArguments(context, node.arguments);
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
export default rule;
|
|
@@ -1,13 +1,15 @@
|
|
|
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
|
|
|
10
11
|
export default {
|
|
12
|
+
'convert-props-syntax': convertPropsSyntax,
|
|
11
13
|
'local-cx-xcss': localCxXcss,
|
|
12
14
|
'no-classname-prop': noClassnameProp,
|
|
13
15
|
'no-important-styles': noImportantStyles,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This contains utility functions copied over from `packages/design-system/eslint-plugin/src/rules/utils/` until we move the relevant eslint rules over to `packages/design-system/eslint-plugin-ui-styling-standard`.
|
|
@@ -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
|
import type { Rule, Scope } from 'eslint';
|
|
3
10
|
import type { CallExpression } from 'estree';
|