@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.
Files changed (38) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +18 -18
  3. package/dist/cjs/rules/no-important-styles/index.js +79 -99
  4. package/dist/cjs/rules/no-nested-selectors/index.js +46 -32
  5. package/dist/cjs/rules/no-unsafe-selectors/index.js +87 -84
  6. package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +140 -101
  7. package/dist/cjs/rules/no-unsafe-selectors/walk-css-map.js +20 -50
  8. package/dist/cjs/rules/utils/create-rule-with-typed-config.js +29 -10
  9. package/dist/cjs/rules/utils/is-simple-selector.js +70 -0
  10. package/dist/cjs/rules/utils/parse-selector.js +3 -4
  11. package/dist/cjs/rules/utils/style-calls.js +127 -0
  12. package/dist/cjs/rules/utils/walk-style-call-properties.js +50 -0
  13. package/dist/es2019/rules/no-important-styles/index.js +79 -70
  14. package/dist/es2019/rules/no-nested-selectors/index.js +36 -34
  15. package/dist/es2019/rules/no-unsafe-selectors/index.js +67 -87
  16. package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +29 -23
  17. package/dist/es2019/rules/no-unsafe-selectors/walk-css-map.js +4 -30
  18. package/dist/es2019/rules/utils/create-rule-with-typed-config.js +30 -10
  19. package/dist/es2019/rules/utils/is-simple-selector.js +63 -0
  20. package/dist/es2019/rules/utils/parse-selector.js +3 -4
  21. package/dist/es2019/rules/utils/style-calls.js +89 -0
  22. package/dist/es2019/rules/utils/walk-style-call-properties.js +23 -0
  23. package/dist/esm/rules/no-important-styles/index.js +80 -100
  24. package/dist/esm/rules/no-nested-selectors/index.js +46 -32
  25. package/dist/esm/rules/no-unsafe-selectors/index.js +88 -85
  26. package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +141 -102
  27. package/dist/esm/rules/no-unsafe-selectors/walk-css-map.js +19 -49
  28. package/dist/esm/rules/utils/create-rule-with-typed-config.js +30 -10
  29. package/dist/esm/rules/utils/is-simple-selector.js +64 -0
  30. package/dist/esm/rules/utils/parse-selector.js +3 -4
  31. package/dist/esm/rules/utils/style-calls.js +120 -0
  32. package/dist/esm/rules/utils/walk-style-call-properties.js +44 -0
  33. package/dist/types/rules/no-unsafe-selectors/walk-css-map.d.ts +1 -7
  34. package/dist/types/rules/utils/is-simple-selector.d.ts +11 -0
  35. package/dist/types/rules/utils/parse-selector.d.ts +1 -2
  36. package/dist/types/rules/utils/style-calls.d.ts +17 -0
  37. package/dist/types/rules/utils/walk-style-call-properties.d.ts +5 -0
  38. package/package.json +1 -1
@@ -1,6 +1,26 @@
1
1
  import Ajv from 'ajv';
2
2
  import { getRuleUrl } from './get-rule-url';
3
+
4
+ /**
5
+ * A single shared Ajv instance is reused across every rule instead of constructing
6
+ * a new one each time a default config is needed. Ajv's constructor is expensive
7
+ * (it compiles its own meta-schemas), so we only ever want to pay for it once.
8
+ */
9
+ const ajv = new Ajv({
10
+ useDefaults: true
11
+ });
3
12
  export const createLintRuleWithTypedConfig = rule => {
13
+ /**
14
+ * Lazily-computed, cached default config.
15
+ *
16
+ * The default config is derived purely from the rule's (static) schema, so it is
17
+ * constant for the life of the process. Compiling the schema with Ajv involves
18
+ * runtime code generation (`new Function`), which is expensive — previously this
19
+ * ran on every `getDefaultConfig()` call, i.e. once per linted file. We now compute
20
+ * it at most once per rule and reuse the result.
21
+ */
22
+ let defaultConfig;
23
+
4
24
  /**
5
25
  * Gets the default config by running the same validator
6
26
  * ESLint does against an empty config object with
@@ -10,16 +30,16 @@ export const createLintRuleWithTypedConfig = rule => {
10
30
  * will be populated with the specified default value.
11
31
  */
12
32
  function getDefaultConfig() {
13
- const ajv = new Ajv({
14
- useDefaults: true
15
- });
16
- const validate = ajv.compile(rule.meta.schema);
17
- const config = {};
18
- /**
19
- * The `validate()` function mutates the config object.
20
- */
21
- validate(config);
22
- return config;
33
+ if (defaultConfig === undefined) {
34
+ const validate = ajv.compile(rule.meta.schema);
35
+ const config = {};
36
+ /**
37
+ * The `validate()` function mutates the config object.
38
+ */
39
+ validate(config);
40
+ defaultConfig = config;
41
+ }
42
+ return defaultConfig;
23
43
  }
24
44
  return {
25
45
  ...rule,
@@ -0,0 +1,63 @@
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, options = {}) {
6
+ let index = 0;
7
+ while (index < selectorText.length) {
8
+ index = skipWhitespace(selectorText, index);
9
+ const hasNesting = selectorText.charCodeAt(index) === 38;
10
+ if (hasNesting) {
11
+ index++;
12
+ if (selectorText.charCodeAt(index) === 38) {
13
+ return false;
14
+ }
15
+ } else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
16
+ return false;
17
+ }
18
+ let pseudoCount = 0;
19
+ while (selectorText.charCodeAt(index) === 58) {
20
+ const pseudoStart = index++;
21
+ if (selectorText.charCodeAt(index) === 58) {
22
+ index++;
23
+ }
24
+ const nameStart = index;
25
+ while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
26
+ index++;
27
+ }
28
+ if (index === nameStart || selectorText.charCodeAt(index) === 40) {
29
+ return false;
30
+ }
31
+ if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
32
+ return false;
33
+ }
34
+ pseudoCount++;
35
+ }
36
+ if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
37
+ return false;
38
+ }
39
+ index = skipWhitespace(selectorText, index);
40
+ if (index === selectorText.length) {
41
+ return true;
42
+ }
43
+ if (selectorText.charCodeAt(index) !== 44) {
44
+ return false;
45
+ }
46
+ index++;
47
+ }
48
+ return false;
49
+ }
50
+ function skipWhitespace(value, start) {
51
+ let index = start;
52
+ while (index < value.length) {
53
+ const character = value.charCodeAt(index);
54
+ if (character !== 32 && (character < 9 || character > 13)) {
55
+ break;
56
+ }
57
+ index++;
58
+ }
59
+ return index;
60
+ }
61
+ function isPseudoNameCharacter(character) {
62
+ return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
63
+ }
@@ -4,8 +4,7 @@ const cssSelectorProcessor = cssSelectorParser();
4
4
  const selectorCache = new WeakMap();
5
5
 
6
6
  /**
7
- * Parse each selector once per linted source file. Return a clone so selector traversal state is
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
  const sourceCode = getSourceCode(context);
@@ -16,9 +15,9 @@ export function parseSelector(context, selectorText) {
16
15
  }
17
16
  const cachedSelector = sourceFileCache.get(selectorText);
18
17
  if (cachedSelector) {
19
- return cachedSelector.clone();
18
+ return cachedSelector;
20
19
  }
21
20
  const selector = cssSelectorProcessor.astSync(selectorText);
22
21
  sourceFileCache.set(selectorText, selector);
23
- return selector.clone();
22
+ return selector;
24
23
  }
@@ -0,0 +1,89 @@
1
+ import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
2
+ const styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
3
+ const defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
4
+ const styleCallsBySourceCode = new WeakMap();
5
+
6
+ /**
7
+ * Visit imported style calls without resolving every CallExpression against its current scope.
8
+ *
9
+ * ESLint has already linked import bindings to all of their references. Building this index once
10
+ * per source file makes each styling rule proportional to the number of style calls instead of the
11
+ * number of all calls in the file.
12
+ */
13
+ export function getStyleCalls(context) {
14
+ const sourceCode = getSourceCode(context);
15
+ let styleCalls = styleCallsBySourceCode.get(sourceCode);
16
+ if (!styleCalls) {
17
+ styleCalls = buildStyleCallIndex(sourceCode);
18
+ styleCallsBySourceCode.set(sourceCode, styleCalls);
19
+ }
20
+ return styleCalls;
21
+ }
22
+ function buildStyleCallIndex(sourceCode) {
23
+ const styleCalls = [];
24
+ const seenCalls = new Set();
25
+ const {
26
+ scopeManager
27
+ } = sourceCode;
28
+ if (!scopeManager) {
29
+ return styleCalls;
30
+ }
31
+ for (const statement of sourceCode.ast.body) {
32
+ if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
33
+ continue;
34
+ }
35
+ const importSource = statement.source.value;
36
+ for (const specifier of statement.specifiers) {
37
+ const styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
38
+ if (!styleFunction) {
39
+ continue;
40
+ }
41
+ const [variable] = scopeManager.getDeclaredVariables(specifier);
42
+ if (!variable) {
43
+ continue;
44
+ }
45
+ for (const reference of variable.references) {
46
+ const call = getStyleCall(reference.identifier, styleFunction);
47
+ if (!call || seenCalls.has(call)) {
48
+ continue;
49
+ }
50
+ seenCalls.add(call);
51
+ styleCalls.push({
52
+ importSource,
53
+ node: call,
54
+ styleFunction
55
+ });
56
+ }
57
+ }
58
+ }
59
+ return styleCalls;
60
+ }
61
+ function getStyleFunctionFromSpecifier(specifier, importSource) {
62
+ if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
63
+ return specifier.imported.name;
64
+ }
65
+ if (specifier.type !== 'ImportDefaultSpecifier') {
66
+ return null;
67
+ }
68
+ if (styleFunctionNames.has(specifier.local.name)) {
69
+ return specifier.local.name;
70
+ }
71
+ return defaultStyledImportSources.has(importSource) ? 'styled' : null;
72
+ }
73
+ function getStyleCall(identifier, styleFunction) {
74
+ var _parent2, _parent$parent;
75
+ let callee = identifier;
76
+ let parent = callee.parent;
77
+ while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
78
+ var _parent;
79
+ callee = parent;
80
+ parent = callee.parent;
81
+ }
82
+ if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
83
+ return null;
84
+ }
85
+ if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
86
+ return parent.parent;
87
+ }
88
+ return parent;
89
+ }
@@ -0,0 +1,23 @@
1
+ export function walkStyleCallProperties(styleCall, visitor, options) {
2
+ const skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
3
+ for (const argument of styleCall.node.arguments) {
4
+ if (argument.type === 'ObjectExpression') {
5
+ walkStyleObject(argument, visitor, skipTopLevel);
6
+ } else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
7
+ walkStyleObject(argument.body, visitor, skipTopLevel);
8
+ }
9
+ }
10
+ }
11
+ function walkStyleObject(object, visitor, skipVisitor) {
12
+ for (const property of object.properties) {
13
+ if (property.type !== 'Property') {
14
+ continue;
15
+ }
16
+ if (!skipVisitor) {
17
+ visitor(property);
18
+ }
19
+ if (property.value.type === 'ObjectExpression') {
20
+ walkStyleObject(property.value, visitor, false);
21
+ }
22
+ }
23
+ }
@@ -1,10 +1,7 @@
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
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
5
- import { hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
1
+ import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
6
2
  import { importSources } from '@atlaskit/eslint-utils/schema';
7
3
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
4
+ import { getStyleCalls } from '../utils/style-calls';
8
5
  var rule = createLintRuleWithTypedConfig({
9
6
  meta: {
10
7
  name: 'no-important-styles',
@@ -26,118 +23,101 @@ var rule = createLintRuleWithTypedConfig({
26
23
  },
27
24
  create: function create(context, _ref) {
28
25
  var importSources = _ref.importSources;
26
+ var sourceCode = getSourceCode(context);
27
+ if (!sourceCode.text.includes('important')) {
28
+ return {};
29
+ }
29
30
  return {
30
- CallExpression: function CallExpression(node) {
31
- var _getScope = getScope(context, node),
32
- references = _getScope.references;
33
- if (!hasStyleObjectArguments(node.callee, references, importSources)) {
34
- return;
35
- }
36
- walkProperties(node, function (propertyNode) {
37
- if (isImportant(propertyNode.value)) {
31
+ Program: function Program() {
32
+ var text = sourceCode.text;
33
+ var reportedValues = new Set();
34
+ var importantIndex = text.indexOf('important');
35
+ while (importantIndex !== -1) {
36
+ var value = findStyleValue(sourceCode, importantIndex);
37
+ if (value && !reportedValues.has(value) && isImportant(value) && isInSupportedStyleCall(context, value, importSources)) {
38
+ reportedValues.add(value);
38
39
  context.report({
39
- node: propertyNode.value,
40
+ node: value,
40
41
  messageId: 'no-important-styles'
41
42
  });
42
43
  }
43
- });
44
+ importantIndex = text.indexOf('important', importantIndex + 9);
45
+ }
44
46
  }
45
47
  };
46
48
  }
47
49
  });
48
50
  export default rule;
49
- var IMPORTANT_SUFFIX_REGEX = /!\s*important\s*$/;
50
-
51
- /**
52
- * Walk through all Property nodes in the AST tree recursively.
53
- * This is faster than esquery for this specific use case as it avoids
54
- * selector compilation and executes a simple depth-first traversal.
55
- */
56
- function walkProperties(node, callback) {
57
- var _node$arguments;
58
- if (node.type === 'Property' && node.value) {
59
- callback(node);
51
+ function isImportant(node) {
52
+ if (node.type === 'Literal') {
53
+ return typeof node.value === 'string' && hasImportantSuffix(node.value);
60
54
  }
61
-
62
- // Iterate through object properties
63
- if ((_node$arguments = node.arguments) !== null && _node$arguments !== void 0 && _node$arguments.length) {
64
- var _iterator = _createForOfIteratorHelper(node.arguments),
65
- _step;
66
- try {
67
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
68
- var arg = _step.value;
69
- if (arg.type === 'ObjectExpression' && arg.properties) {
70
- var _iterator2 = _createForOfIteratorHelper(arg.properties),
71
- _step2;
72
- try {
73
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
74
- var prop = _step2.value;
75
- walkObjectProperty(prop, callback);
76
- }
77
- } catch (err) {
78
- _iterator2.e(err);
79
- } finally {
80
- _iterator2.f();
81
- }
82
- }
83
- }
84
- } catch (err) {
85
- _iterator.e(err);
86
- } finally {
87
- _iterator.f();
88
- }
55
+ if (node.type === 'TemplateLiteral') {
56
+ var lastQuasi = node.quasis[node.quasis.length - 1];
57
+ return Boolean(lastQuasi && hasImportantSuffix(lastQuasi.value.raw));
89
58
  }
59
+ return false;
90
60
  }
91
-
92
- /**
93
- * Recursively walk object properties to find nested Property nodes.
94
- */
95
- function walkObjectProperty(node, callback) {
96
- if (node.type === 'Property') {
97
- var _node$value;
98
- callback(node);
99
- // Recursively check nested objects
100
- if (((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'ObjectExpression' && node.value.properties) {
101
- var _iterator3 = _createForOfIteratorHelper(node.value.properties),
102
- _step3;
103
- try {
104
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
105
- var prop = _step3.value;
106
- walkObjectProperty(prop, callback);
107
- }
108
- } catch (err) {
109
- _iterator3.e(err);
110
- } finally {
111
- _iterator3.f();
61
+ function findStyleValue(sourceCode, index) {
62
+ var node = sourceCode.getNodeByRangeIndex(index);
63
+ while (node && node.type !== 'Literal' && node.type !== 'TemplateLiteral') {
64
+ node = node.parent;
65
+ }
66
+ return node;
67
+ }
68
+ function isInSupportedStyleCall(context, value, importSources) {
69
+ var call = findContainingCall(value);
70
+ return Boolean(call && getStyleCalls(context).some(function (styleCall) {
71
+ return styleCall.node === call && importSources.includes(styleCall.importSource);
72
+ }));
73
+ }
74
+ function findContainingCall(value) {
75
+ var _property, _object;
76
+ var property = value.parent;
77
+ if (((_property = property) === null || _property === void 0 ? void 0 : _property.type) !== 'Property' || property.value !== value) {
78
+ return null;
79
+ }
80
+ var object = property.parent;
81
+ if (((_object = object) === null || _object === void 0 ? void 0 : _object.type) !== 'ObjectExpression') {
82
+ return null;
83
+ }
84
+ while (object) {
85
+ var _parent$parent;
86
+ var parent = object.parent;
87
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'Property' && parent.value === object) {
88
+ var _object2;
89
+ property = parent;
90
+ object = property.parent;
91
+ if (((_object2 = object) === null || _object2 === void 0 ? void 0 : _object2.type) !== 'ObjectExpression') {
92
+ return null;
112
93
  }
94
+ continue;
95
+ }
96
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'CallExpression' && parent.arguments.includes(object)) {
97
+ return parent;
113
98
  }
99
+ 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)) {
100
+ return parent.parent;
101
+ }
102
+ return null;
114
103
  }
104
+ return null;
115
105
  }
116
-
117
- /**
118
- * Check if a value contains !important flag.
119
- * Uses fast string checks before regex to avoid regex overhead for common cases.
120
- */
121
- function isImportant(node) {
122
- if (node.type === 'Literal') {
123
- if (typeof node.value !== 'string') {
124
- return false;
125
- }
126
- // Fast check: string must end with 'important' (after optional whitespace)
127
- var len = node.value.length;
128
- if (len < 10) {
129
- return false;
130
- } // "!important" is 10 chars minimum
131
- return IMPORTANT_SUFFIX_REGEX.test(node.value);
106
+ function hasImportantSuffix(value) {
107
+ var index = value.length - 1;
108
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
109
+ index--;
132
110
  }
133
- if (node.type === 'TemplateLiteral') {
134
- var _node$quasis;
135
- if (!((_node$quasis = node.quasis) !== null && _node$quasis !== void 0 && _node$quasis.length)) {
136
- return false;
137
- }
138
- // Only check the last quasi's raw value (most likely to contain the !important)
139
- var lastQuasi = node.quasis[node.quasis.length - 1];
140
- return IMPORTANT_SUFFIX_REGEX.test(lastQuasi.value.raw);
111
+ var importantStart = index - 8;
112
+ if (importantStart < 0 || value.slice(importantStart, index + 1) !== 'important') {
113
+ return false;
141
114
  }
142
- return false;
115
+ index = importantStart - 1;
116
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
117
+ index--;
118
+ }
119
+ return value.charCodeAt(index) === 33;
120
+ }
121
+ function isWhitespace(character) {
122
+ return character === 32 || character >= 9 && character <= 13;
143
123
  }
@@ -1,11 +1,14 @@
1
1
  import _toArray from "@babel/runtime/helpers/toArray";
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; } }
2
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; }
3
5
  import { isNesting, isPseudo } from 'postcss-selector-parser';
4
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
5
6
  import { importSources } from '@atlaskit/eslint-utils/schema';
6
- import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
7
7
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
8
+ import { isSimpleSelector } from '../utils/is-simple-selector';
8
9
  import { parseSelector } from '../utils/parse-selector';
10
+ import { getStyleCalls } from '../utils/style-calls';
11
+ import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
9
12
  var getCssSelector = function getCssSelector(key) {
10
13
  if (key.type === 'Literal' && typeof key.value === 'string') {
11
14
  return key.value;
@@ -54,38 +57,49 @@ var rule = createLintRuleWithTypedConfig({
54
57
  create: function create(context, _ref) {
55
58
  var importSources = _ref.importSources;
56
59
  return {
57
- CallExpression: function CallExpression(node) {
58
- var _getScope = getScope(context, node),
59
- references = _getScope.references;
60
- walkStyleProperties(node, references, importSources, function (_ref2) {
61
- var key = _ref2.key,
62
- value = _ref2.value;
63
- if (value.type !== 'ObjectExpression') {
64
- // If the value is a CSS object, safe to assume we're at a CSS selector
65
- return;
66
- }
67
- var selectorText = getCssSelector(key);
68
- if (selectorText === null) {
69
- return;
70
- }
71
-
72
- // Ignore at-rules
73
- if (selectorText.includes('@')) {
74
- return;
75
- }
76
- try {
77
- var selectorList = parseSelector(context, selectorText);
78
- if (!selectorList.nodes.every(isAllowedSelector)) {
79
- context.report({
80
- messageId: 'no-nested-selectors',
81
- node: key
82
- });
60
+ Program: function Program() {
61
+ var _iterator = _createForOfIteratorHelper(getStyleCalls(context)),
62
+ _step;
63
+ try {
64
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
65
+ var styleCall = _step.value;
66
+ if (!importSources.includes(styleCall.importSource)) {
67
+ continue;
83
68
  }
84
- } catch (_) {
85
- // If it is not parsable then `no-unsafe-selectors` will give it an error,
86
- // we can ignore it here.
69
+ walkStyleCallProperties(styleCall, function (_ref2) {
70
+ var key = _ref2.key,
71
+ value = _ref2.value;
72
+ if (value.type !== 'ObjectExpression') {
73
+ return;
74
+ }
75
+ var selectorText = getCssSelector(key);
76
+ if (selectorText === null || selectorText.includes('@')) {
77
+ return;
78
+ }
79
+ if (isSimpleSelector(selectorText, {
80
+ allowBareNesting: true,
81
+ allowLeadingPseudo: true
82
+ })) {
83
+ return;
84
+ }
85
+ try {
86
+ var selectorList = parseSelector(context, selectorText);
87
+ if (!selectorList.nodes.every(isAllowedSelector)) {
88
+ context.report({
89
+ messageId: 'no-nested-selectors',
90
+ node: key
91
+ });
92
+ }
93
+ } catch (_unused) {
94
+ // `no-unsafe-selectors` reports unparsable selectors.
95
+ }
96
+ });
87
97
  }
88
- });
98
+ } catch (err) {
99
+ _iterator.e(err);
100
+ } finally {
101
+ _iterator.f();
102
+ }
89
103
  }
90
104
  };
91
105
  }