@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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 2.1.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [`fdedfb06aa229`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/fdedfb06aa229) -
8
+ Allow canonical typed media queries and typed container-query exceptions in styling lint rules.
9
+ - Updated dependencies
10
+
11
+ ## 2.1.4
12
+
13
+ ### Patch Changes
14
+
15
+ - [`4b07551b64b89`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/4b07551b64b89) -
16
+ Improve the performance of the `no-important-styles`, `no-nested-selectors`, and
17
+ `no-unsafe-selectors` rules.
18
+
3
19
  ## 2.1.3
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -42,24 +42,24 @@ module.exports = {
42
42
 
43
43
  | Rule | Description | Recommended | Fixable | Suggestions |
44
44
  | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
45
- | <a href="./src/rules/atlaskit-theme/README.md">atlaskit-theme</a> | Ban certain usages of `@atlaskit/theme` that `@compiled/react` does not understand | Yes | | |
46
- | <a href="./src/rules/convert-props-syntax/README.md">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
47
- | <a href="./src/rules/enforce-style-prop/README.md">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
48
- | <a href="./src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
49
- | <a href="./src/rules/no-array-arguments/README.md">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
50
- | <a href="./src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
51
- | <a href="./src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
52
- | <a href="./src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
53
- | <a href="./src/rules/no-exported-styles/README.md">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
54
- | <a href="./src/rules/no-global-styles/README.md">no-global-styles</a> | Prevents global styles through CSS-in-JS or CSS module imports | Yes | | |
55
- | <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
56
- | <a href="./src/rules/no-imported-style-values/README.md">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
57
- | <a href="./src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
58
- | <a href="./src/rules/no-styled/README.md">no-styled</a> | Disallows usage of the `styled` imports | Yes | | |
59
- | <a href="./src/rules/no-unsafe-selectors/README.md">no-unsafe-selectors</a> | Disallows use of nested styles in `css` functions. | Yes | Yes | Yes |
60
- | <a href="./src/rules/no-unsafe-values/README.md">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
61
- | <a href="./src/rules/no-unused-cssmap-properties/README.md">no-unused-cssmap-properties</a> | Detects unused properties in cssMap style objects that are not exported. Helps maintain clean code by identifying style variants that are defined but never used. | Yes | | |
62
- | <a href="./src/rules/use-compiled/README.md">use-compiled</a> | Ensures usage of `@compiled/react` or `@atlaskit/css` instead of other CSS-in-JS libraries | Yes | Yes | |
45
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/atlaskit-theme/README.md">atlaskit-theme</a> | Ban certain usages of `@atlaskit/theme` that `@compiled/react` does not understand | Yes | | |
46
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/convert-props-syntax/README.md">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
47
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/enforce-style-prop/README.md">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
48
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
49
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-array-arguments/README.md">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
50
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
51
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
52
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
53
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-exported-styles/README.md">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
54
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-global-styles/README.md">no-global-styles</a> | Prevents global styles through CSS-in-JS or CSS module imports | Yes | | |
55
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
56
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-imported-style-values/README.md">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
57
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
58
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-styled/README.md">no-styled</a> | Disallows usage of the `styled` imports | Yes | | |
59
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unsafe-selectors/README.md">no-unsafe-selectors</a> | Disallows use of nested styles in `css` functions. | Yes | Yes | Yes |
60
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unsafe-values/README.md">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
61
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unused-cssmap-properties/README.md">no-unused-cssmap-properties</a> | Detects unused properties in cssMap style objects that are not exported. Helps maintain clean code by identifying style variants that are defined but never used. | Yes | | |
62
+ | <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/use-compiled/README.md">use-compiled</a> | Ensures usage of `@compiled/react` or `@atlaskit/css` instead of other CSS-in-JS libraries | Yes | Yes | |
63
63
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
64
64
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-css-tagged-template-expression/usage">@atlaskit/design-system/no-css-tagged-template-expression</a> | Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
65
65
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-keyframes-tagged-template-expression/usage">@atlaskit/design-system/no-keyframes-tagged-template-expression</a> | Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
@@ -21,6 +21,60 @@ var schema = [{
21
21
  }
22
22
  }
23
23
  }];
24
+ var unwrapTypeScriptExpression = function unwrapTypeScriptExpression(node) {
25
+ var expression = node;
26
+ while (expression.type === 'TSAsExpression' || expression.type === 'TSTypeAssertion' || expression.type === 'TSNonNullExpression' || expression.type === 'TSSatisfiesExpression') {
27
+ expression = expression.expression;
28
+ }
29
+ return expression;
30
+ };
31
+ var isStaticContainerKey = function isStaticContainerKey(node) {
32
+ var _expression$expressio;
33
+ var expression = unwrapTypeScriptExpression(node);
34
+ if (expression.type === 'Literal') {
35
+ return typeof expression.value === 'string';
36
+ }
37
+ return expression.type === 'TemplateLiteral' && ((_expression$expressio = expression.expressions) === null || _expression$expressio === void 0 ? void 0 : _expression$expressio.length) === 0;
38
+ };
39
+ var isImportedContainerType = function isImportedContainerType(node, context) {
40
+ var expression = node;
41
+ var _loop = function _loop() {
42
+ var _typeAnnotation$typeN;
43
+ var _expression = expression,
44
+ typeAnnotation = _expression.typeAnnotation;
45
+ 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') {
46
+ var typeName = typeAnnotation.typeName.name;
47
+ var isImported = (0, _contextCompat.getSourceCode)(context).ast.body.some(function (statement) {
48
+ return statement.type === 'ImportDeclaration' && statement.source.value === '@atlaskit/css/at-rules/container' && statement.specifiers.some(function (specifier) {
49
+ return specifier.type === 'ImportDefaultSpecifier' && specifier.local.name === typeName;
50
+ });
51
+ });
52
+ if (isImported) {
53
+ return {
54
+ v: true
55
+ };
56
+ }
57
+ }
58
+ expression = expression.expression;
59
+ },
60
+ _ret;
61
+ while (expression.type === 'TSSatisfiesExpression') {
62
+ _ret = _loop();
63
+ if (_ret) return _ret.v;
64
+ }
65
+ return false;
66
+ };
67
+ var isContainerQuery = function isContainerQuery(node) {
68
+ var _expression$quasis;
69
+ var expression = unwrapTypeScriptExpression(node);
70
+ if (expression.type === 'Literal' && typeof expression.value === 'string') {
71
+ return expression.value.includes('@container');
72
+ }
73
+ return expression.type === 'TemplateLiteral' && Boolean((_expression$quasis = expression.quasis) === null || _expression$quasis === void 0 ? void 0 : _expression$quasis.some(function (quasi) {
74
+ var _quasi$value;
75
+ return (_quasi$value = quasi.value) === null || _quasi$value === void 0 || (_quasi$value = _quasi$value.raw) === null || _quasi$value === void 0 ? void 0 : _quasi$value.includes('@container');
76
+ }));
77
+ };
24
78
  var rule = (0, _createRule.createLintRule)({
25
79
  meta: {
26
80
  name: 'no-container-queries',
@@ -44,25 +98,20 @@ var rule = (0, _createRule.createLintRule)({
44
98
  if ((0, _isSupportedImport.isCss)(node.callee, references, importSources) || (0, _isSupportedImport.isStyled)(node.callee, references, importSources) || (0, _isSupportedImport.isKeyframes)(node.callee, references, importSources) || (0, _isSupportedImport.isCssMap)(node.callee, references, importSources) || (0, _isSupportedImport.isXcss)(node.callee, references, importSources)) {
45
99
  _estraverse.default.traverse(node, {
46
100
  enter: function enter(node) {
47
- var _node$key$value, _node$key$quasis$;
48
101
  if (node.type !== 'Property') {
49
102
  return;
50
103
  }
51
- if (node.key.type !== 'Literal' && node.key.type !== 'TemplateLiteral') {
104
+ var key = node.key;
105
+ if (!isContainerQuery(key)) {
52
106
  return;
53
107
  }
54
- if (node.key.type === 'Literal' && (_node$key$value = node.key.value) !== null && _node$key$value !== void 0 && _node$key$value.toString().match(/@container/)) {
55
- context.report({
56
- node: node.key,
57
- messageId: 'no-container-queries'
58
- });
59
- }
60
- 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/)) {
61
- context.report({
62
- node: node.key,
63
- messageId: 'no-container-queries'
64
- });
108
+ if (isImportedContainerType(key, context) && isStaticContainerKey(key.expression)) {
109
+ return;
65
110
  }
111
+ context.report({
112
+ node: node.key,
113
+ messageId: 'no-container-queries'
114
+ });
66
115
  },
67
116
  /**
68
117
  * This is needed to handle unknown node types. Otherwise an error is thrown.
@@ -5,12 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
8
- var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
9
8
  var _schema = require("@atlaskit/eslint-utils/schema");
10
9
  var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
11
- 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; } } }; }
12
- 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; } }
13
- 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
+ var _styleCalls = require("../utils/style-calls");
14
11
  var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
15
12
  meta: {
16
13
  name: 'no-important-styles',
@@ -32,118 +29,101 @@ var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
32
29
  },
33
30
  create: function create(context, _ref) {
34
31
  var importSources = _ref.importSources;
32
+ var sourceCode = (0, _contextCompat.getSourceCode)(context);
33
+ if (!sourceCode.text.includes('important')) {
34
+ return {};
35
+ }
35
36
  return {
36
- CallExpression: function CallExpression(node) {
37
- var _getScope = (0, _contextCompat.getScope)(context, node),
38
- references = _getScope.references;
39
- if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) {
40
- return;
41
- }
42
- walkProperties(node, function (propertyNode) {
43
- if (isImportant(propertyNode.value)) {
37
+ Program: function Program() {
38
+ var text = sourceCode.text;
39
+ var reportedValues = new Set();
40
+ var importantIndex = text.indexOf('important');
41
+ while (importantIndex !== -1) {
42
+ var value = findStyleValue(sourceCode, importantIndex);
43
+ if (value && !reportedValues.has(value) && isImportant(value) && isInSupportedStyleCall(context, value, importSources)) {
44
+ reportedValues.add(value);
44
45
  context.report({
45
- node: propertyNode.value,
46
+ node: value,
46
47
  messageId: 'no-important-styles'
47
48
  });
48
49
  }
49
- });
50
+ importantIndex = text.indexOf('important', importantIndex + 9);
51
+ }
50
52
  }
51
53
  };
52
54
  }
53
55
  });
54
56
  var _default = exports.default = rule;
55
- var IMPORTANT_SUFFIX_REGEX = /!\s*important\s*$/;
56
-
57
- /**
58
- * Walk through all Property nodes in the AST tree recursively.
59
- * This is faster than esquery for this specific use case as it avoids
60
- * selector compilation and executes a simple depth-first traversal.
61
- */
62
- function walkProperties(node, callback) {
63
- var _node$arguments;
64
- if (node.type === 'Property' && node.value) {
65
- callback(node);
57
+ function isImportant(node) {
58
+ if (node.type === 'Literal') {
59
+ return typeof node.value === 'string' && hasImportantSuffix(node.value);
66
60
  }
67
-
68
- // Iterate through object properties
69
- if ((_node$arguments = node.arguments) !== null && _node$arguments !== void 0 && _node$arguments.length) {
70
- var _iterator = _createForOfIteratorHelper(node.arguments),
71
- _step;
72
- try {
73
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
74
- var arg = _step.value;
75
- if (arg.type === 'ObjectExpression' && arg.properties) {
76
- var _iterator2 = _createForOfIteratorHelper(arg.properties),
77
- _step2;
78
- try {
79
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
80
- var prop = _step2.value;
81
- walkObjectProperty(prop, callback);
82
- }
83
- } catch (err) {
84
- _iterator2.e(err);
85
- } finally {
86
- _iterator2.f();
87
- }
88
- }
89
- }
90
- } catch (err) {
91
- _iterator.e(err);
92
- } finally {
93
- _iterator.f();
94
- }
61
+ if (node.type === 'TemplateLiteral') {
62
+ var lastQuasi = node.quasis[node.quasis.length - 1];
63
+ return Boolean(lastQuasi && hasImportantSuffix(lastQuasi.value.raw));
95
64
  }
65
+ return false;
96
66
  }
97
-
98
- /**
99
- * Recursively walk object properties to find nested Property nodes.
100
- */
101
- function walkObjectProperty(node, callback) {
102
- if (node.type === 'Property') {
103
- var _node$value;
104
- callback(node);
105
- // Recursively check nested objects
106
- if (((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'ObjectExpression' && node.value.properties) {
107
- var _iterator3 = _createForOfIteratorHelper(node.value.properties),
108
- _step3;
109
- try {
110
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
111
- var prop = _step3.value;
112
- walkObjectProperty(prop, callback);
113
- }
114
- } catch (err) {
115
- _iterator3.e(err);
116
- } finally {
117
- _iterator3.f();
67
+ function findStyleValue(sourceCode, index) {
68
+ var node = sourceCode.getNodeByRangeIndex(index);
69
+ while (node && node.type !== 'Literal' && node.type !== 'TemplateLiteral') {
70
+ node = node.parent;
71
+ }
72
+ return node;
73
+ }
74
+ function isInSupportedStyleCall(context, value, importSources) {
75
+ var call = findContainingCall(value);
76
+ return Boolean(call && (0, _styleCalls.getStyleCalls)(context).some(function (styleCall) {
77
+ return styleCall.node === call && importSources.includes(styleCall.importSource);
78
+ }));
79
+ }
80
+ function findContainingCall(value) {
81
+ var _property, _object;
82
+ var property = value.parent;
83
+ if (((_property = property) === null || _property === void 0 ? void 0 : _property.type) !== 'Property' || property.value !== value) {
84
+ return null;
85
+ }
86
+ var object = property.parent;
87
+ if (((_object = object) === null || _object === void 0 ? void 0 : _object.type) !== 'ObjectExpression') {
88
+ return null;
89
+ }
90
+ while (object) {
91
+ var _parent$parent;
92
+ var parent = object.parent;
93
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'Property' && parent.value === object) {
94
+ var _object2;
95
+ property = parent;
96
+ object = property.parent;
97
+ if (((_object2 = object) === null || _object2 === void 0 ? void 0 : _object2.type) !== 'ObjectExpression') {
98
+ return null;
118
99
  }
100
+ continue;
119
101
  }
102
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'CallExpression' && parent.arguments.includes(object)) {
103
+ return parent;
104
+ }
105
+ 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)) {
106
+ return parent.parent;
107
+ }
108
+ return null;
120
109
  }
110
+ return null;
121
111
  }
122
-
123
- /**
124
- * Check if a value contains !important flag.
125
- * Uses fast string checks before regex to avoid regex overhead for common cases.
126
- */
127
- function isImportant(node) {
128
- if (node.type === 'Literal') {
129
- if (typeof node.value !== 'string') {
130
- return false;
131
- }
132
- // Fast check: string must end with 'important' (after optional whitespace)
133
- var len = node.value.length;
134
- if (len < 10) {
135
- return false;
136
- } // "!important" is 10 chars minimum
137
- return IMPORTANT_SUFFIX_REGEX.test(node.value);
112
+ function hasImportantSuffix(value) {
113
+ var index = value.length - 1;
114
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
115
+ index--;
138
116
  }
139
- if (node.type === 'TemplateLiteral') {
140
- var _node$quasis;
141
- if (!((_node$quasis = node.quasis) !== null && _node$quasis !== void 0 && _node$quasis.length)) {
142
- return false;
143
- }
144
- // Only check the last quasi's raw value (most likely to contain the !important)
145
- var lastQuasi = node.quasis[node.quasis.length - 1];
146
- return IMPORTANT_SUFFIX_REGEX.test(lastQuasi.value.raw);
117
+ var importantStart = index - 8;
118
+ if (importantStart < 0 || value.slice(importantStart, index + 1) !== 'important') {
119
+ return false;
147
120
  }
148
- return false;
121
+ index = importantStart - 1;
122
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
123
+ index--;
124
+ }
125
+ return value.charCodeAt(index) === 33;
126
+ }
127
+ function isWhitespace(character) {
128
+ return character === 32 || character >= 9 && character <= 13;
149
129
  }
@@ -7,11 +7,14 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.default = void 0;
8
8
  var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
9
9
  var _postcssSelectorParser = require("postcss-selector-parser");
10
- var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
11
10
  var _schema = require("@atlaskit/eslint-utils/schema");
12
- var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
13
11
  var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
12
+ var _isSimpleSelector = require("../utils/is-simple-selector");
14
13
  var _parseSelector = require("../utils/parse-selector");
14
+ var _styleCalls = require("../utils/style-calls");
15
+ var _walkStyleCallProperties = require("../utils/walk-style-call-properties");
16
+ 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; } } }; }
17
+ 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; } }
15
18
  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; }
16
19
  var getCssSelector = function getCssSelector(key) {
17
20
  if (key.type === 'Literal' && typeof key.value === 'string') {
@@ -61,38 +64,49 @@ var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
61
64
  create: function create(context, _ref) {
62
65
  var importSources = _ref.importSources;
63
66
  return {
64
- CallExpression: function CallExpression(node) {
65
- var _getScope = (0, _contextCompat.getScope)(context, node),
66
- references = _getScope.references;
67
- (0, _walkStyleProperties.walkStyleProperties)(node, references, importSources, function (_ref2) {
68
- var key = _ref2.key,
69
- value = _ref2.value;
70
- if (value.type !== 'ObjectExpression') {
71
- // If the value is a CSS object, safe to assume we're at a CSS selector
72
- return;
73
- }
74
- var selectorText = getCssSelector(key);
75
- if (selectorText === null) {
76
- return;
77
- }
78
-
79
- // Ignore at-rules
80
- if (selectorText.includes('@')) {
81
- return;
82
- }
83
- try {
84
- var selectorList = (0, _parseSelector.parseSelector)(context, selectorText);
85
- if (!selectorList.nodes.every(isAllowedSelector)) {
86
- context.report({
87
- messageId: 'no-nested-selectors',
88
- node: key
89
- });
67
+ Program: function Program() {
68
+ var _iterator = _createForOfIteratorHelper((0, _styleCalls.getStyleCalls)(context)),
69
+ _step;
70
+ try {
71
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
72
+ var styleCall = _step.value;
73
+ if (!importSources.includes(styleCall.importSource)) {
74
+ continue;
90
75
  }
91
- } catch (_) {
92
- // If it is not parsable then `no-unsafe-selectors` will give it an error,
93
- // we can ignore it here.
76
+ (0, _walkStyleCallProperties.walkStyleCallProperties)(styleCall, function (_ref2) {
77
+ var key = _ref2.key,
78
+ value = _ref2.value;
79
+ if (value.type !== 'ObjectExpression') {
80
+ return;
81
+ }
82
+ var selectorText = getCssSelector(key);
83
+ if (selectorText === null || selectorText.includes('@')) {
84
+ return;
85
+ }
86
+ if ((0, _isSimpleSelector.isSimpleSelector)(selectorText, {
87
+ allowBareNesting: true,
88
+ allowLeadingPseudo: true
89
+ })) {
90
+ return;
91
+ }
92
+ try {
93
+ var selectorList = (0, _parseSelector.parseSelector)(context, selectorText);
94
+ if (!selectorList.nodes.every(isAllowedSelector)) {
95
+ context.report({
96
+ messageId: 'no-nested-selectors',
97
+ node: key
98
+ });
99
+ }
100
+ } catch (_unused) {
101
+ // `no-unsafe-selectors` reports unparsable selectors.
102
+ }
103
+ });
94
104
  }
95
- });
105
+ } catch (err) {
106
+ _iterator.e(err);
107
+ } finally {
108
+ _iterator.f();
109
+ }
96
110
  }
97
111
  };
98
112
  }