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

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 (95) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/constellation/index/migration-guide.mdx +309 -0
  3. package/constellation/index/overview.mdx +4 -0
  4. package/constellation/index/props.mdx +9 -5
  5. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
  6. package/index.js +9 -9
  7. package/package.json +3 -3
  8. package/scripts/codegen.tsx +223 -284
  9. package/src/rules/__tests__/utils/_tester.tsx +24 -24
  10. package/src/rules/atlaskit-theme/README.md +21 -19
  11. package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
  12. package/src/rules/atlaskit-theme/index.ts +107 -115
  13. package/src/rules/convert-props-syntax/README.md +32 -22
  14. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
  15. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
  16. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
  17. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
  18. package/src/rules/convert-props-syntax/index.tsx +356 -386
  19. package/src/rules/enforce-style-prop/README.md +21 -18
  20. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
  21. package/src/rules/enforce-style-prop/index.tsx +144 -160
  22. package/src/rules/external-rules.tsx +163 -164
  23. package/src/rules/local-cx-xcss/README.md +10 -6
  24. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
  25. package/src/rules/local-cx-xcss/index.tsx +46 -56
  26. package/src/rules/no-array-arguments/README.md +2 -1
  27. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
  28. package/src/rules/no-array-arguments/index.tsx +68 -69
  29. package/src/rules/no-classname-prop/README.md +7 -4
  30. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
  31. package/src/rules/no-classname-prop/index.tsx +25 -25
  32. package/src/rules/no-container-queries/README.md +11 -9
  33. package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
  34. package/src/rules/no-container-queries/index.tsx +75 -81
  35. package/src/rules/no-dynamic-styles/README.md +16 -19
  36. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
  37. package/src/rules/no-dynamic-styles/index.tsx +45 -48
  38. package/src/rules/no-exported-styles/README.md +17 -12
  39. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
  40. package/src/rules/no-exported-styles/index.tsx +36 -36
  41. package/src/rules/no-global-styles/README.md +15 -14
  42. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
  43. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
  44. package/src/rules/no-global-styles/index.tsx +59 -62
  45. package/src/rules/no-important-styles/README.md +5 -3
  46. package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
  47. package/src/rules/no-important-styles/index.tsx +49 -49
  48. package/src/rules/no-imported-style-values/README.md +23 -15
  49. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
  50. package/src/rules/no-imported-style-values/index.tsx +116 -120
  51. package/src/rules/no-nested-selectors/README.md +15 -13
  52. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +128 -128
  53. package/src/rules/no-styled/README.md +14 -9
  54. package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
  55. package/src/rules/no-styled/index.tsx +34 -41
  56. package/src/rules/no-unsafe-selectors/README.md +103 -93
  57. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
  58. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
  59. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
  60. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
  61. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
  62. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
  63. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
  64. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
  65. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
  66. package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
  67. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
  68. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
  69. package/src/rules/no-unsafe-values/README.md +34 -40
  70. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
  71. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
  72. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
  73. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
  74. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
  75. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
  76. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
  77. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
  78. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
  79. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
  80. package/src/rules/no-unsafe-values/index.tsx +76 -78
  81. package/src/rules/no-unsafe-values/messages.tsx +12 -12
  82. package/src/rules/no-unsafe-values/utils.tsx +370 -382
  83. package/src/rules/use-compiled/README.md +11 -8
  84. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
  85. package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
  86. package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
  87. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
  88. package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
  89. package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
  90. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
  91. package/src/rules/use-compiled/index.tsx +203 -226
  92. package/src/rules/use-compiled/is-safe.tsx +113 -117
  93. package/src/rules/use-compiled/utils.tsx +14 -18
  94. package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
  95. package/src/rules/utils/create-rule.tsx +3 -6
@@ -1,10 +1,5 @@
1
1
  import type { Rule } from 'eslint';
2
- import {
3
- identifier,
4
- importDeclaration,
5
- importSpecifier,
6
- literal,
7
- } from 'eslint-codemod-utils';
2
+ import { identifier, importDeclaration, importSpecifier, literal } from 'eslint-codemod-utils';
8
3
  import type * as ESTree from 'eslint-codemod-utils';
9
4
  import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
10
5
 
@@ -12,264 +7,246 @@ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
12
7
 
13
8
  import { createLintRule } from '../utils/create-rule';
14
9
  import {
15
- getFirstImportFromSource,
16
- isImportDefaultSpecifier,
17
- isImportNamespaceSpecifier,
18
- isImportSpecifier,
10
+ getFirstImportFromSource,
11
+ isImportDefaultSpecifier,
12
+ isImportNamespaceSpecifier,
13
+ isImportSpecifier,
19
14
  } from './utils';
20
15
  import { isSafeStyled, isSafeUsage } from './is-safe';
21
16
 
22
- const SUPPORTED_EMOTION_IMPORTS = new Set([
23
- 'css',
24
- 'keyframes',
25
- 'ClassNames',
26
- 'jsx',
27
- ]);
17
+ const SUPPORTED_EMOTION_IMPORTS = new Set(['css', 'keyframes', 'ClassNames', 'jsx']);
28
18
 
29
19
  const SUPPORTED_IMPORT_MAP = {
30
- '@emotion/core': SUPPORTED_EMOTION_IMPORTS,
31
- '@emotion/react': SUPPORTED_EMOTION_IMPORTS,
32
- '@emotion/styled': new Set(['default']),
33
- 'styled-components': new Set(['default', 'css', 'keyframes']),
20
+ '@emotion/core': SUPPORTED_EMOTION_IMPORTS,
21
+ '@emotion/react': SUPPORTED_EMOTION_IMPORTS,
22
+ '@emotion/styled': new Set(['default']),
23
+ 'styled-components': new Set(['default', 'css', 'keyframes']),
34
24
  };
35
25
 
36
26
  type SupportedImportSource = keyof typeof SUPPORTED_IMPORT_MAP;
37
27
 
38
- function isSupportedImportSource(
39
- importSource: string,
40
- ): importSource is SupportedImportSource {
41
- return importSource in SUPPORTED_IMPORT_MAP;
28
+ function isSupportedImportSource(importSource: string): importSource is SupportedImportSource {
29
+ return importSource in SUPPORTED_IMPORT_MAP;
42
30
  }
43
31
 
44
32
  const schema = {
45
- type: 'array',
46
- items: [
47
- {
48
- type: 'object',
49
- properties: {
50
- canAutoFix: {
51
- type: 'boolean',
52
- },
53
- },
54
- additionalProperties: false,
55
- },
56
- ],
33
+ type: 'array',
34
+ items: [
35
+ {
36
+ type: 'object',
37
+ properties: {
38
+ canAutoFix: {
39
+ type: 'boolean',
40
+ },
41
+ },
42
+ additionalProperties: false,
43
+ },
44
+ ],
57
45
  } as const satisfies JSONSchema;
58
46
 
59
47
  type Config = {
60
- canAutoFix: boolean;
48
+ canAutoFix: boolean;
61
49
  };
62
50
 
63
51
  const defaultConfig: Config = {
64
- canAutoFix: false,
52
+ canAutoFix: false,
65
53
  };
66
54
 
67
55
  function readConfig(context: Rule.RuleContext): Config {
68
- const [config] = context.options as FromSchema<typeof schema>;
69
- return Object.assign({}, defaultConfig, config);
56
+ const [config] = context.options as FromSchema<typeof schema>;
57
+ return Object.assign({}, defaultConfig, config);
70
58
  }
71
59
 
72
60
  export const rule: Rule.RuleModule = createLintRule({
73
- meta: {
74
- name: 'use-compiled',
75
- type: 'problem',
76
- fixable: 'code',
77
- docs: {
78
- description:
79
- 'Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries',
80
- recommended: true,
81
- severity: 'warn',
82
- },
83
- messages: {
84
- 'use-compiled': `Use '@compiled/react' instead of '{{importSource}}'`,
85
- },
86
- schema,
87
- },
88
- create(context) {
89
- const config = readConfig(context);
90
-
91
- return {
92
- Program() {
93
- const pragma = context
94
- .getSourceCode()
95
- .getAllComments()
96
- .find((n) => n.value.includes('@jsxImportSource @emotion/react'));
97
-
98
- if (!pragma) {
99
- return;
100
- }
101
-
102
- context.report({
103
- messageId: 'use-compiled',
104
- data: {
105
- importSource: '@emotion/react',
106
- },
107
- loc: pragma.loc!,
108
- *fix(fixer) {
109
- if (!config.canAutoFix) {
110
- return;
111
- }
112
-
113
- const importDeclarations = context
114
- .getSourceCode()
115
- .ast.body.filter(
116
- (node): node is ESTree.ImportDeclaration =>
117
- node.type === 'ImportDeclaration',
118
- );
119
-
120
- const hasEmotionImport = importDeclarations.some(
121
- (importDeclaration) => {
122
- const importSource = importDeclaration.source.value as string;
123
- return importSource.startsWith('@emotion/');
124
- },
125
- );
126
- const hasCompiledImport = importDeclarations.some(
127
- (importDeclaration) =>
128
- importDeclaration.source.value === '@compiled/react',
129
- );
130
-
131
- if (hasEmotionImport || !hasCompiledImport) {
132
- // Considering this unsafe to auto-fix
133
- return;
134
- }
135
-
136
- yield fixer.replaceText(
137
- // @ts-expect-error - types don't accept comment nodes
138
- pragma,
139
- `/** @jsxImportSource @compiled/react */`,
140
- );
141
- },
142
- });
143
- },
144
- ImportDeclaration(node: ESTree.ImportDeclaration) {
145
- const importSource = node.source.value as string;
146
- if (!isSupportedImportSource(importSource)) {
147
- return;
148
- }
149
-
150
- context.report({
151
- node: node.source,
152
- messageId: 'use-compiled',
153
- data: {
154
- importSource,
155
- },
156
- *fix(fixer) {
157
- if (!config.canAutoFix) {
158
- return;
159
- }
160
-
161
- const specifiers = getSpecifiers({ context, node, importSource });
162
-
163
- if (!specifiers) {
164
- // Cannot safely auto-fix
165
- return;
166
- }
167
-
168
- yield* fixImport({
169
- context,
170
- fixer,
171
- node,
172
- specifiers,
173
- });
174
- },
175
- });
176
- },
177
- };
178
- },
61
+ meta: {
62
+ name: 'use-compiled',
63
+ type: 'problem',
64
+ fixable: 'code',
65
+ docs: {
66
+ description: 'Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries',
67
+ recommended: true,
68
+ severity: 'warn',
69
+ },
70
+ messages: {
71
+ 'use-compiled': `Use '@compiled/react' instead of '{{importSource}}'`,
72
+ },
73
+ schema,
74
+ },
75
+ create(context) {
76
+ const config = readConfig(context);
77
+
78
+ return {
79
+ Program() {
80
+ const pragma = context
81
+ .getSourceCode()
82
+ .getAllComments()
83
+ .find((n) => n.value.includes('@jsxImportSource @emotion/react'));
84
+
85
+ if (!pragma) {
86
+ return;
87
+ }
88
+
89
+ context.report({
90
+ messageId: 'use-compiled',
91
+ data: {
92
+ importSource: '@emotion/react',
93
+ },
94
+ loc: pragma.loc!,
95
+ *fix(fixer) {
96
+ if (!config.canAutoFix) {
97
+ return;
98
+ }
99
+
100
+ const importDeclarations = context
101
+ .getSourceCode()
102
+ .ast.body.filter(
103
+ (node): node is ESTree.ImportDeclaration => node.type === 'ImportDeclaration',
104
+ );
105
+
106
+ const hasEmotionImport = importDeclarations.some((importDeclaration) => {
107
+ const importSource = importDeclaration.source.value as string;
108
+ return importSource.startsWith('@emotion/');
109
+ });
110
+ const hasCompiledImport = importDeclarations.some(
111
+ (importDeclaration) => importDeclaration.source.value === '@compiled/react',
112
+ );
113
+
114
+ if (hasEmotionImport || !hasCompiledImport) {
115
+ // Considering this unsafe to auto-fix
116
+ return;
117
+ }
118
+
119
+ yield fixer.replaceText(
120
+ // @ts-expect-error - types don't accept comment nodes
121
+ pragma,
122
+ `/** @jsxImportSource @compiled/react */`,
123
+ );
124
+ },
125
+ });
126
+ },
127
+ ImportDeclaration(node: ESTree.ImportDeclaration) {
128
+ const importSource = node.source.value as string;
129
+ if (!isSupportedImportSource(importSource)) {
130
+ return;
131
+ }
132
+
133
+ context.report({
134
+ node: node.source,
135
+ messageId: 'use-compiled',
136
+ data: {
137
+ importSource,
138
+ },
139
+ *fix(fixer) {
140
+ if (!config.canAutoFix) {
141
+ return;
142
+ }
143
+
144
+ const specifiers = getSpecifiers({ context, node, importSource });
145
+
146
+ if (!specifiers) {
147
+ // Cannot safely auto-fix
148
+ return;
149
+ }
150
+
151
+ yield* fixImport({
152
+ context,
153
+ fixer,
154
+ node,
155
+ specifiers,
156
+ });
157
+ },
158
+ });
159
+ },
160
+ };
161
+ },
179
162
  });
180
163
 
181
164
  export default rule;
182
165
 
183
166
  function getSpecifiers({
184
- context,
185
- node,
186
- importSource,
167
+ context,
168
+ node,
169
+ importSource,
187
170
  }: {
188
- context: Rule.RuleContext;
189
- node: ESTree.ImportDeclaration;
190
- importSource: SupportedImportSource;
171
+ context: Rule.RuleContext;
172
+ node: ESTree.ImportDeclaration;
173
+ importSource: SupportedImportSource;
191
174
  }) {
192
- const supportedImports = SUPPORTED_IMPORT_MAP[importSource];
193
-
194
- // We aren't supporting namespace imports
195
- if (node.specifiers.some(isImportNamespaceSpecifier)) {
196
- return;
197
- }
198
-
199
- const defaultSpecifier = node.specifiers.find(isImportDefaultSpecifier);
200
- if (defaultSpecifier) {
201
- if (!supportedImports.has('default')) {
202
- return;
203
- }
204
- const styled = findVariable({
205
- identifier: defaultSpecifier.local,
206
- sourceCode: context.getSourceCode(),
207
- });
208
- if (!styled || !isSafeStyled(styled)) {
209
- return;
210
- }
211
- }
212
-
213
- const namedSpecifiers = node.specifiers.filter(isImportSpecifier);
214
- if (
215
- !namedSpecifiers.every((specifier) =>
216
- isSafeUsage({
217
- context,
218
- specifier,
219
- supportedImports,
220
- }),
221
- )
222
- ) {
223
- return;
224
- }
225
-
226
- if (defaultSpecifier) {
227
- return [
228
- ...namedSpecifiers,
229
- // ASSUMPTION: the default import, if allowed, is for `styled`
230
- importSpecifier({
231
- imported: identifier('styled'),
232
- local: node.specifiers[0].local,
233
- }),
234
- ];
235
- }
236
-
237
- return namedSpecifiers;
175
+ const supportedImports = SUPPORTED_IMPORT_MAP[importSource];
176
+
177
+ // We aren't supporting namespace imports
178
+ if (node.specifiers.some(isImportNamespaceSpecifier)) {
179
+ return;
180
+ }
181
+
182
+ const defaultSpecifier = node.specifiers.find(isImportDefaultSpecifier);
183
+ if (defaultSpecifier) {
184
+ if (!supportedImports.has('default')) {
185
+ return;
186
+ }
187
+ const styled = findVariable({
188
+ identifier: defaultSpecifier.local,
189
+ sourceCode: context.getSourceCode(),
190
+ });
191
+ if (!styled || !isSafeStyled(styled)) {
192
+ return;
193
+ }
194
+ }
195
+
196
+ const namedSpecifiers = node.specifiers.filter(isImportSpecifier);
197
+ if (
198
+ !namedSpecifiers.every((specifier) =>
199
+ isSafeUsage({
200
+ context,
201
+ specifier,
202
+ supportedImports,
203
+ }),
204
+ )
205
+ ) {
206
+ return;
207
+ }
208
+
209
+ if (defaultSpecifier) {
210
+ return [
211
+ ...namedSpecifiers,
212
+ // ASSUMPTION: the default import, if allowed, is for `styled`
213
+ importSpecifier({
214
+ imported: identifier('styled'),
215
+ local: node.specifiers[0].local,
216
+ }),
217
+ ];
218
+ }
219
+
220
+ return namedSpecifiers;
238
221
  }
239
222
 
240
223
  function* fixImport({
241
- context,
242
- fixer,
243
- node,
244
- specifiers,
224
+ context,
225
+ fixer,
226
+ node,
227
+ specifiers,
245
228
  }: {
246
- context: Rule.RuleContext;
247
- fixer: Rule.RuleFixer;
248
- node: ESTree.ImportDeclaration;
249
- specifiers: ESTree.ImportSpecifier[];
229
+ context: Rule.RuleContext;
230
+ fixer: Rule.RuleFixer;
231
+ node: ESTree.ImportDeclaration;
232
+ specifiers: ESTree.ImportSpecifier[];
250
233
  }): Generator<Rule.Fix> {
251
- const compiledImport = getFirstImportFromSource(context, '@compiled/react');
252
-
253
- if (compiledImport) {
254
- yield fixer.remove(node);
255
- yield fixer.replaceText(
256
- compiledImport,
257
- buildImportDeclaration(
258
- compiledImport.specifiers.concat(specifiers),
259
- '@compiled/react',
260
- ),
261
- );
262
- } else {
263
- yield fixer.replaceText(
264
- node,
265
- buildImportDeclaration(specifiers, '@compiled/react'),
266
- );
267
- }
234
+ const compiledImport = getFirstImportFromSource(context, '@compiled/react');
235
+
236
+ if (compiledImport) {
237
+ yield fixer.remove(node);
238
+ yield fixer.replaceText(
239
+ compiledImport,
240
+ buildImportDeclaration(compiledImport.specifiers.concat(specifiers), '@compiled/react'),
241
+ );
242
+ } else {
243
+ yield fixer.replaceText(node, buildImportDeclaration(specifiers, '@compiled/react'));
244
+ }
268
245
  }
269
246
 
270
247
  function buildImportDeclaration(
271
- specifiers: ESTree.ImportDeclaration['specifiers'],
272
- source: string,
248
+ specifiers: ESTree.ImportDeclaration['specifiers'],
249
+ source: string,
273
250
  ) {
274
- return `${importDeclaration({ specifiers, source: literal(source) })};`;
251
+ return `${importDeclaration({ specifiers, source: literal(source) })};`;
275
252
  }