@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
@@ -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;