@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
@@ -9,7 +9,10 @@
9
9
 
10
10
  const cssPseudos = ['&:active', '&:autofill', '&:blank', '&:checked', '&:default', '&:defined', '&:disabled', '&:empty', '&:enabled', '&:first', '&:focus', '&:focus-visible', '&:focus-within', '&:fullscreen', '&:hover', '&:in-range', '&:indeterminate', '&:invalid', '&:left', '&:link', '&:local-link', '&:optional', '&:out-of-range', '&:paused', '&:picture-in-picture', '&:placeholder-shown', '&:playing', '&:popover-open', '&:read-only', '&:read-write', '&:required', '&:right', '&:target', '&:user-invalid', '&:user-valid', '&:valid', '&:visited', '&::after', '&::backdrop', '&::before', '&::cue', '&::cue-region', '&::first-letter', '&::first-line', '&::grammar-error', '&::marker', '&::placeholder', '&::selection', '&::spelling-error', '&::target-text', '&::view-transition', '&::-webkit-details-marker',
11
11
  // Chained pseudos (CSSFlattenedChainedPsuedos from @compiled/react)
12
- '&:active:visited', '&:focus:not(:focus-visible)', '&:focus::after', '&:focus::before', '&:focus-visible::after', '&:focus-visible::before', '&:focus-within::after', '&:focus-within::before', '&:hover::after', '&:hover::before', '&:visited:active', '&:visited:hover', '&:visited:focus', '&:visited:focus-visible', '&:visited:focus-within'];
12
+ '&:active:visited', '&:focus:not(:focus-visible)', '&:focus::after', '&:focus::before', '&:focus-visible::after', '&:focus-visible::before', '&:focus-within::after', '&:focus-within::before', '&:hover::after', '&:hover::before', '&:visited:active', '&:visited:hover', '&:visited:focus', '&:visited:focus-visible', '&:visited:focus-within',
13
+ // Vendor pseudo-elements with no standard equivalent. These are not part of CSSPseudos, so the
14
+ // typed @atlaskit/css and xcss APIs still reject them.
15
+ '&::-moz-focus-inner', '&::-moz-focus-outer', '&::-moz-range-progress', '&::-moz-range-thumb', '&::-moz-range-track', '&::-webkit-slider-runnable-track', '&::-webkit-slider-thumb'];
13
16
  export const allowedPseudos = new Set(
14
17
  /**
15
18
  * The `&` is included above for type checking reasons,
@@ -71,7 +71,7 @@ function checkNoRestrictedPseudos({
71
71
  sourceNode
72
72
  }, pseudos) {
73
73
  for (const pseudo of pseudos) {
74
- if (allowedPseudos.has(pseudo.value)) {
74
+ if (allowedPseudos.has(pseudo.value) || isInAllowedPseudoChain(pseudo)) {
75
75
  continue;
76
76
  }
77
77
 
@@ -101,6 +101,31 @@ function checkNoRestrictedPseudos({
101
101
  }
102
102
  }
103
103
 
104
+ /**
105
+ * The allowlist includes chained pseudos such as `:focus:not(:focus-visible)` whose parts are not
106
+ * all allowed on their own, so a pseudo is also allowed when a run of adjacent pseudos containing
107
+ * it matches an allowlisted chain.
108
+ */
109
+ function isInAllowedPseudoChain(pseudo) {
110
+ const chain = [pseudo];
111
+ for (let node = pseudo.prev(); isPseudo(node); node = node.prev()) {
112
+ chain.unshift(node);
113
+ }
114
+ for (let node = pseudo.next(); isPseudo(node); node = node.next()) {
115
+ chain.push(node);
116
+ }
117
+ const index = chain.indexOf(pseudo);
118
+ for (let start = 0; start <= index; start++) {
119
+ for (let end = index + 1; end <= chain.length; end++) {
120
+ const text = chain.slice(start, end).map(node => String(node).replace(/\s+/g, '')).join('');
121
+ if (allowedPseudos.has(text)) {
122
+ return true;
123
+ }
124
+ }
125
+ }
126
+ return false;
127
+ }
128
+
104
129
  /**
105
130
  * The original four pseudo-elements have a legacy single colon syntax.
106
131
  *
@@ -1,23 +1,20 @@
1
+ import { walkStyleObjectProperties } from './walk-style-object-properties';
1
2
  export function walkStyleCallProperties(styleCall, visitor, options) {
2
3
  const skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
4
+ const {
5
+ skipSubtree
6
+ } = options !== null && options !== void 0 ? options : {};
3
7
  for (const argument of styleCall.node.arguments) {
4
8
  if (argument.type === 'ObjectExpression') {
5
- walkStyleObject(argument, visitor, skipTopLevel);
9
+ walkStyleObjectProperties(argument, visitor, {
10
+ skipTopLevel,
11
+ skipSubtree
12
+ });
6
13
  } else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
7
- walkStyleObject(argument.body, visitor, skipTopLevel);
8
- }
9
- }
10
- }
11
- function walkStyleObject(object, visitor, skipVisitor) {
12
- for (const property of object.properties) {
13
- if (property.type !== 'Property') {
14
- continue;
15
- }
16
- if (!skipVisitor) {
17
- visitor(property);
18
- }
19
- if (property.value.type === 'ObjectExpression') {
20
- walkStyleObject(property.value, visitor, false);
14
+ walkStyleObjectProperties(argument.body, visitor, {
15
+ skipTopLevel,
16
+ skipSubtree
17
+ });
21
18
  }
22
19
  }
23
20
  }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Walk one style object's properties at every depth. With `skipTopLevel` the top-level
3
+ * properties are walked into but not visited, which is how a `cssMap` or `keyframes` argument
4
+ * hides its variant or block names. Nested objects are always visited in full. Use this directly
5
+ * for a style object that is not the argument of a style call, such as the value of a `css` prop.
6
+ *
7
+ * `skipSubtree` stops the descent into a property's value, so its children are never visited.
8
+ * A caller needs this when a nested block's keys are not the same kind of thing as its own key:
9
+ * the children of an `@keyframes` block are offsets (`from`, `50%`), not selectors, and a rule
10
+ * that treats them as selectors will report — or worse, fix — a keyframe offset. Declining to
11
+ * visit the at-rule property itself is not enough, because the walk into its value is what
12
+ * exposes the offsets. Omit it and the walk behaves exactly as before.
13
+ */
14
+ export function walkStyleObjectProperties(object, visitor, options) {
15
+ walk(object, visitor, Boolean(options === null || options === void 0 ? void 0 : options.skipTopLevel), options === null || options === void 0 ? void 0 : options.skipSubtree);
16
+ }
17
+ function walk(object, visitor, skipVisitor, skipSubtree) {
18
+ for (const property of object.properties) {
19
+ if (property.type !== 'Property') {
20
+ continue;
21
+ }
22
+ if (!skipVisitor) {
23
+ visitor(property);
24
+ }
25
+ if (property.value.type === 'ObjectExpression' && !(skipSubtree !== null && skipSubtree !== void 0 && skipSubtree(property))) {
26
+ walk(property.value, visitor, false, skipSubtree);
27
+ }
28
+ }
29
+ }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::7f3972a0198dc6de5194e21d5f32c3b0>>
3
+ * @codegen <<SignedSource::75b18fe1e3e0e0eeeb87a8dd4ed975ae>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -16,7 +16,7 @@ var config = {
16
16
  '@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
17
17
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
18
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'error',
19
- '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
19
+ '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'error',
20
20
  '@atlaskit/ui-styling-standard/no-classname-prop': 'error',
21
21
  '@atlaskit/ui-styling-standard/no-container-queries': 'error',
22
22
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
@@ -26,6 +26,7 @@ var config = {
26
26
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'error',
27
27
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'error',
28
28
  '@atlaskit/ui-styling-standard/no-styled': 'error',
29
+ '@atlaskit/ui-styling-standard/no-top-layer-unsafe-selectors': 'error',
29
30
  '@atlaskit/ui-styling-standard/no-unsafe-selectors': 'error',
30
31
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'error',
31
32
  '@atlaskit/ui-styling-standard/no-unused-cssmap-properties': 'warn',
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::138a044301c208ca5eecc78433523b0b>>
3
+ * @codegen <<SignedSource::ff2cac75bedd9b12fc1c17bc6cf66c5c>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -12,7 +12,7 @@ var config = {
12
12
  '@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
13
13
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
14
14
  '@atlaskit/ui-styling-standard/no-array-arguments': 'error',
15
- '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'error',
16
16
  '@atlaskit/ui-styling-standard/no-classname-prop': 'error',
17
17
  '@atlaskit/ui-styling-standard/no-container-queries': 'error',
18
18
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
@@ -22,6 +22,7 @@ var config = {
22
22
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'error',
23
23
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'error',
24
24
  '@atlaskit/ui-styling-standard/no-styled': 'error',
25
+ '@atlaskit/ui-styling-standard/no-top-layer-unsafe-selectors': 'error',
25
26
  '@atlaskit/ui-styling-standard/no-unsafe-selectors': 'error',
26
27
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'error',
27
28
  '@atlaskit/ui-styling-standard/no-unused-cssmap-properties': 'warn',
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::7f3972a0198dc6de5194e21d5f32c3b0>>
3
+ * @codegen <<SignedSource::4d0de15d2213df65666693ea490b4cce>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -16,7 +16,7 @@ var config = {
16
16
  '@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
17
17
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
18
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'error',
19
- '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
19
+ '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'error',
20
20
  '@atlaskit/ui-styling-standard/no-classname-prop': 'error',
21
21
  '@atlaskit/ui-styling-standard/no-container-queries': 'error',
22
22
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::138a044301c208ca5eecc78433523b0b>>
3
+ * @codegen <<SignedSource::56ecb290562bdf88a8a5b5388a39e5f9>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -12,7 +12,7 @@ var config = {
12
12
  '@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
13
13
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
14
14
  '@atlaskit/ui-styling-standard/no-array-arguments': 'error',
15
- '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-atlaskit-theme': 'error',
16
16
  '@atlaskit/ui-styling-standard/no-classname-prop': 'error',
17
17
  '@atlaskit/ui-styling-standard/no-container-queries': 'error',
18
18
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::094f8a058153c7e839c5d98d2718ca4e>>
3
+ * @codegen <<SignedSource::7b2b0662d0eeb0e3a3e4c844a505d27e>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -19,6 +19,7 @@ import noImportantStyles from './no-important-styles';
19
19
  import noImportedStyleValues from './no-imported-style-values';
20
20
  import noNestedSelectors from './no-nested-selectors';
21
21
  import noStyled from './no-styled';
22
+ import noTopLayerUnsafeSelectors from './no-top-layer-unsafe-selectors';
22
23
  import noUnsafeSelectors from './no-unsafe-selectors';
23
24
  import noUnsafeValues from './no-unsafe-values';
24
25
  import noUnusedCssmapProperties from './no-unused-cssmap-properties';
@@ -39,6 +40,7 @@ export var rules = {
39
40
  'no-imported-style-values': noImportedStyleValues,
40
41
  'no-nested-selectors': noNestedSelectors,
41
42
  'no-styled': noStyled,
43
+ 'no-top-layer-unsafe-selectors': noTopLayerUnsafeSelectors,
42
44
  'no-unsafe-selectors': noUnsafeSelectors,
43
45
  'no-unsafe-values': noUnsafeValues,
44
46
  'no-unused-cssmap-properties': noUnusedCssmapProperties,
@@ -5,7 +5,7 @@ var rule = createLintRule({
5
5
  docs: {
6
6
  description: 'Disallow imports from `@atlaskit/theme`',
7
7
  recommended: true,
8
- severity: 'warn'
8
+ severity: 'error'
9
9
  },
10
10
  messages: {
11
11
  'no-atlaskit-theme': 'Do not import `@atlaskit/theme`. Use `@atlaskit/tokens` or Atlassian Design System components instead.'
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Locate the selectors inside a tagged template literal's raw source text.
3
+ *
4
+ * The style-call index only sees call expressions, so `styled.div\`…\`` and `css\`…\`` are
5
+ * invisible to it. This scanner fills that gap: it walks the text between the template's
6
+ * backticks and returns the byte range of every selector, so ranges map 1:1 onto the file and
7
+ * can be handed straight to an ESLint fixer.
8
+ *
9
+ * It deliberately does not try to be a CSS parser. A selector is the text between the last
10
+ * `;`, `{` or `}` and the next `{`. Anything it mis-reads will either fail to parse as a
11
+ * selector — in which case `rewriteSelector` skips it — or be reported for review. What it
12
+ * must not do is mistake a `${…}` interpolation, a quoted string or a comment for CSS
13
+ * structure, so it steps over all three.
14
+ */
15
+
16
+ export function findTemplateSelectors(source, offset) {
17
+ var found = [];
18
+ var segmentStart = 0;
19
+ /**
20
+ * Set when a comment appears after the segment's first non-whitespace character. A selector
21
+ * with a comment inside it — `& /* x *​/ > div` — cannot be replaced as one contiguous range
22
+ * without eating the comment, so it is skipped rather than mangled. A comment *before* the
23
+ * selector just moves `segmentStart` past it.
24
+ */
25
+ var commentInsideSegment = false;
26
+ /**
27
+ * How many `(` are open. Inside a function's parentheses `//` is data, not a comment:
28
+ * `url(//cdn.example.com/a.png)` and an unquoted data URI both carry one.
29
+ */
30
+ var parenthesisDepth = 0;
31
+ /**
32
+ * How many CSS `{` are open. `${…}` braces and parentheses do not count.
33
+ */
34
+ var braceDepth = 0;
35
+ /**
36
+ * The brace depth at which a `@keyframes` block opened, or `null` outside one. Everything
37
+ * nested inside is an offset (`from`, `50%`), not a selector — each parses as a tag selector,
38
+ * so without this the guard is written onto a keyframe and the animation stops. Skipping the
39
+ * at-rule's own prelude is not enough, because the block's contents open their own segments.
40
+ */
41
+ var keyframesDepth = null;
42
+ var startSegment = function startSegment(at) {
43
+ segmentStart = at;
44
+ commentInsideSegment = false;
45
+ };
46
+
47
+ /**
48
+ * Step over a comment spanning `[start, end)`. A comment before the segment's first
49
+ * non-whitespace character just moves the segment past it; a comment inside the segment
50
+ * marks it unfixable. Returns the index to resume scanning at.
51
+ */
52
+ var skipComment = function skipComment(start, end) {
53
+ var isLeading = source.slice(segmentStart, start).trim() === '';
54
+ if (isLeading) {
55
+ segmentStart = end;
56
+ } else {
57
+ commentInsideSegment = true;
58
+ }
59
+ return end;
60
+ };
61
+ for (var index = 0; index < source.length; index++) {
62
+ var character = source[index];
63
+
64
+ /* Step over a quoted string — `content: "{"` must not open a block. */
65
+ if (character === '"' || character === "'") {
66
+ index = skipQuoted(source, index);
67
+ continue;
68
+ }
69
+ if (character === '/' && source[index + 1] === '*') {
70
+ var close = source.indexOf('*/', index + 2);
71
+ var end = close === -1 ? source.length : close + 2;
72
+ index = skipComment(index, end) - 1;
73
+ continue;
74
+ }
75
+
76
+ /**
77
+ * A `//` line comment. Emotion and styled-components strip these before the CSS is
78
+ * parsed, so a selector written after one on the next line is real. Inside parentheses
79
+ * the same two characters are part of a URL, so the depth check keeps them.
80
+ */
81
+ if (character === '/' && source[index + 1] === '/' && parenthesisDepth === 0) {
82
+ var newline = source.indexOf('\n', index + 2);
83
+ var _end = newline === -1 ? source.length : newline + 1;
84
+ index = skipComment(index, _end) - 1;
85
+ continue;
86
+ }
87
+ if (character === '(') {
88
+ parenthesisDepth++;
89
+ continue;
90
+ }
91
+ if (character === ')') {
92
+ parenthesisDepth = Math.max(0, parenthesisDepth - 1);
93
+ continue;
94
+ }
95
+
96
+ /* Step over a `${…}` interpolation. Its braces are not CSS braces, but the `${…}` text
97
+ * itself stays in the segment so the transform can recognise the selector as dynamic. */
98
+ if (character === '{' && index > 0 && source[index - 1] === '$') {
99
+ var isLeading = source.slice(segmentStart, index - 1).trim() === '';
100
+ index = skipInterpolation(source, index);
101
+ /**
102
+ * A `${mixin}` alone on its line, with no `;` after it, is a statement of its own:
103
+ * the selector on the next line starts a fresh segment rather than inheriting the
104
+ * interpolation and being read as dynamic.
105
+ */
106
+ if (isLeading && isRestOfLineBlank(source, index + 1)) {
107
+ startSegment(index + 1);
108
+ }
109
+ continue;
110
+ }
111
+ if (character === '{') {
112
+ var raw = source.slice(segmentStart, index);
113
+ var leading = raw.length - raw.trimStart().length;
114
+ var text = raw.trim();
115
+
116
+ /* At-rules are blocks but not selectors; the transform has nothing to say about them. */
117
+ if (text !== '' && !text.startsWith('@') && !commentInsideSegment && keyframesDepth === null) {
118
+ found.push({
119
+ text: text,
120
+ start: offset + segmentStart + leading,
121
+ end: offset + segmentStart + leading + text.length
122
+ });
123
+ }
124
+ if (keyframesDepth === null && /^@(-[a-z]+-)?keyframes\b/i.test(text)) {
125
+ keyframesDepth = braceDepth;
126
+ }
127
+ braceDepth++;
128
+ startSegment(index + 1);
129
+ continue;
130
+ }
131
+ if (character === '}') {
132
+ braceDepth = Math.max(0, braceDepth - 1);
133
+ if (keyframesDepth !== null && braceDepth <= keyframesDepth) {
134
+ keyframesDepth = null;
135
+ }
136
+ }
137
+ if (character === '}' || character === ';') {
138
+ startSegment(index + 1);
139
+ }
140
+ }
141
+ return found;
142
+ }
143
+
144
+ /**
145
+ * Is everything from `start` to the next newline whitespace? A missing newline does not count:
146
+ * `${selector}` at the very end has nothing after it to separate from.
147
+ */
148
+ function isRestOfLineBlank(source, start) {
149
+ var newline = source.indexOf('\n', start);
150
+ return newline !== -1 && source.slice(start, newline).trim() === '';
151
+ }
152
+
153
+ /**
154
+ * Index of the quote closing the string that opens at `start`, or the end of the source when
155
+ * the string is unterminated.
156
+ */
157
+ function skipQuoted(source, start) {
158
+ var quote = source[start];
159
+ var index = start + 1;
160
+ while (index < source.length) {
161
+ if (source[index] === '\\') {
162
+ index += 2;
163
+ continue;
164
+ }
165
+ if (source[index] === quote) {
166
+ return index;
167
+ }
168
+ index++;
169
+ }
170
+ return source.length;
171
+ }
172
+
173
+ /**
174
+ * Index of the `}` closing the interpolation whose `{` is at `start`, or the end of the source
175
+ * when it is unterminated.
176
+ *
177
+ * The interpolation holds JavaScript, so a brace inside one of its string literals is not a
178
+ * brace: `${cond ? '}' : ''}` closes at the last character, not at the quoted one. Quoted
179
+ * strings and template literals are stepped over; a template literal may in turn hold its own
180
+ * interpolations, so the two skips call each other.
181
+ */
182
+ function skipInterpolation(source, start) {
183
+ var depth = 1;
184
+ var index = start + 1;
185
+ while (index < source.length && depth > 0) {
186
+ var character = source[index];
187
+ if (character === '"' || character === "'") {
188
+ index = skipQuoted(source, index) + 1;
189
+ continue;
190
+ }
191
+ if (character === '`') {
192
+ index = skipTemplateLiteral(source, index) + 1;
193
+ continue;
194
+ }
195
+ if (character === '{') {
196
+ depth++;
197
+ } else if (character === '}') {
198
+ depth--;
199
+ }
200
+ index++;
201
+ }
202
+ return index - 1;
203
+ }
204
+
205
+ /**
206
+ * Index of the backtick closing the template literal that opens at `start`, or the end of the
207
+ * source when it is unterminated.
208
+ */
209
+ function skipTemplateLiteral(source, start) {
210
+ var index = start + 1;
211
+ while (index < source.length) {
212
+ if (source[index] === '\\') {
213
+ index += 2;
214
+ continue;
215
+ }
216
+ if (source[index] === '`') {
217
+ return index;
218
+ }
219
+ if (source[index] === '$' && source[index + 1] === '{') {
220
+ index = skipInterpolation(source, index + 1) + 1;
221
+ continue;
222
+ }
223
+ index++;
224
+ }
225
+ return source.length;
226
+ }