@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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-ui-styling-standard
|
|
2
2
|
|
|
3
|
+
## 2.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`0418e1c376844`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/0418e1c376844) -
|
|
8
|
+
Add the `no-top-layer-unsafe-selectors` rule. It reports selectors that a Design System layering
|
|
9
|
+
surface rendered in the browser top layer can wrongly match or wrongly displace, and autofixes
|
|
10
|
+
most of them to a guarded form with the same specificity. Positional pseudo-classes such as
|
|
11
|
+
`:first-child` are reported without a fix, because no guard form for them survives the build.
|
|
12
|
+
|
|
13
|
+
The rule is not in the `recommended` preset, so `recommended` users see no change. It is in the
|
|
14
|
+
`all` and `all-flat` presets at `error`, so users of those presets get new errors.
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- Updated dependencies
|
|
19
|
+
|
|
20
|
+
## 2.2.0
|
|
21
|
+
|
|
22
|
+
### Minor Changes
|
|
23
|
+
|
|
24
|
+
- [`a5d06059c9ac7`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/a5d06059c9ac7) -
|
|
25
|
+
Add the no-atlaskit-theme lint rule and enable it as a warning in the recommended config. Imports
|
|
26
|
+
from @atlaskit/theme are flagged without failing lint.
|
|
27
|
+
|
|
3
28
|
## 2.1.10
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -47,6 +47,7 @@ module.exports = {
|
|
|
47
47
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/enforce-style-prop/README.md">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
|
|
48
48
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
|
|
49
49
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-array-arguments/README.md">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
|
|
50
|
+
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-atlaskit-theme/README.md">no-atlaskit-theme</a> | Disallow imports from `@atlaskit/theme` | Yes | | |
|
|
50
51
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
|
|
51
52
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
|
|
52
53
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
|
|
@@ -56,6 +57,7 @@ module.exports = {
|
|
|
56
57
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-imported-style-values/README.md">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
|
|
57
58
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
|
|
58
59
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-styled/README.md">no-styled</a> | Disallows usage of the `styled` imports | Yes | | |
|
|
60
|
+
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-top-layer-unsafe-selectors/README.md">no-top-layer-unsafe-selectors</a> | Disallows selectors that a Design System layering surface rendered in the browser top layer can wrongly match, or wrongly displace. | | Yes | |
|
|
59
61
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unsafe-selectors/README.md">no-unsafe-selectors</a> | Disallows use of nested styles in `css` functions. | Yes | Yes | Yes |
|
|
60
62
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unsafe-values/README.md">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
|
|
61
63
|
| <a href="./packages/design-system/eslint-plugin-ui-styling-standard/src/rules/no-unused-cssmap-properties/README.md">no-unused-cssmap-properties</a> | Detects unused properties in cssMap style objects that are not exported. Helps maintain clean code by identifying style variants that are defined but never used. | Yes | | |
|
|
@@ -9,7 +9,7 @@ var atlaskitDesignSystemPlugin = _interopRequireWildcard(require("@atlaskit/esli
|
|
|
9
9
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
10
10
|
/**
|
|
11
11
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
12
|
-
* @codegen <<SignedSource::
|
|
12
|
+
* @codegen <<SignedSource::b13b370d5591cbe6a4d23e6afa0ee390>>
|
|
13
13
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
14
14
|
*/
|
|
15
15
|
|
|
@@ -24,6 +24,7 @@ var config = {
|
|
|
24
24
|
'@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
|
|
25
25
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
|
|
26
26
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'error',
|
|
27
|
+
'@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
|
|
27
28
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'error',
|
|
28
29
|
'@atlaskit/ui-styling-standard/no-container-queries': 'error',
|
|
29
30
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
|
|
@@ -33,6 +34,7 @@ var config = {
|
|
|
33
34
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'error',
|
|
34
35
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'error',
|
|
35
36
|
'@atlaskit/ui-styling-standard/no-styled': 'error',
|
|
37
|
+
'@atlaskit/ui-styling-standard/no-top-layer-unsafe-selectors': 'error',
|
|
36
38
|
'@atlaskit/ui-styling-standard/no-unsafe-selectors': 'error',
|
|
37
39
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'error',
|
|
38
40
|
'@atlaskit/ui-styling-standard/no-unused-cssmap-properties': 'warn',
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
9
|
-
* @codegen <<SignedSource::
|
|
9
|
+
* @codegen <<SignedSource::cc3bb00111b60fbcdfa28799aa0671ea>>
|
|
10
10
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
11
11
|
*/
|
|
12
12
|
|
|
@@ -18,6 +18,7 @@ var config = {
|
|
|
18
18
|
'@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
|
|
19
19
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
|
|
20
20
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'error',
|
|
21
|
+
'@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'error',
|
|
22
23
|
'@atlaskit/ui-styling-standard/no-container-queries': 'error',
|
|
23
24
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
|
|
@@ -27,6 +28,7 @@ var config = {
|
|
|
27
28
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'error',
|
|
28
29
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'error',
|
|
29
30
|
'@atlaskit/ui-styling-standard/no-styled': 'error',
|
|
31
|
+
'@atlaskit/ui-styling-standard/no-top-layer-unsafe-selectors': 'error',
|
|
30
32
|
'@atlaskit/ui-styling-standard/no-unsafe-selectors': 'error',
|
|
31
33
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'error',
|
|
32
34
|
'@atlaskit/ui-styling-standard/no-unused-cssmap-properties': 'warn',
|
|
@@ -9,7 +9,7 @@ var atlaskitDesignSystemPlugin = _interopRequireWildcard(require("@atlaskit/esli
|
|
|
9
9
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
10
10
|
/**
|
|
11
11
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
12
|
-
* @codegen <<SignedSource::
|
|
12
|
+
* @codegen <<SignedSource::7f3972a0198dc6de5194e21d5f32c3b0>>
|
|
13
13
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
14
14
|
*/
|
|
15
15
|
|
|
@@ -24,6 +24,7 @@ var config = {
|
|
|
24
24
|
'@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
|
|
25
25
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
|
|
26
26
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'error',
|
|
27
|
+
'@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
|
|
27
28
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'error',
|
|
28
29
|
'@atlaskit/ui-styling-standard/no-container-queries': 'error',
|
|
29
30
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
9
|
-
* @codegen <<SignedSource::
|
|
9
|
+
* @codegen <<SignedSource::138a044301c208ca5eecc78433523b0b>>
|
|
10
10
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
11
11
|
*/
|
|
12
12
|
|
|
@@ -18,6 +18,7 @@ var config = {
|
|
|
18
18
|
'@atlaskit/ui-styling-standard/enforce-style-prop': 'error',
|
|
19
19
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'error',
|
|
20
20
|
'@atlaskit/ui-styling-standard/no-array-arguments': 'error',
|
|
21
|
+
'@atlaskit/ui-styling-standard/no-atlaskit-theme': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'error',
|
|
22
23
|
'@atlaskit/ui-styling-standard/no-container-queries': 'error',
|
|
23
24
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'error',
|
|
@@ -10,6 +10,7 @@ var _convertPropsSyntax = _interopRequireDefault(require("./convert-props-syntax
|
|
|
10
10
|
var _enforceStyleProp = _interopRequireDefault(require("./enforce-style-prop"));
|
|
11
11
|
var _localCxXcss = _interopRequireDefault(require("./local-cx-xcss"));
|
|
12
12
|
var _noArrayArguments = _interopRequireDefault(require("./no-array-arguments"));
|
|
13
|
+
var _noAtlaskitTheme = _interopRequireDefault(require("./no-atlaskit-theme"));
|
|
13
14
|
var _noClassnameProp = _interopRequireDefault(require("./no-classname-prop"));
|
|
14
15
|
var _noContainerQueries = _interopRequireDefault(require("./no-container-queries"));
|
|
15
16
|
var _noDynamicStyles = _interopRequireDefault(require("./no-dynamic-styles"));
|
|
@@ -19,13 +20,14 @@ var _noImportantStyles = _interopRequireDefault(require("./no-important-styles")
|
|
|
19
20
|
var _noImportedStyleValues = _interopRequireDefault(require("./no-imported-style-values"));
|
|
20
21
|
var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
|
|
21
22
|
var _noStyled = _interopRequireDefault(require("./no-styled"));
|
|
23
|
+
var _noTopLayerUnsafeSelectors = _interopRequireDefault(require("./no-top-layer-unsafe-selectors"));
|
|
22
24
|
var _noUnsafeSelectors = _interopRequireDefault(require("./no-unsafe-selectors"));
|
|
23
25
|
var _noUnsafeValues = _interopRequireDefault(require("./no-unsafe-values"));
|
|
24
26
|
var _noUnusedCssmapProperties = _interopRequireDefault(require("./no-unused-cssmap-properties"));
|
|
25
27
|
var _useCompiled = _interopRequireDefault(require("./use-compiled"));
|
|
26
28
|
/**
|
|
27
29
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
28
|
-
* @codegen <<SignedSource::
|
|
30
|
+
* @codegen <<SignedSource::7b2b0662d0eeb0e3a3e4c844a505d27e>>
|
|
29
31
|
* @codegenCommand afm workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
30
32
|
*/
|
|
31
33
|
|
|
@@ -35,6 +37,7 @@ var rules = exports.rules = {
|
|
|
35
37
|
'enforce-style-prop': _enforceStyleProp.default,
|
|
36
38
|
'local-cx-xcss': _localCxXcss.default,
|
|
37
39
|
'no-array-arguments': _noArrayArguments.default,
|
|
40
|
+
'no-atlaskit-theme': _noAtlaskitTheme.default,
|
|
38
41
|
'no-classname-prop': _noClassnameProp.default,
|
|
39
42
|
'no-container-queries': _noContainerQueries.default,
|
|
40
43
|
'no-dynamic-styles': _noDynamicStyles.default,
|
|
@@ -44,6 +47,7 @@ var rules = exports.rules = {
|
|
|
44
47
|
'no-imported-style-values': _noImportedStyleValues.default,
|
|
45
48
|
'no-nested-selectors': _noNestedSelectors.default,
|
|
46
49
|
'no-styled': _noStyled.default,
|
|
50
|
+
'no-top-layer-unsafe-selectors': _noTopLayerUnsafeSelectors.default,
|
|
47
51
|
'no-unsafe-selectors': _noUnsafeSelectors.default,
|
|
48
52
|
'no-unsafe-values': _noUnsafeValues.default,
|
|
49
53
|
'no-unused-cssmap-properties': _noUnusedCssmapProperties.default,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _createRule = require("../utils/create-rule");
|
|
8
|
+
var rule = (0, _createRule.createLintRule)({
|
|
9
|
+
meta: {
|
|
10
|
+
name: 'no-atlaskit-theme',
|
|
11
|
+
docs: {
|
|
12
|
+
description: 'Disallow imports from `@atlaskit/theme`',
|
|
13
|
+
recommended: true,
|
|
14
|
+
severity: 'warn'
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
'no-atlaskit-theme': 'Do not import `@atlaskit/theme`. Use `@atlaskit/tokens` or Atlassian Design System components instead.'
|
|
18
|
+
},
|
|
19
|
+
type: 'problem'
|
|
20
|
+
},
|
|
21
|
+
create: function create(context) {
|
|
22
|
+
return {
|
|
23
|
+
// esquery regexes cannot contain a raw `/`, so the path separator is `\x2F`.
|
|
24
|
+
// Unanchored at the end, so this matches `@atlaskit/theme` and any subpath.
|
|
25
|
+
'ImportDeclaration[source.value=/^@atlaskit\\x2Ftheme(\\x2F|$)/]': function ImportDeclarationSourceValue_AtlaskitX2FthemeX2F$_(node) {
|
|
26
|
+
context.report({
|
|
27
|
+
node: node,
|
|
28
|
+
messageId: 'no-atlaskit-theme'
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
var _default = exports.default = rule;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findTemplateSelectors = findTemplateSelectors;
|
|
7
|
+
/**
|
|
8
|
+
* Locate the selectors inside a tagged template literal's raw source text.
|
|
9
|
+
*
|
|
10
|
+
* The style-call index only sees call expressions, so `styled.div\`…\`` and `css\`…\`` are
|
|
11
|
+
* invisible to it. This scanner fills that gap: it walks the text between the template's
|
|
12
|
+
* backticks and returns the byte range of every selector, so ranges map 1:1 onto the file and
|
|
13
|
+
* can be handed straight to an ESLint fixer.
|
|
14
|
+
*
|
|
15
|
+
* It deliberately does not try to be a CSS parser. A selector is the text between the last
|
|
16
|
+
* `;`, `{` or `}` and the next `{`. Anything it mis-reads will either fail to parse as a
|
|
17
|
+
* selector — in which case `rewriteSelector` skips it — or be reported for review. What it
|
|
18
|
+
* must not do is mistake a `${…}` interpolation, a quoted string or a comment for CSS
|
|
19
|
+
* structure, so it steps over all three.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
function findTemplateSelectors(source, offset) {
|
|
23
|
+
var found = [];
|
|
24
|
+
var segmentStart = 0;
|
|
25
|
+
/**
|
|
26
|
+
* Set when a comment appears after the segment's first non-whitespace character. A selector
|
|
27
|
+
* with a comment inside it — `& /* x */ > div` — cannot be replaced as one contiguous range
|
|
28
|
+
* without eating the comment, so it is skipped rather than mangled. A comment *before* the
|
|
29
|
+
* selector just moves `segmentStart` past it.
|
|
30
|
+
*/
|
|
31
|
+
var commentInsideSegment = false;
|
|
32
|
+
/**
|
|
33
|
+
* How many `(` are open. Inside a function's parentheses `//` is data, not a comment:
|
|
34
|
+
* `url(//cdn.example.com/a.png)` and an unquoted data URI both carry one.
|
|
35
|
+
*/
|
|
36
|
+
var parenthesisDepth = 0;
|
|
37
|
+
/**
|
|
38
|
+
* How many CSS `{` are open. `${…}` braces and parentheses do not count.
|
|
39
|
+
*/
|
|
40
|
+
var braceDepth = 0;
|
|
41
|
+
/**
|
|
42
|
+
* The brace depth at which a `@keyframes` block opened, or `null` outside one. Everything
|
|
43
|
+
* nested inside is an offset (`from`, `50%`), not a selector — each parses as a tag selector,
|
|
44
|
+
* so without this the guard is written onto a keyframe and the animation stops. Skipping the
|
|
45
|
+
* at-rule's own prelude is not enough, because the block's contents open their own segments.
|
|
46
|
+
*/
|
|
47
|
+
var keyframesDepth = null;
|
|
48
|
+
var startSegment = function startSegment(at) {
|
|
49
|
+
segmentStart = at;
|
|
50
|
+
commentInsideSegment = false;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Step over a comment spanning `[start, end)`. A comment before the segment's first
|
|
55
|
+
* non-whitespace character just moves the segment past it; a comment inside the segment
|
|
56
|
+
* marks it unfixable. Returns the index to resume scanning at.
|
|
57
|
+
*/
|
|
58
|
+
var skipComment = function skipComment(start, end) {
|
|
59
|
+
var isLeading = source.slice(segmentStart, start).trim() === '';
|
|
60
|
+
if (isLeading) {
|
|
61
|
+
segmentStart = end;
|
|
62
|
+
} else {
|
|
63
|
+
commentInsideSegment = true;
|
|
64
|
+
}
|
|
65
|
+
return end;
|
|
66
|
+
};
|
|
67
|
+
for (var index = 0; index < source.length; index++) {
|
|
68
|
+
var character = source[index];
|
|
69
|
+
|
|
70
|
+
/* Step over a quoted string — `content: "{"` must not open a block. */
|
|
71
|
+
if (character === '"' || character === "'") {
|
|
72
|
+
index = skipQuoted(source, index);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (character === '/' && source[index + 1] === '*') {
|
|
76
|
+
var close = source.indexOf('*/', index + 2);
|
|
77
|
+
var end = close === -1 ? source.length : close + 2;
|
|
78
|
+
index = skipComment(index, end) - 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A `//` line comment. Emotion and styled-components strip these before the CSS is
|
|
84
|
+
* parsed, so a selector written after one on the next line is real. Inside parentheses
|
|
85
|
+
* the same two characters are part of a URL, so the depth check keeps them.
|
|
86
|
+
*/
|
|
87
|
+
if (character === '/' && source[index + 1] === '/' && parenthesisDepth === 0) {
|
|
88
|
+
var newline = source.indexOf('\n', index + 2);
|
|
89
|
+
var _end = newline === -1 ? source.length : newline + 1;
|
|
90
|
+
index = skipComment(index, _end) - 1;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (character === '(') {
|
|
94
|
+
parenthesisDepth++;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (character === ')') {
|
|
98
|
+
parenthesisDepth = Math.max(0, parenthesisDepth - 1);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* Step over a `${…}` interpolation. Its braces are not CSS braces, but the `${…}` text
|
|
103
|
+
* itself stays in the segment so the transform can recognise the selector as dynamic. */
|
|
104
|
+
if (character === '{' && index > 0 && source[index - 1] === '$') {
|
|
105
|
+
var isLeading = source.slice(segmentStart, index - 1).trim() === '';
|
|
106
|
+
index = skipInterpolation(source, index);
|
|
107
|
+
/**
|
|
108
|
+
* A `${mixin}` alone on its line, with no `;` after it, is a statement of its own:
|
|
109
|
+
* the selector on the next line starts a fresh segment rather than inheriting the
|
|
110
|
+
* interpolation and being read as dynamic.
|
|
111
|
+
*/
|
|
112
|
+
if (isLeading && isRestOfLineBlank(source, index + 1)) {
|
|
113
|
+
startSegment(index + 1);
|
|
114
|
+
}
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (character === '{') {
|
|
118
|
+
var raw = source.slice(segmentStart, index);
|
|
119
|
+
var leading = raw.length - raw.trimStart().length;
|
|
120
|
+
var text = raw.trim();
|
|
121
|
+
|
|
122
|
+
/* At-rules are blocks but not selectors; the transform has nothing to say about them. */
|
|
123
|
+
if (text !== '' && !text.startsWith('@') && !commentInsideSegment && keyframesDepth === null) {
|
|
124
|
+
found.push({
|
|
125
|
+
text: text,
|
|
126
|
+
start: offset + segmentStart + leading,
|
|
127
|
+
end: offset + segmentStart + leading + text.length
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (keyframesDepth === null && /^@(-[a-z]+-)?keyframes\b/i.test(text)) {
|
|
131
|
+
keyframesDepth = braceDepth;
|
|
132
|
+
}
|
|
133
|
+
braceDepth++;
|
|
134
|
+
startSegment(index + 1);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (character === '}') {
|
|
138
|
+
braceDepth = Math.max(0, braceDepth - 1);
|
|
139
|
+
if (keyframesDepth !== null && braceDepth <= keyframesDepth) {
|
|
140
|
+
keyframesDepth = null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (character === '}' || character === ';') {
|
|
144
|
+
startSegment(index + 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return found;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Is everything from `start` to the next newline whitespace? A missing newline does not count:
|
|
152
|
+
* `${selector}` at the very end has nothing after it to separate from.
|
|
153
|
+
*/
|
|
154
|
+
function isRestOfLineBlank(source, start) {
|
|
155
|
+
var newline = source.indexOf('\n', start);
|
|
156
|
+
return newline !== -1 && source.slice(start, newline).trim() === '';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Index of the quote closing the string that opens at `start`, or the end of the source when
|
|
161
|
+
* the string is unterminated.
|
|
162
|
+
*/
|
|
163
|
+
function skipQuoted(source, start) {
|
|
164
|
+
var quote = source[start];
|
|
165
|
+
var index = start + 1;
|
|
166
|
+
while (index < source.length) {
|
|
167
|
+
if (source[index] === '\\') {
|
|
168
|
+
index += 2;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (source[index] === quote) {
|
|
172
|
+
return index;
|
|
173
|
+
}
|
|
174
|
+
index++;
|
|
175
|
+
}
|
|
176
|
+
return source.length;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Index of the `}` closing the interpolation whose `{` is at `start`, or the end of the source
|
|
181
|
+
* when it is unterminated.
|
|
182
|
+
*
|
|
183
|
+
* The interpolation holds JavaScript, so a brace inside one of its string literals is not a
|
|
184
|
+
* brace: `${cond ? '}' : ''}` closes at the last character, not at the quoted one. Quoted
|
|
185
|
+
* strings and template literals are stepped over; a template literal may in turn hold its own
|
|
186
|
+
* interpolations, so the two skips call each other.
|
|
187
|
+
*/
|
|
188
|
+
function skipInterpolation(source, start) {
|
|
189
|
+
var depth = 1;
|
|
190
|
+
var index = start + 1;
|
|
191
|
+
while (index < source.length && depth > 0) {
|
|
192
|
+
var character = source[index];
|
|
193
|
+
if (character === '"' || character === "'") {
|
|
194
|
+
index = skipQuoted(source, index) + 1;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (character === '`') {
|
|
198
|
+
index = skipTemplateLiteral(source, index) + 1;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (character === '{') {
|
|
202
|
+
depth++;
|
|
203
|
+
} else if (character === '}') {
|
|
204
|
+
depth--;
|
|
205
|
+
}
|
|
206
|
+
index++;
|
|
207
|
+
}
|
|
208
|
+
return index - 1;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Index of the backtick closing the template literal that opens at `start`, or the end of the
|
|
213
|
+
* source when it is unterminated.
|
|
214
|
+
*/
|
|
215
|
+
function skipTemplateLiteral(source, start) {
|
|
216
|
+
var index = start + 1;
|
|
217
|
+
while (index < source.length) {
|
|
218
|
+
if (source[index] === '\\') {
|
|
219
|
+
index += 2;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (source[index] === '`') {
|
|
223
|
+
return index;
|
|
224
|
+
}
|
|
225
|
+
if (source[index] === '$' && source[index + 1] === '{') {
|
|
226
|
+
index = skipInterpolation(source, index + 1) + 1;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
index++;
|
|
230
|
+
}
|
|
231
|
+
return source.length;
|
|
232
|
+
}
|