@atlaskit/eslint-plugin-ui-styling-standard 2.4.0 → 3.0.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 +23 -0
- package/README.md +0 -1
- package/dist/cjs/presets/all-flat.codegen.js +1 -2
- package/dist/cjs/presets/all.codegen.js +1 -2
- package/dist/cjs/rules/index.codegen.js +1 -3
- package/dist/cjs/rules/no-imported-style-values/index.js +8 -1
- package/dist/cjs/rules/no-nested-selectors/index.js +13 -1
- package/dist/cjs/rules/utils/walk-style-call-properties.js +24 -11
- package/dist/es2019/presets/all-flat.codegen.js +1 -2
- package/dist/es2019/presets/all.codegen.js +1 -2
- package/dist/es2019/rules/index.codegen.js +1 -3
- package/dist/es2019/rules/no-imported-style-values/index.js +8 -1
- package/dist/es2019/rules/no-nested-selectors/index.js +13 -1
- package/dist/es2019/rules/utils/walk-style-call-properties.js +15 -12
- package/dist/esm/presets/all-flat.codegen.js +1 -2
- package/dist/esm/presets/all.codegen.js +1 -2
- package/dist/esm/rules/index.codegen.js +1 -3
- package/dist/esm/rules/no-imported-style-values/index.js +8 -1
- package/dist/esm/rules/no-nested-selectors/index.js +13 -1
- package/dist/esm/rules/utils/walk-style-call-properties.js +24 -11
- package/dist/types/presets/all-flat.codegen.d.ts +1 -1
- package/dist/types/presets/all.codegen.d.ts +1 -1
- package/dist/types/rules/index.codegen.d.ts +1 -1
- package/dist/types/rules/utils/walk-style-call-properties.d.ts +0 -1
- package/package.json +1 -1
- package/dist/cjs/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +0 -232
- package/dist/cjs/rules/no-top-layer-unsafe-selectors/index.js +0 -425
- package/dist/cjs/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +0 -63
- package/dist/cjs/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +0 -2415
- package/dist/cjs/rules/utils/walk-style-object-properties.js +0 -47
- package/dist/es2019/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +0 -226
- package/dist/es2019/rules/no-top-layer-unsafe-selectors/index.js +0 -382
- package/dist/es2019/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +0 -57
- package/dist/es2019/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +0 -2131
- package/dist/es2019/rules/utils/walk-style-object-properties.js +0 -29
- package/dist/esm/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +0 -226
- package/dist/esm/rules/no-top-layer-unsafe-selectors/index.js +0 -419
- package/dist/esm/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +0 -57
- package/dist/esm/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +0 -2409
- package/dist/esm/rules/utils/walk-style-object-properties.js +0 -41
- package/dist/types/rules/no-top-layer-unsafe-selectors/find-template-selectors.d.ts +0 -29
- package/dist/types/rules/no-top-layer-unsafe-selectors/index.d.ts +0 -3
- package/dist/types/rules/no-top-layer-unsafe-selectors/preserve-list-layout.d.ts +0 -12
- package/dist/types/rules/no-top-layer-unsafe-selectors/rewrite-selector.d.ts +0 -164
- package/dist/types/rules/utils/walk-style-object-properties.d.ts +0 -18
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.walkStyleObjectProperties = walkStyleObjectProperties;
|
|
7
|
-
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; } } }; }
|
|
8
|
-
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; } }
|
|
9
|
-
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; }
|
|
10
|
-
/**
|
|
11
|
-
* Walk one style object's properties at every depth. With `skipTopLevel` the top-level
|
|
12
|
-
* properties are walked into but not visited, which is how a `cssMap` or `keyframes` argument
|
|
13
|
-
* hides its variant or block names. Nested objects are always visited in full. Use this directly
|
|
14
|
-
* for a style object that is not the argument of a style call, such as the value of a `css` prop.
|
|
15
|
-
*
|
|
16
|
-
* `skipSubtree` stops the descent into a property's value, so its children are never visited.
|
|
17
|
-
* A caller needs this when a nested block's keys are not the same kind of thing as its own key:
|
|
18
|
-
* the children of an `@keyframes` block are offsets (`from`, `50%`), not selectors, and a rule
|
|
19
|
-
* that treats them as selectors will report — or worse, fix — a keyframe offset. Declining to
|
|
20
|
-
* visit the at-rule property itself is not enough, because the walk into its value is what
|
|
21
|
-
* exposes the offsets. Omit it and the walk behaves exactly as before.
|
|
22
|
-
*/
|
|
23
|
-
function walkStyleObjectProperties(object, visitor, options) {
|
|
24
|
-
walk(object, visitor, Boolean(options === null || options === void 0 ? void 0 : options.skipTopLevel), options === null || options === void 0 ? void 0 : options.skipSubtree);
|
|
25
|
-
}
|
|
26
|
-
function walk(object, visitor, skipVisitor, skipSubtree) {
|
|
27
|
-
var _iterator = _createForOfIteratorHelper(object.properties),
|
|
28
|
-
_step;
|
|
29
|
-
try {
|
|
30
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
31
|
-
var property = _step.value;
|
|
32
|
-
if (property.type !== 'Property') {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
if (!skipVisitor) {
|
|
36
|
-
visitor(property);
|
|
37
|
-
}
|
|
38
|
-
if (property.value.type === 'ObjectExpression' && !(skipSubtree !== null && skipSubtree !== void 0 && skipSubtree(property))) {
|
|
39
|
-
walk(property.value, visitor, false, skipSubtree);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} catch (err) {
|
|
43
|
-
_iterator.e(err);
|
|
44
|
-
} finally {
|
|
45
|
-
_iterator.f();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
@@ -1,226 +0,0 @@
|
|
|
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
|
-
const found = [];
|
|
18
|
-
let 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
|
-
let 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
|
-
let parenthesisDepth = 0;
|
|
31
|
-
/**
|
|
32
|
-
* How many CSS `{` are open. `${…}` braces and parentheses do not count.
|
|
33
|
-
*/
|
|
34
|
-
let 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
|
-
let keyframesDepth = null;
|
|
42
|
-
const 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
|
-
const skipComment = (start, end) => {
|
|
53
|
-
const isLeading = source.slice(segmentStart, start).trim() === '';
|
|
54
|
-
if (isLeading) {
|
|
55
|
-
segmentStart = end;
|
|
56
|
-
} else {
|
|
57
|
-
commentInsideSegment = true;
|
|
58
|
-
}
|
|
59
|
-
return end;
|
|
60
|
-
};
|
|
61
|
-
for (let index = 0; index < source.length; index++) {
|
|
62
|
-
const 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
|
-
const close = source.indexOf('*/', index + 2);
|
|
71
|
-
const 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
|
-
const newline = source.indexOf('\n', index + 2);
|
|
83
|
-
const 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
|
-
const 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
|
-
const raw = source.slice(segmentStart, index);
|
|
113
|
-
const leading = raw.length - raw.trimStart().length;
|
|
114
|
-
const 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,
|
|
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
|
-
const 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
|
-
const quote = source[start];
|
|
159
|
-
let 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
|
-
let depth = 1;
|
|
184
|
-
let index = start + 1;
|
|
185
|
-
while (index < source.length && depth > 0) {
|
|
186
|
-
const 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
|
-
let 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
|
-
}
|
|
@@ -1,382 +0,0 @@
|
|
|
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
|
-
}
|