@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,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
|
|
3
|
+
* Returning false means the caller must use the full selector parser.
|
|
4
|
+
*/
|
|
5
|
+
export function isSimpleSelector(selectorText) {
|
|
6
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
7
|
+
var index = 0;
|
|
8
|
+
while (index < selectorText.length) {
|
|
9
|
+
index = skipWhitespace(selectorText, index);
|
|
10
|
+
var hasNesting = selectorText.charCodeAt(index) === 38;
|
|
11
|
+
if (hasNesting) {
|
|
12
|
+
index++;
|
|
13
|
+
if (selectorText.charCodeAt(index) === 38) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
} else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
var pseudoCount = 0;
|
|
20
|
+
while (selectorText.charCodeAt(index) === 58) {
|
|
21
|
+
var pseudoStart = index++;
|
|
22
|
+
if (selectorText.charCodeAt(index) === 58) {
|
|
23
|
+
index++;
|
|
24
|
+
}
|
|
25
|
+
var nameStart = index;
|
|
26
|
+
while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
|
|
27
|
+
index++;
|
|
28
|
+
}
|
|
29
|
+
if (index === nameStart || selectorText.charCodeAt(index) === 40) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
pseudoCount++;
|
|
36
|
+
}
|
|
37
|
+
if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
index = skipWhitespace(selectorText, index);
|
|
41
|
+
if (index === selectorText.length) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
if (selectorText.charCodeAt(index) !== 44) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
index++;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
function skipWhitespace(value, start) {
|
|
52
|
+
var index = start;
|
|
53
|
+
while (index < value.length) {
|
|
54
|
+
var character = value.charCodeAt(index);
|
|
55
|
+
if (character !== 32 && (character < 9 || character > 13)) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
index++;
|
|
59
|
+
}
|
|
60
|
+
return index;
|
|
61
|
+
}
|
|
62
|
+
function isPseudoNameCharacter(character) {
|
|
63
|
+
return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
|
|
64
|
+
}
|
|
@@ -4,8 +4,7 @@ var cssSelectorProcessor = cssSelectorParser();
|
|
|
4
4
|
var selectorCache = new WeakMap();
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Parse each selector once per linted source file.
|
|
8
|
-
* not shared between rules.
|
|
7
|
+
* Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
|
|
9
8
|
*/
|
|
10
9
|
export function parseSelector(context, selectorText) {
|
|
11
10
|
var sourceCode = getSourceCode(context);
|
|
@@ -16,9 +15,9 @@ export function parseSelector(context, selectorText) {
|
|
|
16
15
|
}
|
|
17
16
|
var cachedSelector = sourceFileCache.get(selectorText);
|
|
18
17
|
if (cachedSelector) {
|
|
19
|
-
return cachedSelector
|
|
18
|
+
return cachedSelector;
|
|
20
19
|
}
|
|
21
20
|
var selector = cssSelectorProcessor.astSync(selectorText);
|
|
22
21
|
sourceFileCache.set(selectorText, selector);
|
|
23
|
-
return selector
|
|
22
|
+
return selector;
|
|
24
23
|
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
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; } } }; }
|
|
3
|
+
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; } }
|
|
4
|
+
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; }
|
|
5
|
+
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
6
|
+
var styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
|
|
7
|
+
var defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
|
|
8
|
+
var styleCallsBySourceCode = new WeakMap();
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Visit imported style calls without resolving every CallExpression against its current scope.
|
|
12
|
+
*
|
|
13
|
+
* ESLint has already linked import bindings to all of their references. Building this index once
|
|
14
|
+
* per source file makes each styling rule proportional to the number of style calls instead of the
|
|
15
|
+
* number of all calls in the file.
|
|
16
|
+
*/
|
|
17
|
+
export function getStyleCalls(context) {
|
|
18
|
+
var sourceCode = getSourceCode(context);
|
|
19
|
+
var styleCalls = styleCallsBySourceCode.get(sourceCode);
|
|
20
|
+
if (!styleCalls) {
|
|
21
|
+
styleCalls = buildStyleCallIndex(sourceCode);
|
|
22
|
+
styleCallsBySourceCode.set(sourceCode, styleCalls);
|
|
23
|
+
}
|
|
24
|
+
return styleCalls;
|
|
25
|
+
}
|
|
26
|
+
function buildStyleCallIndex(sourceCode) {
|
|
27
|
+
var styleCalls = [];
|
|
28
|
+
var seenCalls = new Set();
|
|
29
|
+
var scopeManager = sourceCode.scopeManager;
|
|
30
|
+
if (!scopeManager) {
|
|
31
|
+
return styleCalls;
|
|
32
|
+
}
|
|
33
|
+
var _iterator = _createForOfIteratorHelper(sourceCode.ast.body),
|
|
34
|
+
_step;
|
|
35
|
+
try {
|
|
36
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
37
|
+
var statement = _step.value;
|
|
38
|
+
if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
var importSource = statement.source.value;
|
|
42
|
+
var _iterator2 = _createForOfIteratorHelper(statement.specifiers),
|
|
43
|
+
_step2;
|
|
44
|
+
try {
|
|
45
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
46
|
+
var specifier = _step2.value;
|
|
47
|
+
var styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
|
|
48
|
+
if (!styleFunction) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
var _scopeManager$getDecl = scopeManager.getDeclaredVariables(specifier),
|
|
52
|
+
_scopeManager$getDecl2 = _slicedToArray(_scopeManager$getDecl, 1),
|
|
53
|
+
variable = _scopeManager$getDecl2[0];
|
|
54
|
+
if (!variable) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
var _iterator3 = _createForOfIteratorHelper(variable.references),
|
|
58
|
+
_step3;
|
|
59
|
+
try {
|
|
60
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
61
|
+
var reference = _step3.value;
|
|
62
|
+
var call = getStyleCall(reference.identifier, styleFunction);
|
|
63
|
+
if (!call || seenCalls.has(call)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
seenCalls.add(call);
|
|
67
|
+
styleCalls.push({
|
|
68
|
+
importSource: importSource,
|
|
69
|
+
node: call,
|
|
70
|
+
styleFunction: styleFunction
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
} catch (err) {
|
|
74
|
+
_iterator3.e(err);
|
|
75
|
+
} finally {
|
|
76
|
+
_iterator3.f();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} catch (err) {
|
|
80
|
+
_iterator2.e(err);
|
|
81
|
+
} finally {
|
|
82
|
+
_iterator2.f();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} catch (err) {
|
|
86
|
+
_iterator.e(err);
|
|
87
|
+
} finally {
|
|
88
|
+
_iterator.f();
|
|
89
|
+
}
|
|
90
|
+
return styleCalls;
|
|
91
|
+
}
|
|
92
|
+
function getStyleFunctionFromSpecifier(specifier, importSource) {
|
|
93
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
|
|
94
|
+
return specifier.imported.name;
|
|
95
|
+
}
|
|
96
|
+
if (specifier.type !== 'ImportDefaultSpecifier') {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (styleFunctionNames.has(specifier.local.name)) {
|
|
100
|
+
return specifier.local.name;
|
|
101
|
+
}
|
|
102
|
+
return defaultStyledImportSources.has(importSource) ? 'styled' : null;
|
|
103
|
+
}
|
|
104
|
+
function getStyleCall(identifier, styleFunction) {
|
|
105
|
+
var _parent2, _parent$parent;
|
|
106
|
+
var callee = identifier;
|
|
107
|
+
var parent = callee.parent;
|
|
108
|
+
while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
|
|
109
|
+
var _parent;
|
|
110
|
+
callee = parent;
|
|
111
|
+
parent = callee.parent;
|
|
112
|
+
}
|
|
113
|
+
if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
|
|
117
|
+
return parent.parent;
|
|
118
|
+
}
|
|
119
|
+
return parent;
|
|
120
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
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; } } }; }
|
|
2
|
+
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; } }
|
|
3
|
+
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; }
|
|
4
|
+
export function walkStyleCallProperties(styleCall, visitor, options) {
|
|
5
|
+
var skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
|
|
6
|
+
var _iterator = _createForOfIteratorHelper(styleCall.node.arguments),
|
|
7
|
+
_step;
|
|
8
|
+
try {
|
|
9
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
10
|
+
var argument = _step.value;
|
|
11
|
+
if (argument.type === 'ObjectExpression') {
|
|
12
|
+
walkStyleObject(argument, visitor, skipTopLevel);
|
|
13
|
+
} else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
|
|
14
|
+
walkStyleObject(argument.body, visitor, skipTopLevel);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
} catch (err) {
|
|
18
|
+
_iterator.e(err);
|
|
19
|
+
} finally {
|
|
20
|
+
_iterator.f();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function walkStyleObject(object, visitor, skipVisitor) {
|
|
24
|
+
var _iterator2 = _createForOfIteratorHelper(object.properties),
|
|
25
|
+
_step2;
|
|
26
|
+
try {
|
|
27
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
28
|
+
var property = _step2.value;
|
|
29
|
+
if (property.type !== 'Property') {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (!skipVisitor) {
|
|
33
|
+
visitor(property);
|
|
34
|
+
}
|
|
35
|
+
if (property.value.type === 'ObjectExpression') {
|
|
36
|
+
walkStyleObject(property.value, visitor, false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch (err) {
|
|
40
|
+
_iterator2.e(err);
|
|
41
|
+
} finally {
|
|
42
|
+
_iterator2.f();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This is because the API is likely to change and should not be considered stable.
|
|
5
5
|
*/
|
|
6
|
-
import type { Rule } from 'eslint';
|
|
7
6
|
import type * as ESTree from 'eslint-codemod-utils';
|
|
8
7
|
type CssMapVisitorArgs = {
|
|
9
8
|
type: 'variant' | 'declaration' | 'ruleset' | 'selectors';
|
|
@@ -14,10 +13,5 @@ type CssMapVisitorArgs = {
|
|
|
14
13
|
atRule: string;
|
|
15
14
|
};
|
|
16
15
|
type CssMapVisitor = (args: CssMapVisitorArgs) => void;
|
|
17
|
-
export declare function
|
|
18
|
-
context: Rule.RuleContext;
|
|
19
|
-
program: ESTree.Program;
|
|
20
|
-
importSources: string[];
|
|
21
|
-
visitor: CssMapVisitor;
|
|
22
|
-
}): void;
|
|
16
|
+
export declare function walkCssMapCall(call: ESTree.CallExpression, visitor: CssMapVisitor): void;
|
|
23
17
|
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
allowedPseudos?: ReadonlySet<string>;
|
|
3
|
+
allowBareNesting?: boolean;
|
|
4
|
+
allowLeadingPseudo?: boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
|
|
8
|
+
* Returning false means the caller must use the full selector parser.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isSimpleSelector(selectorText: string, options?: Options): boolean;
|
|
11
|
+
export {};
|
|
@@ -3,8 +3,7 @@ import cssSelectorParser from 'postcss-selector-parser';
|
|
|
3
3
|
declare const cssSelectorProcessor: ReturnType<typeof cssSelectorParser>;
|
|
4
4
|
type ParsedSelector = ReturnType<typeof cssSelectorProcessor.astSync>;
|
|
5
5
|
/**
|
|
6
|
-
* Parse each selector once per linted source file.
|
|
7
|
-
* not shared between rules.
|
|
6
|
+
* Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
|
|
8
7
|
*/
|
|
9
8
|
export declare function parseSelector(context: Rule.RuleContext, selectorText: string): ParsedSelector;
|
|
10
9
|
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import type * as ESTree from 'eslint-codemod-utils';
|
|
3
|
+
type StyleFunctionName = 'css' | 'cssMap' | 'keyframes' | 'styled' | 'xcss';
|
|
4
|
+
export type StyleCall = {
|
|
5
|
+
importSource: string;
|
|
6
|
+
node: ESTree.CallExpression;
|
|
7
|
+
styleFunction: StyleFunctionName;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Visit imported style calls without resolving every CallExpression against its current scope.
|
|
11
|
+
*
|
|
12
|
+
* ESLint has already linked import bindings to all of their references. Building this index once
|
|
13
|
+
* per source file makes each styling rule proportional to the number of style calls instead of the
|
|
14
|
+
* number of all calls in the file.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getStyleCalls(context: Rule.RuleContext): readonly StyleCall[];
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type * as ESTree from 'eslint-codemod-utils';
|
|
2
|
+
import type { StyleCall } from './style-calls';
|
|
3
|
+
export declare function walkStyleCallProperties(styleCall: StyleCall, visitor: (property: ESTree.Property) => void, options?: {
|
|
4
|
+
includeTopLevel?: boolean;
|
|
5
|
+
}): void;
|
package/package.json
CHANGED