@atlaskit/eslint-plugin-ui-styling-standard 0.15.0 → 0.15.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 +189 -172
- package/constellation/atlaskit-theme/usage.mdx +17 -17
- package/constellation/convert-props-syntax/usage.mdx +14 -14
- package/constellation/enforce-style-prop/usage.mdx +17 -17
- package/constellation/index/overview.mdx +70 -70
- package/constellation/local-cx-xcss/usage.mdx +4 -4
- package/constellation/no-array-arguments/usage.mdx +7 -7
- package/constellation/no-container-queries/usage.mdx +14 -14
- package/constellation/no-dynamic-styles/usage.mdx +19 -19
- package/constellation/no-exported-styles/usage.mdx +15 -15
- package/constellation/no-global-styles/usage.mdx +13 -13
- package/constellation/no-important-styles/usage.mdx +2 -2
- package/constellation/no-imported-style-values/usage.mdx +16 -16
- package/constellation/no-nested-selectors/usage.mdx +18 -18
- package/constellation/no-styled/usage.mdx +6 -6
- package/constellation/no-unsafe-selectors/usage.mdx +90 -90
- package/constellation/no-unsafe-values/usage.mdx +33 -33
- package/dist/cjs/index.codegen.js +1 -1
- package/dist/cjs/presets/all.codegen.js +1 -1
- package/dist/cjs/presets/recommended.codegen.js +1 -1
- package/dist/cjs/rules/index.codegen.js +1 -1
- package/dist/cjs/rules/no-nested-selectors/index.js +68 -49
- package/dist/cjs/rules/no-unsafe-selectors/index.js +4 -1
- package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +10 -1
- package/dist/es2019/index.codegen.js +1 -1
- package/dist/es2019/presets/all.codegen.js +1 -1
- package/dist/es2019/presets/recommended.codegen.js +1 -1
- package/dist/es2019/rules/index.codegen.js +1 -1
- package/dist/es2019/rules/no-nested-selectors/index.js +63 -45
- package/dist/es2019/rules/no-unsafe-selectors/index.js +4 -1
- package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +10 -1
- package/dist/esm/index.codegen.js +1 -1
- package/dist/esm/presets/all.codegen.js +1 -1
- package/dist/esm/presets/recommended.codegen.js +1 -1
- package/dist/esm/rules/index.codegen.js +1 -1
- package/dist/esm/rules/no-nested-selectors/index.js +64 -49
- package/dist/esm/rules/no-unsafe-selectors/index.js +4 -1
- package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +10 -1
- package/dist/types/presets/all.codegen.d.ts +1 -1
- package/dist/types/presets/recommended.codegen.d.ts +1 -1
- package/dist/types/rules/no-unsafe-selectors/lint-selector.d.ts +1 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +1 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +1 -1
- package/dist/types-ts4.5/rules/no-unsafe-selectors/lint-selector.d.ts +1 -0
- package/package.json +2 -4
- package/src/index.codegen.tsx +3 -3
- package/src/presets/all.codegen.tsx +70 -73
- package/src/presets/recommended.codegen.tsx +70 -73
- package/src/rules/index.codegen.tsx +18 -18
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +93 -56
- package/src/rules/no-nested-selectors/index.tsx +92 -105
- package/src/rules/no-unsafe-selectors/__tests__/no-ambiguous-pseudos.test.tsx +211 -201
- package/src/rules/no-unsafe-selectors/index.tsx +174 -177
- package/src/rules/no-unsafe-selectors/lint-selector.tsx +128 -141
- package/tsconfig.app.json +45 -45
- package/tsconfig.dev.json +43 -43
|
@@ -1,112 +1,99 @@
|
|
|
1
1
|
import type { Property } from 'estree';
|
|
2
|
+
import cssSelectorParser, { type Selector, isNesting, isPseudo } from 'postcss-selector-parser';
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
},
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
const getCssSelector = (node: Property): string | null => {
|
|
26
|
-
if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
|
|
27
|
-
return node.key.value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
|
|
31
|
-
return node.key.name;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (node.key.type === 'TemplateLiteral') {
|
|
35
|
-
return node.key.quasis.map((quasi) => quasi.value.raw).join(' ');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return null;
|
|
4
|
+
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
5
|
+
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
6
|
+
|
|
7
|
+
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
8
|
+
|
|
9
|
+
const getCssSelector = (key: Property['key']): string | null => {
|
|
10
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
11
|
+
return key.value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (key.type === 'Identifier' && typeof key.name === 'string') {
|
|
15
|
+
return key.name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (key.type === 'TemplateLiteral') {
|
|
19
|
+
return key.quasis.map((quasi) => quasi.value.raw).join(' ');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return null;
|
|
39
23
|
};
|
|
40
24
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
25
|
+
function isAllowedSelector(selector: Selector): boolean {
|
|
26
|
+
const [head, ...tail] = selector.nodes;
|
|
27
|
+
|
|
28
|
+
// The first node must be a nesting selector (`&`) or pseudo-selector.
|
|
29
|
+
const isHeadAllowed = isNesting(head) || isPseudo(head);
|
|
30
|
+
|
|
31
|
+
// All remaining nodes must be pseudo selectors.
|
|
32
|
+
const isTailAllowed = tail.every(isPseudo);
|
|
33
|
+
|
|
34
|
+
return isHeadAllowed && isTailAllowed;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const cssSelectorProcessor = cssSelectorParser();
|
|
38
|
+
|
|
39
|
+
export const rule = createLintRuleWithTypedConfig({
|
|
40
|
+
meta: {
|
|
41
|
+
name: 'no-nested-selectors',
|
|
42
|
+
docs: {
|
|
43
|
+
description: 'Prevents usage of nested selectors within css styling',
|
|
44
|
+
recommended: true,
|
|
45
|
+
severity: 'warn',
|
|
46
|
+
},
|
|
47
|
+
messages: {
|
|
48
|
+
'no-nested-selectors':
|
|
49
|
+
'Please avoid setting styles for child elements or elements that require context from other elements.',
|
|
50
|
+
},
|
|
51
|
+
type: 'problem',
|
|
52
|
+
schema: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
importSources,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
create(context, { importSources }) {
|
|
60
|
+
return {
|
|
61
|
+
CallExpression(node) {
|
|
62
|
+
const { references } = context.getScope();
|
|
63
|
+
|
|
64
|
+
walkStyleProperties(node, references, importSources, ({ key, value }) => {
|
|
65
|
+
if (value.type !== 'ObjectExpression') {
|
|
66
|
+
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const selectorText = getCssSelector(key);
|
|
71
|
+
if (selectorText === null) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Ignore at-rules
|
|
76
|
+
if (selectorText.includes('@')) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
82
|
+
|
|
83
|
+
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
84
|
+
context.report({
|
|
85
|
+
messageId: 'no-nested-selectors',
|
|
86
|
+
node: key,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
} catch (_) {
|
|
90
|
+
// If it is not parsable then `no-unsafe-selectors` will give it an error,
|
|
91
|
+
// we can ignore it here.
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
110
97
|
});
|
|
111
98
|
|
|
112
99
|
export default rule;
|