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