@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.
Files changed (39) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +1 -0
  3. package/dist/cjs/presets/all-flat.codegen.js +2 -1
  4. package/dist/cjs/presets/all.codegen.js +2 -1
  5. package/dist/cjs/rules/index.codegen.js +3 -1
  6. package/dist/cjs/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +232 -0
  7. package/dist/cjs/rules/no-top-layer-unsafe-selectors/index.js +425 -0
  8. package/dist/cjs/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +63 -0
  9. package/dist/cjs/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2415 -0
  10. package/dist/cjs/rules/utils/walk-style-call-properties.js +11 -24
  11. package/dist/cjs/rules/utils/walk-style-object-properties.js +47 -0
  12. package/dist/es2019/presets/all-flat.codegen.js +2 -1
  13. package/dist/es2019/presets/all.codegen.js +2 -1
  14. package/dist/es2019/rules/index.codegen.js +3 -1
  15. package/dist/es2019/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +226 -0
  16. package/dist/es2019/rules/no-top-layer-unsafe-selectors/index.js +382 -0
  17. package/dist/es2019/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +57 -0
  18. package/dist/es2019/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2131 -0
  19. package/dist/es2019/rules/utils/walk-style-call-properties.js +12 -15
  20. package/dist/es2019/rules/utils/walk-style-object-properties.js +29 -0
  21. package/dist/esm/presets/all-flat.codegen.js +2 -1
  22. package/dist/esm/presets/all.codegen.js +2 -1
  23. package/dist/esm/rules/index.codegen.js +3 -1
  24. package/dist/esm/rules/no-top-layer-unsafe-selectors/find-template-selectors.js +226 -0
  25. package/dist/esm/rules/no-top-layer-unsafe-selectors/index.js +419 -0
  26. package/dist/esm/rules/no-top-layer-unsafe-selectors/preserve-list-layout.js +57 -0
  27. package/dist/esm/rules/no-top-layer-unsafe-selectors/rewrite-selector.js +2409 -0
  28. package/dist/esm/rules/utils/walk-style-call-properties.js +11 -24
  29. package/dist/esm/rules/utils/walk-style-object-properties.js +41 -0
  30. package/dist/types/presets/all-flat.codegen.d.ts +1 -1
  31. package/dist/types/presets/all.codegen.d.ts +1 -1
  32. package/dist/types/rules/index.codegen.d.ts +1 -1
  33. package/dist/types/rules/no-top-layer-unsafe-selectors/find-template-selectors.d.ts +29 -0
  34. package/dist/types/rules/no-top-layer-unsafe-selectors/index.d.ts +3 -0
  35. package/dist/types/rules/no-top-layer-unsafe-selectors/preserve-list-layout.d.ts +12 -0
  36. package/dist/types/rules/no-top-layer-unsafe-selectors/rewrite-selector.d.ts +164 -0
  37. package/dist/types/rules/utils/walk-style-call-properties.d.ts +1 -0
  38. package/dist/types/rules/utils/walk-style-object-properties.d.ts +18 -0
  39. package/package.json +1 -1
@@ -4,20 +4,29 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.walkStyleCallProperties = walkStyleCallProperties;
7
+ var _walkStyleObjectProperties = require("./walk-style-object-properties");
7
8
  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
9
  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
10
  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
  function walkStyleCallProperties(styleCall, visitor, options) {
11
12
  var skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
13
+ var _ref = options !== null && options !== void 0 ? options : {},
14
+ skipSubtree = _ref.skipSubtree;
12
15
  var _iterator = _createForOfIteratorHelper(styleCall.node.arguments),
13
16
  _step;
14
17
  try {
15
18
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
16
19
  var argument = _step.value;
17
20
  if (argument.type === 'ObjectExpression') {
18
- walkStyleObject(argument, visitor, skipTopLevel);
21
+ (0, _walkStyleObjectProperties.walkStyleObjectProperties)(argument, visitor, {
22
+ skipTopLevel: skipTopLevel,
23
+ skipSubtree: skipSubtree
24
+ });
19
25
  } else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
20
- walkStyleObject(argument.body, visitor, skipTopLevel);
26
+ (0, _walkStyleObjectProperties.walkStyleObjectProperties)(argument.body, visitor, {
27
+ skipTopLevel: skipTopLevel,
28
+ skipSubtree: skipSubtree
29
+ });
21
30
  }
22
31
  }
23
32
  } catch (err) {
@@ -25,26 +34,4 @@ function walkStyleCallProperties(styleCall, visitor, options) {
25
34
  } finally {
26
35
  _iterator.f();
27
36
  }
28
- }
29
- function walkStyleObject(object, visitor, skipVisitor) {
30
- var _iterator2 = _createForOfIteratorHelper(object.properties),
31
- _step2;
32
- try {
33
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
34
- var property = _step2.value;
35
- if (property.type !== 'Property') {
36
- continue;
37
- }
38
- if (!skipVisitor) {
39
- visitor(property);
40
- }
41
- if (property.value.type === 'ObjectExpression') {
42
- walkStyleObject(property.value, visitor, false);
43
- }
44
- }
45
- } catch (err) {
46
- _iterator2.e(err);
47
- } finally {
48
- _iterator2.f();
49
- }
50
37
  }
@@ -0,0 +1,47 @@
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,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::7f3972a0198dc6de5194e21d5f32c3b0>>
3
+ * @codegen <<SignedSource::b13b370d5591cbe6a4d23e6afa0ee390>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -26,6 +26,7 @@ const config = {
26
26
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'error',
27
27
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'error',
28
28
  '@atlaskit/ui-styling-standard/no-styled': 'error',
29
+ '@atlaskit/ui-styling-standard/no-top-layer-unsafe-selectors': 'error',
29
30
  '@atlaskit/ui-styling-standard/no-unsafe-selectors': 'error',
30
31
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'error',
31
32
  '@atlaskit/ui-styling-standard/no-unused-cssmap-properties': 'warn',
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::138a044301c208ca5eecc78433523b0b>>
3
+ * @codegen <<SignedSource::cc3bb00111b60fbcdfa28799aa0671ea>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -22,6 +22,7 @@ const 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::094f8a058153c7e839c5d98d2718ca4e>>
3
+ * @codegen <<SignedSource::7b2b0662d0eeb0e3a3e4c844a505d27e>>
4
4
  * @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
 
@@ -19,6 +19,7 @@ import noImportantStyles from './no-important-styles';
19
19
  import noImportedStyleValues from './no-imported-style-values';
20
20
  import noNestedSelectors from './no-nested-selectors';
21
21
  import noStyled from './no-styled';
22
+ import noTopLayerUnsafeSelectors from './no-top-layer-unsafe-selectors';
22
23
  import noUnsafeSelectors from './no-unsafe-selectors';
23
24
  import noUnsafeValues from './no-unsafe-values';
24
25
  import noUnusedCssmapProperties from './no-unused-cssmap-properties';
@@ -39,6 +40,7 @@ export const 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
+ 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
+ }