@atlaskit/eslint-plugin-ui-styling-standard 2.2.0 → 2.4.0

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 (57) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +20 -19
  3. package/dist/cjs/presets/all-flat.codegen.js +3 -2
  4. package/dist/cjs/presets/all.codegen.js +3 -2
  5. package/dist/cjs/presets/recommended-flat.codegen.js +2 -2
  6. package/dist/cjs/presets/recommended.codegen.js +2 -2
  7. package/dist/cjs/rules/index.codegen.js +3 -1
  8. package/dist/cjs/rules/no-atlaskit-theme/index.js +1 -1
  9. package/dist/cjs/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +232 -0
  10. package/dist/cjs/rules/no-top-layer-unsafe-selectors/index.js +425 -0
  11. package/dist/cjs/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +63 -0
  12. package/dist/cjs/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2415 -0
  13. package/dist/cjs/rules/no-unsafe-selectors/constants.js +4 -1
  14. package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +28 -1
  15. package/dist/cjs/rules/utils/walk-style-call-properties.js +11 -24
  16. package/dist/cjs/rules/utils/walk-style-object-properties.js +47 -0
  17. package/dist/es2019/presets/all-flat.codegen.js +3 -2
  18. package/dist/es2019/presets/all.codegen.js +3 -2
  19. package/dist/es2019/presets/recommended-flat.codegen.js +2 -2
  20. package/dist/es2019/presets/recommended.codegen.js +2 -2
  21. package/dist/es2019/rules/index.codegen.js +3 -1
  22. package/dist/es2019/rules/no-atlaskit-theme/index.js +1 -1
  23. package/dist/es2019/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +226 -0
  24. package/dist/es2019/rules/no-top-layer-unsafe-selectors/index.js +382 -0
  25. package/dist/es2019/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +57 -0
  26. package/dist/es2019/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2131 -0
  27. package/dist/es2019/rules/no-unsafe-selectors/constants.js +4 -1
  28. package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +26 -1
  29. package/dist/es2019/rules/utils/walk-style-call-properties.js +12 -15
  30. package/dist/es2019/rules/utils/walk-style-object-properties.js +29 -0
  31. package/dist/esm/presets/all-flat.codegen.js +3 -2
  32. package/dist/esm/presets/all.codegen.js +3 -2
  33. package/dist/esm/presets/recommended-flat.codegen.js +2 -2
  34. package/dist/esm/presets/recommended.codegen.js +2 -2
  35. package/dist/esm/rules/index.codegen.js +3 -1
  36. package/dist/esm/rules/no-atlaskit-theme/index.js +1 -1
  37. package/dist/esm/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +226 -0
  38. package/dist/esm/rules/no-top-layer-unsafe-selectors/index.js +419 -0
  39. package/dist/esm/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +57 -0
  40. package/dist/esm/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2409 -0
  41. package/dist/esm/rules/no-unsafe-selectors/constants.js +4 -1
  42. package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +28 -1
  43. package/dist/esm/rules/utils/walk-style-call-properties.js +11 -24
  44. package/dist/esm/rules/utils/walk-style-object-properties.js +41 -0
  45. package/dist/types/presets/all-flat.codegen.d.ts +1 -1
  46. package/dist/types/presets/all.codegen.d.ts +1 -1
  47. package/dist/types/presets/recommended-flat.codegen.d.ts +1 -1
  48. package/dist/types/presets/recommended.codegen.d.ts +1 -1
  49. package/dist/types/rules/index.codegen.d.ts +1 -1
  50. package/dist/types/rules/no-top-layer-unsafe-selectors/find-template-selectors.d.ts +29 -0
  51. package/dist/types/rules/no-top-layer-unsafe-selectors/index.d.ts +3 -0
  52. package/dist/types/rules/no-top-layer-unsafe-selectors/preserve-list-layout.d.ts +12 -0
  53. package/dist/types/rules/no-top-layer-unsafe-selectors/rewrite-selector.d.ts +164 -0
  54. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -1
  55. package/dist/types/rules/utils/walk-style-call-properties.d.ts +1 -0
  56. package/dist/types/rules/utils/walk-style-object-properties.d.ts +18 -0
  57. package/package.json +1 -1
@@ -0,0 +1,419 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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; } }
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; }
5
+ import { getScope, getSourceCode } from '@atlaskit/eslint-utils/context-compat';
6
+ import { isCss, isStyled } from '@atlaskit/eslint-utils/is-supported-import';
7
+ import { importSources } from '@atlaskit/eslint-utils/schema';
8
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
9
+ import { getStyleCalls } from '../utils/style-calls';
10
+ import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
11
+ import { walkStyleObjectProperties } from '../utils/walk-style-object-properties';
12
+ import { findTemplateSelectors } from './find-template-selectors';
13
+ import { preserveListLayout } from './preserve-list-layout';
14
+ import { rewriteSelector } from './rewrite-selector';
15
+
16
+ /**
17
+ * Names of JSX props whose value can be an inline style object.
18
+ */
19
+ var STYLE_PROP_NAMES = new Set(['css', 'xcss']);
20
+
21
+ /**
22
+ * Element names an unquoted style-object key can spell: HTML, plus the SVG elements a component
23
+ * styles often enough to write unquoted. Hyphenated custom elements cannot be identifiers.
24
+ */
25
+ var HTML_TAG_NAMES = new Set(['a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'mark', 'menu', 'meter', 'nav', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'picture', 'pre', 'progress', 'q', 's', 'samp', 'section', 'select', 'small', 'source', 'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'u', 'ul', 'var', 'video', 'circle', 'ellipse', 'g', 'line', 'path', 'polygon', 'polyline', 'rect', 'svg', 'text', 'use']);
26
+
27
+ /**
28
+ * TypeScript wrappers around an expression that ESTree does not model. All three leave the
29
+ * runtime value untouched, so the rule looks through them.
30
+ */
31
+ var TYPE_WRAPPER_TYPES = new Set(['TSAsExpression', 'TSSatisfiesExpression', 'TSTypeAssertion']);
32
+
33
+ /**
34
+ * One selector the rule has located in the source, with everything the fixer needs.
35
+ */
36
+
37
+ var rule = createLintRuleWithTypedConfig({
38
+ meta: {
39
+ name: 'no-top-layer-unsafe-selectors',
40
+ docs: {
41
+ description: 'Disallows selectors that a Design System layering surface rendered in the browser top layer can wrongly match, or wrongly displace.',
42
+ /**
43
+ * Deliberately not in `recommended` yet. The UI Styling Standard `recommended` preset
44
+ * is extended unscoped by `platform`, `jira`, `confluence` and eight other products, so
45
+ * enabling this before the bulk sweep has landed would break CI monorepo-wide on day
46
+ * one. Move it to `recommended` once the sweep is in and the ratchet is re-baselined.
47
+ */
48
+ recommended: false,
49
+ severity: 'error'
50
+ },
51
+ fixable: 'code',
52
+ messages: {
53
+ 'unsafe-selector': 'This selector can match a Design System layering surface rendered in the browser top layer, or be displaced by one. Add the top layer guard.',
54
+ /**
55
+ * The guard closes part of the exposure and the rest needs a human: the transform's
56
+ * `reason` on a successful rewrite is a residual exposure, not decoration.
57
+ */
58
+ 'unsafe-selector-with-residual-exposure': 'This selector can match a Design System layering surface rendered in the browser top layer, or be displaced by one. The top layer guard closes part of that, and the rest needs review: {{reason}}',
59
+ /**
60
+ * "no guard form is available" rather than "exists": the positional family does have a
61
+ * guard form, verified in Chromium, and it is withheld because Compiled's extract mode
62
+ * emits no CSS for it. The `reason` says which of the two this site is.
63
+ */
64
+ 'no-guard-form': 'This selector can be broken by a Design System layering surface rendered in the browser top layer, and no guard form is available for it. {{reason}}',
65
+ 'dynamic-selector': 'This selector is constructed dynamically, so it cannot be checked for top layer safety. {{reason}}',
66
+ 'malformed-selector': 'This selector could not be parsed, so it cannot be checked for top layer safety. {{reason}}'
67
+ },
68
+ schema: {
69
+ type: 'object',
70
+ properties: {
71
+ importSources: importSources,
72
+ /**
73
+ * Report selectors that are unsafe but for which no guard form is available. These
74
+ * cannot be autofixed — they need a human decision — so a codebase mid-migration
75
+ * may want them off until the residue has been triaged.
76
+ *
77
+ * Turning it off also hides the whole positional family, which is routed through
78
+ * this message while the `of S` form is withdrawn. See the README.
79
+ */
80
+ reportSelectorsWithNoGuardForm: {
81
+ type: 'boolean',
82
+ default: true
83
+ },
84
+ /**
85
+ * Report selectors the transform had to skip: ones constructed dynamically, and ones
86
+ * that do not parse as a selector at all. Off by default: neither is actionable
87
+ * in an editor.
88
+ */
89
+ reportDynamicSelectors: {
90
+ type: 'boolean',
91
+ default: false
92
+ }
93
+ },
94
+ additionalProperties: false
95
+ }
96
+ },
97
+ create: function create(context, config) {
98
+ /**
99
+ * Report one selector, and autofix it when a guard form exists.
100
+ *
101
+ * Every selector the rule sees is written inside a style object, a style template or a
102
+ * `css` prop, so the transform is always asked for the `nested` reading.
103
+ */
104
+ function check(_ref) {
105
+ var range = _ref.range,
106
+ selector = _ref.selector,
107
+ toSource = _ref.toSource;
108
+ var result = rewriteSelector(selector, undefined, {
109
+ scope: 'nested'
110
+ });
111
+ var source = getSourceCode(context);
112
+ var loc = {
113
+ start: source.getLocFromIndex(range[0]),
114
+ end: source.getLocFromIndex(range[1])
115
+ };
116
+ if (result.population === 'guardable') {
117
+ if (result.after === null) {
118
+ /* Provably already safe — nothing to write and nothing to report. */
119
+ return;
120
+ }
121
+ var replacement = toSource(preserveListLayout({
122
+ original: selector,
123
+ rewritten: result.after
124
+ }));
125
+ context.report({
126
+ loc: loc,
127
+ messageId: result.reason ? 'unsafe-selector-with-residual-exposure' : 'unsafe-selector',
128
+ data: reportData(result),
129
+ fix: function fix(fixer) {
130
+ return fixer.replaceTextRange(range, replacement);
131
+ }
132
+ });
133
+ return;
134
+ }
135
+ if (result.population === 'residue') {
136
+ if (config.reportSelectorsWithNoGuardForm) {
137
+ context.report({
138
+ loc: loc,
139
+ messageId: 'no-guard-form',
140
+ data: reportData(result)
141
+ });
142
+ }
143
+ return;
144
+ }
145
+ if (result.population === 'skipped') {
146
+ if (config.reportDynamicSelectors) {
147
+ context.report({
148
+ loc: loc,
149
+ messageId: result.skipCause === 'malformed' ? 'malformed-selector' : 'dynamic-selector',
150
+ data: reportData(result)
151
+ });
152
+ }
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Check one property of a style object. A key is only a selector when its value is another
158
+ * style object — the same test `no-unsafe-selectors` and `no-nested-selectors` use — so a
159
+ * declaration such as `color: 'red'` or `'font-size': '12px'` is never read as one.
160
+ *
161
+ * Returns without reporting when the key is an at-rule, a computed name the rule cannot
162
+ * read, or a literal whose raw text is not a plain, unescaped string, because the fixer's
163
+ * range arithmetic would be wrong and a wrong fix is worse than none.
164
+ */
165
+ function checkProperty(property) {
166
+ if (property.value.type !== 'ObjectExpression') {
167
+ return;
168
+ }
169
+ var source = getSourceCode(context);
170
+ var key = property.key;
171
+ if (key.type === 'Literal' && typeof key.value === 'string') {
172
+ if (isAtRule(key.value)) {
173
+ return;
174
+ }
175
+ var raw = source.getText(key);
176
+ var inner = raw.slice(1, -1);
177
+ if (inner !== key.value) {
178
+ /* The literal uses escapes; leave it to a human. */
179
+ return;
180
+ }
181
+ if (hasComment(key.value)) {
182
+ return;
183
+ }
184
+ var _ref2 = key.range,
185
+ _ref3 = _slicedToArray(_ref2, 2),
186
+ start = _ref3[0],
187
+ end = _ref3[1];
188
+ check({
189
+ range: [start + 1, end - 1],
190
+ selector: key.value,
191
+ toSource: identity
192
+ });
193
+ return;
194
+ }
195
+ if (key.type === 'TemplateLiteral') {
196
+ var _ref4 = key.range,
197
+ _ref5 = _slicedToArray(_ref4, 2),
198
+ _start = _ref5[0],
199
+ _end = _ref5[1];
200
+ var text = source.getText(key).slice(1, -1);
201
+ if (isAtRule(text) || hasComment(text)) {
202
+ return;
203
+ }
204
+ check({
205
+ range: [_start + 1, _end - 1],
206
+ selector: text,
207
+ toSource: identity
208
+ });
209
+ return;
210
+ }
211
+
212
+ /**
213
+ * `css({ span: {…} })`. An unquoted key is the same selector as its quoted spelling,
214
+ * and the styling APIs read it the same way. A *computed* identifier (`[selector]: {…}`)
215
+ * is a variable, not a name, and is left alone.
216
+ *
217
+ * Only an element name counts. A camelCase key with an object value is far more often a
218
+ * container of the styling API itself (`selectors: {…}`) than a custom element, and
219
+ * "fixing" it to `'selectors:not(…)'` would break it.
220
+ */
221
+ if (key.type === 'Identifier' && !property.computed && HTML_TAG_NAMES.has(key.name)) {
222
+ check({
223
+ range: key.range,
224
+ selector: key.name,
225
+ toSource: function toSource(rewritten) {
226
+ return "'".concat(rewritten, "'");
227
+ }
228
+ });
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Stops the walk at a `@keyframes` block, whose keys are offsets rather than selectors.
234
+ * See {@link isKeyframesAtRule} for what goes wrong without it.
235
+ */
236
+ function skipSubtree(property) {
237
+ var key = property.key;
238
+ if (key.type === 'Literal' && typeof key.value === 'string') {
239
+ return isKeyframesAtRule(key.value);
240
+ }
241
+ if (key.type === 'TemplateLiteral') {
242
+ var source = getSourceCode(context);
243
+ return isKeyframesAtRule(source.getText(key).slice(1, -1));
244
+ }
245
+
246
+ /* An identifier or a computed key cannot spell an at-rule. */
247
+ return false;
248
+ }
249
+
250
+ /**
251
+ * `css={{…}}`, `css={[{…}, {…}]}`, and either wrapped in `as` / `satisfies` / `<T>`.
252
+ */
253
+ function walkStyleProp(expression) {
254
+ var unwrapped = unwrapTypeWrappers(expression);
255
+ if (unwrapped.type === 'ObjectExpression') {
256
+ walkStyleObjectProperties(unwrapped, checkProperty, {
257
+ skipSubtree: skipSubtree
258
+ });
259
+ return;
260
+ }
261
+ if (unwrapped.type === 'ArrayExpression') {
262
+ var _iterator = _createForOfIteratorHelper(unwrapped.elements),
263
+ _step;
264
+ try {
265
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
266
+ var element = _step.value;
267
+ if (element && element.type !== 'SpreadElement') {
268
+ walkStyleProp(element);
269
+ }
270
+ }
271
+ } catch (err) {
272
+ _iterator.e(err);
273
+ } finally {
274
+ _iterator.f();
275
+ }
276
+ }
277
+ }
278
+ return {
279
+ /**
280
+ * `css({…})`, `cssMap({…})`, `xcss({…})`, `styled.div({…})`, `styled(Base)({…})`.
281
+ *
282
+ * The style calls are indexed once per file from the import bindings, so the rule does
283
+ * work proportional to the number of style calls rather than the number of calls.
284
+ */
285
+ Program: function Program() {
286
+ var _iterator2 = _createForOfIteratorHelper(getStyleCalls(context)),
287
+ _step2;
288
+ try {
289
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
290
+ var styleCall = _step2.value;
291
+ if (!config.importSources.includes(styleCall.importSource)) {
292
+ continue;
293
+ }
294
+ walkStyleCallProperties(styleCall, checkProperty, {
295
+ skipSubtree: skipSubtree
296
+ });
297
+ }
298
+ } catch (err) {
299
+ _iterator2.e(err);
300
+ } finally {
301
+ _iterator2.f();
302
+ }
303
+ },
304
+ /**
305
+ * ``styled.div`…` `` and ``css`…` ``. The style-call index cannot see these, so the
306
+ * template's raw text is scanned directly. ``keyframes`…` `` is not scanned: its blocks
307
+ * are keyed by `from`, `to` and percentages, none of which is an element selector.
308
+ */
309
+ TaggedTemplateExpression: function TaggedTemplateExpression(node) {
310
+ var _getScope = getScope(context, node),
311
+ references = _getScope.references;
312
+ var tag = node.tag;
313
+ var isStyleTemplate = isStyled(tag, references, config.importSources) || isCss(tag, references, config.importSources);
314
+ if (!isStyleTemplate) {
315
+ return;
316
+ }
317
+ var source = getSourceCode(context);
318
+ var quasi = node.quasi;
319
+ var _ref6 = quasi.range,
320
+ _ref7 = _slicedToArray(_ref6, 1),
321
+ quasiStart = _ref7[0];
322
+
323
+ /* Scan the text *inside* the backticks, so the opening backtick can never be
324
+ * swallowed into the first selector. */
325
+ var text = source.getText(quasi).slice(1, -1);
326
+ var _iterator3 = _createForOfIteratorHelper(findTemplateSelectors(text, quasiStart + 1)),
327
+ _step3;
328
+ try {
329
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
330
+ var selector = _step3.value;
331
+ check({
332
+ range: [selector.start, selector.end],
333
+ selector: selector.text,
334
+ toSource: identity
335
+ });
336
+ }
337
+ } catch (err) {
338
+ _iterator3.e(err);
339
+ } finally {
340
+ _iterator3.f();
341
+ }
342
+ },
343
+ /**
344
+ * `<div css={{ '& > div': {} }} />`. Only a *direct* object literal, or an array of
345
+ * them, is handled here — `css={css({…})}` is already covered by the style-call index,
346
+ * and handling it twice would double-report.
347
+ *
348
+ * No import is checked. Under the automatic JSX runtime the `css` prop needs no import
349
+ * at all, so gating on one would miss most real usage; `no-imported-style-values` reads
350
+ * the same props the same way.
351
+ */
352
+ JSXAttribute: function JSXAttribute(node) {
353
+ if (node.type !== 'JSXAttribute') {
354
+ return;
355
+ }
356
+ if (node.name.type !== 'JSXIdentifier' || !STYLE_PROP_NAMES.has(node.name.name)) {
357
+ return;
358
+ }
359
+ if (!node.value || node.value.type !== 'JSXExpressionContainer') {
360
+ return;
361
+ }
362
+ walkStyleProp(node.value.expression);
363
+ }
364
+ };
365
+ }
366
+ });
367
+ export default rule;
368
+ function identity(text) {
369
+ return text;
370
+ }
371
+
372
+ /**
373
+ * At-rules are blocks but not selectors; the transform has nothing to say about them, and
374
+ * parsing `@media screen and (…)` as a selector would guard the media query.
375
+ */
376
+ function isAtRule(text) {
377
+ return text.trimStart().startsWith('@');
378
+ }
379
+
380
+ /**
381
+ * A comment vanishes when the CSS is tokenised, so a guard appended after one lands on the far
382
+ * side of the whitespace around it: `& > div /* c *​/` would become the descendant selector
383
+ * `div :not(…)`. Such a key is left alone, as a template selector with a comment inside it is.
384
+ */
385
+ function hasComment(text) {
386
+ return text.includes('/*');
387
+ }
388
+
389
+ /**
390
+ * `@keyframes` is the one at-rule whose children are not selectors either. Its keys are offsets
391
+ * — `from`, `to`, `0%`, `50%` — and each parses cleanly as a tag selector, so without this the
392
+ * walk into the block reports them and the autofix rewrites `from` to
393
+ * `from:not(:where(…))`. That is not a valid keyframe selector, so the keyframe is dropped and
394
+ * the animation stops, with no build error and nothing left for a re-lint to catch.
395
+ *
396
+ * Declining to *visit* the `@keyframes` property is not enough; the descent into its value has
397
+ * to stop. Every other at-rule (`@media`, `@supports`, `@container`) nests real selectors and
398
+ * must still be walked. Vendor prefixes are matched because `@-webkit-keyframes` is still shipped.
399
+ */
400
+ function isKeyframesAtRule(text) {
401
+ return /^@(-[a-z]+-)?keyframes\b/i.test(text.trimStart());
402
+ }
403
+ function reportData(result) {
404
+ var _result$reason;
405
+ return {
406
+ reason: (_result$reason = result.reason) !== null && _result$reason !== void 0 ? _result$reason : ''
407
+ };
408
+ }
409
+
410
+ /**
411
+ * Look through `{…} as T`, `{…} satisfies T` and `<T>{…}`, which ESTree types do not model.
412
+ */
413
+ function unwrapTypeWrappers(node) {
414
+ var current = node;
415
+ while (TYPE_WRAPPER_TYPES.has(current.type)) {
416
+ current = current.expression;
417
+ }
418
+ return current;
419
+ }
@@ -0,0 +1,57 @@
1
+ import selectorParser from 'postcss-selector-parser';
2
+
3
+ /**
4
+ * Split a selector list on its top-level commas only, keeping the whitespace and comments
5
+ * around each branch exactly as written. Commas inside `(…)`, `[…]`, quotes, comments and
6
+ * escapes belong to their branch, because the parser already knows which commas separate
7
+ * branches and which do not.
8
+ *
9
+ * Returns `null` when the text does not parse, or when the branches do not join back into the
10
+ * original text, so the caller falls back to the transform's own text rather than re-assembling
11
+ * a list it cannot fully account for.
12
+ */
13
+ function splitTopLevel(_ref) {
14
+ var text = _ref.text;
15
+ try {
16
+ var root = selectorParser().astSync(text);
17
+ var parts = root.nodes.map(function (branch) {
18
+ return branch.toString();
19
+ });
20
+ if (parts.join(',') !== text) {
21
+ return null;
22
+ }
23
+ return parts;
24
+ } catch (_unused) {
25
+ return null;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Re-apply the author's layout of a selector list to the rewritten one.
31
+ *
32
+ * The transform joins its branches with `, `. A selector list written over several lines would
33
+ * be collapsed onto one by the fixer, so each rewritten branch is put back between the
34
+ * separators the author wrote. Falls back to the transform's text when either list does not
35
+ * split cleanly, or when the two do not have the same number of branches.
36
+ */
37
+ export function preserveListLayout(_ref2) {
38
+ var original = _ref2.original,
39
+ rewritten = _ref2.rewritten;
40
+ var originalParts = splitTopLevel({
41
+ text: original
42
+ });
43
+ var rewrittenParts = splitTopLevel({
44
+ text: rewritten
45
+ });
46
+ if (originalParts === null || rewrittenParts === null) {
47
+ return rewritten;
48
+ }
49
+ if (originalParts.length < 2 || originalParts.length !== rewrittenParts.length) {
50
+ return rewritten;
51
+ }
52
+ return originalParts.map(function (part, index) {
53
+ var leading = part.length - part.trimStart().length;
54
+ var trailing = part.length - part.trimEnd().length;
55
+ return "".concat(part.slice(0, leading)).concat(rewrittenParts[index].trim()).concat(part.slice(part.length - trailing));
56
+ }).join(',');
57
+ }