@atlaskit/eslint-plugin-ui-styling-standard 2.1.3 → 2.1.5

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 (41) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +18 -18
  3. package/dist/cjs/rules/no-container-queries/index.js +62 -13
  4. package/dist/cjs/rules/no-important-styles/index.js +79 -99
  5. package/dist/cjs/rules/no-nested-selectors/index.js +46 -32
  6. package/dist/cjs/rules/no-unsafe-selectors/index.js +87 -84
  7. package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +140 -101
  8. package/dist/cjs/rules/no-unsafe-selectors/walk-css-map.js +20 -50
  9. package/dist/cjs/rules/utils/create-rule-with-typed-config.js +29 -10
  10. package/dist/cjs/rules/utils/is-simple-selector.js +70 -0
  11. package/dist/cjs/rules/utils/parse-selector.js +3 -4
  12. package/dist/cjs/rules/utils/style-calls.js +127 -0
  13. package/dist/cjs/rules/utils/walk-style-call-properties.js +50 -0
  14. package/dist/es2019/rules/no-container-queries/index.js +53 -14
  15. package/dist/es2019/rules/no-important-styles/index.js +79 -70
  16. package/dist/es2019/rules/no-nested-selectors/index.js +36 -34
  17. package/dist/es2019/rules/no-unsafe-selectors/index.js +67 -87
  18. package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +29 -23
  19. package/dist/es2019/rules/no-unsafe-selectors/walk-css-map.js +4 -30
  20. package/dist/es2019/rules/utils/create-rule-with-typed-config.js +30 -10
  21. package/dist/es2019/rules/utils/is-simple-selector.js +63 -0
  22. package/dist/es2019/rules/utils/parse-selector.js +3 -4
  23. package/dist/es2019/rules/utils/style-calls.js +89 -0
  24. package/dist/es2019/rules/utils/walk-style-call-properties.js +23 -0
  25. package/dist/esm/rules/no-container-queries/index.js +63 -14
  26. package/dist/esm/rules/no-important-styles/index.js +80 -100
  27. package/dist/esm/rules/no-nested-selectors/index.js +46 -32
  28. package/dist/esm/rules/no-unsafe-selectors/index.js +88 -85
  29. package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +141 -102
  30. package/dist/esm/rules/no-unsafe-selectors/walk-css-map.js +19 -49
  31. package/dist/esm/rules/utils/create-rule-with-typed-config.js +30 -10
  32. package/dist/esm/rules/utils/is-simple-selector.js +64 -0
  33. package/dist/esm/rules/utils/parse-selector.js +3 -4
  34. package/dist/esm/rules/utils/style-calls.js +120 -0
  35. package/dist/esm/rules/utils/walk-style-call-properties.js +44 -0
  36. package/dist/types/rules/no-unsafe-selectors/walk-css-map.d.ts +1 -7
  37. package/dist/types/rules/utils/is-simple-selector.d.ts +11 -0
  38. package/dist/types/rules/utils/parse-selector.d.ts +1 -2
  39. package/dist/types/rules/utils/style-calls.d.ts +17 -0
  40. package/dist/types/rules/utils/walk-style-call-properties.d.ts +5 -0
  41. package/package.json +1 -1
@@ -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
+ }
@@ -11,8 +11,7 @@ var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
11
11
  var selectorCache = new WeakMap();
12
12
 
13
13
  /**
14
- * Parse each selector once per linted source file. Return a clone so selector traversal state is
15
- * not shared between rules.
14
+ * Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
16
15
  */
17
16
  function parseSelector(context, selectorText) {
18
17
  var sourceCode = (0, _contextCompat.getSourceCode)(context);
@@ -23,9 +22,9 @@ function parseSelector(context, selectorText) {
23
22
  }
24
23
  var cachedSelector = sourceFileCache.get(selectorText);
25
24
  if (cachedSelector) {
26
- return cachedSelector.clone();
25
+ return cachedSelector;
27
26
  }
28
27
  var selector = cssSelectorProcessor.astSync(selectorText);
29
28
  sourceFileCache.set(selectorText, selector);
30
- return selector.clone();
29
+ return selector;
31
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,5 +1,5 @@
1
1
  import estraverse from 'estraverse';
2
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
2
+ import { getScope, getSourceCode } from '@atlaskit/eslint-utils/context-compat';
3
3
  import { getImportSources, isCss, isCssMap, isKeyframes, isStyled, isXcss } from '@atlaskit/eslint-utils/is-supported-import';
4
4
  import { createLintRule } from '../utils/create-rule';
5
5
  const schema = [{
@@ -14,6 +14,50 @@ const schema = [{
14
14
  }
15
15
  }
16
16
  }];
17
+ const unwrapTypeScriptExpression = node => {
18
+ let expression = node;
19
+ while (expression.type === 'TSAsExpression' || expression.type === 'TSTypeAssertion' || expression.type === 'TSNonNullExpression' || expression.type === 'TSSatisfiesExpression') {
20
+ expression = expression.expression;
21
+ }
22
+ return expression;
23
+ };
24
+ const isStaticContainerKey = node => {
25
+ var _expression$expressio;
26
+ const expression = unwrapTypeScriptExpression(node);
27
+ if (expression.type === 'Literal') {
28
+ return typeof expression.value === 'string';
29
+ }
30
+ return expression.type === 'TemplateLiteral' && ((_expression$expressio = expression.expressions) === null || _expression$expressio === void 0 ? void 0 : _expression$expressio.length) === 0;
31
+ };
32
+ const isImportedContainerType = (node, context) => {
33
+ let expression = node;
34
+ while (expression.type === 'TSSatisfiesExpression') {
35
+ var _typeAnnotation$typeN;
36
+ const {
37
+ typeAnnotation
38
+ } = expression;
39
+ if ((typeAnnotation === null || typeAnnotation === void 0 ? void 0 : typeAnnotation.type) === 'TSTypeReference' && ((_typeAnnotation$typeN = typeAnnotation.typeName) === null || _typeAnnotation$typeN === void 0 ? void 0 : _typeAnnotation$typeN.type) === 'Identifier') {
40
+ const typeName = typeAnnotation.typeName.name;
41
+ const isImported = getSourceCode(context).ast.body.some(statement => statement.type === 'ImportDeclaration' && statement.source.value === '@atlaskit/css/at-rules/container' && statement.specifiers.some(specifier => specifier.type === 'ImportDefaultSpecifier' && specifier.local.name === typeName));
42
+ if (isImported) {
43
+ return true;
44
+ }
45
+ }
46
+ expression = expression.expression;
47
+ }
48
+ return false;
49
+ };
50
+ const isContainerQuery = node => {
51
+ var _expression$quasis;
52
+ const expression = unwrapTypeScriptExpression(node);
53
+ if (expression.type === 'Literal' && typeof expression.value === 'string') {
54
+ return expression.value.includes('@container');
55
+ }
56
+ return expression.type === 'TemplateLiteral' && Boolean((_expression$quasis = expression.quasis) === null || _expression$quasis === void 0 ? void 0 : _expression$quasis.some(quasi => {
57
+ var _quasi$value, _quasi$value$raw;
58
+ return (_quasi$value = quasi.value) === null || _quasi$value === void 0 ? void 0 : (_quasi$value$raw = _quasi$value.raw) === null || _quasi$value$raw === void 0 ? void 0 : _quasi$value$raw.includes('@container');
59
+ }));
60
+ };
17
61
  const rule = createLintRule({
18
62
  meta: {
19
63
  name: 'no-container-queries',
@@ -38,25 +82,20 @@ const rule = createLintRule({
38
82
  if (isCss(node.callee, references, importSources) || isStyled(node.callee, references, importSources) || isKeyframes(node.callee, references, importSources) || isCssMap(node.callee, references, importSources) || isXcss(node.callee, references, importSources)) {
39
83
  estraverse.traverse(node, {
40
84
  enter(node) {
41
- var _node$key$value, _node$key$quasis$;
42
85
  if (node.type !== 'Property') {
43
86
  return;
44
87
  }
45
- if (node.key.type !== 'Literal' && node.key.type !== 'TemplateLiteral') {
88
+ const key = node.key;
89
+ if (!isContainerQuery(key)) {
46
90
  return;
47
91
  }
48
- if (node.key.type === 'Literal' && (_node$key$value = node.key.value) !== null && _node$key$value !== void 0 && _node$key$value.toString().match(/@container/)) {
49
- context.report({
50
- node: node.key,
51
- messageId: 'no-container-queries'
52
- });
53
- }
54
- if (node.key.type === 'TemplateLiteral' && (_node$key$quasis$ = node.key.quasis[0]) !== null && _node$key$quasis$ !== void 0 && _node$key$quasis$.value.raw.match(/@container/)) {
55
- context.report({
56
- node: node.key,
57
- messageId: 'no-container-queries'
58
- });
92
+ if (isImportedContainerType(key, context) && isStaticContainerKey(key.expression)) {
93
+ return;
59
94
  }
95
+ context.report({
96
+ node: node.key,
97
+ messageId: 'no-container-queries'
98
+ });
60
99
  },
61
100
  /**
62
101
  * This is needed to handle unknown node types. Otherwise an error is thrown.
@@ -1,7 +1,7 @@
1
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
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
- CallExpression(node) {
32
+ Program() {
29
33
  const {
30
- references
31
- } = getScope(context, node);
32
- if (!hasStyleObjectArguments(node.callee, references, importSources)) {
33
- return;
34
- }
35
- walkProperties(node, propertyNode => {
36
- if (isImportant(propertyNode.value)) {
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: propertyNode.value,
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
- const IMPORTANT_SUFFIX_REGEX = /!\s*important\s*$/;
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
- // Iterate through object properties
62
- if ((_node$arguments = node.arguments) !== null && _node$arguments !== void 0 && _node$arguments.length) {
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
- * Recursively walk object properties to find nested Property nodes.
75
- */
76
- function walkObjectProperty(node, callback) {
77
- if (node.type === 'Property') {
78
- var _node$value;
79
- callback(node);
80
- // Recursively check nested objects
81
- if (((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'ObjectExpression' && node.value.properties) {
82
- for (const prop of node.value.properties) {
83
- walkObjectProperty(prop, callback);
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
- * Check if a value contains !important flag.
91
- * Uses fast string checks before regex to avoid regex overhead for common cases.
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
- if (node.type === 'TemplateLiteral') {
106
- var _node$quasis;
107
- if (!((_node$quasis = node.quasis) !== null && _node$quasis !== void 0 && _node$quasis.length)) {
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
- return false;
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
- CallExpression(node) {
53
- const {
54
- references
55
- } = getScope(context, node);
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
- const selectorText = getCssSelector(key);
65
- if (selectorText === null) {
66
- return;
67
- }
68
-
69
- // Ignore at-rules
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
- } catch (_) {
82
- // If it is not parsable then `no-unsafe-selectors` will give it an error,
83
- // we can ignore it here.
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
  }