@atlaskit/eslint-plugin-ui-styling-standard 0.15.1 → 0.15.2

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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.15.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#105238](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/105238)
8
+ [`8840e6d3180d`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8840e6d3180d) -
9
+ Fixes `no-nested-selectors` to handle additional APIs (such as `cssMap` and `keyframes`) as well
10
+ as catch more cases of nested selectors.
11
+
3
12
  ## 0.15.1
4
13
 
5
14
  ### Patch Changes
@@ -1,38 +1,46 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports.rule = exports.default = void 0;
7
- var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
8
- var _createRule = require("../utils/create-rule");
9
- var schema = [{
10
- type: 'object',
11
- properties: {
12
- importSources: {
13
- type: 'array',
14
- items: {
15
- type: 'string'
16
- },
17
- uniqueItems: true
18
- }
19
- }
20
- }];
21
- var getCssSelector = function getCssSelector(node) {
22
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
23
- return node.key.value;
9
+ var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
10
+ var _postcssSelectorParser = _interopRequireWildcard(require("postcss-selector-parser"));
11
+ var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
12
+ var _schema = require("@atlaskit/eslint-utils/schema");
13
+ var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
14
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
15
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
16
+ var getCssSelector = function getCssSelector(key) {
17
+ if (key.type === 'Literal' && typeof key.value === 'string') {
18
+ return key.value;
24
19
  }
25
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
26
- return node.key.name;
20
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
21
+ return key.name;
27
22
  }
28
- if (node.key.type === 'TemplateLiteral') {
29
- return node.key.quasis.map(function (quasi) {
23
+ if (key.type === 'TemplateLiteral') {
24
+ return key.quasis.map(function (quasi) {
30
25
  return quasi.value.raw;
31
26
  }).join(' ');
32
27
  }
33
28
  return null;
34
29
  };
35
- var rule = exports.rule = (0, _createRule.createLintRule)({
30
+ function isAllowedSelector(selector) {
31
+ var _selector$nodes = (0, _toArray2.default)(selector.nodes),
32
+ head = _selector$nodes[0],
33
+ tail = _selector$nodes.slice(1);
34
+
35
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
36
+ var isHeadAllowed = (0, _postcssSelectorParser.isNesting)(head) || (0, _postcssSelectorParser.isPseudo)(head);
37
+
38
+ // All remaining nodes must be pseudo selectors.
39
+ var isTailAllowed = tail.every(_postcssSelectorParser.isPseudo);
40
+ return isHeadAllowed && isTailAllowed;
41
+ }
42
+ var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
43
+ var rule = exports.rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
36
44
  meta: {
37
45
  name: 'no-nested-selectors',
38
46
  docs: {
@@ -44,37 +52,48 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
44
52
  'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
45
53
  },
46
54
  type: 'problem',
47
- schema: schema
55
+ schema: {
56
+ type: 'object',
57
+ properties: {
58
+ importSources: _schema.importSources
59
+ }
60
+ }
48
61
  },
49
- create: function create(context) {
50
- var importSources = (0, _isSupportedImport.getImportSources)(context);
62
+ create: function create(context, _ref) {
63
+ var importSources = _ref.importSources;
51
64
  return {
52
- 'CallExpression Property': function CallExpressionProperty(node) {
65
+ CallExpression: function CallExpression(node) {
53
66
  var _context$getScope = context.getScope(),
54
67
  references = _context$getScope.references;
55
- var ancestors = context.getAncestors();
56
- if (ancestors.every(function (ancestor) {
57
- return ancestor.type !== 'CallExpression' || !(ancestor !== null && ancestor !== void 0 && ancestor.callee) || !((0, _isSupportedImport.isCss)(ancestor.callee, references, importSources) || (0, _isSupportedImport.isStyled)(ancestor.callee, references, importSources) || (0, _isSupportedImport.isXcss)(ancestor.callee, references, importSources));
58
- })) {
59
- return;
60
- }
61
- if (node.value.type !== 'ObjectExpression') {
62
- // If the value is a CSS object, safe to assume we're at a CSS selector
63
- return;
64
- }
65
- var cssSelector = getCssSelector(node);
66
- if (cssSelector === null) {
67
- return;
68
- }
69
- var tokens = cssSelector.split(/[, ]+/);
70
- if (!cssSelector.includes('@') && !tokens.every(function (token) {
71
- return token.length === 0 || token.startsWith('&') || token.startsWith(':') || token.includes('+') || token.includes('~') || token.endsWith('&');
72
- })) {
73
- context.report({
74
- messageId: 'no-nested-selectors',
75
- node: node.key
76
- });
77
- }
68
+ (0, _walkStyleProperties.walkStyleProperties)(node, references, importSources, function (_ref2) {
69
+ var key = _ref2.key,
70
+ value = _ref2.value;
71
+ if (value.type !== 'ObjectExpression') {
72
+ // If the value is a CSS object, safe to assume we're at a CSS selector
73
+ return;
74
+ }
75
+ var selectorText = getCssSelector(key);
76
+ if (selectorText === null) {
77
+ return;
78
+ }
79
+
80
+ // Ignore at-rules
81
+ if (selectorText.includes('@')) {
82
+ return;
83
+ }
84
+ try {
85
+ var selectorList = cssSelectorProcessor.astSync(selectorText);
86
+ if (!selectorList.nodes.every(isAllowedSelector)) {
87
+ context.report({
88
+ messageId: 'no-nested-selectors',
89
+ node: key
90
+ });
91
+ }
92
+ } catch (_) {
93
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
94
+ // we can ignore it here.
95
+ }
96
+ });
78
97
  }
79
98
  };
80
99
  }
@@ -1,30 +1,31 @@
1
- import { isStyled, isCss, isXcss, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
2
- import { createLintRule } from '../utils/create-rule';
3
- const schema = [{
4
- type: 'object',
5
- properties: {
6
- importSources: {
7
- type: 'array',
8
- items: {
9
- type: 'string'
10
- },
11
- uniqueItems: true
12
- }
13
- }
14
- }];
15
- const getCssSelector = node => {
16
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
17
- return node.key.value;
1
+ import cssSelectorParser, { isNesting, isPseudo } from 'postcss-selector-parser';
2
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
3
+ import { importSources } from '@atlaskit/eslint-utils/schema';
4
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
5
+ const getCssSelector = key => {
6
+ if (key.type === 'Literal' && typeof key.value === 'string') {
7
+ return key.value;
18
8
  }
19
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
20
- return node.key.name;
9
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
10
+ return key.name;
21
11
  }
22
- if (node.key.type === 'TemplateLiteral') {
23
- return node.key.quasis.map(quasi => quasi.value.raw).join(' ');
12
+ if (key.type === 'TemplateLiteral') {
13
+ return key.quasis.map(quasi => quasi.value.raw).join(' ');
24
14
  }
25
15
  return null;
26
16
  };
27
- export const rule = createLintRule({
17
+ function isAllowedSelector(selector) {
18
+ const [head, ...tail] = selector.nodes;
19
+
20
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
21
+ const isHeadAllowed = isNesting(head) || isPseudo(head);
22
+
23
+ // All remaining nodes must be pseudo selectors.
24
+ const isTailAllowed = tail.every(isPseudo);
25
+ return isHeadAllowed && isTailAllowed;
26
+ }
27
+ const cssSelectorProcessor = cssSelectorParser();
28
+ export const rule = createLintRuleWithTypedConfig({
28
29
  meta: {
29
30
  name: 'no-nested-selectors',
30
31
  docs: {
@@ -36,34 +37,51 @@ export const rule = createLintRule({
36
37
  'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
37
38
  },
38
39
  type: 'problem',
39
- schema
40
+ schema: {
41
+ type: 'object',
42
+ properties: {
43
+ importSources
44
+ }
45
+ }
40
46
  },
41
- create(context) {
42
- const importSources = getImportSources(context);
47
+ create(context, {
48
+ importSources
49
+ }) {
43
50
  return {
44
- 'CallExpression Property': node => {
51
+ CallExpression(node) {
45
52
  const {
46
53
  references
47
54
  } = context.getScope();
48
- const ancestors = context.getAncestors();
49
- if (ancestors.every(ancestor => ancestor.type !== 'CallExpression' || !(ancestor !== null && ancestor !== void 0 && ancestor.callee) || !(isCss(ancestor.callee, references, importSources) || isStyled(ancestor.callee, references, importSources) || isXcss(ancestor.callee, references, importSources)))) {
50
- return;
51
- }
52
- if (node.value.type !== 'ObjectExpression') {
53
- // If the value is a CSS object, safe to assume we're at a CSS selector
54
- return;
55
- }
56
- const cssSelector = getCssSelector(node);
57
- if (cssSelector === null) {
58
- return;
59
- }
60
- const tokens = cssSelector.split(/[, ]+/);
61
- if (!cssSelector.includes('@') && !tokens.every(token => token.length === 0 || token.startsWith('&') || token.startsWith(':') || token.includes('+') || token.includes('~') || token.endsWith('&'))) {
62
- context.report({
63
- messageId: 'no-nested-selectors',
64
- node: node.key
65
- });
66
- }
55
+ walkStyleProperties(node, references, importSources, ({
56
+ key,
57
+ value
58
+ }) => {
59
+ if (value.type !== 'ObjectExpression') {
60
+ // If the value is a CSS object, safe to assume we're at a CSS selector
61
+ return;
62
+ }
63
+ const selectorText = getCssSelector(key);
64
+ if (selectorText === null) {
65
+ return;
66
+ }
67
+
68
+ // Ignore at-rules
69
+ if (selectorText.includes('@')) {
70
+ return;
71
+ }
72
+ try {
73
+ const selectorList = cssSelectorProcessor.astSync(selectorText);
74
+ if (!selectorList.nodes.every(isAllowedSelector)) {
75
+ context.report({
76
+ messageId: 'no-nested-selectors',
77
+ node: key
78
+ });
79
+ }
80
+ } catch (_) {
81
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
82
+ // we can ignore it here.
83
+ }
84
+ });
67
85
  }
68
86
  };
69
87
  }
@@ -1,32 +1,36 @@
1
- import { isStyled, isCss, isXcss, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
2
- import { createLintRule } from '../utils/create-rule';
3
- var schema = [{
4
- type: 'object',
5
- properties: {
6
- importSources: {
7
- type: 'array',
8
- items: {
9
- type: 'string'
10
- },
11
- uniqueItems: true
12
- }
13
- }
14
- }];
15
- var getCssSelector = function getCssSelector(node) {
16
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
17
- return node.key.value;
1
+ import _toArray from "@babel/runtime/helpers/toArray";
2
+ import cssSelectorParser, { isNesting, isPseudo } from 'postcss-selector-parser';
3
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
4
+ import { importSources } from '@atlaskit/eslint-utils/schema';
5
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
6
+ var getCssSelector = function getCssSelector(key) {
7
+ if (key.type === 'Literal' && typeof key.value === 'string') {
8
+ return key.value;
18
9
  }
19
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
20
- return node.key.name;
10
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
11
+ return key.name;
21
12
  }
22
- if (node.key.type === 'TemplateLiteral') {
23
- return node.key.quasis.map(function (quasi) {
13
+ if (key.type === 'TemplateLiteral') {
14
+ return key.quasis.map(function (quasi) {
24
15
  return quasi.value.raw;
25
16
  }).join(' ');
26
17
  }
27
18
  return null;
28
19
  };
29
- export var rule = createLintRule({
20
+ function isAllowedSelector(selector) {
21
+ var _selector$nodes = _toArray(selector.nodes),
22
+ head = _selector$nodes[0],
23
+ tail = _selector$nodes.slice(1);
24
+
25
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
26
+ var isHeadAllowed = isNesting(head) || isPseudo(head);
27
+
28
+ // All remaining nodes must be pseudo selectors.
29
+ var isTailAllowed = tail.every(isPseudo);
30
+ return isHeadAllowed && isTailAllowed;
31
+ }
32
+ var cssSelectorProcessor = cssSelectorParser();
33
+ export var rule = createLintRuleWithTypedConfig({
30
34
  meta: {
31
35
  name: 'no-nested-selectors',
32
36
  docs: {
@@ -38,37 +42,48 @@ export var rule = createLintRule({
38
42
  'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
39
43
  },
40
44
  type: 'problem',
41
- schema: schema
45
+ schema: {
46
+ type: 'object',
47
+ properties: {
48
+ importSources: importSources
49
+ }
50
+ }
42
51
  },
43
- create: function create(context) {
44
- var importSources = getImportSources(context);
52
+ create: function create(context, _ref) {
53
+ var importSources = _ref.importSources;
45
54
  return {
46
- 'CallExpression Property': function CallExpressionProperty(node) {
55
+ CallExpression: function CallExpression(node) {
47
56
  var _context$getScope = context.getScope(),
48
57
  references = _context$getScope.references;
49
- var ancestors = context.getAncestors();
50
- if (ancestors.every(function (ancestor) {
51
- return ancestor.type !== 'CallExpression' || !(ancestor !== null && ancestor !== void 0 && ancestor.callee) || !(isCss(ancestor.callee, references, importSources) || isStyled(ancestor.callee, references, importSources) || isXcss(ancestor.callee, references, importSources));
52
- })) {
53
- return;
54
- }
55
- if (node.value.type !== 'ObjectExpression') {
56
- // If the value is a CSS object, safe to assume we're at a CSS selector
57
- return;
58
- }
59
- var cssSelector = getCssSelector(node);
60
- if (cssSelector === null) {
61
- return;
62
- }
63
- var tokens = cssSelector.split(/[, ]+/);
64
- if (!cssSelector.includes('@') && !tokens.every(function (token) {
65
- return token.length === 0 || token.startsWith('&') || token.startsWith(':') || token.includes('+') || token.includes('~') || token.endsWith('&');
66
- })) {
67
- context.report({
68
- messageId: 'no-nested-selectors',
69
- node: node.key
70
- });
71
- }
58
+ walkStyleProperties(node, references, importSources, function (_ref2) {
59
+ var key = _ref2.key,
60
+ value = _ref2.value;
61
+ if (value.type !== 'ObjectExpression') {
62
+ // If the value is a CSS object, safe to assume we're at a CSS selector
63
+ return;
64
+ }
65
+ var selectorText = getCssSelector(key);
66
+ if (selectorText === null) {
67
+ return;
68
+ }
69
+
70
+ // Ignore at-rules
71
+ if (selectorText.includes('@')) {
72
+ return;
73
+ }
74
+ try {
75
+ var selectorList = cssSelectorProcessor.astSync(selectorText);
76
+ if (!selectorList.nodes.every(isAllowedSelector)) {
77
+ context.report({
78
+ messageId: 'no-nested-selectors',
79
+ node: key
80
+ });
81
+ }
82
+ } catch (_) {
83
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
84
+ // we can ignore it here.
85
+ }
86
+ });
72
87
  }
73
88
  };
74
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.15.1",
3
+ "version": "0.15.2",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
6
6
  "license": "Apache-2.0",
@@ -72,28 +72,6 @@ typescriptEslintTester.run(
72
72
  });
73
73
  `,
74
74
  },
75
- {
76
- name: 'Next sibling selector on non-HTML element',
77
- code: `
78
- import { styled } from '@compiled/react';
79
- styled.div({
80
- '& + &': {
81
- color: 'orange'
82
- }
83
- });
84
- `,
85
- },
86
- {
87
- name: 'Subsequent sibling selector on non-HTML element',
88
- code: `
89
- import { styled } from '@compiled/react';
90
- styled.div({
91
- '& ~ &': {
92
- color: 'orange'
93
- }
94
- });
95
- `,
96
- },
97
75
  {
98
76
  name: 'Skips @ queries',
99
77
  code: `
@@ -109,40 +87,6 @@ typescriptEslintTester.run(
109
87
  });
110
88
  `,
111
89
  },
112
- {
113
- name: 'Skip cssMap call',
114
- code: `
115
- import { cssMap } from '@atlaskit/css';
116
-
117
- const styles = cssMap({
118
- success: {
119
- color: 'var(--ds-text-inverse)',
120
- backgroundColor: 'var(--ds-background-success-bold)',
121
- '&:hover': {
122
- backgroundColor: 'var(--ds-background-success-bold-hovered)',
123
- },
124
- '&:active': {
125
- backgroundColor: 'var(--ds-background-success-bold-pressed)',
126
- },
127
- },
128
- });
129
- `,
130
- },
131
- {
132
- name: 'Skip keyframes call',
133
- code: `
134
- import { keyframes } from '@emotion/react';
135
-
136
- const keyFrameStyles = keyframes({
137
- 'from, to': {
138
- opacity: 0,
139
- },
140
- '50%': {
141
- opacity: 1,
142
- },
143
- });
144
- `,
145
- },
146
90
  ],
147
91
  invalid: [
148
92
  {
@@ -256,6 +200,18 @@ typescriptEslintTester.run(
256
200
  `,
257
201
  errors: [{ messageId: 'no-nested-selectors' }],
258
202
  },
203
+ {
204
+ name: 'Child combinator without whitespace',
205
+ code: `
206
+ import { styled } from '@compiled/react';
207
+ styled.div({
208
+ '&>a': {
209
+ color: 'orange'
210
+ }
211
+ });
212
+ `,
213
+ errors: [{ messageId: 'no-nested-selectors' }],
214
+ },
259
215
  {
260
216
  name: 'Column combinator',
261
217
  code: `
@@ -280,6 +236,87 @@ typescriptEslintTester.run(
280
236
  `,
281
237
  errors: [{ messageId: 'no-nested-selectors' }],
282
238
  },
239
+ {
240
+ name: 'Descendant combinator',
241
+ code: `
242
+ import { styled } from '@compiled/react';
243
+ const Component = styled.div({
244
+ '& &:hover': {
245
+ padding: 0,
246
+ }
247
+ });
248
+ `,
249
+ errors: [{ messageId: 'no-nested-selectors' }],
250
+ },
251
+ {
252
+ name: 'cssMap API',
253
+ code: `
254
+ import { cssMap } from '@compiled/react';
255
+
256
+ const styles = cssMap({
257
+ success: {
258
+ '[data-component-selector="my.button"]': {
259
+ color: 'var(--ds-text-inverse)',
260
+ }
261
+ },
262
+ });
263
+ `,
264
+ errors: [{ messageId: 'no-nested-selectors' }],
265
+ },
266
+ {
267
+ name: 'keyframes API',
268
+ code: `
269
+ import { keyframes } from '@emotion/react';
270
+
271
+ const keyFrameStyles = keyframes({
272
+ 'from, to': {
273
+ div: {
274
+ opacity: 0,
275
+ }
276
+ },
277
+ '50%': {
278
+ opacity: 1,
279
+ },
280
+ });
281
+ `,
282
+ errors: [{ messageId: 'no-nested-selectors' }],
283
+ },
284
+ {
285
+ name: 'Next sibling selector',
286
+ code: `
287
+ import { styled } from '@compiled/react';
288
+ styled.div({
289
+ '&+a': {
290
+ color: 'orange'
291
+ }
292
+ });
293
+ `,
294
+ errors: [{ messageId: 'no-nested-selectors' }],
295
+ },
296
+ {
297
+ name: 'Next sibling selector on non-HTML element',
298
+ code: `
299
+ import { styled } from '@compiled/react';
300
+ styled.div({
301
+ '& + &': {
302
+ color: 'orange'
303
+ }
304
+ });
305
+ `,
306
+ errors: [{ messageId: 'no-nested-selectors' }],
307
+ },
308
+ {
309
+ name: 'Subsequent sibling selector on non-HTML element',
310
+ code: `
311
+ import { styled } from '@compiled/react';
312
+ styled.div({
313
+ '& ~ &': {
314
+ color: 'orange'
315
+ }
316
+ });
317
+ `,
318
+ errors: [{ messageId: 'no-nested-selectors' }],
319
+ },
283
320
  ],
284
321
  },
285
322
  );
@@ -1,112 +1,99 @@
1
1
  import type { Property } from 'estree';
2
+ import cssSelectorParser, { type Selector, isNesting, isPseudo } from 'postcss-selector-parser';
2
3
 
3
- import {
4
- isStyled,
5
- isCss,
6
- isXcss,
7
- getImportSources,
8
- } from '@atlaskit/eslint-utils/is-supported-import';
9
- import { createLintRule } from '../utils/create-rule';
10
- import { type JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
11
-
12
- const schema: JSONSchema4 = [
13
- {
14
- type: 'object',
15
- properties: {
16
- importSources: {
17
- type: 'array',
18
- items: { type: 'string' },
19
- uniqueItems: true,
20
- },
21
- },
22
- },
23
- ];
24
-
25
- const getCssSelector = (node: Property): string | null => {
26
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
27
- return node.key.value;
28
- }
29
-
30
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
31
- return node.key.name;
32
- }
33
-
34
- if (node.key.type === 'TemplateLiteral') {
35
- return node.key.quasis.map((quasi) => quasi.value.raw).join(' ');
36
- }
37
-
38
- return null;
4
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
5
+ import { importSources } from '@atlaskit/eslint-utils/schema';
6
+
7
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
8
+
9
+ const getCssSelector = (key: Property['key']): string | null => {
10
+ if (key.type === 'Literal' && typeof key.value === 'string') {
11
+ return key.value;
12
+ }
13
+
14
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
15
+ return key.name;
16
+ }
17
+
18
+ if (key.type === 'TemplateLiteral') {
19
+ return key.quasis.map((quasi) => quasi.value.raw).join(' ');
20
+ }
21
+
22
+ return null;
39
23
  };
40
24
 
41
- export const rule = createLintRule({
42
- meta: {
43
- name: 'no-nested-selectors',
44
- docs: {
45
- description: 'Prevents usage of nested selectors within css styling',
46
- recommended: true,
47
- severity: 'warn',
48
- },
49
- messages: {
50
- 'no-nested-selectors':
51
- 'Please avoid setting styles for child elements or elements that require context from other elements.',
52
- },
53
- type: 'problem',
54
- schema,
55
- },
56
- create(context) {
57
- const importSources = getImportSources(context);
58
- return {
59
- 'CallExpression Property': (node: Property): void => {
60
- const { references } = context.getScope();
61
- const ancestors = context.getAncestors();
62
-
63
- if (
64
- ancestors.every(
65
- (ancestor) =>
66
- ancestor.type !== 'CallExpression' ||
67
- !ancestor?.callee ||
68
- !(
69
- isCss(ancestor.callee, references, importSources) ||
70
- isStyled(ancestor.callee, references, importSources) ||
71
- isXcss(ancestor.callee, references, importSources)
72
- ),
73
- )
74
- ) {
75
- return;
76
- }
77
-
78
- if (node.value.type !== 'ObjectExpression') {
79
- // If the value is a CSS object, safe to assume we're at a CSS selector
80
- return;
81
- }
82
-
83
- const cssSelector = getCssSelector(node);
84
- if (cssSelector === null) {
85
- return;
86
- }
87
-
88
- const tokens = cssSelector.split(/[, ]+/);
89
-
90
- if (
91
- !cssSelector.includes('@') &&
92
- !tokens.every(
93
- (token) =>
94
- token.length === 0 ||
95
- token.startsWith('&') ||
96
- token.startsWith(':') ||
97
- token.includes('+') ||
98
- token.includes('~') ||
99
- token.endsWith('&'),
100
- )
101
- ) {
102
- context.report({
103
- messageId: 'no-nested-selectors',
104
- node: node.key,
105
- });
106
- }
107
- },
108
- };
109
- },
25
+ function isAllowedSelector(selector: Selector): boolean {
26
+ const [head, ...tail] = selector.nodes;
27
+
28
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
29
+ const isHeadAllowed = isNesting(head) || isPseudo(head);
30
+
31
+ // All remaining nodes must be pseudo selectors.
32
+ const isTailAllowed = tail.every(isPseudo);
33
+
34
+ return isHeadAllowed && isTailAllowed;
35
+ }
36
+
37
+ const cssSelectorProcessor = cssSelectorParser();
38
+
39
+ export const rule = createLintRuleWithTypedConfig({
40
+ meta: {
41
+ name: 'no-nested-selectors',
42
+ docs: {
43
+ description: 'Prevents usage of nested selectors within css styling',
44
+ recommended: true,
45
+ severity: 'warn',
46
+ },
47
+ messages: {
48
+ 'no-nested-selectors':
49
+ 'Please avoid setting styles for child elements or elements that require context from other elements.',
50
+ },
51
+ type: 'problem',
52
+ schema: {
53
+ type: 'object',
54
+ properties: {
55
+ importSources,
56
+ },
57
+ },
58
+ },
59
+ create(context, { importSources }) {
60
+ return {
61
+ CallExpression(node) {
62
+ const { references } = context.getScope();
63
+
64
+ walkStyleProperties(node, references, importSources, ({ key, value }) => {
65
+ if (value.type !== 'ObjectExpression') {
66
+ // If the value is a CSS object, safe to assume we're at a CSS selector
67
+ return;
68
+ }
69
+
70
+ const selectorText = getCssSelector(key);
71
+ if (selectorText === null) {
72
+ return;
73
+ }
74
+
75
+ // Ignore at-rules
76
+ if (selectorText.includes('@')) {
77
+ return;
78
+ }
79
+
80
+ try {
81
+ const selectorList = cssSelectorProcessor.astSync(selectorText);
82
+
83
+ if (!selectorList.nodes.every(isAllowedSelector)) {
84
+ context.report({
85
+ messageId: 'no-nested-selectors',
86
+ node: key,
87
+ });
88
+ }
89
+ } catch (_) {
90
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
91
+ // we can ignore it here.
92
+ }
93
+ });
94
+ },
95
+ };
96
+ },
110
97
  });
111
98
 
112
99
  export default rule;