@atlaskit/eslint-plugin-ui-styling-standard 0.7.2 → 0.8.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 +10 -0
- package/README.md +2 -1
- package/constellation/index/overview.mdx +2 -1
- package/constellation/no-nested-selectors/usage.mdx +60 -0
- package/dist/cjs/presets/all.codegen.js +3 -4
- package/dist/cjs/presets/recommended.codegen.js +3 -4
- package/dist/cjs/rules/external-rules.js +1 -27
- package/dist/cjs/rules/index.codegen.js +4 -2
- package/dist/cjs/rules/no-nested-selectors/index.js +82 -0
- package/dist/es2019/presets/all.codegen.js +3 -4
- package/dist/es2019/presets/recommended.codegen.js +3 -4
- package/dist/es2019/rules/external-rules.js +1 -27
- package/dist/es2019/rules/index.codegen.js +4 -2
- package/dist/es2019/rules/no-nested-selectors/index.js +71 -0
- package/dist/esm/presets/all.codegen.js +3 -4
- package/dist/esm/presets/recommended.codegen.js +3 -4
- package/dist/esm/rules/external-rules.js +1 -27
- package/dist/esm/rules/index.codegen.js +4 -2
- package/dist/esm/rules/no-nested-selectors/index.js +76 -0
- package/dist/types/index.codegen.d.ts +4 -6
- package/dist/types/presets/all.codegen.d.ts +3 -4
- package/dist/types/presets/recommended.codegen.d.ts +3 -4
- package/dist/types/rules/index.codegen.d.ts +1 -0
- package/dist/types/rules/no-nested-selectors/index.d.ts +2 -0
- package/dist/types-ts4.5/index.codegen.d.ts +4 -6
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -4
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -4
- package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
- package/dist/types-ts4.5/rules/no-nested-selectors/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/presets/all.codegen.tsx +3 -13
- package/src/presets/recommended.codegen.tsx +3 -13
- package/src/rules/external-rules.tsx +0 -30
- package/src/rules/index.codegen.tsx +3 -1
- package/src/rules/no-nested-selectors/README.md +58 -0
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +236 -0
- package/src/rules/no-nested-selectors/index.tsx +115 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'no-nested-selectors',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [
|
|
10
|
+
{
|
|
11
|
+
name: 'Basic valid test',
|
|
12
|
+
code: `
|
|
13
|
+
import { css } from '@compiled/react';
|
|
14
|
+
const styles = css({
|
|
15
|
+
display: 'flex',
|
|
16
|
+
flexDirection: 'column',
|
|
17
|
+
});
|
|
18
|
+
`,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'Basic valid test using emotion',
|
|
22
|
+
code: `
|
|
23
|
+
import styled from '@emotion/styled';
|
|
24
|
+
const Container = styled.div({
|
|
25
|
+
display: 'flex',
|
|
26
|
+
flexDirection: 'column'
|
|
27
|
+
})
|
|
28
|
+
`,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Custom import sources (subtractive)',
|
|
32
|
+
code: `
|
|
33
|
+
import { css } from '@compiled/react';
|
|
34
|
+
const styles = css({
|
|
35
|
+
h2: {
|
|
36
|
+
fontSize: '1.5rem',
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
`,
|
|
40
|
+
options: [
|
|
41
|
+
{
|
|
42
|
+
importSources: [],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'Styled div with no selectors',
|
|
48
|
+
code: `
|
|
49
|
+
import { styled } from '@compiled/react';
|
|
50
|
+
const Component = styled.div({
|
|
51
|
+
padding: 0,
|
|
52
|
+
display: 'flex',
|
|
53
|
+
flexDirection: 'column',
|
|
54
|
+
boxSizing: 'border-box',
|
|
55
|
+
});
|
|
56
|
+
`,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'Pseudo element selectors',
|
|
60
|
+
code: `
|
|
61
|
+
import { styled } from '@compiled/react';
|
|
62
|
+
const Component = styled.div({
|
|
63
|
+
'&:hover': {
|
|
64
|
+
color: 'red'
|
|
65
|
+
},
|
|
66
|
+
'&:focus, :last-child': {
|
|
67
|
+
color: 'yellow'
|
|
68
|
+
},
|
|
69
|
+
'&:first-of-type:last-child': {
|
|
70
|
+
color: 'green'
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
`,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: 'Next sibling selector on non-HTML element',
|
|
77
|
+
code: `
|
|
78
|
+
import { styled } from '@compiled/react';
|
|
79
|
+
styled.div({
|
|
80
|
+
'& + &': {
|
|
81
|
+
color: 'orange'
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
`,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: 'Subsequent sibling selector on non-HTML element',
|
|
88
|
+
code: `
|
|
89
|
+
import { styled } from '@compiled/react';
|
|
90
|
+
styled.div({
|
|
91
|
+
'& ~ &': {
|
|
92
|
+
color: 'orange'
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
`,
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
invalid: [
|
|
99
|
+
{
|
|
100
|
+
name: 'Universal selector',
|
|
101
|
+
code: `
|
|
102
|
+
import { styled } from '@compiled/react';
|
|
103
|
+
const Component = styled.div({
|
|
104
|
+
'*': {
|
|
105
|
+
padding: 0,
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
`,
|
|
109
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'Class selector',
|
|
113
|
+
code: `
|
|
114
|
+
import { styled } from '@compiled/react';
|
|
115
|
+
const Component = styled.div({
|
|
116
|
+
'.myclass': {
|
|
117
|
+
padding: 0,
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
`,
|
|
121
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'ID selector',
|
|
125
|
+
code: `
|
|
126
|
+
import { styled } from '@compiled/react';
|
|
127
|
+
const Component = styled.div({
|
|
128
|
+
'#myid': {
|
|
129
|
+
padding: 0,
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
`,
|
|
133
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'HTML element selector',
|
|
137
|
+
code: `
|
|
138
|
+
import { styled } from '@compiled/react';
|
|
139
|
+
const Component = styled.div({
|
|
140
|
+
h2: {
|
|
141
|
+
fontSize: '1.5rem',
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
`,
|
|
145
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
name: 'Data-component-selector',
|
|
149
|
+
code: `
|
|
150
|
+
import { styled } from '@compiled/react';
|
|
151
|
+
const Component = styled.div({
|
|
152
|
+
'[data-component-selector="my.button"]': {
|
|
153
|
+
color: 'blue',
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
`,
|
|
157
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: 'Grouping selector',
|
|
161
|
+
code: `
|
|
162
|
+
import { styled } from '@compiled/react';
|
|
163
|
+
const Component = styled.div({
|
|
164
|
+
'div, p, span': {
|
|
165
|
+
fontSize: '1.5rem',
|
|
166
|
+
},
|
|
167
|
+
color: 'blue',
|
|
168
|
+
backgroundColor: 'red',
|
|
169
|
+
});
|
|
170
|
+
`,
|
|
171
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'Selector including a variable',
|
|
175
|
+
code: `
|
|
176
|
+
import { styled } from '@compiled/react';
|
|
177
|
+
styled.div({
|
|
178
|
+
[\`\${SOME_VAR} > *\`]: {
|
|
179
|
+
color: 'red'
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
`,
|
|
183
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'Current element selector',
|
|
187
|
+
code: `
|
|
188
|
+
import { styled } from '@compiled/react';
|
|
189
|
+
styled.div({
|
|
190
|
+
color: 'darkorchid',
|
|
191
|
+
'.name &': {
|
|
192
|
+
color: 'orange'
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
`,
|
|
196
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: 'Child combinator',
|
|
200
|
+
code: `
|
|
201
|
+
import { styled } from '@compiled/react';
|
|
202
|
+
styled.div({
|
|
203
|
+
'& > &': {
|
|
204
|
+
color: 'orange'
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
`,
|
|
208
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: 'Column combinator',
|
|
212
|
+
code: `
|
|
213
|
+
import { styled } from '@compiled/react';
|
|
214
|
+
styled.div({
|
|
215
|
+
'col.selected||td': {
|
|
216
|
+
color: 'orange'
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
`,
|
|
220
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
name: 'Namespace selector',
|
|
224
|
+
code: `
|
|
225
|
+
import { styled } from '@compiled/react';
|
|
226
|
+
styled.div({
|
|
227
|
+
'myNameSpace|a': {
|
|
228
|
+
color: 'orange'
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
`,
|
|
232
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
},
|
|
236
|
+
);
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Property } from 'estree';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isStyled,
|
|
5
|
+
isCss,
|
|
6
|
+
isKeyframes,
|
|
7
|
+
isCssMap,
|
|
8
|
+
isXcss,
|
|
9
|
+
getImportSources,
|
|
10
|
+
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
11
|
+
import { createLintRule } from '../utils/create-rule';
|
|
12
|
+
import { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
13
|
+
|
|
14
|
+
const schema: JSONSchema4 = [
|
|
15
|
+
{
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
importSources: {
|
|
19
|
+
type: 'array',
|
|
20
|
+
items: { type: 'string' },
|
|
21
|
+
uniqueItems: true,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const getCssSelector = (node: Property): string | null => {
|
|
28
|
+
if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
|
|
29
|
+
return node.key.value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
|
|
33
|
+
return node.key.name;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (node.key.type === 'TemplateLiteral') {
|
|
37
|
+
return node.key.quasis.map((quasi) => quasi.value.raw).join(' ');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const rule = createLintRule({
|
|
44
|
+
meta: {
|
|
45
|
+
name: 'no-nested-selectors',
|
|
46
|
+
docs: {
|
|
47
|
+
description: 'Prevents usage of nested selectors within css styling',
|
|
48
|
+
recommended: true,
|
|
49
|
+
severity: 'warn',
|
|
50
|
+
},
|
|
51
|
+
messages: {
|
|
52
|
+
'no-nested-selectors':
|
|
53
|
+
'Please avoid setting styles for child elements or elements that require context from other elements.',
|
|
54
|
+
},
|
|
55
|
+
type: 'problem',
|
|
56
|
+
schema,
|
|
57
|
+
},
|
|
58
|
+
create(context) {
|
|
59
|
+
const importSources = getImportSources(context);
|
|
60
|
+
return {
|
|
61
|
+
'CallExpression Property': (node: Property): void => {
|
|
62
|
+
const { references } = context.getScope();
|
|
63
|
+
const ancestors = context.getAncestors();
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
ancestors.every(
|
|
67
|
+
(ancestor) =>
|
|
68
|
+
ancestor.type !== 'CallExpression' ||
|
|
69
|
+
!ancestor?.callee ||
|
|
70
|
+
!(
|
|
71
|
+
isCss(ancestor.callee, references, importSources) ||
|
|
72
|
+
isStyled(ancestor.callee, references, importSources) ||
|
|
73
|
+
isKeyframes(ancestor.callee, references, importSources) ||
|
|
74
|
+
isCssMap(ancestor.callee, references, importSources) ||
|
|
75
|
+
isXcss(ancestor.callee, references, importSources)
|
|
76
|
+
),
|
|
77
|
+
)
|
|
78
|
+
) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (node.value.type !== 'ObjectExpression') {
|
|
83
|
+
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const cssSelector = getCssSelector(node);
|
|
88
|
+
if (cssSelector === null) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const tokens = cssSelector.split(/[, ]+/);
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
!tokens.every(
|
|
96
|
+
(token) =>
|
|
97
|
+
token.length === 0 ||
|
|
98
|
+
token.startsWith('&') ||
|
|
99
|
+
token.startsWith(':') ||
|
|
100
|
+
token.includes('+') ||
|
|
101
|
+
token.includes('~') ||
|
|
102
|
+
token.endsWith('&'),
|
|
103
|
+
)
|
|
104
|
+
) {
|
|
105
|
+
context.report({
|
|
106
|
+
messageId: 'no-nested-selectors',
|
|
107
|
+
node: node.key,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
export default rule;
|