@atlaskit/eslint-plugin-ui-styling-standard 0.15.0 → 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.
Files changed (56) hide show
  1. package/CHANGELOG.md +189 -172
  2. package/constellation/atlaskit-theme/usage.mdx +17 -17
  3. package/constellation/convert-props-syntax/usage.mdx +14 -14
  4. package/constellation/enforce-style-prop/usage.mdx +17 -17
  5. package/constellation/index/overview.mdx +70 -70
  6. package/constellation/local-cx-xcss/usage.mdx +4 -4
  7. package/constellation/no-array-arguments/usage.mdx +7 -7
  8. package/constellation/no-container-queries/usage.mdx +14 -14
  9. package/constellation/no-dynamic-styles/usage.mdx +19 -19
  10. package/constellation/no-exported-styles/usage.mdx +15 -15
  11. package/constellation/no-global-styles/usage.mdx +13 -13
  12. package/constellation/no-important-styles/usage.mdx +2 -2
  13. package/constellation/no-imported-style-values/usage.mdx +16 -16
  14. package/constellation/no-nested-selectors/usage.mdx +18 -18
  15. package/constellation/no-styled/usage.mdx +6 -6
  16. package/constellation/no-unsafe-selectors/usage.mdx +90 -90
  17. package/constellation/no-unsafe-values/usage.mdx +33 -33
  18. package/dist/cjs/index.codegen.js +1 -1
  19. package/dist/cjs/presets/all.codegen.js +1 -1
  20. package/dist/cjs/presets/recommended.codegen.js +1 -1
  21. package/dist/cjs/rules/index.codegen.js +1 -1
  22. package/dist/cjs/rules/no-nested-selectors/index.js +68 -49
  23. package/dist/cjs/rules/no-unsafe-selectors/index.js +4 -1
  24. package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +10 -1
  25. package/dist/es2019/index.codegen.js +1 -1
  26. package/dist/es2019/presets/all.codegen.js +1 -1
  27. package/dist/es2019/presets/recommended.codegen.js +1 -1
  28. package/dist/es2019/rules/index.codegen.js +1 -1
  29. package/dist/es2019/rules/no-nested-selectors/index.js +63 -45
  30. package/dist/es2019/rules/no-unsafe-selectors/index.js +4 -1
  31. package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +10 -1
  32. package/dist/esm/index.codegen.js +1 -1
  33. package/dist/esm/presets/all.codegen.js +1 -1
  34. package/dist/esm/presets/recommended.codegen.js +1 -1
  35. package/dist/esm/rules/index.codegen.js +1 -1
  36. package/dist/esm/rules/no-nested-selectors/index.js +64 -49
  37. package/dist/esm/rules/no-unsafe-selectors/index.js +4 -1
  38. package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +10 -1
  39. package/dist/types/presets/all.codegen.d.ts +1 -1
  40. package/dist/types/presets/recommended.codegen.d.ts +1 -1
  41. package/dist/types/rules/no-unsafe-selectors/lint-selector.d.ts +1 -0
  42. package/dist/types-ts4.5/presets/all.codegen.d.ts +1 -1
  43. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +1 -1
  44. package/dist/types-ts4.5/rules/no-unsafe-selectors/lint-selector.d.ts +1 -0
  45. package/package.json +2 -4
  46. package/src/index.codegen.tsx +3 -3
  47. package/src/presets/all.codegen.tsx +70 -73
  48. package/src/presets/recommended.codegen.tsx +70 -73
  49. package/src/rules/index.codegen.tsx +18 -18
  50. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +93 -56
  51. package/src/rules/no-nested-selectors/index.tsx +92 -105
  52. package/src/rules/no-unsafe-selectors/__tests__/no-ambiguous-pseudos.test.tsx +211 -201
  53. package/src/rules/no-unsafe-selectors/index.tsx +174 -177
  54. package/src/rules/no-unsafe-selectors/lint-selector.tsx +128 -141
  55. package/tsconfig.app.json +45 -45
  56. package/tsconfig.dev.json +43 -43
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
9
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
9
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -24,7 +24,7 @@ var _noUnsafeValues = _interopRequireDefault(require("./no-unsafe-values"));
24
24
  var _useCompiled = _interopRequireDefault(require("./use-compiled"));
25
25
  /**
26
26
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
27
- * @codegen <<SignedSource::c907cf4b4e11348239dadaefebe80fca>>
27
+ * @codegen <<SignedSource::6ad0de2d4bdac726387f27681ad9fcf4>>
28
28
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
29
29
  */
30
30
  var _default = exports.default = {
@@ -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
  }
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.default = void 0;
8
8
  var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
9
+ var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
9
10
  var _schema = require("@atlaskit/eslint-utils/schema");
10
11
  var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
11
12
  var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
@@ -94,6 +95,7 @@ var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
94
95
  }(function (node) {
95
96
  var _context$getScope = context.getScope(),
96
97
  references = _context$getScope.references;
98
+ var isXcssCall = (0, _isSupportedImport.isXcss)(node, references, config.importSources);
97
99
  (0, _walkStyleProperties.walkStyleProperties)(node, references, config.importSources, function (_ref) {
98
100
  var key = _ref.key,
99
101
  value = _ref.value;
@@ -138,7 +140,8 @@ var rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
138
140
  context: context,
139
141
  sourceNode: key,
140
142
  selector: selector,
141
- config: config
143
+ config: config,
144
+ isXcssCall: isXcssCall
142
145
  });
143
146
  });
144
147
  } catch (e) {
@@ -24,7 +24,16 @@ function checkNoAmbiguousPseudos(_ref) {
24
24
  var context = _ref.context,
25
25
  sourceNode = _ref.sourceNode,
26
26
  selector = _ref.selector,
27
- config = _ref.config;
27
+ config = _ref.config,
28
+ isXcssCall = _ref.isXcssCall;
29
+ if (isXcssCall) {
30
+ /**
31
+ * The `xcss` API does not currently support a leading `&`.
32
+ *
33
+ * This may change in the future, but we are ignoring `xcss` calls for now.
34
+ */
35
+ return;
36
+ }
28
37
  if (selector.first.type !== 'pseudo') {
29
38
  return;
30
39
  }
@@ -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::16a3441765e5a97f9f32de7fd0de17f0>>
3
+ * @codegen <<SignedSource::aff3fad2b8534fad07ba5f0f1b981a55>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import all from './presets/all.codegen';
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -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::c907cf4b4e11348239dadaefebe80fca>>
3
+ * @codegen <<SignedSource::6ad0de2d4bdac726387f27681ad9fcf4>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import atlaskitTheme from './atlaskit-theme';
@@ -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,4 +1,5 @@
1
1
  import cssSelectorParser from 'postcss-selector-parser';
2
+ import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
2
3
  import { importSources } from '@atlaskit/eslint-utils/schema';
3
4
  import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
4
5
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
@@ -82,6 +83,7 @@ const rule = createLintRuleWithTypedConfig({
82
83
  const {
83
84
  references
84
85
  } = context.getScope();
86
+ const isXcssCall = isXcss(node, references, config.importSources);
85
87
  walkStyleProperties(node, references, config.importSources, ({
86
88
  key,
87
89
  value
@@ -126,7 +128,8 @@ const rule = createLintRuleWithTypedConfig({
126
128
  context,
127
129
  sourceNode: key,
128
130
  selector,
129
- config
131
+ config,
132
+ isXcssCall
130
133
  }));
131
134
  } catch (e) {
132
135
  context.report({
@@ -15,8 +15,17 @@ function checkNoAmbiguousPseudos({
15
15
  context,
16
16
  sourceNode,
17
17
  selector,
18
- config
18
+ config,
19
+ isXcssCall
19
20
  }) {
21
+ if (isXcssCall) {
22
+ /**
23
+ * The `xcss` API does not currently support a leading `&`.
24
+ *
25
+ * This may change in the future, but we are ignoring `xcss` calls for now.
26
+ */
27
+ return;
28
+ }
20
29
  if (selector.first.type !== 'pseudo') {
21
30
  return;
22
31
  }
@@ -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::16a3441765e5a97f9f32de7fd0de17f0>>
3
+ * @codegen <<SignedSource::aff3fad2b8534fad07ba5f0f1b981a55>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import all from './presets/all.codegen';
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -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::a4ab25dd4ce24b183125bfbc709e3943>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -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::c907cf4b4e11348239dadaefebe80fca>>
3
+ * @codegen <<SignedSource::6ad0de2d4bdac726387f27681ad9fcf4>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import atlaskitTheme from './atlaskit-theme';
@@ -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
  }
@@ -1,4 +1,5 @@
1
1
  import cssSelectorParser from 'postcss-selector-parser';
2
+ import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
2
3
  import { importSources } from '@atlaskit/eslint-utils/schema';
3
4
  import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
4
5
  import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
@@ -87,6 +88,7 @@ var rule = createLintRuleWithTypedConfig({
87
88
  }(function (node) {
88
89
  var _context$getScope = context.getScope(),
89
90
  references = _context$getScope.references;
91
+ var isXcssCall = isXcss(node, references, config.importSources);
90
92
  walkStyleProperties(node, references, config.importSources, function (_ref) {
91
93
  var key = _ref.key,
92
94
  value = _ref.value;
@@ -131,7 +133,8 @@ var rule = createLintRuleWithTypedConfig({
131
133
  context: context,
132
134
  sourceNode: key,
133
135
  selector: selector,
134
- config: config
136
+ config: config,
137
+ isXcssCall: isXcssCall
135
138
  });
136
139
  });
137
140
  } catch (e) {
@@ -17,7 +17,16 @@ function checkNoAmbiguousPseudos(_ref) {
17
17
  var context = _ref.context,
18
18
  sourceNode = _ref.sourceNode,
19
19
  selector = _ref.selector,
20
- config = _ref.config;
20
+ config = _ref.config,
21
+ isXcssCall = _ref.isXcssCall;
22
+ if (isXcssCall) {
23
+ /**
24
+ * The `xcss` API does not currently support a leading `&`.
25
+ *
26
+ * This may change in the future, but we are ignoring `xcss` calls for now.
27
+ */
28
+ return;
29
+ }
21
30
  if (selector.first.type !== 'pseudo') {
22
31
  return;
23
32
  }