@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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 2.1.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [`4b07551b64b89`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/4b07551b64b89) -
8
+ Improve the performance of the `no-important-styles`, `no-nested-selectors`, and
9
+ `no-unsafe-selectors` rules.
10
+
3
11
  ## 2.1.3
4
12
 
5
13
  ### 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 | |
@@ -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
  }
@@ -4,14 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
8
- var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
9
7
  var _schema = require("@atlaskit/eslint-utils/schema");
10
- var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
11
8
  var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
9
+ var _isSimpleSelector = require("../utils/is-simple-selector");
12
10
  var _parseSelector = require("../utils/parse-selector");
11
+ var _styleCalls = require("../utils/style-calls");
12
+ var _walkStyleCallProperties = require("../utils/walk-style-call-properties");
13
13
  var _lintSelector = require("./lint-selector");
14
14
  var _walkCssMap = require("./walk-css-map");
15
+ var _constants = require("./constants");
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; } }
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; }
15
19
  var ignoredAtRules = new Set(['@container',
16
20
  // ignored because it's covered by `no-container-queries`
17
21
  '@media',
@@ -62,93 +66,92 @@ var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
62
66
  },
63
67
  create: function create(context, config) {
64
68
  return {
65
- Program: function Program(program) {
66
- (0, _walkCssMap.walkCssMap)({
67
- context: context,
68
- program: program,
69
- importSources: config.importSources,
70
- visitor: function visitor(property) {
71
- var type = property.type,
72
- node = property.node;
73
- if (type === 'grouped-at-rules') {
74
- context.report({
75
- node: node.key,
76
- messageId: 'no-grouped-at-rules'
77
- });
78
- return;
69
+ Program: function Program() {
70
+ var _iterator = _createForOfIteratorHelper((0, _styleCalls.getStyleCalls)(context)),
71
+ _step;
72
+ try {
73
+ var _loop = function _loop() {
74
+ var styleCall = _step.value;
75
+ if (!config.importSources.includes(styleCall.importSource)) {
76
+ return 1; // continue
79
77
  }
80
- if (type === 'selectors') {
81
- context.report({
82
- node: node.key,
83
- messageId: 'no-selectors-object'
78
+ if (styleCall.styleFunction === 'cssMap') {
79
+ (0, _walkCssMap.walkCssMapCall)(styleCall.node, function (property) {
80
+ var type = property.type,
81
+ node = property.node;
82
+ if (type === 'grouped-at-rules') {
83
+ context.report({
84
+ node: node.key,
85
+ messageId: 'no-grouped-at-rules'
86
+ });
87
+ } else if (type === 'selectors') {
88
+ context.report({
89
+ node: node.key,
90
+ messageId: 'no-selectors-object'
91
+ });
92
+ }
84
93
  });
85
- return;
86
94
  }
87
- }
88
- });
89
- },
90
- CallExpression: function CallExpression(node) {
91
- var _getScope = (0, _contextCompat.getScope)(context, node),
92
- references = _getScope.references;
93
- var isXcssCall = (0, _isSupportedImport.isXcss)(node, references, config.importSources);
94
- (0, _walkStyleProperties.walkStyleProperties)(node, references, config.importSources, function (_ref) {
95
- var key = _ref.key,
96
- value = _ref.value;
97
- /**
98
- * If the value isn't an object expression then the key can't be a selector.
99
- *
100
- * This assumes all styles have to be inline, which is enforced by `no-unsafe-values`
101
- */
102
- if (value.type !== 'ObjectExpression') {
103
- return;
104
- }
105
-
106
- /**
107
- * Technically the key could be an identifier too, e.g.
108
- *
109
- * ```ts
110
- * css({
111
- * // This key is an identifier, not a literal
112
- * selector: {}
113
- * });
114
- * ```
115
- *
116
- * But an identifier couldn't violate anything in this rule,
117
- * because it can't contain '@' or ':' characters.
118
- */
119
- if (key.type !== 'Literal' || typeof key.value !== 'string') {
120
- return;
121
- }
122
- var selectorText = key.value;
123
- if (selectorText.includes('@')) {
124
- lintAtRule({
125
- context: context,
126
- sourceNode: key,
127
- atRule: selectorText
128
- });
129
- return;
130
- }
131
- try {
132
- var selectorList = (0, _parseSelector.parseSelector)(context, selectorText);
133
- selectorList.nodes.forEach(function (selector) {
134
- return (0, _lintSelector.lintSelector)({
135
- context: context,
136
- sourceNode: key,
137
- selector: selector,
138
- config: config,
139
- isXcssCall: isXcssCall
140
- });
141
- });
142
- } catch (_unused) {
143
- context.report({
144
- node: key,
145
- messageId: 'no-unparsable-selectors',
146
- data: {
147
- selectorText: selectorText
95
+ (0, _walkStyleCallProperties.walkStyleCallProperties)(styleCall, function (_ref) {
96
+ var key = _ref.key,
97
+ value = _ref.value;
98
+ if (value.type !== 'ObjectExpression' || key.type !== 'Literal' || typeof key.value !== 'string') {
99
+ return;
100
+ }
101
+ var selectorText = key.value;
102
+ if (selectorText.includes('@')) {
103
+ lintAtRule({
104
+ context: context,
105
+ sourceNode: key,
106
+ atRule: selectorText
107
+ });
108
+ return;
109
+ }
110
+ if ((0, _isSimpleSelector.isSimpleSelector)(selectorText, {
111
+ allowedPseudos: _constants.allowedPseudos,
112
+ allowLeadingPseudo: styleCall.styleFunction === 'xcss'
113
+ })) {
114
+ return;
115
+ }
116
+ try {
117
+ var selectorList = (0, _parseSelector.parseSelector)(context, selectorText);
118
+ var _iterator2 = _createForOfIteratorHelper(selectorList.nodes),
119
+ _step2;
120
+ try {
121
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
122
+ var selector = _step2.value;
123
+ (0, _lintSelector.lintSelector)({
124
+ context: context,
125
+ sourceNode: key,
126
+ selector: selector,
127
+ config: config,
128
+ isXcssCall: styleCall.styleFunction === 'xcss'
129
+ });
130
+ }
131
+ } catch (err) {
132
+ _iterator2.e(err);
133
+ } finally {
134
+ _iterator2.f();
135
+ }
136
+ } catch (_unused) {
137
+ context.report({
138
+ node: key,
139
+ messageId: 'no-unparsable-selectors',
140
+ data: {
141
+ selectorText: selectorText
142
+ }
143
+ });
148
144
  }
149
145
  });
146
+ };
147
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
148
+ if (_loop()) continue;
150
149
  }
151
- });
150
+ } catch (err) {
151
+ _iterator.e(err);
152
+ } finally {
153
+ _iterator.f();
154
+ }
152
155
  }
153
156
  };
154
157
  }