@atlaskit/eslint-plugin-ui-styling-standard 2.2.0 → 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 +17 -0
- package/README.md +1 -0
- package/dist/cjs/presets/all-flat.codegen.js +2 -1
- package/dist/cjs/presets/all.codegen.js +2 -1
- package/dist/cjs/rules/index.codegen.js +3 -1
- 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 +2 -1
- package/dist/es2019/presets/all.codegen.js +2 -1
- package/dist/es2019/rules/index.codegen.js +3 -1
- 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 +2 -1
- package/dist/esm/presets/all.codegen.js +2 -1
- package/dist/esm/rules/index.codegen.js +3 -1
- 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/rules/index.codegen.d.ts +1 -1
- 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
|
@@ -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
|
-
|
|
9
|
+
walkStyleObjectProperties(argument, visitor, {
|
|
10
|
+
skipTopLevel,
|
|
11
|
+
skipSubtree
|
|
12
|
+
});
|
|
6
13
|
} else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
|
|
7
|
-
|
|
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::
|
|
3
|
+
* @codegen <<SignedSource::b13b370d5591cbe6a4d23e6afa0ee390>>
|
|
4
4
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::cc3bb00111b60fbcdfa28799aa0671ea>>
|
|
4
4
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -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::
|
|
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,
|
|
@@ -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
|
+
}
|