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