@atlaskit/eslint-plugin-ui-styling-standard 2.1.3 → 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 +8 -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 +46 -32
- package/dist/cjs/rules/no-unsafe-selectors/index.js +87 -84
- 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 +3 -4
- 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 +36 -34
- package/dist/es2019/rules/no-unsafe-selectors/index.js +67 -87
- 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 +3 -4
- 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 +46 -32
- package/dist/esm/rules/no-unsafe-selectors/index.js +88 -85
- 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 +3 -4
- 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/walk-css-map.d.ts +1 -7
- package/dist/types/rules/utils/is-simple-selector.d.ts +11 -0
- package/dist/types/rules/utils/parse-selector.d.ts +1 -2
- 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 +1 -1
|
@@ -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,9 +1,10 @@
|
|
|
1
1
|
import { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
2
|
-
import { getScope } from '@atlaskit/eslint-utils/context-compat';
|
|
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';
|
|
6
5
|
import { parseSelector } from '../utils/parse-selector';
|
|
6
|
+
import { getStyleCalls } from '../utils/style-calls';
|
|
7
|
+
import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
|
|
7
8
|
const getCssSelector = key => {
|
|
8
9
|
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
9
10
|
return key.value;
|
|
@@ -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 = parseSelector(context, 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
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { getScope } from '@atlaskit/eslint-utils/context-compat';
|
|
2
|
-
import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
|
|
3
1
|
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
4
|
-
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
5
2
|
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
3
|
+
import { isSimpleSelector } from '../utils/is-simple-selector';
|
|
6
4
|
import { parseSelector } from '../utils/parse-selector';
|
|
5
|
+
import { getStyleCalls } from '../utils/style-calls';
|
|
6
|
+
import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
|
|
7
7
|
import { lintSelector } from './lint-selector';
|
|
8
|
-
import {
|
|
8
|
+
import { walkCssMapCall } from './walk-css-map';
|
|
9
|
+
import { allowedPseudos } from './constants';
|
|
9
10
|
const ignoredAtRules = new Set(['@container',
|
|
10
11
|
// ignored because it's covered by `no-container-queries`
|
|
11
12
|
'@media',
|
|
@@ -56,95 +57,74 @@ const rule = createLintRuleWithTypedConfig({
|
|
|
56
57
|
},
|
|
57
58
|
create(context, config) {
|
|
58
59
|
return {
|
|
59
|
-
Program(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
60
|
+
Program() {
|
|
61
|
+
for (const styleCall of getStyleCalls(context)) {
|
|
62
|
+
if (!config.importSources.includes(styleCall.importSource)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (styleCall.styleFunction === 'cssMap') {
|
|
66
|
+
walkCssMapCall(styleCall.node, property => {
|
|
67
|
+
const {
|
|
68
|
+
type,
|
|
69
|
+
node
|
|
70
|
+
} = property;
|
|
71
|
+
if (type === 'grouped-at-rules') {
|
|
72
|
+
context.report({
|
|
73
|
+
node: node.key,
|
|
74
|
+
messageId: 'no-grouped-at-rules'
|
|
75
|
+
});
|
|
76
|
+
} else if (type === 'selectors') {
|
|
77
|
+
context.report({
|
|
78
|
+
node: node.key,
|
|
79
|
+
messageId: 'no-selectors-object'
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
walkStyleCallProperties(styleCall, ({
|
|
85
|
+
key,
|
|
86
|
+
value
|
|
87
|
+
}) => {
|
|
88
|
+
if (value.type !== 'ObjectExpression' || key.type !== 'Literal' || typeof key.value !== 'string') {
|
|
74
89
|
return;
|
|
75
90
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
91
|
+
const selectorText = key.value;
|
|
92
|
+
if (selectorText.includes('@')) {
|
|
93
|
+
lintAtRule({
|
|
94
|
+
context,
|
|
95
|
+
sourceNode: key,
|
|
96
|
+
atRule: selectorText
|
|
80
97
|
});
|
|
81
98
|
return;
|
|
82
99
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*/
|
|
100
|
-
if (value.type !== 'ObjectExpression') {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Technically the key could be an identifier too, e.g.
|
|
106
|
-
*
|
|
107
|
-
* ```ts
|
|
108
|
-
* css({
|
|
109
|
-
* // This key is an identifier, not a literal
|
|
110
|
-
* selector: {}
|
|
111
|
-
* });
|
|
112
|
-
* ```
|
|
113
|
-
*
|
|
114
|
-
* But an identifier couldn't violate anything in this rule,
|
|
115
|
-
* because it can't contain '@' or ':' characters.
|
|
116
|
-
*/
|
|
117
|
-
if (key.type !== 'Literal' || typeof key.value !== 'string') {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const selectorText = key.value;
|
|
121
|
-
if (selectorText.includes('@')) {
|
|
122
|
-
lintAtRule({
|
|
123
|
-
context,
|
|
124
|
-
sourceNode: key,
|
|
125
|
-
atRule: selectorText
|
|
126
|
-
});
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
try {
|
|
130
|
-
const selectorList = parseSelector(context, selectorText);
|
|
131
|
-
selectorList.nodes.forEach(selector => lintSelector({
|
|
132
|
-
context,
|
|
133
|
-
sourceNode: key,
|
|
134
|
-
selector,
|
|
135
|
-
config,
|
|
136
|
-
isXcssCall
|
|
137
|
-
}));
|
|
138
|
-
} catch {
|
|
139
|
-
context.report({
|
|
140
|
-
node: key,
|
|
141
|
-
messageId: 'no-unparsable-selectors',
|
|
142
|
-
data: {
|
|
143
|
-
selectorText
|
|
100
|
+
if (isSimpleSelector(selectorText, {
|
|
101
|
+
allowedPseudos,
|
|
102
|
+
allowLeadingPseudo: styleCall.styleFunction === 'xcss'
|
|
103
|
+
})) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const selectorList = parseSelector(context, selectorText);
|
|
108
|
+
for (const selector of selectorList.nodes) {
|
|
109
|
+
lintSelector({
|
|
110
|
+
context,
|
|
111
|
+
sourceNode: key,
|
|
112
|
+
selector,
|
|
113
|
+
config,
|
|
114
|
+
isXcssCall: styleCall.styleFunction === 'xcss'
|
|
115
|
+
});
|
|
144
116
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
117
|
+
} catch {
|
|
118
|
+
context.report({
|
|
119
|
+
node: key,
|
|
120
|
+
messageId: 'no-unparsable-selectors',
|
|
121
|
+
data: {
|
|
122
|
+
selectorText
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
148
128
|
}
|
|
149
129
|
};
|
|
150
130
|
}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import { isNesting } from 'postcss-selector-parser';
|
|
1
|
+
import { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
2
2
|
import { allowedPseudos } from './constants';
|
|
3
3
|
import { getSourceLocationFromRange } from './get-source-location-from-range';
|
|
4
4
|
import { getRangeFromNode } from './source-position-utils';
|
|
5
5
|
export function lintSelector(args) {
|
|
6
6
|
checkNoAmbiguousPseudos(args);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const pseudos = [];
|
|
8
|
+
const nestings = [];
|
|
9
|
+
args.selector.walk(node => {
|
|
10
|
+
if (isPseudo(node)) {
|
|
11
|
+
pseudos.push(node);
|
|
12
|
+
} else if (isNesting(node)) {
|
|
13
|
+
nestings.push(node);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
checkNoRestrictedPseudos(args, pseudos);
|
|
17
|
+
checkNoLegacyPseudoElementSyntax(args, pseudos);
|
|
18
|
+
checkNoIncreasedSpecificity(args, nestings);
|
|
10
19
|
}
|
|
11
20
|
const legacyPseudoElements = new Set([':after', ':before', ':first-letter', ':first-line']);
|
|
12
21
|
|
|
@@ -59,12 +68,11 @@ function checkNoAmbiguousPseudos({
|
|
|
59
68
|
}
|
|
60
69
|
function checkNoRestrictedPseudos({
|
|
61
70
|
context,
|
|
62
|
-
sourceNode
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
selector.walkPseudos(pseudo => {
|
|
71
|
+
sourceNode
|
|
72
|
+
}, pseudos) {
|
|
73
|
+
for (const pseudo of pseudos) {
|
|
66
74
|
if (allowedPseudos.has(pseudo.value)) {
|
|
67
|
-
|
|
75
|
+
continue;
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
/**
|
|
@@ -74,7 +82,7 @@ function checkNoRestrictedPseudos({
|
|
|
74
82
|
* This will be caught by the `no-legacy-pseudo-element-syntax` check.
|
|
75
83
|
*/
|
|
76
84
|
if (legacyPseudoElements.has(pseudo.value) && allowedPseudos.has(`:${pseudo.value}`)) {
|
|
77
|
-
|
|
85
|
+
continue;
|
|
78
86
|
}
|
|
79
87
|
if (!allowedPseudos.has(pseudo.value)) {
|
|
80
88
|
const range = getRangeFromNode({
|
|
@@ -90,7 +98,7 @@ function checkNoRestrictedPseudos({
|
|
|
90
98
|
}
|
|
91
99
|
});
|
|
92
100
|
}
|
|
93
|
-
}
|
|
101
|
+
}
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
/**
|
|
@@ -100,10 +108,9 @@ function checkNoRestrictedPseudos({
|
|
|
100
108
|
*/
|
|
101
109
|
function checkNoLegacyPseudoElementSyntax({
|
|
102
110
|
context,
|
|
103
|
-
sourceNode
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
selector.walkPseudos(pseudo => {
|
|
111
|
+
sourceNode
|
|
112
|
+
}, pseudos) {
|
|
113
|
+
for (const pseudo of pseudos) {
|
|
107
114
|
if (legacyPseudoElements.has(pseudo.value)) {
|
|
108
115
|
const range = getRangeFromNode({
|
|
109
116
|
node: pseudo,
|
|
@@ -118,19 +125,18 @@ function checkNoLegacyPseudoElementSyntax({
|
|
|
118
125
|
}
|
|
119
126
|
});
|
|
120
127
|
}
|
|
121
|
-
}
|
|
128
|
+
}
|
|
122
129
|
}
|
|
123
130
|
function checkNoIncreasedSpecificity({
|
|
124
131
|
context,
|
|
125
|
-
sourceNode
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
selector.walkNesting(nesting => {
|
|
132
|
+
sourceNode
|
|
133
|
+
}, nestings) {
|
|
134
|
+
for (const nesting of nestings) {
|
|
129
135
|
/**
|
|
130
136
|
* If it's not the start of a chain, then it's already been reported.
|
|
131
137
|
*/
|
|
132
138
|
if (isNesting(nesting.prev())) {
|
|
133
|
-
|
|
139
|
+
continue;
|
|
134
140
|
}
|
|
135
141
|
let next = nesting.next();
|
|
136
142
|
|
|
@@ -139,7 +145,7 @@ function checkNoIncreasedSpecificity({
|
|
|
139
145
|
* then it isn't just there to increase specificity — it is actually necessary.
|
|
140
146
|
*/
|
|
141
147
|
if (next && !isNesting(next)) {
|
|
142
|
-
|
|
148
|
+
continue;
|
|
143
149
|
}
|
|
144
150
|
let lastNesting = nesting;
|
|
145
151
|
while (isNesting(next)) {
|
|
@@ -159,5 +165,5 @@ function checkNoIncreasedSpecificity({
|
|
|
159
165
|
loc,
|
|
160
166
|
messageId: 'no-increased-specificity'
|
|
161
167
|
});
|
|
162
|
-
}
|
|
168
|
+
}
|
|
163
169
|
}
|
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
* This is because the API is likely to change and should not be considered stable.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* At-rules that are allowed to be used in "grouped" form within cssMap.
|
|
11
9
|
*
|
|
@@ -52,38 +50,14 @@ import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
|
52
50
|
*/
|
|
53
51
|
const allowedGroupedAtRules = new Set(['@starting-style' // Has no parameters, can only be written in grouped form
|
|
54
52
|
]);
|
|
55
|
-
export function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
visitor
|
|
59
|
-
}) {
|
|
60
|
-
const program = getSourceCode(context).ast;
|
|
61
|
-
const importDeclaration = program.body.find(node => node.type === 'ImportDeclaration' && importSources.includes(node.source.value));
|
|
62
|
-
const specifier = importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.specifiers.find(specifier => specifier.type === 'ImportSpecifier' && 'name' in specifier.imported && specifier.imported.name === 'cssMap');
|
|
63
|
-
if (!specifier) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const [variable] = getSourceCode(context).scopeManager.getDeclaredVariables(specifier);
|
|
67
|
-
if (!variable) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
variable.references.forEach(reference => {
|
|
71
|
-
const identifier = reference.identifier;
|
|
72
|
-
const {
|
|
73
|
-
parent
|
|
74
|
-
} = identifier;
|
|
75
|
-
if (parent.type !== 'CallExpression') {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const variantMap = parent.arguments[0];
|
|
79
|
-
if (variantMap.type !== 'ObjectExpression') {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
53
|
+
export function walkCssMapCall(call, visitor) {
|
|
54
|
+
const variantMap = call.arguments[0];
|
|
55
|
+
if ((variantMap === null || variantMap === void 0 ? void 0 : variantMap.type) === 'ObjectExpression') {
|
|
82
56
|
walkCssMapVariantMap({
|
|
83
57
|
variantMap,
|
|
84
58
|
visitor
|
|
85
59
|
});
|
|
86
|
-
}
|
|
60
|
+
}
|
|
87
61
|
}
|
|
88
62
|
function walkCssMapVariantMap({
|
|
89
63
|
variantMap,
|