@atlaskit/eslint-plugin-ui-styling-standard 0.11.6 → 0.13.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 (61) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +2 -0
  3. package/constellation/index/overview.mdx +3 -1
  4. package/constellation/no-global-styles/usage.mdx +59 -0
  5. package/constellation/use-compiled/usage.mdx +54 -0
  6. package/dist/cjs/presets/all.codegen.js +3 -1
  7. package/dist/cjs/presets/recommended.codegen.js +3 -1
  8. package/dist/cjs/rules/index.codegen.js +6 -2
  9. package/dist/cjs/rules/no-global-styles/index.js +100 -0
  10. package/dist/cjs/rules/use-compiled/index.js +257 -0
  11. package/dist/cjs/rules/use-compiled/is-safe.js +130 -0
  12. package/dist/cjs/rules/use-compiled/utils.js +26 -0
  13. package/dist/es2019/presets/all.codegen.js +3 -1
  14. package/dist/es2019/presets/recommended.codegen.js +3 -1
  15. package/dist/es2019/rules/index.codegen.js +6 -2
  16. package/dist/es2019/rules/no-global-styles/index.js +67 -0
  17. package/dist/es2019/rules/use-compiled/index.js +182 -0
  18. package/dist/es2019/rules/use-compiled/is-safe.js +118 -0
  19. package/dist/es2019/rules/use-compiled/utils.js +15 -0
  20. package/dist/esm/presets/all.codegen.js +3 -1
  21. package/dist/esm/presets/recommended.codegen.js +3 -1
  22. package/dist/esm/rules/index.codegen.js +6 -2
  23. package/dist/esm/rules/no-global-styles/index.js +94 -0
  24. package/dist/esm/rules/use-compiled/index.js +250 -0
  25. package/dist/esm/rules/use-compiled/is-safe.js +121 -0
  26. package/dist/esm/rules/use-compiled/utils.js +17 -0
  27. package/dist/types/index.codegen.d.ts +4 -0
  28. package/dist/types/presets/all.codegen.d.ts +3 -1
  29. package/dist/types/presets/recommended.codegen.d.ts +3 -1
  30. package/dist/types/rules/index.codegen.d.ts +2 -0
  31. package/dist/types/rules/no-global-styles/index.d.ts +2 -0
  32. package/dist/types/rules/use-compiled/index.d.ts +3 -0
  33. package/dist/types/rules/use-compiled/is-safe.d.ts +10 -0
  34. package/dist/types/rules/use-compiled/utils.d.ts +6 -0
  35. package/dist/types-ts4.5/index.codegen.d.ts +4 -0
  36. package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
  37. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
  38. package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
  39. package/dist/types-ts4.5/rules/no-global-styles/index.d.ts +2 -0
  40. package/dist/types-ts4.5/rules/use-compiled/index.d.ts +3 -0
  41. package/dist/types-ts4.5/rules/use-compiled/is-safe.d.ts +10 -0
  42. package/dist/types-ts4.5/rules/use-compiled/utils.d.ts +6 -0
  43. package/package.json +3 -2
  44. package/src/presets/all.codegen.tsx +3 -1
  45. package/src/presets/recommended.codegen.tsx +3 -1
  46. package/src/rules/index.codegen.tsx +5 -1
  47. package/src/rules/no-global-styles/README.md +56 -0
  48. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +33 -0
  49. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +46 -0
  50. package/src/rules/no-global-styles/index.tsx +85 -0
  51. package/src/rules/use-compiled/README.md +49 -0
  52. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +73 -0
  53. package/src/rules/use-compiled/__tests__/css.test.tsx +126 -0
  54. package/src/rules/use-compiled/__tests__/imports.test.tsx +163 -0
  55. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +140 -0
  56. package/src/rules/use-compiled/__tests__/pragma.test.tsx +108 -0
  57. package/src/rules/use-compiled/__tests__/styled.test.tsx +114 -0
  58. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +15 -0
  59. package/src/rules/use-compiled/index.tsx +275 -0
  60. package/src/rules/use-compiled/is-safe.tsx +163 -0
  61. package/src/rules/use-compiled/utils.tsx +37 -0
@@ -0,0 +1,275 @@
1
+ import type { Rule } from 'eslint';
2
+ import {
3
+ identifier,
4
+ importDeclaration,
5
+ importSpecifier,
6
+ literal,
7
+ } from 'eslint-codemod-utils';
8
+ import type * as ESTree from 'eslint-codemod-utils';
9
+ import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
10
+
11
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
12
+
13
+ import { createLintRule } from '../utils/create-rule';
14
+ import {
15
+ getFirstImportFromSource,
16
+ isImportDefaultSpecifier,
17
+ isImportNamespaceSpecifier,
18
+ isImportSpecifier,
19
+ } from './utils';
20
+ import { isSafeStyled, isSafeUsage } from './is-safe';
21
+
22
+ const SUPPORTED_EMOTION_IMPORTS = new Set([
23
+ 'css',
24
+ 'keyframes',
25
+ 'ClassNames',
26
+ 'jsx',
27
+ ]);
28
+
29
+ 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']),
34
+ };
35
+
36
+ type SupportedImportSource = keyof typeof SUPPORTED_IMPORT_MAP;
37
+
38
+ function isSupportedImportSource(
39
+ importSource: string,
40
+ ): importSource is SupportedImportSource {
41
+ return importSource in SUPPORTED_IMPORT_MAP;
42
+ }
43
+
44
+ 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
+ ],
57
+ } as const satisfies JSONSchema;
58
+
59
+ type Config = {
60
+ canAutoFix: boolean;
61
+ };
62
+
63
+ const defaultConfig: Config = {
64
+ canAutoFix: false,
65
+ };
66
+
67
+ function readConfig(context: Rule.RuleContext): Config {
68
+ const [config] = context.options as FromSchema<typeof schema>;
69
+ return Object.assign({}, defaultConfig, config);
70
+ }
71
+
72
+ 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
+ },
179
+ });
180
+
181
+ export default rule;
182
+
183
+ function getSpecifiers({
184
+ context,
185
+ node,
186
+ importSource,
187
+ }: {
188
+ context: Rule.RuleContext;
189
+ node: ESTree.ImportDeclaration;
190
+ importSource: SupportedImportSource;
191
+ }) {
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;
238
+ }
239
+
240
+ function* fixImport({
241
+ context,
242
+ fixer,
243
+ node,
244
+ specifiers,
245
+ }: {
246
+ context: Rule.RuleContext;
247
+ fixer: Rule.RuleFixer;
248
+ node: ESTree.ImportDeclaration;
249
+ specifiers: ESTree.ImportSpecifier[];
250
+ }): 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
+ }
268
+ }
269
+
270
+ function buildImportDeclaration(
271
+ specifiers: ESTree.ImportDeclaration['specifiers'],
272
+ source: string,
273
+ ) {
274
+ return `${importDeclaration({ specifiers, source: literal(source) })};`;
275
+ }
@@ -0,0 +1,163 @@
1
+ import type { Rule, Scope } from 'eslint';
2
+ import { closestOfType } from 'eslint-codemod-utils';
3
+ import type * as ESTree from 'eslint-codemod-utils';
4
+
5
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
6
+
7
+ export function isSafeUsage({
8
+ context,
9
+ specifier,
10
+ supportedImports,
11
+ }: {
12
+ context: Rule.RuleContext;
13
+ specifier: ESTree.ImportSpecifier;
14
+ supportedImports: Set<string>;
15
+ }): boolean {
16
+ const importName = specifier.imported.name;
17
+
18
+ if (!supportedImports.has(importName)) {
19
+ return false;
20
+ }
21
+
22
+ if (importName === 'css') {
23
+ const css = findVariable({
24
+ identifier: specifier.local,
25
+ sourceCode: context.getSourceCode(),
26
+ });
27
+ return css !== null && isSafeCss(css);
28
+ }
29
+
30
+ if (importName === 'keyframes') {
31
+ const keyframes = findVariable({
32
+ identifier: specifier.local,
33
+ sourceCode: context.getSourceCode(),
34
+ });
35
+ return keyframes !== null && isSafeKeyframes(keyframes);
36
+ }
37
+
38
+ return true;
39
+ }
40
+
41
+ export function isSafeKeyframes(keyframes: Scope.Variable): boolean {
42
+ return keyframes.references.every((ref) => {
43
+ const callExpression = closestOfType(ref.identifier, 'CallExpression');
44
+ if (!callExpression) {
45
+ return false;
46
+ }
47
+
48
+ if (callExpression.callee !== ref.identifier) {
49
+ return false;
50
+ }
51
+
52
+ if (callExpression.arguments.length !== 1) {
53
+ return false;
54
+ }
55
+
56
+ const argument = callExpression.arguments[0];
57
+ if (argument.type !== 'ObjectExpression') {
58
+ return false;
59
+ }
60
+
61
+ return argument.properties.every((property) =>
62
+ isSafeProperty(property, isSafeKeyframePropertyValue),
63
+ );
64
+ });
65
+ }
66
+
67
+ function isSafeKeyframePropertyValue(value: ESTree.Property['value']): boolean {
68
+ if (value.type !== 'ObjectExpression') {
69
+ return false;
70
+ }
71
+ return isSafeStyleObject(value);
72
+ }
73
+
74
+ export function isSafeCss(css: Scope.Variable): boolean {
75
+ return css.references.every((ref) => {
76
+ const callExpression = closestOfType(ref.identifier, 'CallExpression');
77
+ if (!callExpression) {
78
+ return false;
79
+ }
80
+
81
+ if (callExpression.callee !== ref.identifier) {
82
+ return false;
83
+ }
84
+
85
+ return callExpression.arguments.every(isSafeStyleArgument);
86
+ });
87
+ }
88
+
89
+ export function isSafeStyled(styled: Scope.Variable): boolean {
90
+ return styled.references.every((ref) => {
91
+ const callExpression = closestOfType(ref.identifier, 'CallExpression');
92
+ if (!callExpression) {
93
+ return false;
94
+ }
95
+
96
+ /**
97
+ * If it's not in the form `styled.tagName()`
98
+ * then we consider it unsafe to auto-fix.
99
+ */
100
+ if (
101
+ callExpression.callee.type !== 'MemberExpression' ||
102
+ callExpression.callee.object !== ref.identifier
103
+ ) {
104
+ return false;
105
+ }
106
+
107
+ return callExpression.arguments.every(isSafeStyleArgument);
108
+ });
109
+ }
110
+
111
+ /**
112
+ * Determines if a style argument is safe for auto-fix conversion.
113
+ */
114
+ function isSafeStyleArgument(
115
+ arg: ESTree.Expression | ESTree.SpreadElement,
116
+ ): boolean {
117
+ if (arg.type === 'ObjectExpression') {
118
+ return isSafeStyleObject(arg);
119
+ }
120
+
121
+ return false;
122
+ }
123
+
124
+ function isSafeStyleObject(object: ESTree.ObjectExpression): boolean {
125
+ return object.properties.every((property) =>
126
+ isSafeProperty(property, isSafeStyleValue),
127
+ );
128
+ }
129
+
130
+ function isSafeStyleValue(value: ESTree.Expression | ESTree.Pattern): boolean {
131
+ if (value.type === 'Literal') {
132
+ return true;
133
+ }
134
+
135
+ return false;
136
+ }
137
+
138
+ function isSafeProperty(
139
+ property: ESTree.Property | ESTree.SpreadElement,
140
+ isSafePropertyValue: (value: ESTree.Property['value']) => boolean,
141
+ ): boolean {
142
+ if (property.type === 'SpreadElement') {
143
+ return false;
144
+ }
145
+
146
+ if (property.method || property.shorthand || property.kind !== 'init') {
147
+ return false;
148
+ }
149
+
150
+ if (!hasSafeKey(property)) {
151
+ return false;
152
+ }
153
+
154
+ return isSafePropertyValue(property.value);
155
+ }
156
+
157
+ function hasSafeKey(property: ESTree.Property): boolean {
158
+ if (property.computed) {
159
+ return property.key.type === 'Literal';
160
+ }
161
+
162
+ return true;
163
+ }
@@ -0,0 +1,37 @@
1
+ import type { Rule } from 'eslint';
2
+
3
+ import type * as ESTree from 'eslint-codemod-utils';
4
+
5
+ export function getFirstImportFromSource(
6
+ context: Rule.RuleContext,
7
+ source: string,
8
+ ): ESTree.ImportDeclaration | undefined {
9
+ return context
10
+ .getSourceCode()
11
+ .ast.body.filter(isImportDeclaration)
12
+ .find((node) => node.source.value === source);
13
+ }
14
+
15
+ function isImportDeclaration(
16
+ node: ESTree.Node,
17
+ ): node is ESTree.ImportDeclaration {
18
+ return node.type === 'ImportDeclaration';
19
+ }
20
+
21
+ export function isImportSpecifier(
22
+ specifier: ESTree.Node,
23
+ ): specifier is ESTree.ImportSpecifier {
24
+ return specifier.type === 'ImportSpecifier';
25
+ }
26
+
27
+ export function isImportDefaultSpecifier(
28
+ specifier: ESTree.Node,
29
+ ): specifier is ESTree.ImportDefaultSpecifier {
30
+ return specifier.type === 'ImportDefaultSpecifier';
31
+ }
32
+
33
+ export function isImportNamespaceSpecifier(
34
+ specifier: ESTree.Node,
35
+ ): specifier is ESTree.ImportNamespaceSpecifier {
36
+ return specifier.type === 'ImportNamespaceSpecifier';
37
+ }