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

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 (91) hide show
  1. package/CHANGELOG.md +188 -167
  2. package/README.md +1 -0
  3. package/afm-cc/tsconfig.json +10 -3
  4. package/afm-jira/tsconfig.json +28 -0
  5. package/constellation/atlaskit-theme/usage.mdx +17 -17
  6. package/constellation/convert-props-syntax/usage.mdx +14 -14
  7. package/constellation/enforce-style-prop/usage.mdx +17 -17
  8. package/constellation/index/overview.mdx +99 -98
  9. package/constellation/local-cx-xcss/usage.mdx +4 -4
  10. package/constellation/no-array-arguments/usage.mdx +7 -7
  11. package/constellation/no-container-queries/usage.mdx +14 -14
  12. package/constellation/no-dynamic-styles/usage.mdx +19 -19
  13. package/constellation/no-exported-styles/usage.mdx +15 -15
  14. package/constellation/no-global-styles/usage.mdx +13 -13
  15. package/constellation/no-important-styles/usage.mdx +2 -2
  16. package/constellation/no-imported-style-values/usage.mdx +16 -16
  17. package/constellation/no-nested-selectors/usage.mdx +18 -18
  18. package/constellation/no-styled/usage.mdx +6 -6
  19. package/constellation/no-unsafe-selectors/usage.mdx +244 -0
  20. package/constellation/no-unsafe-values/usage.mdx +33 -33
  21. package/dist/cjs/index.codegen.js +1 -1
  22. package/dist/cjs/presets/all.codegen.js +2 -1
  23. package/dist/cjs/presets/recommended.codegen.js +2 -1
  24. package/dist/cjs/rules/index.codegen.js +3 -1
  25. package/dist/cjs/rules/no-unsafe-selectors/constants.js +30 -0
  26. package/dist/cjs/rules/no-unsafe-selectors/index.js +188 -0
  27. package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +198 -0
  28. package/dist/cjs/rules/no-unsafe-selectors/source-position-utils.js +42 -0
  29. package/dist/cjs/rules/no-unsafe-selectors/walk-css-map.js +186 -0
  30. package/dist/es2019/index.codegen.js +1 -1
  31. package/dist/es2019/presets/all.codegen.js +2 -1
  32. package/dist/es2019/presets/recommended.codegen.js +2 -1
  33. package/dist/es2019/rules/index.codegen.js +3 -1
  34. package/dist/es2019/rules/no-unsafe-selectors/constants.js +22 -0
  35. package/dist/es2019/rules/no-unsafe-selectors/index.js +176 -0
  36. package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +161 -0
  37. package/dist/es2019/rules/no-unsafe-selectors/source-position-utils.js +31 -0
  38. package/dist/es2019/rules/no-unsafe-selectors/walk-css-map.js +180 -0
  39. package/dist/esm/index.codegen.js +1 -1
  40. package/dist/esm/presets/all.codegen.js +2 -1
  41. package/dist/esm/presets/recommended.codegen.js +2 -1
  42. package/dist/esm/rules/index.codegen.js +3 -1
  43. package/dist/esm/rules/no-unsafe-selectors/constants.js +24 -0
  44. package/dist/esm/rules/no-unsafe-selectors/index.js +181 -0
  45. package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +191 -0
  46. package/dist/esm/rules/no-unsafe-selectors/source-position-utils.js +34 -0
  47. package/dist/esm/rules/no-unsafe-selectors/walk-css-map.js +179 -0
  48. package/dist/types/index.codegen.d.ts +2 -0
  49. package/dist/types/presets/all.codegen.d.ts +2 -1
  50. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  51. package/dist/types/rules/index.codegen.d.ts +1 -0
  52. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +23 -0
  53. package/dist/types/rules/no-unsafe-selectors/index.d.ts +3 -0
  54. package/dist/types/rules/no-unsafe-selectors/lint-selector.d.ts +14 -0
  55. package/dist/types/rules/no-unsafe-selectors/source-position-utils.d.ts +20 -0
  56. package/dist/types/rules/no-unsafe-selectors/walk-css-map.d.ts +23 -0
  57. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  58. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  59. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  60. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  61. package/dist/types-ts4.5/rules/no-unsafe-selectors/constants.d.ts +84 -0
  62. package/dist/types-ts4.5/rules/no-unsafe-selectors/index.d.ts +3 -0
  63. package/dist/types-ts4.5/rules/no-unsafe-selectors/lint-selector.d.ts +14 -0
  64. package/dist/types-ts4.5/rules/no-unsafe-selectors/source-position-utils.d.ts +23 -0
  65. package/dist/types-ts4.5/rules/no-unsafe-selectors/walk-css-map.d.ts +23 -0
  66. package/package.json +7 -8
  67. package/scripts/codegen.tsx +3 -4
  68. package/src/index.codegen.tsx +3 -3
  69. package/src/presets/all.codegen.tsx +70 -72
  70. package/src/presets/recommended.codegen.tsx +70 -72
  71. package/src/rules/index.codegen.tsx +19 -17
  72. package/src/rules/no-classname-prop/index.tsx +1 -1
  73. package/src/rules/no-nested-selectors/index.tsx +1 -1
  74. package/src/rules/no-unsafe-selectors/README.md +232 -0
  75. package/src/rules/no-unsafe-selectors/__tests__/no-ambiguous-pseudos.test.tsx +368 -0
  76. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +60 -0
  77. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +73 -0
  78. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +40 -0
  79. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +101 -0
  80. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +131 -0
  81. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +132 -0
  82. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +25 -0
  83. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +35 -0
  84. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +238 -0
  85. package/src/rules/no-unsafe-selectors/constants.tsx +99 -0
  86. package/src/rules/no-unsafe-selectors/index.tsx +198 -0
  87. package/src/rules/no-unsafe-selectors/lint-selector.tsx +155 -0
  88. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +55 -0
  89. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +232 -0
  90. package/tsconfig.app.json +45 -45
  91. package/tsconfig.dev.json +43 -43
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.walkCssMap = walkCssMap;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ /**
10
+ * This util is intentionally not in `@atlaskit/eslint-utils` at this time.
11
+ *
12
+ * This is because the API is likely to change and should not be considered stable.
13
+ */
14
+
15
+ function walkCssMap(_ref) {
16
+ var context = _ref.context,
17
+ importSources = _ref.importSources,
18
+ visitor = _ref.visitor;
19
+ var program = context.getSourceCode().ast;
20
+ var importDeclaration = program.body.find(function (node) {
21
+ return node.type === 'ImportDeclaration' && importSources.includes(node.source.value);
22
+ });
23
+ var specifier = importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.specifiers.find(function (specifier) {
24
+ return specifier.type === 'ImportSpecifier' && specifier.imported.name === 'cssMap';
25
+ });
26
+ if (!specifier) {
27
+ return;
28
+ }
29
+ var _context$getSourceCod = context.getSourceCode().scopeManager.getDeclaredVariables(specifier),
30
+ _context$getSourceCod2 = (0, _slicedToArray2.default)(_context$getSourceCod, 1),
31
+ variable = _context$getSourceCod2[0];
32
+ if (!variable) {
33
+ return;
34
+ }
35
+ variable.references.forEach(function (reference) {
36
+ var identifier = reference.identifier;
37
+ var parent = identifier.parent;
38
+ if (parent.type !== 'CallExpression') {
39
+ return;
40
+ }
41
+ var variantMap = parent.arguments[0];
42
+ if (variantMap.type !== 'ObjectExpression') {
43
+ return;
44
+ }
45
+ walkCssMapVariantMap({
46
+ variantMap: variantMap,
47
+ visitor: visitor
48
+ });
49
+ });
50
+ }
51
+ function walkCssMapVariantMap(_ref2) {
52
+ var variantMap = _ref2.variantMap,
53
+ visitor = _ref2.visitor;
54
+ variantMap.properties.forEach(function (property) {
55
+ if (property.type !== 'Property') {
56
+ return;
57
+ }
58
+ walkCssMapVariant({
59
+ variant: property,
60
+ visitor: visitor
61
+ });
62
+ });
63
+ }
64
+ function walkCssMapVariant(_ref3) {
65
+ var variant = _ref3.variant,
66
+ visitor = _ref3.visitor;
67
+ visitor({
68
+ type: 'variant',
69
+ node: variant
70
+ });
71
+ if (variant.value.type !== 'ObjectExpression') {
72
+ return;
73
+ }
74
+ variant.value.properties.forEach(function (property) {
75
+ if (property.type !== 'Property') {
76
+ return;
77
+ }
78
+ if (property.value.type !== 'ObjectExpression') {
79
+ visitor({
80
+ type: 'declaration',
81
+ node: property
82
+ });
83
+ return;
84
+ }
85
+ var keyValue = getKeyValue(property.key);
86
+ if (keyValue && /^@[A-z-]+$/.test(keyValue)) {
87
+ walkCssMapAtRuleGrouping({
88
+ atRuleGrouping: property,
89
+ visitor: visitor
90
+ });
91
+ return;
92
+ }
93
+ if (keyValue === 'selectors') {
94
+ walkCssMapSelectors({
95
+ selectors: property,
96
+ visitor: visitor
97
+ });
98
+ }
99
+ walkStyleRuleset({
100
+ ruleset: property,
101
+ visitor: visitor
102
+ });
103
+ });
104
+ }
105
+ function walkCssMapAtRuleGrouping(_ref4) {
106
+ var atRuleGrouping = _ref4.atRuleGrouping,
107
+ visitor = _ref4.visitor;
108
+ var atRule = getKeyValue(atRuleGrouping.key);
109
+ if (!atRule) {
110
+ return;
111
+ }
112
+ visitor({
113
+ type: 'grouped-at-rules',
114
+ node: atRuleGrouping,
115
+ atRule: atRule
116
+ });
117
+ if (atRuleGrouping.value.type !== 'ObjectExpression') {
118
+ return;
119
+ }
120
+ atRuleGrouping.value.properties.forEach(function (property) {
121
+ if (property.type !== 'Property') {
122
+ return;
123
+ }
124
+ walkStyleRuleset({
125
+ ruleset: property,
126
+ visitor: visitor
127
+ });
128
+ });
129
+ }
130
+ function walkCssMapSelectors(_ref5) {
131
+ var selectors = _ref5.selectors,
132
+ visitor = _ref5.visitor;
133
+ visitor({
134
+ type: 'selectors',
135
+ node: selectors
136
+ });
137
+ if (selectors.value.type !== 'ObjectExpression') {
138
+ return;
139
+ }
140
+ selectors.value.properties.forEach(function (property) {
141
+ if (property.type !== 'Property') {
142
+ return;
143
+ }
144
+ walkStyleRuleset({
145
+ ruleset: property,
146
+ visitor: visitor
147
+ });
148
+ });
149
+ }
150
+ function walkStyleRuleset(_ref6) {
151
+ var ruleset = _ref6.ruleset,
152
+ visitor = _ref6.visitor;
153
+ visitor({
154
+ type: 'ruleset',
155
+ node: ruleset
156
+ });
157
+ if (ruleset.value.type !== 'ObjectExpression') {
158
+ return;
159
+ }
160
+ ruleset.value.properties.forEach(function (property) {
161
+ if (property.type !== 'Property') {
162
+ return;
163
+ }
164
+ if (property.value.type === 'ObjectExpression') {
165
+ walkStyleRuleset({
166
+ ruleset: property,
167
+ visitor: visitor
168
+ });
169
+ return;
170
+ }
171
+ visitor({
172
+ type: 'declaration',
173
+ node: property
174
+ });
175
+ });
176
+ }
177
+ function getKeyValue(key) {
178
+ if (key.type === 'Identifier') {
179
+ return key.name;
180
+ }
181
+ if (key.type === 'Literal') {
182
+ var _key$value$toString, _key$value;
183
+ return (_key$value$toString = (_key$value = key.value) === null || _key$value === void 0 ? void 0 : _key$value.toString()) !== null && _key$value$toString !== void 0 ? _key$value$toString : null;
184
+ }
185
+ return null;
186
+ }
@@ -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::40bdd5a27731185ba04e0b279fc81ac9>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -20,6 +20,7 @@ export default {
20
20
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-styled': 'warn',
23
+ '@atlaskit/ui-styling-standard/no-unsafe-selectors': 'warn',
23
24
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
24
25
  '@atlaskit/ui-styling-standard/use-compiled': 'warn',
25
26
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::40bdd5a27731185ba04e0b279fc81ac9>>
3
+ * @codegen <<SignedSource::f240d1cd5b93827327b66c28ad47e698>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -20,6 +20,7 @@ export default {
20
20
  '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
21
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-styled': 'warn',
23
+ '@atlaskit/ui-styling-standard/no-unsafe-selectors': 'warn',
23
24
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
24
25
  '@atlaskit/ui-styling-standard/use-compiled': 'warn',
25
26
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::417762b5fc4a56c3b69c22b8ac0d12e7>>
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';
@@ -17,6 +17,7 @@ import noImportantStyles from './no-important-styles';
17
17
  import noImportedStyleValues from './no-imported-style-values';
18
18
  import noNestedSelectors from './no-nested-selectors';
19
19
  import noStyled from './no-styled';
20
+ import noUnsafeSelectors from './no-unsafe-selectors';
20
21
  import noUnsafeValues from './no-unsafe-values';
21
22
  import useCompiled from './use-compiled';
22
23
  export default {
@@ -34,6 +35,7 @@ export default {
34
35
  'no-imported-style-values': noImportedStyleValues,
35
36
  'no-nested-selectors': noNestedSelectors,
36
37
  'no-styled': noStyled,
38
+ 'no-unsafe-selectors': noUnsafeSelectors,
37
39
  'no-unsafe-values': noUnsafeValues,
38
40
  'use-compiled': useCompiled
39
41
  };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Validation approach from
3
+ * https://stackoverflow.com/a/71236280
4
+ */
5
+
6
+ /**
7
+ * Exporting this so it isn't considered unused.
8
+ */
9
+
10
+ const cssPseudos = ['&:active', '&:autofill', '&:blank', '&:checked', '&:default', '&:defined', '&:disabled', '&:empty', '&:enabled', '&:first', '&:focus', '&:focus-visible', '&:focus-within', '&:fullscreen', '&:hover', '&:in-range', '&:indeterminate', '&:invalid', '&:left', '&:link', '&:local-link', '&:optional', '&:out-of-range', '&:paused', '&:picture-in-picture', '&:placeholder-shown', '&:playing', '&:read-only', '&:read-write', '&:required', '&:right', '&:target', '&:user-invalid', '&:user-valid', '&:valid', '&:visited', '&::after', '&::backdrop', '&::before', '&::cue', '&::cue-region', '&::first-letter', '&::first-line', '&::grammar-error', '&::marker', '&::placeholder', '&::selection', '&::spelling-error', '&::target-text', '&::view-transition'];
11
+ export const allowedPseudos = new Set(
12
+ /**
13
+ * The `&` is included above for type checking reasons,
14
+ * but we don't actually want it when consuming the values.
15
+ */
16
+ cssPseudos.map(pseudo => pseudo.slice(1)));
17
+ export const legacyPseudoElements = new Set([':after', ':before', ':first-letter', ':first-line']);
18
+ export const ignoredAtRules = new Set(['@container',
19
+ // ignored because it's covered by `no-container-queries`
20
+ '@media',
21
+ // ignored because it's covered by `no-nested-styles`
22
+ '@supports', '@property']);
@@ -0,0 +1,176 @@
1
+ import cssSelectorParser from 'postcss-selector-parser';
2
+ import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
3
+ import { importSources } from '@atlaskit/eslint-utils/schema';
4
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
5
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
6
+ import { ignoredAtRules } from './constants';
7
+ import { lintSelector } from './lint-selector';
8
+ import { walkCssMap } from './walk-css-map';
9
+ const cssSelectorProcessor = cssSelectorParser();
10
+ const rule = createLintRuleWithTypedConfig({
11
+ meta: {
12
+ name: 'no-unsafe-selectors',
13
+ docs: {
14
+ description: 'Disallows use of nested styles in `css` functions.',
15
+ recommended: true,
16
+ severity: 'warn'
17
+ },
18
+ fixable: 'code',
19
+ hasSuggestions: true,
20
+ messages: {
21
+ /**
22
+ * Selector messages
23
+ */
24
+ 'no-ambiguous-pseudos': 'Pseudo-classes and pseudo-elements without a leading selector are ambiguous. Use a more explicit selector.',
25
+ 'no-increased-specificity': 'Do not chain the nesting selector.',
26
+ 'no-restricted-pseudos': 'The {{pseudo}} pseudo is not allowed.',
27
+ 'no-legacy-pseudo-element-syntax': 'Use a double colon for pseudo-elements. The single colon syntax is not supported for all pseudo-elements.',
28
+ 'no-unparsable-selectors': "The selector '{{selectorText}}' could not be parsed and is likely an authoring mistake. If you think this is an error reach out in #help-ui-styling-standard.",
29
+ 'no-selectors-object': 'Do not use the selectors object. Both nested and advanced selectors should be avoided.',
30
+ /**
31
+ * At-rule messages
32
+ */
33
+ 'no-keyframes-at-rules': 'Use the CSS-in-JS `keyframes` API instead of `@keyframes` at-rules.',
34
+ 'no-restricted-at-rules': '{{atRule}} at-rule is not allowed.',
35
+ 'no-grouped-at-rules': 'Do not group at-rules. Write flattened at-rules instead.',
36
+ /**
37
+ * Suggestion messages
38
+ */
39
+ 'insert-nesting-selector': 'Insert a nesting selector `&` to target the element itself. This is usually the intended behavior.'
40
+ },
41
+ schema: {
42
+ type: 'object',
43
+ properties: {
44
+ importSources,
45
+ shouldAlwaysInsertNestingSelectorForAmbiguousPseudos: {
46
+ type: 'boolean',
47
+ default: true
48
+ }
49
+ },
50
+ additionalProperties: false
51
+ }
52
+ },
53
+ create(context, config) {
54
+ return {
55
+ Program(program) {
56
+ walkCssMap({
57
+ context,
58
+ program,
59
+ importSources: config.importSources,
60
+ visitor(property) {
61
+ const {
62
+ type,
63
+ node
64
+ } = property;
65
+ if (type === 'grouped-at-rules') {
66
+ context.report({
67
+ node: node.key,
68
+ messageId: 'no-grouped-at-rules'
69
+ });
70
+ return;
71
+ }
72
+ if (type === 'selectors') {
73
+ context.report({
74
+ node: node.key,
75
+ messageId: 'no-selectors-object'
76
+ });
77
+ return;
78
+ }
79
+ }
80
+ });
81
+ },
82
+ CallExpression(node) {
83
+ const {
84
+ references
85
+ } = context.getScope();
86
+ const isXcssCall = isXcss(node, references, config.importSources);
87
+ walkStyleProperties(node, references, config.importSources, ({
88
+ key,
89
+ value
90
+ }) => {
91
+ /**
92
+ * If the value isn't an object expression then the key can't be a selector.
93
+ *
94
+ * This assumes all styles have to be inline, which is enforced by `no-unsafe-values`
95
+ */
96
+ if (value.type !== 'ObjectExpression') {
97
+ return;
98
+ }
99
+
100
+ /**
101
+ * Technically the key could be an identifier too, e.g.
102
+ *
103
+ * ```ts
104
+ * css({
105
+ * // This key is an identifier, not a literal
106
+ * selector: {}
107
+ * });
108
+ * ```
109
+ *
110
+ * But an identifier couldn't violate anything in this rule,
111
+ * because it can't contain '@' or ':' characters.
112
+ */
113
+ if (key.type !== 'Literal' || typeof key.value !== 'string') {
114
+ return;
115
+ }
116
+ const selectorText = key.value;
117
+ if (selectorText.includes('@')) {
118
+ lintAtRule({
119
+ context,
120
+ sourceNode: key,
121
+ atRule: selectorText
122
+ });
123
+ return;
124
+ }
125
+ try {
126
+ const selectorList = cssSelectorProcessor.astSync(selectorText);
127
+ selectorList.nodes.forEach(selector => lintSelector({
128
+ context,
129
+ sourceNode: key,
130
+ selector,
131
+ config,
132
+ isXcssCall
133
+ }));
134
+ } catch (e) {
135
+ context.report({
136
+ node: key,
137
+ messageId: 'no-unparsable-selectors',
138
+ data: {
139
+ selectorText
140
+ }
141
+ });
142
+ }
143
+ });
144
+ }
145
+ };
146
+ }
147
+ });
148
+ export default rule;
149
+ function lintAtRule({
150
+ context,
151
+ sourceNode,
152
+ atRule
153
+ }) {
154
+ const matches = atRule.trim().match(/^@[A-z-]+/);
155
+ if (!matches) {
156
+ return;
157
+ }
158
+ const ruleName = matches[0];
159
+ if (ignoredAtRules.has(ruleName)) {
160
+ return;
161
+ }
162
+ if (ruleName === '@keyframes') {
163
+ context.report({
164
+ node: sourceNode,
165
+ messageId: 'no-keyframes-at-rules'
166
+ });
167
+ } else {
168
+ context.report({
169
+ node: sourceNode,
170
+ messageId: 'no-restricted-at-rules',
171
+ data: {
172
+ atRule: ruleName
173
+ }
174
+ });
175
+ }
176
+ }
@@ -0,0 +1,161 @@
1
+ import { allowedPseudos, legacyPseudoElements } from './constants';
2
+ import { isNesting } from 'postcss-selector-parser';
3
+ import { getRangeFromNode, getSourceLocationFromRange } from './source-position-utils';
4
+ export function lintSelector(args) {
5
+ checkNoAmbiguousPseudos(args);
6
+ checkNoRestrictedPseudos(args);
7
+ checkNoLegacyPseudoElementSyntax(args);
8
+ checkNoIncreasedSpecificity(args);
9
+ }
10
+
11
+ /**
12
+ * Ensures the use of a nesting selector `&` with pseudo-selectors.
13
+ */
14
+ function checkNoAmbiguousPseudos({
15
+ context,
16
+ sourceNode,
17
+ selector,
18
+ config,
19
+ isXcssCall
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
+ }
29
+ if (selector.first.type !== 'pseudo') {
30
+ return;
31
+ }
32
+ const range = getRangeFromNode({
33
+ node: selector.first,
34
+ sourceNode
35
+ });
36
+ const loc = getSourceLocationFromRange(context, range);
37
+ if (config.shouldAlwaysInsertNestingSelectorForAmbiguousPseudos) {
38
+ context.report({
39
+ loc,
40
+ messageId: 'no-ambiguous-pseudos',
41
+ *fix(fixer) {
42
+ yield fixer.insertTextBeforeRange(range, '&');
43
+ }
44
+ });
45
+ } else {
46
+ context.report({
47
+ loc,
48
+ messageId: 'no-ambiguous-pseudos',
49
+ suggest: [{
50
+ messageId: 'insert-nesting-selector',
51
+ *fix(fixer) {
52
+ yield fixer.insertTextBeforeRange(range, '&');
53
+ }
54
+ }]
55
+ });
56
+ }
57
+ }
58
+ function checkNoRestrictedPseudos({
59
+ context,
60
+ sourceNode,
61
+ selector
62
+ }) {
63
+ selector.walkPseudos(pseudo => {
64
+ if (allowedPseudos.has(pseudo.value)) {
65
+ return;
66
+ }
67
+
68
+ /**
69
+ * If someone has written `:after` it is allowed,
70
+ * they've just used the legacy syntax.
71
+ *
72
+ * This will be caught by the `no-legacy-pseudo-element-syntax` check.
73
+ */
74
+ if (legacyPseudoElements.has(pseudo.value) && allowedPseudos.has(`:${pseudo.value}`)) {
75
+ return;
76
+ }
77
+ if (!allowedPseudos.has(pseudo.value)) {
78
+ const range = getRangeFromNode({
79
+ node: pseudo,
80
+ sourceNode
81
+ });
82
+ const loc = getSourceLocationFromRange(context, range);
83
+ context.report({
84
+ loc,
85
+ messageId: 'no-restricted-pseudos',
86
+ data: {
87
+ pseudo: pseudo.value
88
+ }
89
+ });
90
+ }
91
+ });
92
+ }
93
+
94
+ /**
95
+ * The original four pseudo-elements have a legacy single colon syntax.
96
+ *
97
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements
98
+ */
99
+ function checkNoLegacyPseudoElementSyntax({
100
+ context,
101
+ sourceNode,
102
+ selector
103
+ }) {
104
+ selector.walkPseudos(pseudo => {
105
+ if (legacyPseudoElements.has(pseudo.value)) {
106
+ const range = getRangeFromNode({
107
+ node: pseudo,
108
+ sourceNode
109
+ });
110
+ const loc = getSourceLocationFromRange(context, range);
111
+ context.report({
112
+ loc,
113
+ messageId: 'no-legacy-pseudo-element-syntax',
114
+ *fix(fixer) {
115
+ yield fixer.insertTextBeforeRange(range, ':');
116
+ }
117
+ });
118
+ }
119
+ });
120
+ }
121
+ function checkNoIncreasedSpecificity({
122
+ context,
123
+ sourceNode,
124
+ selector
125
+ }) {
126
+ selector.walkNesting(nesting => {
127
+ /**
128
+ * If it's not the start of a chain, then it's already been reported.
129
+ */
130
+ if (isNesting(nesting.prev())) {
131
+ return;
132
+ }
133
+ let next = nesting.next();
134
+
135
+ /**
136
+ * If it is followed by something other than another nesting selector,
137
+ * then it isn't just there to increase specificity — it is actually necessary.
138
+ */
139
+ if (next && !isNesting(next)) {
140
+ return;
141
+ }
142
+ let lastNesting = nesting;
143
+ while (isNesting(next)) {
144
+ lastNesting = next;
145
+ next = next.next();
146
+ }
147
+ const [start] = getRangeFromNode({
148
+ node: nesting,
149
+ sourceNode
150
+ });
151
+ const [_, end] = getRangeFromNode({
152
+ node: lastNesting,
153
+ sourceNode
154
+ });
155
+ const loc = getSourceLocationFromRange(context, [start, end]);
156
+ context.report({
157
+ loc,
158
+ messageId: 'no-increased-specificity'
159
+ });
160
+ });
161
+ }
@@ -0,0 +1,31 @@
1
+ export function getRangeFromNode({
2
+ node,
3
+ sourceNode
4
+ }) {
5
+ /**
6
+ * The adjustment required to align the selector AST source position
7
+ * with the ESTree source position.
8
+ */
9
+ const adjustment =
10
+ /**
11
+ * Using a non-null `!` is safe here because ESLint requires the parser to
12
+ * include a range value on nodes.
13
+ *
14
+ * https://eslint.org/docs/latest/extend/custom-parsers#all-nodes
15
+ */
16
+ sourceNode.range[0] + (
17
+ /**
18
+ * We add +1 if it's a literal to account for the opening `'` or `"` character.
19
+ */
20
+ sourceNode.type === 'Literal' ? 1 : 0);
21
+ const start = adjustment + node.sourceIndex;
22
+ const end = start + node.value.length;
23
+ return [start, end];
24
+ }
25
+ export function getSourceLocationFromRange(context, [start, end]) {
26
+ const sourceCode = context.getSourceCode();
27
+ return {
28
+ start: sourceCode.getLocFromIndex(start),
29
+ end: sourceCode.getLocFromIndex(end)
30
+ };
31
+ }