@atlaskit/eslint-plugin-ui-styling-standard 0.15.1 → 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 +9 -0
- package/dist/cjs/rules/no-nested-selectors/index.js +68 -49
- package/dist/es2019/rules/no-nested-selectors/index.js +63 -45
- package/dist/esm/rules/no-nested-selectors/index.js +64 -49
- package/package.json +1 -1
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +93 -56
- package/src/rules/no-nested-selectors/index.tsx +92 -105
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-ui-styling-standard
|
|
2
2
|
|
|
3
|
+
## 0.15.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#105238](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/105238)
|
|
8
|
+
[`8840e6d3180d`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8840e6d3180d) -
|
|
9
|
+
Fixes `no-nested-selectors` to handle additional APIs (such as `cssMap` and `keyframes`) as well
|
|
10
|
+
as catch more cases of nested selectors.
|
|
11
|
+
|
|
3
12
|
## 0.15.1
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -1,38 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
3
5
|
Object.defineProperty(exports, "__esModule", {
|
|
4
6
|
value: true
|
|
5
7
|
});
|
|
6
8
|
exports.rule = exports.default = void 0;
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
uniqueItems: true
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}];
|
|
21
|
-
var getCssSelector = function getCssSelector(node) {
|
|
22
|
-
if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
|
|
23
|
-
return node.key.value;
|
|
9
|
+
var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
|
|
10
|
+
var _postcssSelectorParser = _interopRequireWildcard(require("postcss-selector-parser"));
|
|
11
|
+
var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
|
|
12
|
+
var _schema = require("@atlaskit/eslint-utils/schema");
|
|
13
|
+
var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
|
|
14
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
15
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
16
|
+
var getCssSelector = function getCssSelector(key) {
|
|
17
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
18
|
+
return key.value;
|
|
24
19
|
}
|
|
25
|
-
if (
|
|
26
|
-
return
|
|
20
|
+
if (key.type === 'Identifier' && typeof key.name === 'string') {
|
|
21
|
+
return key.name;
|
|
27
22
|
}
|
|
28
|
-
if (
|
|
29
|
-
return
|
|
23
|
+
if (key.type === 'TemplateLiteral') {
|
|
24
|
+
return key.quasis.map(function (quasi) {
|
|
30
25
|
return quasi.value.raw;
|
|
31
26
|
}).join(' ');
|
|
32
27
|
}
|
|
33
28
|
return null;
|
|
34
29
|
};
|
|
35
|
-
|
|
30
|
+
function isAllowedSelector(selector) {
|
|
31
|
+
var _selector$nodes = (0, _toArray2.default)(selector.nodes),
|
|
32
|
+
head = _selector$nodes[0],
|
|
33
|
+
tail = _selector$nodes.slice(1);
|
|
34
|
+
|
|
35
|
+
// The first node must be a nesting selector (`&`) or pseudo-selector.
|
|
36
|
+
var isHeadAllowed = (0, _postcssSelectorParser.isNesting)(head) || (0, _postcssSelectorParser.isPseudo)(head);
|
|
37
|
+
|
|
38
|
+
// All remaining nodes must be pseudo selectors.
|
|
39
|
+
var isTailAllowed = tail.every(_postcssSelectorParser.isPseudo);
|
|
40
|
+
return isHeadAllowed && isTailAllowed;
|
|
41
|
+
}
|
|
42
|
+
var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
|
|
43
|
+
var rule = exports.rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
|
|
36
44
|
meta: {
|
|
37
45
|
name: 'no-nested-selectors',
|
|
38
46
|
docs: {
|
|
@@ -44,37 +52,48 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
|
|
|
44
52
|
'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
|
|
45
53
|
},
|
|
46
54
|
type: 'problem',
|
|
47
|
-
schema:
|
|
55
|
+
schema: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
importSources: _schema.importSources
|
|
59
|
+
}
|
|
60
|
+
}
|
|
48
61
|
},
|
|
49
|
-
create: function create(context) {
|
|
50
|
-
var importSources =
|
|
62
|
+
create: function create(context, _ref) {
|
|
63
|
+
var importSources = _ref.importSources;
|
|
51
64
|
return {
|
|
52
|
-
|
|
65
|
+
CallExpression: function CallExpression(node) {
|
|
53
66
|
var _context$getScope = context.getScope(),
|
|
54
67
|
references = _context$getScope.references;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
(0, _walkStyleProperties.walkStyleProperties)(node, references, importSources, function (_ref2) {
|
|
69
|
+
var key = _ref2.key,
|
|
70
|
+
value = _ref2.value;
|
|
71
|
+
if (value.type !== 'ObjectExpression') {
|
|
72
|
+
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
var selectorText = getCssSelector(key);
|
|
76
|
+
if (selectorText === null) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Ignore at-rules
|
|
81
|
+
if (selectorText.includes('@')) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
var selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
86
|
+
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
87
|
+
context.report({
|
|
88
|
+
messageId: 'no-nested-selectors',
|
|
89
|
+
node: key
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
} catch (_) {
|
|
93
|
+
// If it is not parsable then `no-unsafe-selectors` will give it an error,
|
|
94
|
+
// we can ignore it here.
|
|
95
|
+
}
|
|
96
|
+
});
|
|
78
97
|
}
|
|
79
98
|
};
|
|
80
99
|
}
|
|
@@ -1,30 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
items: {
|
|
9
|
-
type: 'string'
|
|
10
|
-
},
|
|
11
|
-
uniqueItems: true
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}];
|
|
15
|
-
const getCssSelector = node => {
|
|
16
|
-
if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
|
|
17
|
-
return node.key.value;
|
|
1
|
+
import cssSelectorParser, { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
2
|
+
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
3
|
+
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
4
|
+
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
5
|
+
const getCssSelector = key => {
|
|
6
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
7
|
+
return key.value;
|
|
18
8
|
}
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
9
|
+
if (key.type === 'Identifier' && typeof key.name === 'string') {
|
|
10
|
+
return key.name;
|
|
21
11
|
}
|
|
22
|
-
if (
|
|
23
|
-
return
|
|
12
|
+
if (key.type === 'TemplateLiteral') {
|
|
13
|
+
return key.quasis.map(quasi => quasi.value.raw).join(' ');
|
|
24
14
|
}
|
|
25
15
|
return null;
|
|
26
16
|
};
|
|
27
|
-
|
|
17
|
+
function isAllowedSelector(selector) {
|
|
18
|
+
const [head, ...tail] = selector.nodes;
|
|
19
|
+
|
|
20
|
+
// The first node must be a nesting selector (`&`) or pseudo-selector.
|
|
21
|
+
const isHeadAllowed = isNesting(head) || isPseudo(head);
|
|
22
|
+
|
|
23
|
+
// All remaining nodes must be pseudo selectors.
|
|
24
|
+
const isTailAllowed = tail.every(isPseudo);
|
|
25
|
+
return isHeadAllowed && isTailAllowed;
|
|
26
|
+
}
|
|
27
|
+
const cssSelectorProcessor = cssSelectorParser();
|
|
28
|
+
export const rule = createLintRuleWithTypedConfig({
|
|
28
29
|
meta: {
|
|
29
30
|
name: 'no-nested-selectors',
|
|
30
31
|
docs: {
|
|
@@ -36,34 +37,51 @@ export const rule = createLintRule({
|
|
|
36
37
|
'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
|
|
37
38
|
},
|
|
38
39
|
type: 'problem',
|
|
39
|
-
schema
|
|
40
|
+
schema: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
importSources
|
|
44
|
+
}
|
|
45
|
+
}
|
|
40
46
|
},
|
|
41
|
-
create(context
|
|
42
|
-
|
|
47
|
+
create(context, {
|
|
48
|
+
importSources
|
|
49
|
+
}) {
|
|
43
50
|
return {
|
|
44
|
-
|
|
51
|
+
CallExpression(node) {
|
|
45
52
|
const {
|
|
46
53
|
references
|
|
47
54
|
} = context.getScope();
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
walkStyleProperties(node, references, importSources, ({
|
|
56
|
+
key,
|
|
57
|
+
value
|
|
58
|
+
}) => {
|
|
59
|
+
if (value.type !== 'ObjectExpression') {
|
|
60
|
+
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const selectorText = getCssSelector(key);
|
|
64
|
+
if (selectorText === null) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Ignore at-rules
|
|
69
|
+
if (selectorText.includes('@')) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
74
|
+
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
75
|
+
context.report({
|
|
76
|
+
messageId: 'no-nested-selectors',
|
|
77
|
+
node: key
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
} catch (_) {
|
|
81
|
+
// If it is not parsable then `no-unsafe-selectors` will give it an error,
|
|
82
|
+
// we can ignore it here.
|
|
83
|
+
}
|
|
84
|
+
});
|
|
67
85
|
}
|
|
68
86
|
};
|
|
69
87
|
}
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
type: 'string'
|
|
10
|
-
},
|
|
11
|
-
uniqueItems: true
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}];
|
|
15
|
-
var getCssSelector = function getCssSelector(node) {
|
|
16
|
-
if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
|
|
17
|
-
return node.key.value;
|
|
1
|
+
import _toArray from "@babel/runtime/helpers/toArray";
|
|
2
|
+
import cssSelectorParser, { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
3
|
+
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
4
|
+
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
5
|
+
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
6
|
+
var getCssSelector = function getCssSelector(key) {
|
|
7
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
8
|
+
return key.value;
|
|
18
9
|
}
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
10
|
+
if (key.type === 'Identifier' && typeof key.name === 'string') {
|
|
11
|
+
return key.name;
|
|
21
12
|
}
|
|
22
|
-
if (
|
|
23
|
-
return
|
|
13
|
+
if (key.type === 'TemplateLiteral') {
|
|
14
|
+
return key.quasis.map(function (quasi) {
|
|
24
15
|
return quasi.value.raw;
|
|
25
16
|
}).join(' ');
|
|
26
17
|
}
|
|
27
18
|
return null;
|
|
28
19
|
};
|
|
29
|
-
|
|
20
|
+
function isAllowedSelector(selector) {
|
|
21
|
+
var _selector$nodes = _toArray(selector.nodes),
|
|
22
|
+
head = _selector$nodes[0],
|
|
23
|
+
tail = _selector$nodes.slice(1);
|
|
24
|
+
|
|
25
|
+
// The first node must be a nesting selector (`&`) or pseudo-selector.
|
|
26
|
+
var isHeadAllowed = isNesting(head) || isPseudo(head);
|
|
27
|
+
|
|
28
|
+
// All remaining nodes must be pseudo selectors.
|
|
29
|
+
var isTailAllowed = tail.every(isPseudo);
|
|
30
|
+
return isHeadAllowed && isTailAllowed;
|
|
31
|
+
}
|
|
32
|
+
var cssSelectorProcessor = cssSelectorParser();
|
|
33
|
+
export var rule = createLintRuleWithTypedConfig({
|
|
30
34
|
meta: {
|
|
31
35
|
name: 'no-nested-selectors',
|
|
32
36
|
docs: {
|
|
@@ -38,37 +42,48 @@ export var rule = createLintRule({
|
|
|
38
42
|
'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
|
|
39
43
|
},
|
|
40
44
|
type: 'problem',
|
|
41
|
-
schema:
|
|
45
|
+
schema: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
importSources: importSources
|
|
49
|
+
}
|
|
50
|
+
}
|
|
42
51
|
},
|
|
43
|
-
create: function create(context) {
|
|
44
|
-
var importSources =
|
|
52
|
+
create: function create(context, _ref) {
|
|
53
|
+
var importSources = _ref.importSources;
|
|
45
54
|
return {
|
|
46
|
-
|
|
55
|
+
CallExpression: function CallExpression(node) {
|
|
47
56
|
var _context$getScope = context.getScope(),
|
|
48
57
|
references = _context$getScope.references;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
58
|
+
walkStyleProperties(node, references, importSources, function (_ref2) {
|
|
59
|
+
var key = _ref2.key,
|
|
60
|
+
value = _ref2.value;
|
|
61
|
+
if (value.type !== 'ObjectExpression') {
|
|
62
|
+
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
var selectorText = getCssSelector(key);
|
|
66
|
+
if (selectorText === null) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Ignore at-rules
|
|
71
|
+
if (selectorText.includes('@')) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
var selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
76
|
+
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
77
|
+
context.report({
|
|
78
|
+
messageId: 'no-nested-selectors',
|
|
79
|
+
node: key
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
} catch (_) {
|
|
83
|
+
// If it is not parsable then `no-unsafe-selectors` will give it an error,
|
|
84
|
+
// we can ignore it here.
|
|
85
|
+
}
|
|
86
|
+
});
|
|
72
87
|
}
|
|
73
88
|
};
|
|
74
89
|
}
|
package/package.json
CHANGED
|
@@ -72,28 +72,6 @@ typescriptEslintTester.run(
|
|
|
72
72
|
});
|
|
73
73
|
`,
|
|
74
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
75
|
{
|
|
98
76
|
name: 'Skips @ queries',
|
|
99
77
|
code: `
|
|
@@ -109,40 +87,6 @@ typescriptEslintTester.run(
|
|
|
109
87
|
});
|
|
110
88
|
`,
|
|
111
89
|
},
|
|
112
|
-
{
|
|
113
|
-
name: 'Skip cssMap call',
|
|
114
|
-
code: `
|
|
115
|
-
import { cssMap } from '@atlaskit/css';
|
|
116
|
-
|
|
117
|
-
const styles = cssMap({
|
|
118
|
-
success: {
|
|
119
|
-
color: 'var(--ds-text-inverse)',
|
|
120
|
-
backgroundColor: 'var(--ds-background-success-bold)',
|
|
121
|
-
'&:hover': {
|
|
122
|
-
backgroundColor: 'var(--ds-background-success-bold-hovered)',
|
|
123
|
-
},
|
|
124
|
-
'&:active': {
|
|
125
|
-
backgroundColor: 'var(--ds-background-success-bold-pressed)',
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
`,
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
name: 'Skip keyframes call',
|
|
133
|
-
code: `
|
|
134
|
-
import { keyframes } from '@emotion/react';
|
|
135
|
-
|
|
136
|
-
const keyFrameStyles = keyframes({
|
|
137
|
-
'from, to': {
|
|
138
|
-
opacity: 0,
|
|
139
|
-
},
|
|
140
|
-
'50%': {
|
|
141
|
-
opacity: 1,
|
|
142
|
-
},
|
|
143
|
-
});
|
|
144
|
-
`,
|
|
145
|
-
},
|
|
146
90
|
],
|
|
147
91
|
invalid: [
|
|
148
92
|
{
|
|
@@ -256,6 +200,18 @@ typescriptEslintTester.run(
|
|
|
256
200
|
`,
|
|
257
201
|
errors: [{ messageId: 'no-nested-selectors' }],
|
|
258
202
|
},
|
|
203
|
+
{
|
|
204
|
+
name: 'Child combinator without whitespace',
|
|
205
|
+
code: `
|
|
206
|
+
import { styled } from '@compiled/react';
|
|
207
|
+
styled.div({
|
|
208
|
+
'&>a': {
|
|
209
|
+
color: 'orange'
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
`,
|
|
213
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
214
|
+
},
|
|
259
215
|
{
|
|
260
216
|
name: 'Column combinator',
|
|
261
217
|
code: `
|
|
@@ -280,6 +236,87 @@ typescriptEslintTester.run(
|
|
|
280
236
|
`,
|
|
281
237
|
errors: [{ messageId: 'no-nested-selectors' }],
|
|
282
238
|
},
|
|
239
|
+
{
|
|
240
|
+
name: 'Descendant combinator',
|
|
241
|
+
code: `
|
|
242
|
+
import { styled } from '@compiled/react';
|
|
243
|
+
const Component = styled.div({
|
|
244
|
+
'& &:hover': {
|
|
245
|
+
padding: 0,
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
`,
|
|
249
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: 'cssMap API',
|
|
253
|
+
code: `
|
|
254
|
+
import { cssMap } from '@compiled/react';
|
|
255
|
+
|
|
256
|
+
const styles = cssMap({
|
|
257
|
+
success: {
|
|
258
|
+
'[data-component-selector="my.button"]': {
|
|
259
|
+
color: 'var(--ds-text-inverse)',
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
`,
|
|
264
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
name: 'keyframes API',
|
|
268
|
+
code: `
|
|
269
|
+
import { keyframes } from '@emotion/react';
|
|
270
|
+
|
|
271
|
+
const keyFrameStyles = keyframes({
|
|
272
|
+
'from, to': {
|
|
273
|
+
div: {
|
|
274
|
+
opacity: 0,
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
'50%': {
|
|
278
|
+
opacity: 1,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
`,
|
|
282
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
name: 'Next sibling selector',
|
|
286
|
+
code: `
|
|
287
|
+
import { styled } from '@compiled/react';
|
|
288
|
+
styled.div({
|
|
289
|
+
'&+a': {
|
|
290
|
+
color: 'orange'
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
`,
|
|
294
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
name: 'Next sibling selector on non-HTML element',
|
|
298
|
+
code: `
|
|
299
|
+
import { styled } from '@compiled/react';
|
|
300
|
+
styled.div({
|
|
301
|
+
'& + &': {
|
|
302
|
+
color: 'orange'
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
`,
|
|
306
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
name: 'Subsequent sibling selector on non-HTML element',
|
|
310
|
+
code: `
|
|
311
|
+
import { styled } from '@compiled/react';
|
|
312
|
+
styled.div({
|
|
313
|
+
'& ~ &': {
|
|
314
|
+
color: 'orange'
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
`,
|
|
318
|
+
errors: [{ messageId: 'no-nested-selectors' }],
|
|
319
|
+
},
|
|
283
320
|
],
|
|
284
321
|
},
|
|
285
322
|
);
|
|
@@ -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;
|