@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
@@ -11,11 +11,23 @@ var _postcssSelectorParser = require("postcss-selector-parser");
11
11
  var _constants = require("./constants");
12
12
  var _getSourceLocationFromRange = require("./get-source-location-from-range");
13
13
  var _sourcePositionUtils = require("./source-position-utils");
14
+ 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; } } }; }
15
+ 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; } }
16
+ 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; }
14
17
  function lintSelector(args) {
15
18
  checkNoAmbiguousPseudos(args);
16
- checkNoRestrictedPseudos(args);
17
- checkNoLegacyPseudoElementSyntax(args);
18
- checkNoIncreasedSpecificity(args);
19
+ var pseudos = [];
20
+ var nestings = [];
21
+ args.selector.walk(function (node) {
22
+ if ((0, _postcssSelectorParser.isPseudo)(node)) {
23
+ pseudos.push(node);
24
+ } else if ((0, _postcssSelectorParser.isNesting)(node)) {
25
+ nestings.push(node);
26
+ }
27
+ });
28
+ checkNoRestrictedPseudos(args, pseudos);
29
+ checkNoLegacyPseudoElementSyntax(args, pseudos);
30
+ checkNoIncreasedSpecificity(args, nestings);
19
31
  }
20
32
  var legacyPseudoElements = new Set([':after', ':before', ':first-letter', ':first-line']);
21
33
 
@@ -83,39 +95,47 @@ function checkNoAmbiguousPseudos(_ref) {
83
95
  });
84
96
  }
85
97
  }
86
- function checkNoRestrictedPseudos(_ref2) {
98
+ function checkNoRestrictedPseudos(_ref2, pseudos) {
87
99
  var context = _ref2.context,
88
- sourceNode = _ref2.sourceNode,
89
- selector = _ref2.selector;
90
- selector.walkPseudos(function (pseudo) {
91
- if (_constants.allowedPseudos.has(pseudo.value)) {
92
- return;
93
- }
100
+ sourceNode = _ref2.sourceNode;
101
+ var _iterator = _createForOfIteratorHelper(pseudos),
102
+ _step;
103
+ try {
104
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
105
+ var pseudo = _step.value;
106
+ if (_constants.allowedPseudos.has(pseudo.value)) {
107
+ continue;
108
+ }
94
109
 
95
- /**
96
- * If someone has written `:after` it is allowed,
97
- * they've just used the legacy syntax.
98
- *
99
- * This will be caught by the `no-legacy-pseudo-element-syntax` check.
100
- */
101
- if (legacyPseudoElements.has(pseudo.value) && _constants.allowedPseudos.has(":".concat(pseudo.value))) {
102
- return;
103
- }
104
- if (!_constants.allowedPseudos.has(pseudo.value)) {
105
- var range = (0, _sourcePositionUtils.getRangeFromNode)({
106
- node: pseudo,
107
- sourceNode: sourceNode
108
- });
109
- var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, range);
110
- context.report({
111
- loc: loc,
112
- messageId: 'no-restricted-pseudos',
113
- data: {
114
- pseudo: pseudo.value
115
- }
116
- });
110
+ /**
111
+ * If someone has written `:after` it is allowed,
112
+ * they've just used the legacy syntax.
113
+ *
114
+ * This will be caught by the `no-legacy-pseudo-element-syntax` check.
115
+ */
116
+ if (legacyPseudoElements.has(pseudo.value) && _constants.allowedPseudos.has(":".concat(pseudo.value))) {
117
+ continue;
118
+ }
119
+ if (!_constants.allowedPseudos.has(pseudo.value)) {
120
+ var range = (0, _sourcePositionUtils.getRangeFromNode)({
121
+ node: pseudo,
122
+ sourceNode: sourceNode
123
+ });
124
+ var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, range);
125
+ context.report({
126
+ loc: loc,
127
+ messageId: 'no-restricted-pseudos',
128
+ data: {
129
+ pseudo: pseudo.value
130
+ }
131
+ });
132
+ }
117
133
  }
118
- });
134
+ } catch (err) {
135
+ _iterator.e(err);
136
+ } finally {
137
+ _iterator.f();
138
+ }
119
139
  }
120
140
 
121
141
  /**
@@ -123,78 +143,97 @@ function checkNoRestrictedPseudos(_ref2) {
123
143
  *
124
144
  * https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements
125
145
  */
126
- function checkNoLegacyPseudoElementSyntax(_ref3) {
146
+ function checkNoLegacyPseudoElementSyntax(_ref3, pseudos) {
127
147
  var context = _ref3.context,
128
- sourceNode = _ref3.sourceNode,
129
- selector = _ref3.selector;
130
- selector.walkPseudos(function (pseudo) {
131
- if (legacyPseudoElements.has(pseudo.value)) {
132
- var range = (0, _sourcePositionUtils.getRangeFromNode)({
133
- node: pseudo,
134
- sourceNode: sourceNode
135
- });
136
- var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, range);
137
- context.report({
138
- loc: loc,
139
- messageId: 'no-legacy-pseudo-element-syntax',
140
- fix: /*#__PURE__*/_regenerator.default.mark(function fix(fixer) {
141
- return _regenerator.default.wrap(function (_context3) {
142
- while (1) switch (_context3.prev = _context3.next) {
143
- case 0:
144
- _context3.next = 1;
145
- return fixer.insertTextBeforeRange(range, ':');
146
- case 1:
147
- case "end":
148
- return _context3.stop();
149
- }
150
- }, fix);
151
- })
152
- });
148
+ sourceNode = _ref3.sourceNode;
149
+ var _iterator2 = _createForOfIteratorHelper(pseudos),
150
+ _step2;
151
+ try {
152
+ var _loop = function _loop() {
153
+ var pseudo = _step2.value;
154
+ if (legacyPseudoElements.has(pseudo.value)) {
155
+ var range = (0, _sourcePositionUtils.getRangeFromNode)({
156
+ node: pseudo,
157
+ sourceNode: sourceNode
158
+ });
159
+ var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, range);
160
+ context.report({
161
+ loc: loc,
162
+ messageId: 'no-legacy-pseudo-element-syntax',
163
+ fix: /*#__PURE__*/_regenerator.default.mark(function fix(fixer) {
164
+ return _regenerator.default.wrap(function (_context3) {
165
+ while (1) switch (_context3.prev = _context3.next) {
166
+ case 0:
167
+ _context3.next = 1;
168
+ return fixer.insertTextBeforeRange(range, ':');
169
+ case 1:
170
+ case "end":
171
+ return _context3.stop();
172
+ }
173
+ }, fix);
174
+ })
175
+ });
176
+ }
177
+ };
178
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
179
+ _loop();
153
180
  }
154
- });
181
+ } catch (err) {
182
+ _iterator2.e(err);
183
+ } finally {
184
+ _iterator2.f();
185
+ }
155
186
  }
156
- function checkNoIncreasedSpecificity(_ref4) {
187
+ function checkNoIncreasedSpecificity(_ref4, nestings) {
157
188
  var context = _ref4.context,
158
- sourceNode = _ref4.sourceNode,
159
- selector = _ref4.selector;
160
- selector.walkNesting(function (nesting) {
161
- /**
162
- * If it's not the start of a chain, then it's already been reported.
163
- */
164
- if ((0, _postcssSelectorParser.isNesting)(nesting.prev())) {
165
- return;
166
- }
167
- var next = nesting.next();
189
+ sourceNode = _ref4.sourceNode;
190
+ var _iterator3 = _createForOfIteratorHelper(nestings),
191
+ _step3;
192
+ try {
193
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
194
+ var nesting = _step3.value;
195
+ /**
196
+ * If it's not the start of a chain, then it's already been reported.
197
+ */
198
+ if ((0, _postcssSelectorParser.isNesting)(nesting.prev())) {
199
+ continue;
200
+ }
201
+ var next = nesting.next();
168
202
 
169
- /**
170
- * If it is followed by something other than another nesting selector,
171
- * then it isn't just there to increase specificity — it is actually necessary.
172
- */
173
- if (next && !(0, _postcssSelectorParser.isNesting)(next)) {
174
- return;
175
- }
176
- var lastNesting = nesting;
177
- while ((0, _postcssSelectorParser.isNesting)(next)) {
178
- lastNesting = next;
179
- next = next.next();
203
+ /**
204
+ * If it is followed by something other than another nesting selector,
205
+ * then it isn't just there to increase specificity — it is actually necessary.
206
+ */
207
+ if (next && !(0, _postcssSelectorParser.isNesting)(next)) {
208
+ continue;
209
+ }
210
+ var lastNesting = nesting;
211
+ while ((0, _postcssSelectorParser.isNesting)(next)) {
212
+ lastNesting = next;
213
+ next = next.next();
214
+ }
215
+ var _getRangeFromNode = (0, _sourcePositionUtils.getRangeFromNode)({
216
+ node: nesting,
217
+ sourceNode: sourceNode
218
+ }),
219
+ _getRangeFromNode2 = (0, _slicedToArray2.default)(_getRangeFromNode, 1),
220
+ start = _getRangeFromNode2[0];
221
+ var _getRangeFromNode3 = (0, _sourcePositionUtils.getRangeFromNode)({
222
+ node: lastNesting,
223
+ sourceNode: sourceNode
224
+ }),
225
+ _getRangeFromNode4 = (0, _slicedToArray2.default)(_getRangeFromNode3, 2),
226
+ _ = _getRangeFromNode4[0],
227
+ end = _getRangeFromNode4[1];
228
+ var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, [start, end]);
229
+ context.report({
230
+ loc: loc,
231
+ messageId: 'no-increased-specificity'
232
+ });
180
233
  }
181
- var _getRangeFromNode = (0, _sourcePositionUtils.getRangeFromNode)({
182
- node: nesting,
183
- sourceNode: sourceNode
184
- }),
185
- _getRangeFromNode2 = (0, _slicedToArray2.default)(_getRangeFromNode, 1),
186
- start = _getRangeFromNode2[0];
187
- var _getRangeFromNode3 = (0, _sourcePositionUtils.getRangeFromNode)({
188
- node: lastNesting,
189
- sourceNode: sourceNode
190
- }),
191
- _getRangeFromNode4 = (0, _slicedToArray2.default)(_getRangeFromNode3, 2),
192
- _ = _getRangeFromNode4[0],
193
- end = _getRangeFromNode4[1];
194
- var loc = (0, _getSourceLocationFromRange.getSourceLocationFromRange)(context, [start, end]);
195
- context.report({
196
- loc: loc,
197
- messageId: 'no-increased-specificity'
198
- });
199
- });
234
+ } catch (err) {
235
+ _iterator3.e(err);
236
+ } finally {
237
+ _iterator3.f();
238
+ }
200
239
  }
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
7
- exports.walkCssMap = walkCssMap;
8
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
- var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
6
+ exports.walkCssMapCall = walkCssMapCall;
10
7
  /**
11
8
  * This util is intentionally not in `@atlaskit/eslint-utils` at this time.
12
9
  *
@@ -59,45 +56,18 @@ var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
59
56
  */
60
57
  var allowedGroupedAtRules = new Set(['@starting-style' // Has no parameters, can only be written in grouped form
61
58
  ]);
62
- function walkCssMap(_ref) {
63
- var context = _ref.context,
64
- importSources = _ref.importSources,
65
- visitor = _ref.visitor;
66
- var program = (0, _contextCompat.getSourceCode)(context).ast;
67
- var importDeclaration = program.body.find(function (node) {
68
- return node.type === 'ImportDeclaration' && importSources.includes(node.source.value);
69
- });
70
- var specifier = importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.specifiers.find(function (specifier) {
71
- return specifier.type === 'ImportSpecifier' && 'name' in specifier.imported && specifier.imported.name === 'cssMap';
72
- });
73
- if (!specifier) {
74
- return;
75
- }
76
- var _getSourceCode$scopeM = (0, _contextCompat.getSourceCode)(context).scopeManager.getDeclaredVariables(specifier),
77
- _getSourceCode$scopeM2 = (0, _slicedToArray2.default)(_getSourceCode$scopeM, 1),
78
- variable = _getSourceCode$scopeM2[0];
79
- if (!variable) {
80
- return;
81
- }
82
- variable.references.forEach(function (reference) {
83
- var identifier = reference.identifier;
84
- var parent = identifier.parent;
85
- if (parent.type !== 'CallExpression') {
86
- return;
87
- }
88
- var variantMap = parent.arguments[0];
89
- if (variantMap.type !== 'ObjectExpression') {
90
- return;
91
- }
59
+ function walkCssMapCall(call, visitor) {
60
+ var variantMap = call.arguments[0];
61
+ if ((variantMap === null || variantMap === void 0 ? void 0 : variantMap.type) === 'ObjectExpression') {
92
62
  walkCssMapVariantMap({
93
63
  variantMap: variantMap,
94
64
  visitor: visitor
95
65
  });
96
- });
66
+ }
97
67
  }
98
- function walkCssMapVariantMap(_ref2) {
99
- var variantMap = _ref2.variantMap,
100
- visitor = _ref2.visitor;
68
+ function walkCssMapVariantMap(_ref) {
69
+ var variantMap = _ref.variantMap,
70
+ visitor = _ref.visitor;
101
71
  variantMap.properties.forEach(function (property) {
102
72
  if (property.type !== 'Property') {
103
73
  return;
@@ -108,9 +78,9 @@ function walkCssMapVariantMap(_ref2) {
108
78
  });
109
79
  });
110
80
  }
111
- function walkCssMapVariant(_ref3) {
112
- var variant = _ref3.variant,
113
- visitor = _ref3.visitor;
81
+ function walkCssMapVariant(_ref2) {
82
+ var variant = _ref2.variant,
83
+ visitor = _ref2.visitor;
114
84
  visitor({
115
85
  type: 'variant',
116
86
  node: variant
@@ -149,9 +119,9 @@ function walkCssMapVariant(_ref3) {
149
119
  });
150
120
  });
151
121
  }
152
- function walkCssMapAtRuleGrouping(_ref4) {
153
- var atRuleGrouping = _ref4.atRuleGrouping,
154
- visitor = _ref4.visitor;
122
+ function walkCssMapAtRuleGrouping(_ref3) {
123
+ var atRuleGrouping = _ref3.atRuleGrouping,
124
+ visitor = _ref3.visitor;
155
125
  var atRule = getKeyValue(atRuleGrouping.key);
156
126
  if (!atRule) {
157
127
  return;
@@ -178,9 +148,9 @@ function walkCssMapAtRuleGrouping(_ref4) {
178
148
  });
179
149
  });
180
150
  }
181
- function walkCssMapSelectors(_ref5) {
182
- var selectors = _ref5.selectors,
183
- visitor = _ref5.visitor;
151
+ function walkCssMapSelectors(_ref4) {
152
+ var selectors = _ref4.selectors,
153
+ visitor = _ref4.visitor;
184
154
  visitor({
185
155
  type: 'selectors',
186
156
  node: selectors
@@ -198,9 +168,9 @@ function walkCssMapSelectors(_ref5) {
198
168
  });
199
169
  });
200
170
  }
201
- function walkStyleRuleset(_ref6) {
202
- var ruleset = _ref6.ruleset,
203
- visitor = _ref6.visitor;
171
+ function walkStyleRuleset(_ref5) {
172
+ var ruleset = _ref5.ruleset,
173
+ visitor = _ref5.visitor;
204
174
  visitor({
205
175
  type: 'ruleset',
206
176
  node: ruleset
@@ -10,7 +10,26 @@ var _ajv = _interopRequireDefault(require("ajv"));
10
10
  var _getRuleUrl = require("./get-rule-url");
11
11
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
12
12
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
13
+ /**
14
+ * A single shared Ajv instance is reused across every rule instead of constructing
15
+ * a new one each time a default config is needed. Ajv's constructor is expensive
16
+ * (it compiles its own meta-schemas), so we only ever want to pay for it once.
17
+ */
18
+ var ajv = new _ajv.default({
19
+ useDefaults: true
20
+ });
13
21
  var createLintRuleWithTypedConfig = exports.createLintRuleWithTypedConfig = function createLintRuleWithTypedConfig(rule) {
22
+ /**
23
+ * Lazily-computed, cached default config.
24
+ *
25
+ * The default config is derived purely from the rule's (static) schema, so it is
26
+ * constant for the life of the process. Compiling the schema with Ajv involves
27
+ * runtime code generation (`new Function`), which is expensive — previously this
28
+ * ran on every `getDefaultConfig()` call, i.e. once per linted file. We now compute
29
+ * it at most once per rule and reuse the result.
30
+ */
31
+ var defaultConfig;
32
+
14
33
  /**
15
34
  * Gets the default config by running the same validator
16
35
  * ESLint does against an empty config object with
@@ -20,16 +39,16 @@ var createLintRuleWithTypedConfig = exports.createLintRuleWithTypedConfig = func
20
39
  * will be populated with the specified default value.
21
40
  */
22
41
  function getDefaultConfig() {
23
- var ajv = new _ajv.default({
24
- useDefaults: true
25
- });
26
- var validate = ajv.compile(rule.meta.schema);
27
- var config = {};
28
- /**
29
- * The `validate()` function mutates the config object.
30
- */
31
- validate(config);
32
- return config;
42
+ if (defaultConfig === undefined) {
43
+ var validate = ajv.compile(rule.meta.schema);
44
+ var _config = {};
45
+ /**
46
+ * The `validate()` function mutates the config object.
47
+ */
48
+ validate(_config);
49
+ defaultConfig = _config;
50
+ }
51
+ return defaultConfig;
33
52
  }
34
53
  return _objectSpread(_objectSpread({}, rule), {}, {
35
54
  meta: _objectSpread(_objectSpread({}, rule.meta), {}, {
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isSimpleSelector = isSimpleSelector;
7
+ /**
8
+ * Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
9
+ * Returning false means the caller must use the full selector parser.
10
+ */
11
+ function isSimpleSelector(selectorText) {
12
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13
+ var index = 0;
14
+ while (index < selectorText.length) {
15
+ index = skipWhitespace(selectorText, index);
16
+ var hasNesting = selectorText.charCodeAt(index) === 38;
17
+ if (hasNesting) {
18
+ index++;
19
+ if (selectorText.charCodeAt(index) === 38) {
20
+ return false;
21
+ }
22
+ } else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
23
+ return false;
24
+ }
25
+ var pseudoCount = 0;
26
+ while (selectorText.charCodeAt(index) === 58) {
27
+ var pseudoStart = index++;
28
+ if (selectorText.charCodeAt(index) === 58) {
29
+ index++;
30
+ }
31
+ var nameStart = index;
32
+ while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
33
+ index++;
34
+ }
35
+ if (index === nameStart || selectorText.charCodeAt(index) === 40) {
36
+ return false;
37
+ }
38
+ if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
39
+ return false;
40
+ }
41
+ pseudoCount++;
42
+ }
43
+ if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
44
+ return false;
45
+ }
46
+ index = skipWhitespace(selectorText, index);
47
+ if (index === selectorText.length) {
48
+ return true;
49
+ }
50
+ if (selectorText.charCodeAt(index) !== 44) {
51
+ return false;
52
+ }
53
+ index++;
54
+ }
55
+ return false;
56
+ }
57
+ function skipWhitespace(value, start) {
58
+ var index = start;
59
+ while (index < value.length) {
60
+ var character = value.charCodeAt(index);
61
+ if (character !== 32 && (character < 9 || character > 13)) {
62
+ break;
63
+ }
64
+ index++;
65
+ }
66
+ return index;
67
+ }
68
+ function isPseudoNameCharacter(character) {
69
+ return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
70
+ }
@@ -11,8 +11,7 @@ var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
11
11
  var selectorCache = new WeakMap();
12
12
 
13
13
  /**
14
- * Parse each selector once per linted source file. Return a clone so selector traversal state is
15
- * not shared between rules.
14
+ * Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
16
15
  */
17
16
  function parseSelector(context, selectorText) {
18
17
  var sourceCode = (0, _contextCompat.getSourceCode)(context);
@@ -23,9 +22,9 @@ function parseSelector(context, selectorText) {
23
22
  }
24
23
  var cachedSelector = sourceFileCache.get(selectorText);
25
24
  if (cachedSelector) {
26
- return cachedSelector.clone();
25
+ return cachedSelector;
27
26
  }
28
27
  var selector = cssSelectorProcessor.astSync(selectorText);
29
28
  sourceFileCache.set(selectorText, selector);
30
- return selector.clone();
29
+ return selector;
31
30
  }
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.getStyleCalls = getStyleCalls;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
10
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
11
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
12
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
13
+ var styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
14
+ var defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
15
+ var styleCallsBySourceCode = new WeakMap();
16
+
17
+ /**
18
+ * Visit imported style calls without resolving every CallExpression against its current scope.
19
+ *
20
+ * ESLint has already linked import bindings to all of their references. Building this index once
21
+ * per source file makes each styling rule proportional to the number of style calls instead of the
22
+ * number of all calls in the file.
23
+ */
24
+ function getStyleCalls(context) {
25
+ var sourceCode = (0, _contextCompat.getSourceCode)(context);
26
+ var styleCalls = styleCallsBySourceCode.get(sourceCode);
27
+ if (!styleCalls) {
28
+ styleCalls = buildStyleCallIndex(sourceCode);
29
+ styleCallsBySourceCode.set(sourceCode, styleCalls);
30
+ }
31
+ return styleCalls;
32
+ }
33
+ function buildStyleCallIndex(sourceCode) {
34
+ var styleCalls = [];
35
+ var seenCalls = new Set();
36
+ var scopeManager = sourceCode.scopeManager;
37
+ if (!scopeManager) {
38
+ return styleCalls;
39
+ }
40
+ var _iterator = _createForOfIteratorHelper(sourceCode.ast.body),
41
+ _step;
42
+ try {
43
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
44
+ var statement = _step.value;
45
+ if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
46
+ continue;
47
+ }
48
+ var importSource = statement.source.value;
49
+ var _iterator2 = _createForOfIteratorHelper(statement.specifiers),
50
+ _step2;
51
+ try {
52
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
53
+ var specifier = _step2.value;
54
+ var styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
55
+ if (!styleFunction) {
56
+ continue;
57
+ }
58
+ var _scopeManager$getDecl = scopeManager.getDeclaredVariables(specifier),
59
+ _scopeManager$getDecl2 = (0, _slicedToArray2.default)(_scopeManager$getDecl, 1),
60
+ variable = _scopeManager$getDecl2[0];
61
+ if (!variable) {
62
+ continue;
63
+ }
64
+ var _iterator3 = _createForOfIteratorHelper(variable.references),
65
+ _step3;
66
+ try {
67
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
68
+ var reference = _step3.value;
69
+ var call = getStyleCall(reference.identifier, styleFunction);
70
+ if (!call || seenCalls.has(call)) {
71
+ continue;
72
+ }
73
+ seenCalls.add(call);
74
+ styleCalls.push({
75
+ importSource: importSource,
76
+ node: call,
77
+ styleFunction: styleFunction
78
+ });
79
+ }
80
+ } catch (err) {
81
+ _iterator3.e(err);
82
+ } finally {
83
+ _iterator3.f();
84
+ }
85
+ }
86
+ } catch (err) {
87
+ _iterator2.e(err);
88
+ } finally {
89
+ _iterator2.f();
90
+ }
91
+ }
92
+ } catch (err) {
93
+ _iterator.e(err);
94
+ } finally {
95
+ _iterator.f();
96
+ }
97
+ return styleCalls;
98
+ }
99
+ function getStyleFunctionFromSpecifier(specifier, importSource) {
100
+ if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
101
+ return specifier.imported.name;
102
+ }
103
+ if (specifier.type !== 'ImportDefaultSpecifier') {
104
+ return null;
105
+ }
106
+ if (styleFunctionNames.has(specifier.local.name)) {
107
+ return specifier.local.name;
108
+ }
109
+ return defaultStyledImportSources.has(importSource) ? 'styled' : null;
110
+ }
111
+ function getStyleCall(identifier, styleFunction) {
112
+ var _parent2, _parent$parent;
113
+ var callee = identifier;
114
+ var parent = callee.parent;
115
+ while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
116
+ var _parent;
117
+ callee = parent;
118
+ parent = callee.parent;
119
+ }
120
+ if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
121
+ return null;
122
+ }
123
+ if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
124
+ return parent.parent;
125
+ }
126
+ return parent;
127
+ }