@atlaskit/eslint-plugin-ui-styling-standard 0.18.0 → 0.18.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 (137) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +1 -1
  3. package/afm-cc/tsconfig.json +0 -28
  4. package/afm-jira/tsconfig.json +0 -27
  5. package/build/tsconfig.json +0 -18
  6. package/constellation/atlaskit-theme/usage.mdx +0 -132
  7. package/constellation/consistent-css-prop-usage/usage.mdx +0 -7
  8. package/constellation/convert-props-syntax/usage.mdx +0 -130
  9. package/constellation/enforce-style-prop/usage.mdx +0 -47
  10. package/constellation/index/migration-guide.mdx +0 -309
  11. package/constellation/index/overview.mdx +0 -250
  12. package/constellation/index/props.mdx +0 -30
  13. package/constellation/local-cx-xcss/usage.mdx +0 -53
  14. package/constellation/no-array-arguments/usage.mdx +0 -49
  15. package/constellation/no-classname-prop/usage.mdx +0 -49
  16. package/constellation/no-container-queries/usage.mdx +0 -101
  17. package/constellation/no-css-tagged-template-expression/usage.mdx +0 -8
  18. package/constellation/no-dynamic-styles/usage.mdx +0 -130
  19. package/constellation/no-empty-styled-expression/usage.mdx +0 -8
  20. package/constellation/no-exported-css/usage.mdx +0 -8
  21. package/constellation/no-exported-keyframes/usage.mdx +0 -8
  22. package/constellation/no-exported-styles/usage.mdx +0 -87
  23. package/constellation/no-global-styles/usage.mdx +0 -60
  24. package/constellation/no-important-styles/usage.mdx +0 -33
  25. package/constellation/no-imported-style-values/usage.mdx +0 -157
  26. package/constellation/no-invalid-css-map/usage.mdx +0 -8
  27. package/constellation/no-js-xcss/usage.mdx +0 -7
  28. package/constellation/no-keyframes-tagged-template-expression/usage.mdx +0 -8
  29. package/constellation/no-nested-selectors/usage.mdx +0 -95
  30. package/constellation/no-styled/usage.mdx +0 -92
  31. package/constellation/no-styled-tagged-template-expression/usage.mdx +0 -8
  32. package/constellation/no-suppress-xcss/usage.mdx +0 -28
  33. package/constellation/no-unsafe-selectors/usage.mdx +0 -370
  34. package/constellation/no-unsafe-values/usage.mdx +0 -318
  35. package/constellation/use-compiled/usage.mdx +0 -54
  36. package/index.js +0 -24
  37. package/scripts/codegen.tsx +0 -373
  38. package/src/index.codegen.tsx +0 -14
  39. package/src/index.tsx +0 -1
  40. package/src/presets/all.codegen.tsx +0 -79
  41. package/src/presets/recommended.codegen.tsx +0 -79
  42. package/src/rules/TEMPLATE.md +0 -29
  43. package/src/rules/__tests__/utils/_tester.tsx +0 -40
  44. package/src/rules/atlaskit-theme/README.md +0 -130
  45. package/src/rules/atlaskit-theme/__tests__/test.ts +0 -133
  46. package/src/rules/atlaskit-theme/index.ts +0 -134
  47. package/src/rules/convert-props-syntax/README.md +0 -128
  48. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +0 -76
  49. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +0 -584
  50. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +0 -140
  51. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +0 -69
  52. package/src/rules/convert-props-syntax/index.tsx +0 -414
  53. package/src/rules/enforce-style-prop/README.md +0 -45
  54. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +0 -243
  55. package/src/rules/enforce-style-prop/index.tsx +0 -172
  56. package/src/rules/external-rules.tsx +0 -177
  57. package/src/rules/index.codegen.tsx +0 -42
  58. package/src/rules/local-cx-xcss/README.md +0 -51
  59. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +0 -102
  60. package/src/rules/local-cx-xcss/index.tsx +0 -62
  61. package/src/rules/no-array-arguments/README.md +0 -47
  62. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +0 -107
  63. package/src/rules/no-array-arguments/index.tsx +0 -86
  64. package/src/rules/no-classname-prop/README.md +0 -47
  65. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +0 -39
  66. package/src/rules/no-classname-prop/index.tsx +0 -34
  67. package/src/rules/no-container-queries/README.md +0 -99
  68. package/src/rules/no-container-queries/__tests__/rule.test.tsx +0 -139
  69. package/src/rules/no-container-queries/index.tsx +0 -94
  70. package/src/rules/no-dynamic-styles/README.md +0 -128
  71. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +0 -135
  72. package/src/rules/no-dynamic-styles/index.tsx +0 -60
  73. package/src/rules/no-exported-styles/README.md +0 -85
  74. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +0 -216
  75. package/src/rules/no-exported-styles/index.tsx +0 -52
  76. package/src/rules/no-global-styles/README.md +0 -58
  77. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +0 -33
  78. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +0 -46
  79. package/src/rules/no-global-styles/index.tsx +0 -82
  80. package/src/rules/no-important-styles/README.md +0 -31
  81. package/src/rules/no-important-styles/__tests__/rule.test.tsx +0 -165
  82. package/src/rules/no-important-styles/index.tsx +0 -70
  83. package/src/rules/no-imported-style-values/README.md +0 -155
  84. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +0 -491
  85. package/src/rules/no-imported-style-values/index.tsx +0 -188
  86. package/src/rules/no-nested-selectors/README.md +0 -93
  87. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +0 -335
  88. package/src/rules/no-nested-selectors/index.tsx +0 -99
  89. package/src/rules/no-styled/README.md +0 -90
  90. package/src/rules/no-styled/__tests__/rule.test.tsx +0 -73
  91. package/src/rules/no-styled/index.tsx +0 -45
  92. package/src/rules/no-unsafe-selectors/README.md +0 -368
  93. package/src/rules/no-unsafe-selectors/__tests__/no-ambiguous-pseudos.test.tsx +0 -368
  94. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +0 -57
  95. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +0 -73
  96. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +0 -40
  97. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +0 -101
  98. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +0 -131
  99. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +0 -132
  100. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +0 -25
  101. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +0 -35
  102. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +0 -257
  103. package/src/rules/no-unsafe-selectors/constants.tsx +0 -91
  104. package/src/rules/no-unsafe-selectors/index.tsx +0 -198
  105. package/src/rules/no-unsafe-selectors/lint-selector.tsx +0 -155
  106. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +0 -55
  107. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +0 -226
  108. package/src/rules/no-unsafe-values/README.md +0 -316
  109. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +0 -27
  110. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +0 -37
  111. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +0 -229
  112. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +0 -267
  113. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +0 -52
  114. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +0 -106
  115. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +0 -44
  116. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +0 -48
  117. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +0 -75
  118. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +0 -121
  119. package/src/rules/no-unsafe-values/index.tsx +0 -94
  120. package/src/rules/no-unsafe-values/messages.tsx +0 -86
  121. package/src/rules/no-unsafe-values/utils.tsx +0 -387
  122. package/src/rules/use-compiled/README.md +0 -52
  123. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +0 -73
  124. package/src/rules/use-compiled/__tests__/css.test.tsx +0 -126
  125. package/src/rules/use-compiled/__tests__/imports.test.tsx +0 -158
  126. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +0 -140
  127. package/src/rules/use-compiled/__tests__/pragma.test.tsx +0 -108
  128. package/src/rules/use-compiled/__tests__/styled.test.tsx +0 -114
  129. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +0 -15
  130. package/src/rules/use-compiled/index.tsx +0 -252
  131. package/src/rules/use-compiled/is-safe.tsx +0 -159
  132. package/src/rules/use-compiled/utils.tsx +0 -33
  133. package/src/rules/utils/create-rule-with-typed-config.tsx +0 -86
  134. package/src/rules/utils/create-rule.tsx +0 -13
  135. package/tsconfig.app.json +0 -47
  136. package/tsconfig.dev.json +0 -45
  137. package/tsconfig.json +0 -25
@@ -1,198 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- import type * as ESTree from 'eslint-codemod-utils';
3
- import cssSelectorParser from 'postcss-selector-parser';
4
-
5
- import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
6
- import { importSources } from '@atlaskit/eslint-utils/schema';
7
- import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
8
-
9
- import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
10
-
11
- import { ignoredAtRules } from './constants';
12
- import { lintSelector } from './lint-selector';
13
- import { walkCssMap } from './walk-css-map';
14
-
15
- const cssSelectorProcessor = cssSelectorParser();
16
-
17
- const rule = createLintRuleWithTypedConfig({
18
- meta: {
19
- name: 'no-unsafe-selectors',
20
- docs: {
21
- description: 'Disallows use of nested styles in `css` functions.',
22
- recommended: true,
23
- severity: 'error',
24
- },
25
- fixable: 'code',
26
- hasSuggestions: true,
27
- messages: {
28
- /**
29
- * Selector messages
30
- */
31
- 'no-ambiguous-pseudos':
32
- 'Pseudo-classes and pseudo-elements without a leading selector are ambiguous. Use a more explicit selector.',
33
-
34
- 'no-increased-specificity': 'Do not chain the nesting selector.',
35
-
36
- 'no-restricted-pseudos': 'The {{pseudo}} pseudo is not allowed.',
37
-
38
- 'no-legacy-pseudo-element-syntax':
39
- 'Use a double colon for pseudo-elements. The single colon syntax is not supported for all pseudo-elements.',
40
-
41
- 'no-unparsable-selectors':
42
- "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.",
43
-
44
- 'no-selectors-object':
45
- 'Do not use the selectors object. Both nested and advanced selectors should be avoided.',
46
-
47
- /**
48
- * At-rule messages
49
- */
50
- 'no-keyframes-at-rules':
51
- 'Use the CSS-in-JS `keyframes` API instead of `@keyframes` at-rules.',
52
-
53
- 'no-restricted-at-rules': '{{atRule}} at-rule is not allowed.',
54
-
55
- 'no-grouped-at-rules': 'Do not group at-rules. Write flattened at-rules instead.',
56
-
57
- /**
58
- * Suggestion messages
59
- */
60
- 'insert-nesting-selector':
61
- 'Insert a nesting selector `&` to target the element itself. This is usually the intended behavior.',
62
- },
63
-
64
- schema: {
65
- type: 'object',
66
- properties: {
67
- importSources,
68
- shouldAlwaysInsertNestingSelectorForAmbiguousPseudos: {
69
- type: 'boolean',
70
- default: true,
71
- },
72
- },
73
- additionalProperties: false,
74
- },
75
- },
76
- create(context, config) {
77
- return {
78
- Program(program) {
79
- walkCssMap({
80
- context,
81
- program,
82
- importSources: config.importSources,
83
- visitor(property) {
84
- const { type, node } = property;
85
-
86
- if (type === 'grouped-at-rules') {
87
- context.report({
88
- node: node.key,
89
- messageId: 'no-grouped-at-rules',
90
- });
91
- return;
92
- }
93
-
94
- if (type === 'selectors') {
95
- context.report({
96
- node: node.key,
97
- messageId: 'no-selectors-object',
98
- });
99
- return;
100
- }
101
- },
102
- });
103
- },
104
- CallExpression(node: ESTree.CallExpression) {
105
- const { references } = context.getScope();
106
-
107
- const isXcssCall = isXcss(node, references, config.importSources);
108
-
109
- walkStyleProperties(node, references, config.importSources, ({ key, value }) => {
110
- /**
111
- * If the value isn't an object expression then the key can't be a selector.
112
- *
113
- * This assumes all styles have to be inline, which is enforced by `no-unsafe-values`
114
- */
115
- if (value.type !== 'ObjectExpression') {
116
- return;
117
- }
118
-
119
- /**
120
- * Technically the key could be an identifier too, e.g.
121
- *
122
- * ```ts
123
- * css({
124
- * // This key is an identifier, not a literal
125
- * selector: {}
126
- * });
127
- * ```
128
- *
129
- * But an identifier couldn't violate anything in this rule,
130
- * because it can't contain '@' or ':' characters.
131
- */
132
- if (key.type !== 'Literal' || typeof key.value !== 'string') {
133
- return;
134
- }
135
-
136
- const selectorText = key.value;
137
-
138
- if (selectorText.includes('@')) {
139
- lintAtRule({ context, sourceNode: key, atRule: selectorText });
140
- return;
141
- }
142
-
143
- try {
144
- const selectorList = cssSelectorProcessor.astSync(selectorText);
145
-
146
- selectorList.nodes.forEach((selector) =>
147
- lintSelector({ context, sourceNode: key, selector, config, isXcssCall }),
148
- );
149
- } catch (e) {
150
- context.report({
151
- node: key,
152
- messageId: 'no-unparsable-selectors',
153
- data: { selectorText },
154
- });
155
- }
156
- });
157
- },
158
- };
159
- },
160
- });
161
-
162
- export default rule;
163
-
164
- function lintAtRule({
165
- context,
166
- sourceNode,
167
- atRule,
168
- }: {
169
- context: Rule.RuleContext;
170
- sourceNode: ESTree.Node;
171
- atRule: string;
172
- }) {
173
- const matches = atRule.trim().match(/^@[A-z-]+/);
174
- if (!matches) {
175
- return;
176
- }
177
-
178
- const ruleName = matches[0];
179
-
180
- if (ignoredAtRules.has(ruleName)) {
181
- return;
182
- }
183
-
184
- if (ruleName === '@keyframes') {
185
- context.report({
186
- node: sourceNode,
187
- messageId: 'no-keyframes-at-rules',
188
- });
189
- } else {
190
- context.report({
191
- node: sourceNode,
192
- messageId: 'no-restricted-at-rules',
193
- data: {
194
- atRule: ruleName,
195
- },
196
- });
197
- }
198
- }
@@ -1,155 +0,0 @@
1
- import type { Rule } from 'eslint';
2
- import type * as ESTree from 'eslint-codemod-utils';
3
-
4
- import { allowedPseudos, legacyPseudoElements } from './constants';
5
- import { type Selector, isNesting } from 'postcss-selector-parser';
6
- import { getRangeFromNode, getSourceLocationFromRange } from './source-position-utils';
7
-
8
- type CheckArgs = {
9
- context: Rule.RuleContext;
10
- sourceNode: ESTree.Node;
11
- selector: Selector;
12
- config: { shouldAlwaysInsertNestingSelectorForAmbiguousPseudos: boolean };
13
- isXcssCall: boolean;
14
- };
15
-
16
- export function lintSelector(args: CheckArgs) {
17
- checkNoAmbiguousPseudos(args);
18
- checkNoRestrictedPseudos(args);
19
- checkNoLegacyPseudoElementSyntax(args);
20
- checkNoIncreasedSpecificity(args);
21
- }
22
-
23
- /**
24
- * Ensures the use of a nesting selector `&` with pseudo-selectors.
25
- */
26
- function checkNoAmbiguousPseudos({ context, sourceNode, selector, config, isXcssCall }: CheckArgs) {
27
- if (isXcssCall) {
28
- /**
29
- * The `xcss` API does not currently support a leading `&`.
30
- *
31
- * This may change in the future, but we are ignoring `xcss` calls for now.
32
- */
33
- return;
34
- }
35
-
36
- if (selector.first.type !== 'pseudo') {
37
- return;
38
- }
39
-
40
- const range = getRangeFromNode({ node: selector.first, sourceNode });
41
- const loc = getSourceLocationFromRange(context, range);
42
-
43
- if (config.shouldAlwaysInsertNestingSelectorForAmbiguousPseudos) {
44
- context.report({
45
- loc,
46
- messageId: 'no-ambiguous-pseudos',
47
- *fix(fixer) {
48
- yield fixer.insertTextBeforeRange(range, '&');
49
- },
50
- });
51
- } else {
52
- context.report({
53
- loc,
54
- messageId: 'no-ambiguous-pseudos',
55
- suggest: [
56
- {
57
- messageId: 'insert-nesting-selector',
58
- *fix(fixer) {
59
- yield fixer.insertTextBeforeRange(range, '&');
60
- },
61
- },
62
- ],
63
- });
64
- }
65
- }
66
-
67
- function checkNoRestrictedPseudos({ context, sourceNode, selector }: CheckArgs) {
68
- selector.walkPseudos((pseudo) => {
69
- if (allowedPseudos.has(pseudo.value)) {
70
- return;
71
- }
72
-
73
- /**
74
- * If someone has written `:after` it is allowed,
75
- * they've just used the legacy syntax.
76
- *
77
- * This will be caught by the `no-legacy-pseudo-element-syntax` check.
78
- */
79
- if (legacyPseudoElements.has(pseudo.value) && allowedPseudos.has(`:${pseudo.value}`)) {
80
- return;
81
- }
82
-
83
- if (!allowedPseudos.has(pseudo.value)) {
84
- const range = getRangeFromNode({ node: pseudo, sourceNode });
85
- const loc = getSourceLocationFromRange(context, range);
86
-
87
- context.report({
88
- loc,
89
- messageId: 'no-restricted-pseudos',
90
- data: {
91
- pseudo: pseudo.value,
92
- },
93
- });
94
- }
95
- });
96
- }
97
-
98
- /**
99
- * The original four pseudo-elements have a legacy single colon syntax.
100
- *
101
- * https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements
102
- */
103
- function checkNoLegacyPseudoElementSyntax({ context, sourceNode, selector }: CheckArgs) {
104
- selector.walkPseudos((pseudo) => {
105
- if (legacyPseudoElements.has(pseudo.value)) {
106
- const range = getRangeFromNode({ node: pseudo, sourceNode });
107
- const loc = getSourceLocationFromRange(context, range);
108
-
109
- context.report({
110
- loc,
111
- messageId: 'no-legacy-pseudo-element-syntax',
112
- *fix(fixer) {
113
- yield fixer.insertTextBeforeRange(range, ':');
114
- },
115
- });
116
- }
117
- });
118
- }
119
-
120
- function checkNoIncreasedSpecificity({ context, sourceNode, selector }: CheckArgs) {
121
- selector.walkNesting((nesting) => {
122
- /**
123
- * If it's not the start of a chain, then it's already been reported.
124
- */
125
- if (isNesting(nesting.prev())) {
126
- return;
127
- }
128
-
129
- let next = nesting.next();
130
-
131
- /**
132
- * If it is followed by something other than another nesting selector,
133
- * then it isn't just there to increase specificity — it is actually necessary.
134
- */
135
- if (next && !isNesting(next)) {
136
- return;
137
- }
138
-
139
- let lastNesting = nesting;
140
- while (isNesting(next)) {
141
- lastNesting = next;
142
- next = next.next();
143
- }
144
-
145
- const [start] = getRangeFromNode({ node: nesting, sourceNode });
146
- const [_, end] = getRangeFromNode({ node: lastNesting, sourceNode });
147
-
148
- const loc = getSourceLocationFromRange(context, [start, end]);
149
-
150
- context.report({
151
- loc,
152
- messageId: 'no-increased-specificity',
153
- });
154
- });
155
- }
@@ -1,55 +0,0 @@
1
- import type { AST, Rule } from 'eslint';
2
- import type * as ESTree from 'eslint-codemod-utils';
3
-
4
- import { type Node as SelectorNode } from 'postcss-selector-parser';
5
-
6
- type Range = [number, number];
7
-
8
- export function getRangeFromNode({
9
- node,
10
- sourceNode,
11
- }: {
12
- /**
13
- * The selector AST node to get the range of.
14
- */
15
- node: SelectorNode & { value: string };
16
- /**
17
- * The source ESTree node whose value was parsed to obtain the selector AST.
18
- *
19
- * The range is calculated relative to this node's range.
20
- */
21
- sourceNode: ESTree.Node;
22
- }): Range {
23
- /**
24
- * The adjustment required to align the selector AST source position
25
- * with the ESTree source position.
26
- */
27
- const adjustment =
28
- /**
29
- * Using a non-null `!` is safe here because ESLint requires the parser to
30
- * include a range value on nodes.
31
- *
32
- * https://eslint.org/docs/latest/extend/custom-parsers#all-nodes
33
- */
34
- sourceNode.range![0] +
35
- /**
36
- * We add +1 if it's a literal to account for the opening `'` or `"` character.
37
- */
38
- (sourceNode.type === 'Literal' ? 1 : 0);
39
-
40
- const start = adjustment + node.sourceIndex;
41
- const end = start + node.value.length;
42
-
43
- return [start, end];
44
- }
45
-
46
- export function getSourceLocationFromRange(
47
- context: Rule.RuleContext,
48
- [start, end]: Range,
49
- ): AST.SourceLocation {
50
- const sourceCode = context.getSourceCode();
51
- return {
52
- start: sourceCode.getLocFromIndex(start),
53
- end: sourceCode.getLocFromIndex(end),
54
- };
55
- }
@@ -1,226 +0,0 @@
1
- /**
2
- * This util is intentionally not in `@atlaskit/eslint-utils` at this time.
3
- *
4
- * This is because the API is likely to change and should not be considered stable.
5
- */
6
-
7
- import type { Rule } from 'eslint';
8
- import type * as ESTree from 'eslint-codemod-utils';
9
-
10
- type CssMapVisitorArgs =
11
- | {
12
- type: 'variant' | 'declaration' | 'ruleset' | 'selectors';
13
- node: ESTree.Property;
14
- }
15
- | { type: 'grouped-at-rules'; node: ESTree.Property; atRule: string };
16
-
17
- type CssMapVisitor = (args: CssMapVisitorArgs) => void;
18
-
19
- export function walkCssMap({
20
- context,
21
- importSources,
22
- visitor,
23
- }: {
24
- context: Rule.RuleContext;
25
- program: ESTree.Program;
26
- importSources: string[];
27
- visitor: CssMapVisitor;
28
- }) {
29
- const program = context.getSourceCode().ast;
30
-
31
- const importDeclaration = program.body.find(
32
- (node): node is ESTree.ImportDeclaration =>
33
- node.type === 'ImportDeclaration' && importSources.includes(node.source.value as string),
34
- );
35
-
36
- const specifier = importDeclaration?.specifiers.find(
37
- (specifier) => specifier.type === 'ImportSpecifier' && specifier.imported.name === 'cssMap',
38
- );
39
-
40
- if (!specifier) {
41
- return;
42
- }
43
-
44
- const [variable] = context.getSourceCode().scopeManager.getDeclaredVariables(specifier);
45
-
46
- if (!variable) {
47
- return;
48
- }
49
-
50
- variable.references.forEach((reference) => {
51
- const identifier = reference.identifier as ESTree.Identifier & Rule.NodeParentExtension;
52
-
53
- const { parent } = identifier;
54
- if (parent.type !== 'CallExpression') {
55
- return;
56
- }
57
-
58
- const variantMap = parent.arguments[0];
59
- if (variantMap.type !== 'ObjectExpression') {
60
- return;
61
- }
62
-
63
- walkCssMapVariantMap({ variantMap, visitor });
64
- });
65
- }
66
-
67
- function walkCssMapVariantMap({
68
- variantMap,
69
- visitor,
70
- }: {
71
- variantMap: ESTree.ObjectExpression;
72
- visitor: CssMapVisitor;
73
- }) {
74
- variantMap.properties.forEach((property) => {
75
- if (property.type !== 'Property') {
76
- return;
77
- }
78
-
79
- walkCssMapVariant({ variant: property, visitor });
80
- });
81
- }
82
-
83
- function walkCssMapVariant({
84
- variant,
85
- visitor,
86
- }: {
87
- variant: ESTree.Property;
88
- visitor: CssMapVisitor;
89
- }) {
90
- visitor({
91
- type: 'variant',
92
- node: variant,
93
- });
94
-
95
- if (variant.value.type !== 'ObjectExpression') {
96
- return;
97
- }
98
-
99
- variant.value.properties.forEach((property) => {
100
- if (property.type !== 'Property') {
101
- return;
102
- }
103
-
104
- if (property.value.type !== 'ObjectExpression') {
105
- visitor({
106
- type: 'declaration',
107
- node: property,
108
- });
109
- return;
110
- }
111
-
112
- const keyValue = getKeyValue(property.key);
113
-
114
- if (keyValue && /^@[A-z-]+$/.test(keyValue)) {
115
- walkCssMapAtRuleGrouping({ atRuleGrouping: property, visitor });
116
- return;
117
- }
118
-
119
- if (keyValue === 'selectors') {
120
- walkCssMapSelectors({ selectors: property, visitor });
121
- }
122
-
123
- walkStyleRuleset({ ruleset: property, visitor });
124
- });
125
- }
126
-
127
- function walkCssMapAtRuleGrouping({
128
- atRuleGrouping,
129
- visitor,
130
- }: {
131
- atRuleGrouping: ESTree.Property;
132
- visitor: CssMapVisitor;
133
- }) {
134
- const atRule = getKeyValue(atRuleGrouping.key);
135
- if (!atRule) {
136
- return;
137
- }
138
-
139
- visitor({
140
- type: 'grouped-at-rules',
141
- node: atRuleGrouping,
142
- atRule,
143
- });
144
-
145
- if (atRuleGrouping.value.type !== 'ObjectExpression') {
146
- return;
147
- }
148
-
149
- atRuleGrouping.value.properties.forEach((property) => {
150
- if (property.type !== 'Property') {
151
- return;
152
- }
153
-
154
- walkStyleRuleset({ ruleset: property, visitor });
155
- });
156
- }
157
-
158
- function walkCssMapSelectors({
159
- selectors,
160
- visitor,
161
- }: {
162
- selectors: ESTree.Property;
163
- visitor: CssMapVisitor;
164
- }) {
165
- visitor({
166
- type: 'selectors',
167
- node: selectors,
168
- });
169
-
170
- if (selectors.value.type !== 'ObjectExpression') {
171
- return;
172
- }
173
-
174
- selectors.value.properties.forEach((property) => {
175
- if (property.type !== 'Property') {
176
- return;
177
- }
178
-
179
- walkStyleRuleset({ ruleset: property, visitor });
180
- });
181
- }
182
-
183
- function walkStyleRuleset({
184
- ruleset,
185
- visitor,
186
- }: {
187
- ruleset: ESTree.Property;
188
- visitor: CssMapVisitor;
189
- }) {
190
- visitor({
191
- type: 'ruleset',
192
- node: ruleset,
193
- });
194
-
195
- if (ruleset.value.type !== 'ObjectExpression') {
196
- return;
197
- }
198
-
199
- ruleset.value.properties.forEach((property) => {
200
- if (property.type !== 'Property') {
201
- return;
202
- }
203
-
204
- if (property.value.type === 'ObjectExpression') {
205
- walkStyleRuleset({ ruleset: property, visitor });
206
- return;
207
- }
208
-
209
- visitor({
210
- type: 'declaration',
211
- node: property,
212
- });
213
- });
214
- }
215
-
216
- function getKeyValue(key: ESTree.Property['key']): string | null {
217
- if (key.type === 'Identifier') {
218
- return key.name;
219
- }
220
-
221
- if (key.type === 'Literal') {
222
- return key.value?.toString() ?? null;
223
- }
224
-
225
- return null;
226
- }