@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
@@ -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
  var schema = [{
@@ -14,6 +14,60 @@ var schema = [{
14
14
  }
15
15
  }
16
16
  }];
17
+ var unwrapTypeScriptExpression = function unwrapTypeScriptExpression(node) {
18
+ var 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
+ var isStaticContainerKey = function isStaticContainerKey(node) {
25
+ var _expression$expressio;
26
+ var 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
+ var isImportedContainerType = function isImportedContainerType(node, context) {
33
+ var expression = node;
34
+ var _loop = function _loop() {
35
+ var _typeAnnotation$typeN;
36
+ var _expression = expression,
37
+ typeAnnotation = _expression.typeAnnotation;
38
+ 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') {
39
+ var typeName = typeAnnotation.typeName.name;
40
+ var isImported = getSourceCode(context).ast.body.some(function (statement) {
41
+ return statement.type === 'ImportDeclaration' && statement.source.value === '@atlaskit/css/at-rules/container' && statement.specifiers.some(function (specifier) {
42
+ return specifier.type === 'ImportDefaultSpecifier' && specifier.local.name === typeName;
43
+ });
44
+ });
45
+ if (isImported) {
46
+ return {
47
+ v: true
48
+ };
49
+ }
50
+ }
51
+ expression = expression.expression;
52
+ },
53
+ _ret;
54
+ while (expression.type === 'TSSatisfiesExpression') {
55
+ _ret = _loop();
56
+ if (_ret) return _ret.v;
57
+ }
58
+ return false;
59
+ };
60
+ var isContainerQuery = function isContainerQuery(node) {
61
+ var _expression$quasis;
62
+ var expression = unwrapTypeScriptExpression(node);
63
+ if (expression.type === 'Literal' && typeof expression.value === 'string') {
64
+ return expression.value.includes('@container');
65
+ }
66
+ return expression.type === 'TemplateLiteral' && Boolean((_expression$quasis = expression.quasis) === null || _expression$quasis === void 0 ? void 0 : _expression$quasis.some(function (quasi) {
67
+ var _quasi$value;
68
+ 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');
69
+ }));
70
+ };
17
71
  var rule = createLintRule({
18
72
  meta: {
19
73
  name: 'no-container-queries',
@@ -37,25 +91,20 @@ var rule = createLintRule({
37
91
  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)) {
38
92
  estraverse.traverse(node, {
39
93
  enter: function enter(node) {
40
- var _node$key$value, _node$key$quasis$;
41
94
  if (node.type !== 'Property') {
42
95
  return;
43
96
  }
44
- if (node.key.type !== 'Literal' && node.key.type !== 'TemplateLiteral') {
97
+ var key = node.key;
98
+ if (!isContainerQuery(key)) {
45
99
  return;
46
100
  }
47
- if (node.key.type === 'Literal' && (_node$key$value = node.key.value) !== null && _node$key$value !== void 0 && _node$key$value.toString().match(/@container/)) {
48
- context.report({
49
- node: node.key,
50
- messageId: 'no-container-queries'
51
- });
52
- }
53
- 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/)) {
54
- context.report({
55
- node: node.key,
56
- messageId: 'no-container-queries'
57
- });
101
+ if (isImportedContainerType(key, context) && isStaticContainerKey(key.expression)) {
102
+ return;
58
103
  }
104
+ context.report({
105
+ node: node.key,
106
+ messageId: 'no-container-queries'
107
+ });
59
108
  },
60
109
  /**
61
110
  * This is needed to handle unknown node types. Otherwise an error is thrown.
@@ -1,10 +1,7 @@
1
- function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
2
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
3
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
4
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
5
- import { hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
1
+ import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
6
2
  import { importSources } from '@atlaskit/eslint-utils/schema';
7
3
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
4
+ import { getStyleCalls } from '../utils/style-calls';
8
5
  var rule = createLintRuleWithTypedConfig({
9
6
  meta: {
10
7
  name: 'no-important-styles',
@@ -26,118 +23,101 @@ var rule = createLintRuleWithTypedConfig({
26
23
  },
27
24
  create: function create(context, _ref) {
28
25
  var importSources = _ref.importSources;
26
+ var sourceCode = getSourceCode(context);
27
+ if (!sourceCode.text.includes('important')) {
28
+ return {};
29
+ }
29
30
  return {
30
- CallExpression: function CallExpression(node) {
31
- var _getScope = getScope(context, node),
32
- references = _getScope.references;
33
- if (!hasStyleObjectArguments(node.callee, references, importSources)) {
34
- return;
35
- }
36
- walkProperties(node, function (propertyNode) {
37
- if (isImportant(propertyNode.value)) {
31
+ Program: function Program() {
32
+ var text = sourceCode.text;
33
+ var reportedValues = new Set();
34
+ var importantIndex = text.indexOf('important');
35
+ while (importantIndex !== -1) {
36
+ var value = findStyleValue(sourceCode, importantIndex);
37
+ if (value && !reportedValues.has(value) && isImportant(value) && isInSupportedStyleCall(context, value, importSources)) {
38
+ reportedValues.add(value);
38
39
  context.report({
39
- node: propertyNode.value,
40
+ node: value,
40
41
  messageId: 'no-important-styles'
41
42
  });
42
43
  }
43
- });
44
+ importantIndex = text.indexOf('important', importantIndex + 9);
45
+ }
44
46
  }
45
47
  };
46
48
  }
47
49
  });
48
50
  export default rule;
49
- var IMPORTANT_SUFFIX_REGEX = /!\s*important\s*$/;
50
-
51
- /**
52
- * Walk through all Property nodes in the AST tree recursively.
53
- * This is faster than esquery for this specific use case as it avoids
54
- * selector compilation and executes a simple depth-first traversal.
55
- */
56
- function walkProperties(node, callback) {
57
- var _node$arguments;
58
- if (node.type === 'Property' && node.value) {
59
- callback(node);
51
+ function isImportant(node) {
52
+ if (node.type === 'Literal') {
53
+ return typeof node.value === 'string' && hasImportantSuffix(node.value);
60
54
  }
61
-
62
- // Iterate through object properties
63
- if ((_node$arguments = node.arguments) !== null && _node$arguments !== void 0 && _node$arguments.length) {
64
- var _iterator = _createForOfIteratorHelper(node.arguments),
65
- _step;
66
- try {
67
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
68
- var arg = _step.value;
69
- if (arg.type === 'ObjectExpression' && arg.properties) {
70
- var _iterator2 = _createForOfIteratorHelper(arg.properties),
71
- _step2;
72
- try {
73
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
74
- var prop = _step2.value;
75
- walkObjectProperty(prop, callback);
76
- }
77
- } catch (err) {
78
- _iterator2.e(err);
79
- } finally {
80
- _iterator2.f();
81
- }
82
- }
83
- }
84
- } catch (err) {
85
- _iterator.e(err);
86
- } finally {
87
- _iterator.f();
88
- }
55
+ if (node.type === 'TemplateLiteral') {
56
+ var lastQuasi = node.quasis[node.quasis.length - 1];
57
+ return Boolean(lastQuasi && hasImportantSuffix(lastQuasi.value.raw));
89
58
  }
59
+ return false;
90
60
  }
91
-
92
- /**
93
- * Recursively walk object properties to find nested Property nodes.
94
- */
95
- function walkObjectProperty(node, callback) {
96
- if (node.type === 'Property') {
97
- var _node$value;
98
- callback(node);
99
- // Recursively check nested objects
100
- if (((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'ObjectExpression' && node.value.properties) {
101
- var _iterator3 = _createForOfIteratorHelper(node.value.properties),
102
- _step3;
103
- try {
104
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
105
- var prop = _step3.value;
106
- walkObjectProperty(prop, callback);
107
- }
108
- } catch (err) {
109
- _iterator3.e(err);
110
- } finally {
111
- _iterator3.f();
61
+ function findStyleValue(sourceCode, index) {
62
+ var node = sourceCode.getNodeByRangeIndex(index);
63
+ while (node && node.type !== 'Literal' && node.type !== 'TemplateLiteral') {
64
+ node = node.parent;
65
+ }
66
+ return node;
67
+ }
68
+ function isInSupportedStyleCall(context, value, importSources) {
69
+ var call = findContainingCall(value);
70
+ return Boolean(call && getStyleCalls(context).some(function (styleCall) {
71
+ return styleCall.node === call && importSources.includes(styleCall.importSource);
72
+ }));
73
+ }
74
+ function findContainingCall(value) {
75
+ var _property, _object;
76
+ var property = value.parent;
77
+ if (((_property = property) === null || _property === void 0 ? void 0 : _property.type) !== 'Property' || property.value !== value) {
78
+ return null;
79
+ }
80
+ var object = property.parent;
81
+ if (((_object = object) === null || _object === void 0 ? void 0 : _object.type) !== 'ObjectExpression') {
82
+ return null;
83
+ }
84
+ while (object) {
85
+ var _parent$parent;
86
+ var parent = object.parent;
87
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'Property' && parent.value === object) {
88
+ var _object2;
89
+ property = parent;
90
+ object = property.parent;
91
+ if (((_object2 = object) === null || _object2 === void 0 ? void 0 : _object2.type) !== 'ObjectExpression') {
92
+ return null;
112
93
  }
94
+ continue;
95
+ }
96
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'CallExpression' && parent.arguments.includes(object)) {
97
+ return parent;
113
98
  }
99
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'ArrowFunctionExpression' && parent.body === object && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.arguments.includes(parent)) {
100
+ return parent.parent;
101
+ }
102
+ return null;
114
103
  }
104
+ return null;
115
105
  }
116
-
117
- /**
118
- * Check if a value contains !important flag.
119
- * Uses fast string checks before regex to avoid regex overhead for common cases.
120
- */
121
- function isImportant(node) {
122
- if (node.type === 'Literal') {
123
- if (typeof node.value !== 'string') {
124
- return false;
125
- }
126
- // Fast check: string must end with 'important' (after optional whitespace)
127
- var len = node.value.length;
128
- if (len < 10) {
129
- return false;
130
- } // "!important" is 10 chars minimum
131
- return IMPORTANT_SUFFIX_REGEX.test(node.value);
106
+ function hasImportantSuffix(value) {
107
+ var index = value.length - 1;
108
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
109
+ index--;
132
110
  }
133
- if (node.type === 'TemplateLiteral') {
134
- var _node$quasis;
135
- if (!((_node$quasis = node.quasis) !== null && _node$quasis !== void 0 && _node$quasis.length)) {
136
- return false;
137
- }
138
- // Only check the last quasi's raw value (most likely to contain the !important)
139
- var lastQuasi = node.quasis[node.quasis.length - 1];
140
- return IMPORTANT_SUFFIX_REGEX.test(lastQuasi.value.raw);
111
+ var importantStart = index - 8;
112
+ if (importantStart < 0 || value.slice(importantStart, index + 1) !== 'important') {
113
+ return false;
141
114
  }
142
- return false;
115
+ index = importantStart - 1;
116
+ while (index >= 0 && isWhitespace(value.charCodeAt(index))) {
117
+ index--;
118
+ }
119
+ return value.charCodeAt(index) === 33;
120
+ }
121
+ function isWhitespace(character) {
122
+ return character === 32 || character >= 9 && character <= 13;
143
123
  }
@@ -1,11 +1,14 @@
1
1
  import _toArray from "@babel/runtime/helpers/toArray";
2
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
3
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
2
4
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
3
5
  import { isNesting, isPseudo } from 'postcss-selector-parser';
4
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
5
6
  import { importSources } from '@atlaskit/eslint-utils/schema';
6
- import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
7
7
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
8
+ import { isSimpleSelector } from '../utils/is-simple-selector';
8
9
  import { parseSelector } from '../utils/parse-selector';
10
+ import { getStyleCalls } from '../utils/style-calls';
11
+ import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
9
12
  var getCssSelector = function getCssSelector(key) {
10
13
  if (key.type === 'Literal' && typeof key.value === 'string') {
11
14
  return key.value;
@@ -54,38 +57,49 @@ var rule = createLintRuleWithTypedConfig({
54
57
  create: function create(context, _ref) {
55
58
  var importSources = _ref.importSources;
56
59
  return {
57
- CallExpression: function CallExpression(node) {
58
- var _getScope = getScope(context, node),
59
- references = _getScope.references;
60
- walkStyleProperties(node, references, importSources, function (_ref2) {
61
- var key = _ref2.key,
62
- value = _ref2.value;
63
- if (value.type !== 'ObjectExpression') {
64
- // If the value is a CSS object, safe to assume we're at a CSS selector
65
- return;
66
- }
67
- var selectorText = getCssSelector(key);
68
- if (selectorText === null) {
69
- return;
70
- }
71
-
72
- // Ignore at-rules
73
- if (selectorText.includes('@')) {
74
- return;
75
- }
76
- try {
77
- var selectorList = parseSelector(context, selectorText);
78
- if (!selectorList.nodes.every(isAllowedSelector)) {
79
- context.report({
80
- messageId: 'no-nested-selectors',
81
- node: key
82
- });
60
+ Program: function Program() {
61
+ var _iterator = _createForOfIteratorHelper(getStyleCalls(context)),
62
+ _step;
63
+ try {
64
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
65
+ var styleCall = _step.value;
66
+ if (!importSources.includes(styleCall.importSource)) {
67
+ continue;
83
68
  }
84
- } catch (_) {
85
- // If it is not parsable then `no-unsafe-selectors` will give it an error,
86
- // we can ignore it here.
69
+ walkStyleCallProperties(styleCall, function (_ref2) {
70
+ var key = _ref2.key,
71
+ value = _ref2.value;
72
+ if (value.type !== 'ObjectExpression') {
73
+ return;
74
+ }
75
+ var selectorText = getCssSelector(key);
76
+ if (selectorText === null || selectorText.includes('@')) {
77
+ return;
78
+ }
79
+ if (isSimpleSelector(selectorText, {
80
+ allowBareNesting: true,
81
+ allowLeadingPseudo: true
82
+ })) {
83
+ return;
84
+ }
85
+ try {
86
+ var selectorList = parseSelector(context, selectorText);
87
+ if (!selectorList.nodes.every(isAllowedSelector)) {
88
+ context.report({
89
+ messageId: 'no-nested-selectors',
90
+ node: key
91
+ });
92
+ }
93
+ } catch (_unused) {
94
+ // `no-unsafe-selectors` reports unparsable selectors.
95
+ }
96
+ });
87
97
  }
88
- });
98
+ } catch (err) {
99
+ _iterator.e(err);
100
+ } finally {
101
+ _iterator.f();
102
+ }
89
103
  }
90
104
  };
91
105
  }
@@ -1,11 +1,15 @@
1
- import { getScope } from '@atlaskit/eslint-utils/context-compat';
2
- import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
1
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
2
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
3
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
3
4
  import { importSources } from '@atlaskit/eslint-utils/schema';
4
- import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
5
5
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
6
+ import { isSimpleSelector } from '../utils/is-simple-selector';
6
7
  import { parseSelector } from '../utils/parse-selector';
8
+ import { getStyleCalls } from '../utils/style-calls';
9
+ import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
7
10
  import { lintSelector } from './lint-selector';
8
- import { walkCssMap } from './walk-css-map';
11
+ import { walkCssMapCall } from './walk-css-map';
12
+ import { allowedPseudos } from './constants';
9
13
  var ignoredAtRules = new Set(['@container',
10
14
  // ignored because it's covered by `no-container-queries`
11
15
  '@media',
@@ -56,93 +60,92 @@ var rule = createLintRuleWithTypedConfig({
56
60
  },
57
61
  create: function create(context, config) {
58
62
  return {
59
- Program: function Program(program) {
60
- walkCssMap({
61
- context: context,
62
- program: program,
63
- importSources: config.importSources,
64
- visitor: function visitor(property) {
65
- var type = property.type,
66
- node = property.node;
67
- if (type === 'grouped-at-rules') {
68
- context.report({
69
- node: node.key,
70
- messageId: 'no-grouped-at-rules'
71
- });
72
- return;
63
+ Program: function Program() {
64
+ var _iterator = _createForOfIteratorHelper(getStyleCalls(context)),
65
+ _step;
66
+ try {
67
+ var _loop = function _loop() {
68
+ var styleCall = _step.value;
69
+ if (!config.importSources.includes(styleCall.importSource)) {
70
+ return 1; // continue
73
71
  }
74
- if (type === 'selectors') {
75
- context.report({
76
- node: node.key,
77
- messageId: 'no-selectors-object'
72
+ if (styleCall.styleFunction === 'cssMap') {
73
+ walkCssMapCall(styleCall.node, function (property) {
74
+ var type = property.type,
75
+ node = property.node;
76
+ if (type === 'grouped-at-rules') {
77
+ context.report({
78
+ node: node.key,
79
+ messageId: 'no-grouped-at-rules'
80
+ });
81
+ } else if (type === 'selectors') {
82
+ context.report({
83
+ node: node.key,
84
+ messageId: 'no-selectors-object'
85
+ });
86
+ }
78
87
  });
79
- return;
80
88
  }
81
- }
82
- });
83
- },
84
- CallExpression: function CallExpression(node) {
85
- var _getScope = getScope(context, node),
86
- references = _getScope.references;
87
- var isXcssCall = isXcss(node, references, config.importSources);
88
- walkStyleProperties(node, references, config.importSources, function (_ref) {
89
- var key = _ref.key,
90
- value = _ref.value;
91
- /**
92
- * If the value isn't an object expression then the key can't be a selector.
93
- *
94
- * This assumes all styles have to be inline, which is enforced by `no-unsafe-values`
95
- */
96
- if (value.type !== 'ObjectExpression') {
97
- return;
98
- }
99
-
100
- /**
101
- * Technically the key could be an identifier too, e.g.
102
- *
103
- * ```ts
104
- * css({
105
- * // This key is an identifier, not a literal
106
- * selector: {}
107
- * });
108
- * ```
109
- *
110
- * But an identifier couldn't violate anything in this rule,
111
- * because it can't contain '@' or ':' characters.
112
- */
113
- if (key.type !== 'Literal' || typeof key.value !== 'string') {
114
- return;
115
- }
116
- var selectorText = key.value;
117
- if (selectorText.includes('@')) {
118
- lintAtRule({
119
- context: context,
120
- sourceNode: key,
121
- atRule: selectorText
122
- });
123
- return;
124
- }
125
- try {
126
- var selectorList = parseSelector(context, selectorText);
127
- selectorList.nodes.forEach(function (selector) {
128
- return lintSelector({
129
- context: context,
130
- sourceNode: key,
131
- selector: selector,
132
- config: config,
133
- isXcssCall: isXcssCall
134
- });
135
- });
136
- } catch (_unused) {
137
- context.report({
138
- node: key,
139
- messageId: 'no-unparsable-selectors',
140
- data: {
141
- selectorText: selectorText
89
+ walkStyleCallProperties(styleCall, function (_ref) {
90
+ var key = _ref.key,
91
+ value = _ref.value;
92
+ if (value.type !== 'ObjectExpression' || key.type !== 'Literal' || typeof key.value !== 'string') {
93
+ return;
94
+ }
95
+ var selectorText = key.value;
96
+ if (selectorText.includes('@')) {
97
+ lintAtRule({
98
+ context: context,
99
+ sourceNode: key,
100
+ atRule: selectorText
101
+ });
102
+ return;
103
+ }
104
+ if (isSimpleSelector(selectorText, {
105
+ allowedPseudos: allowedPseudos,
106
+ allowLeadingPseudo: styleCall.styleFunction === 'xcss'
107
+ })) {
108
+ return;
109
+ }
110
+ try {
111
+ var selectorList = parseSelector(context, selectorText);
112
+ var _iterator2 = _createForOfIteratorHelper(selectorList.nodes),
113
+ _step2;
114
+ try {
115
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
116
+ var selector = _step2.value;
117
+ lintSelector({
118
+ context: context,
119
+ sourceNode: key,
120
+ selector: selector,
121
+ config: config,
122
+ isXcssCall: styleCall.styleFunction === 'xcss'
123
+ });
124
+ }
125
+ } catch (err) {
126
+ _iterator2.e(err);
127
+ } finally {
128
+ _iterator2.f();
129
+ }
130
+ } catch (_unused) {
131
+ context.report({
132
+ node: key,
133
+ messageId: 'no-unparsable-selectors',
134
+ data: {
135
+ selectorText: selectorText
136
+ }
137
+ });
142
138
  }
143
139
  });
140
+ };
141
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
142
+ if (_loop()) continue;
144
143
  }
145
- });
144
+ } catch (err) {
145
+ _iterator.e(err);
146
+ } finally {
147
+ _iterator.f();
148
+ }
146
149
  }
147
150
  };
148
151
  }