@atlaskit/eslint-plugin-ui-styling-standard 2.1.2 → 2.1.4
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 +16 -0
- package/README.md +18 -18
- package/dist/cjs/rules/no-important-styles/index.js +79 -99
- package/dist/cjs/rules/no-nested-selectors/index.js +48 -36
- package/dist/cjs/rules/no-unsafe-selectors/index.js +88 -87
- package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +140 -101
- package/dist/cjs/rules/no-unsafe-selectors/walk-css-map.js +20 -50
- package/dist/cjs/rules/utils/create-rule-with-typed-config.js +29 -10
- package/dist/cjs/rules/utils/is-simple-selector.js +70 -0
- package/dist/cjs/rules/utils/parse-selector.js +30 -0
- package/dist/cjs/rules/utils/style-calls.js +127 -0
- package/dist/cjs/rules/utils/walk-style-call-properties.js +50 -0
- package/dist/es2019/rules/no-important-styles/index.js +79 -70
- package/dist/es2019/rules/no-nested-selectors/index.js +38 -36
- package/dist/es2019/rules/no-unsafe-selectors/index.js +68 -89
- package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +29 -23
- package/dist/es2019/rules/no-unsafe-selectors/walk-css-map.js +4 -30
- package/dist/es2019/rules/utils/create-rule-with-typed-config.js +30 -10
- package/dist/es2019/rules/utils/is-simple-selector.js +63 -0
- package/dist/es2019/rules/utils/parse-selector.js +23 -0
- package/dist/es2019/rules/utils/style-calls.js +89 -0
- package/dist/es2019/rules/utils/walk-style-call-properties.js +23 -0
- package/dist/esm/rules/no-important-styles/index.js +80 -100
- package/dist/esm/rules/no-nested-selectors/index.js +48 -34
- package/dist/esm/rules/no-unsafe-selectors/index.js +89 -87
- package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +141 -102
- package/dist/esm/rules/no-unsafe-selectors/walk-css-map.js +19 -49
- package/dist/esm/rules/utils/create-rule-with-typed-config.js +30 -10
- package/dist/esm/rules/utils/is-simple-selector.js +64 -0
- package/dist/esm/rules/utils/parse-selector.js +23 -0
- package/dist/esm/rules/utils/style-calls.js +120 -0
- package/dist/esm/rules/utils/walk-style-call-properties.js +44 -0
- package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -1
- package/dist/types/rules/no-unsafe-selectors/walk-css-map.d.ts +1 -7
- package/dist/types/rules/utils/create-rule-with-typed-config.d.ts +1 -1
- package/dist/types/rules/utils/is-simple-selector.d.ts +11 -0
- package/dist/types/rules/utils/parse-selector.d.ts +9 -0
- package/dist/types/rules/utils/style-calls.d.ts +17 -0
- package/dist/types/rules/utils/walk-style-call-properties.d.ts +5 -0
- package/package.json +3 -3
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isSimpleSelector = isSimpleSelector;
|
|
7
|
+
/**
|
|
8
|
+
* Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
|
|
9
|
+
* Returning false means the caller must use the full selector parser.
|
|
10
|
+
*/
|
|
11
|
+
function isSimpleSelector(selectorText) {
|
|
12
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
13
|
+
var index = 0;
|
|
14
|
+
while (index < selectorText.length) {
|
|
15
|
+
index = skipWhitespace(selectorText, index);
|
|
16
|
+
var hasNesting = selectorText.charCodeAt(index) === 38;
|
|
17
|
+
if (hasNesting) {
|
|
18
|
+
index++;
|
|
19
|
+
if (selectorText.charCodeAt(index) === 38) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
} else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
var pseudoCount = 0;
|
|
26
|
+
while (selectorText.charCodeAt(index) === 58) {
|
|
27
|
+
var pseudoStart = index++;
|
|
28
|
+
if (selectorText.charCodeAt(index) === 58) {
|
|
29
|
+
index++;
|
|
30
|
+
}
|
|
31
|
+
var nameStart = index;
|
|
32
|
+
while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
|
|
33
|
+
index++;
|
|
34
|
+
}
|
|
35
|
+
if (index === nameStart || selectorText.charCodeAt(index) === 40) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
pseudoCount++;
|
|
42
|
+
}
|
|
43
|
+
if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
index = skipWhitespace(selectorText, index);
|
|
47
|
+
if (index === selectorText.length) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
if (selectorText.charCodeAt(index) !== 44) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
index++;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
function skipWhitespace(value, start) {
|
|
58
|
+
var index = start;
|
|
59
|
+
while (index < value.length) {
|
|
60
|
+
var character = value.charCodeAt(index);
|
|
61
|
+
if (character !== 32 && (character < 9 || character > 13)) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
index++;
|
|
65
|
+
}
|
|
66
|
+
return index;
|
|
67
|
+
}
|
|
68
|
+
function isPseudoNameCharacter(character) {
|
|
69
|
+
return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
|
|
70
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.parseSelector = parseSelector;
|
|
8
|
+
var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
|
|
9
|
+
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
|
|
10
|
+
var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
|
|
11
|
+
var selectorCache = new WeakMap();
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
|
|
15
|
+
*/
|
|
16
|
+
function parseSelector(context, selectorText) {
|
|
17
|
+
var sourceCode = (0, _contextCompat.getSourceCode)(context);
|
|
18
|
+
var sourceFileCache = selectorCache.get(sourceCode);
|
|
19
|
+
if (!sourceFileCache) {
|
|
20
|
+
sourceFileCache = new Map();
|
|
21
|
+
selectorCache.set(sourceCode, sourceFileCache);
|
|
22
|
+
}
|
|
23
|
+
var cachedSelector = sourceFileCache.get(selectorText);
|
|
24
|
+
if (cachedSelector) {
|
|
25
|
+
return cachedSelector;
|
|
26
|
+
}
|
|
27
|
+
var selector = cssSelectorProcessor.astSync(selectorText);
|
|
28
|
+
sourceFileCache.set(selectorText, selector);
|
|
29
|
+
return selector;
|
|
30
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.getStyleCalls = getStyleCalls;
|
|
8
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
|
+
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
|
|
10
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
11
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
12
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
13
|
+
var styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
|
|
14
|
+
var defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
|
|
15
|
+
var styleCallsBySourceCode = new WeakMap();
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Visit imported style calls without resolving every CallExpression against its current scope.
|
|
19
|
+
*
|
|
20
|
+
* ESLint has already linked import bindings to all of their references. Building this index once
|
|
21
|
+
* per source file makes each styling rule proportional to the number of style calls instead of the
|
|
22
|
+
* number of all calls in the file.
|
|
23
|
+
*/
|
|
24
|
+
function getStyleCalls(context) {
|
|
25
|
+
var sourceCode = (0, _contextCompat.getSourceCode)(context);
|
|
26
|
+
var styleCalls = styleCallsBySourceCode.get(sourceCode);
|
|
27
|
+
if (!styleCalls) {
|
|
28
|
+
styleCalls = buildStyleCallIndex(sourceCode);
|
|
29
|
+
styleCallsBySourceCode.set(sourceCode, styleCalls);
|
|
30
|
+
}
|
|
31
|
+
return styleCalls;
|
|
32
|
+
}
|
|
33
|
+
function buildStyleCallIndex(sourceCode) {
|
|
34
|
+
var styleCalls = [];
|
|
35
|
+
var seenCalls = new Set();
|
|
36
|
+
var scopeManager = sourceCode.scopeManager;
|
|
37
|
+
if (!scopeManager) {
|
|
38
|
+
return styleCalls;
|
|
39
|
+
}
|
|
40
|
+
var _iterator = _createForOfIteratorHelper(sourceCode.ast.body),
|
|
41
|
+
_step;
|
|
42
|
+
try {
|
|
43
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
44
|
+
var statement = _step.value;
|
|
45
|
+
if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
var importSource = statement.source.value;
|
|
49
|
+
var _iterator2 = _createForOfIteratorHelper(statement.specifiers),
|
|
50
|
+
_step2;
|
|
51
|
+
try {
|
|
52
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
53
|
+
var specifier = _step2.value;
|
|
54
|
+
var styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
|
|
55
|
+
if (!styleFunction) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
var _scopeManager$getDecl = scopeManager.getDeclaredVariables(specifier),
|
|
59
|
+
_scopeManager$getDecl2 = (0, _slicedToArray2.default)(_scopeManager$getDecl, 1),
|
|
60
|
+
variable = _scopeManager$getDecl2[0];
|
|
61
|
+
if (!variable) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
var _iterator3 = _createForOfIteratorHelper(variable.references),
|
|
65
|
+
_step3;
|
|
66
|
+
try {
|
|
67
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
68
|
+
var reference = _step3.value;
|
|
69
|
+
var call = getStyleCall(reference.identifier, styleFunction);
|
|
70
|
+
if (!call || seenCalls.has(call)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
seenCalls.add(call);
|
|
74
|
+
styleCalls.push({
|
|
75
|
+
importSource: importSource,
|
|
76
|
+
node: call,
|
|
77
|
+
styleFunction: styleFunction
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
_iterator3.e(err);
|
|
82
|
+
} finally {
|
|
83
|
+
_iterator3.f();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
_iterator2.e(err);
|
|
88
|
+
} finally {
|
|
89
|
+
_iterator2.f();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} catch (err) {
|
|
93
|
+
_iterator.e(err);
|
|
94
|
+
} finally {
|
|
95
|
+
_iterator.f();
|
|
96
|
+
}
|
|
97
|
+
return styleCalls;
|
|
98
|
+
}
|
|
99
|
+
function getStyleFunctionFromSpecifier(specifier, importSource) {
|
|
100
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
|
|
101
|
+
return specifier.imported.name;
|
|
102
|
+
}
|
|
103
|
+
if (specifier.type !== 'ImportDefaultSpecifier') {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
if (styleFunctionNames.has(specifier.local.name)) {
|
|
107
|
+
return specifier.local.name;
|
|
108
|
+
}
|
|
109
|
+
return defaultStyledImportSources.has(importSource) ? 'styled' : null;
|
|
110
|
+
}
|
|
111
|
+
function getStyleCall(identifier, styleFunction) {
|
|
112
|
+
var _parent2, _parent$parent;
|
|
113
|
+
var callee = identifier;
|
|
114
|
+
var parent = callee.parent;
|
|
115
|
+
while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
|
|
116
|
+
var _parent;
|
|
117
|
+
callee = parent;
|
|
118
|
+
parent = callee.parent;
|
|
119
|
+
}
|
|
120
|
+
if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
|
|
124
|
+
return parent.parent;
|
|
125
|
+
}
|
|
126
|
+
return parent;
|
|
127
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.walkStyleCallProperties = walkStyleCallProperties;
|
|
7
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
8
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
9
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
10
|
+
function walkStyleCallProperties(styleCall, visitor, options) {
|
|
11
|
+
var skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
|
|
12
|
+
var _iterator = _createForOfIteratorHelper(styleCall.node.arguments),
|
|
13
|
+
_step;
|
|
14
|
+
try {
|
|
15
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
16
|
+
var argument = _step.value;
|
|
17
|
+
if (argument.type === 'ObjectExpression') {
|
|
18
|
+
walkStyleObject(argument, visitor, skipTopLevel);
|
|
19
|
+
} else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
|
|
20
|
+
walkStyleObject(argument.body, visitor, skipTopLevel);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
} catch (err) {
|
|
24
|
+
_iterator.e(err);
|
|
25
|
+
} finally {
|
|
26
|
+
_iterator.f();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function walkStyleObject(object, visitor, skipVisitor) {
|
|
30
|
+
var _iterator2 = _createForOfIteratorHelper(object.properties),
|
|
31
|
+
_step2;
|
|
32
|
+
try {
|
|
33
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
34
|
+
var property = _step2.value;
|
|
35
|
+
if (property.type !== 'Property') {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (!skipVisitor) {
|
|
39
|
+
visitor(property);
|
|
40
|
+
}
|
|
41
|
+
if (property.value.type === 'ObjectExpression') {
|
|
42
|
+
walkStyleObject(property.value, visitor, false);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} catch (err) {
|
|
46
|
+
_iterator2.e(err);
|
|
47
|
+
} finally {
|
|
48
|
+
_iterator2.f();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
|
|
1
|
+
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
3
2
|
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
4
3
|
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
4
|
+
import { getStyleCalls } from '../utils/style-calls';
|
|
5
5
|
const rule = createLintRuleWithTypedConfig({
|
|
6
6
|
meta: {
|
|
7
7
|
name: 'no-important-styles',
|
|
@@ -24,92 +24,101 @@ const rule = createLintRuleWithTypedConfig({
|
|
|
24
24
|
create(context, {
|
|
25
25
|
importSources
|
|
26
26
|
}) {
|
|
27
|
+
const sourceCode = getSourceCode(context);
|
|
28
|
+
if (!sourceCode.text.includes('important')) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
27
31
|
return {
|
|
28
|
-
|
|
32
|
+
Program() {
|
|
29
33
|
const {
|
|
30
|
-
|
|
31
|
-
} =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (isImportant(
|
|
34
|
+
text
|
|
35
|
+
} = sourceCode;
|
|
36
|
+
const reportedValues = new Set();
|
|
37
|
+
let importantIndex = text.indexOf('important');
|
|
38
|
+
while (importantIndex !== -1) {
|
|
39
|
+
const value = findStyleValue(sourceCode, importantIndex);
|
|
40
|
+
if (value && !reportedValues.has(value) && isImportant(value) && isInSupportedStyleCall(context, value, importSources)) {
|
|
41
|
+
reportedValues.add(value);
|
|
37
42
|
context.report({
|
|
38
|
-
node:
|
|
43
|
+
node: value,
|
|
39
44
|
messageId: 'no-important-styles'
|
|
40
45
|
});
|
|
41
46
|
}
|
|
42
|
-
|
|
47
|
+
importantIndex = text.indexOf('important', importantIndex + 9);
|
|
48
|
+
}
|
|
43
49
|
}
|
|
44
50
|
};
|
|
45
51
|
}
|
|
46
52
|
});
|
|
47
53
|
export default rule;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
* Walk through all Property nodes in the AST tree recursively.
|
|
52
|
-
* This is faster than esquery for this specific use case as it avoids
|
|
53
|
-
* selector compilation and executes a simple depth-first traversal.
|
|
54
|
-
*/
|
|
55
|
-
function walkProperties(node, callback) {
|
|
56
|
-
var _node$arguments;
|
|
57
|
-
if (node.type === 'Property' && node.value) {
|
|
58
|
-
callback(node);
|
|
54
|
+
function isImportant(node) {
|
|
55
|
+
if (node.type === 'Literal') {
|
|
56
|
+
return typeof node.value === 'string' && hasImportantSuffix(node.value);
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (const arg of node.arguments) {
|
|
64
|
-
if (arg.type === 'ObjectExpression' && arg.properties) {
|
|
65
|
-
for (const prop of arg.properties) {
|
|
66
|
-
walkObjectProperty(prop, callback);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
58
|
+
if (node.type === 'TemplateLiteral') {
|
|
59
|
+
const lastQuasi = node.quasis[node.quasis.length - 1];
|
|
60
|
+
return Boolean(lastQuasi && hasImportantSuffix(lastQuasi.value.raw));
|
|
70
61
|
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
function findStyleValue(sourceCode, index) {
|
|
65
|
+
let node = sourceCode.getNodeByRangeIndex(index);
|
|
66
|
+
while (node && node.type !== 'Literal' && node.type !== 'TemplateLiteral') {
|
|
67
|
+
node = node.parent;
|
|
68
|
+
}
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
function isInSupportedStyleCall(context, value, importSources) {
|
|
72
|
+
const call = findContainingCall(value);
|
|
73
|
+
return Boolean(call && getStyleCalls(context).some(styleCall => styleCall.node === call && importSources.includes(styleCall.importSource)));
|
|
71
74
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
function findContainingCall(value) {
|
|
76
|
+
var _property, _object;
|
|
77
|
+
let property = value.parent;
|
|
78
|
+
if (((_property = property) === null || _property === void 0 ? void 0 : _property.type) !== 'Property' || property.value !== value) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
let object = property.parent;
|
|
82
|
+
if (((_object = object) === null || _object === void 0 ? void 0 : _object.type) !== 'ObjectExpression') {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
while (object) {
|
|
86
|
+
var _parent$parent;
|
|
87
|
+
const parent = object.parent;
|
|
88
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'Property' && parent.value === object) {
|
|
89
|
+
var _object2;
|
|
90
|
+
property = parent;
|
|
91
|
+
object = property.parent;
|
|
92
|
+
if (((_object2 = object) === null || _object2 === void 0 ? void 0 : _object2.type) !== 'ObjectExpression') {
|
|
93
|
+
return null;
|
|
84
94
|
}
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'CallExpression' && parent.arguments.includes(object)) {
|
|
98
|
+
return parent;
|
|
99
|
+
}
|
|
100
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'ArrowFunctionExpression' && parent.body === object && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.arguments.includes(parent)) {
|
|
101
|
+
return parent.parent;
|
|
85
102
|
}
|
|
103
|
+
return null;
|
|
86
104
|
}
|
|
105
|
+
return null;
|
|
87
106
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*/
|
|
93
|
-
function isImportant(node) {
|
|
94
|
-
if (node.type === 'Literal') {
|
|
95
|
-
if (typeof node.value !== 'string') {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
// Fast check: string must end with 'important' (after optional whitespace)
|
|
99
|
-
const len = node.value.length;
|
|
100
|
-
if (len < 10) {
|
|
101
|
-
return false;
|
|
102
|
-
} // "!important" is 10 chars minimum
|
|
103
|
-
return IMPORTANT_SUFFIX_REGEX.test(node.value);
|
|
107
|
+
function hasImportantSuffix(value) {
|
|
108
|
+
let index = value.length - 1;
|
|
109
|
+
while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
|
|
110
|
+
index--;
|
|
104
111
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
// Only check the last quasi's raw value (most likely to contain the !important)
|
|
111
|
-
const lastQuasi = node.quasis[node.quasis.length - 1];
|
|
112
|
-
return IMPORTANT_SUFFIX_REGEX.test(lastQuasi.value.raw);
|
|
112
|
+
const importantStart = index - 8;
|
|
113
|
+
if (importantStart < 0 || value.slice(importantStart, index + 1) !== 'important') {
|
|
114
|
+
return false;
|
|
113
115
|
}
|
|
114
|
-
|
|
116
|
+
index = importantStart - 1;
|
|
117
|
+
while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
|
|
118
|
+
index--;
|
|
119
|
+
}
|
|
120
|
+
return value.charCodeAt(index) === 33;
|
|
121
|
+
}
|
|
122
|
+
function isWhitespace(character) {
|
|
123
|
+
return character === 32 || character >= 9 && character <= 13;
|
|
115
124
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getScope } from '@atlaskit/eslint-utils/context-compat';
|
|
1
|
+
import { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
3
2
|
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
4
|
-
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
5
3
|
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
4
|
+
import { isSimpleSelector } from '../utils/is-simple-selector';
|
|
5
|
+
import { parseSelector } from '../utils/parse-selector';
|
|
6
|
+
import { getStyleCalls } from '../utils/style-calls';
|
|
7
|
+
import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
|
|
6
8
|
const getCssSelector = key => {
|
|
7
9
|
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
8
10
|
return key.value;
|
|
@@ -25,7 +27,6 @@ function isAllowedSelector(selector) {
|
|
|
25
27
|
const isTailAllowed = tail.every(isPseudo);
|
|
26
28
|
return isHeadAllowed && isTailAllowed;
|
|
27
29
|
}
|
|
28
|
-
const cssSelectorProcessor = cssSelectorParser();
|
|
29
30
|
const rule = createLintRuleWithTypedConfig({
|
|
30
31
|
meta: {
|
|
31
32
|
name: 'no-nested-selectors',
|
|
@@ -49,40 +50,41 @@ const rule = createLintRuleWithTypedConfig({
|
|
|
49
50
|
importSources
|
|
50
51
|
}) {
|
|
51
52
|
return {
|
|
52
|
-
|
|
53
|
-
const {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
walkStyleProperties(node, references, importSources, ({
|
|
57
|
-
key,
|
|
58
|
-
value
|
|
59
|
-
}) => {
|
|
60
|
-
if (value.type !== 'ObjectExpression') {
|
|
61
|
-
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
62
|
-
return;
|
|
53
|
+
Program() {
|
|
54
|
+
for (const styleCall of getStyleCalls(context)) {
|
|
55
|
+
if (!importSources.includes(styleCall.importSource)) {
|
|
56
|
+
continue;
|
|
63
57
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (selectorText.includes('@')) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
const selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
75
|
-
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
76
|
-
context.report({
|
|
77
|
-
messageId: 'no-nested-selectors',
|
|
78
|
-
node: key
|
|
79
|
-
});
|
|
58
|
+
walkStyleCallProperties(styleCall, ({
|
|
59
|
+
key,
|
|
60
|
+
value
|
|
61
|
+
}) => {
|
|
62
|
+
if (value.type !== 'ObjectExpression') {
|
|
63
|
+
return;
|
|
80
64
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
65
|
+
const selectorText = getCssSelector(key);
|
|
66
|
+
if (selectorText === null || selectorText.includes('@')) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (isSimpleSelector(selectorText, {
|
|
70
|
+
allowBareNesting: true,
|
|
71
|
+
allowLeadingPseudo: true
|
|
72
|
+
})) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const selectorList = parseSelector(context, selectorText);
|
|
77
|
+
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
78
|
+
context.report({
|
|
79
|
+
messageId: 'no-nested-selectors',
|
|
80
|
+
node: key
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
// `no-unsafe-selectors` reports unparsable selectors.
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
86
88
|
}
|
|
87
89
|
};
|
|
88
90
|
}
|