@atlaskit/eslint-plugin-ui-styling-standard 0.11.6 → 0.12.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.
- package/CHANGELOG.md +9 -0
- package/README.md +1 -0
- package/constellation/index/overview.mdx +1 -0
- package/constellation/use-compiled/usage.mdx +51 -0
- package/dist/cjs/presets/all.codegen.js +2 -1
- package/dist/cjs/presets/recommended.codegen.js +2 -1
- package/dist/cjs/rules/index.codegen.js +4 -2
- package/dist/cjs/rules/use-compiled/index.js +257 -0
- package/dist/cjs/rules/use-compiled/is-safe.js +130 -0
- package/dist/cjs/rules/use-compiled/utils.js +26 -0
- package/dist/es2019/presets/all.codegen.js +2 -1
- package/dist/es2019/presets/recommended.codegen.js +2 -1
- package/dist/es2019/rules/index.codegen.js +4 -2
- package/dist/es2019/rules/use-compiled/index.js +182 -0
- package/dist/es2019/rules/use-compiled/is-safe.js +118 -0
- package/dist/es2019/rules/use-compiled/utils.js +15 -0
- package/dist/esm/presets/all.codegen.js +2 -1
- package/dist/esm/presets/recommended.codegen.js +2 -1
- package/dist/esm/rules/index.codegen.js +4 -2
- package/dist/esm/rules/use-compiled/index.js +250 -0
- package/dist/esm/rules/use-compiled/is-safe.js +121 -0
- package/dist/esm/rules/use-compiled/utils.js +17 -0
- package/dist/types/index.codegen.d.ts +2 -0
- package/dist/types/presets/all.codegen.d.ts +2 -1
- package/dist/types/presets/recommended.codegen.d.ts +2 -1
- package/dist/types/rules/index.codegen.d.ts +1 -0
- package/dist/types/rules/use-compiled/index.d.ts +3 -0
- package/dist/types/rules/use-compiled/is-safe.d.ts +10 -0
- package/dist/types/rules/use-compiled/utils.d.ts +6 -0
- package/dist/types-ts4.5/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
- package/dist/types-ts4.5/rules/use-compiled/index.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-compiled/is-safe.d.ts +10 -0
- package/dist/types-ts4.5/rules/use-compiled/utils.d.ts +6 -0
- package/package.json +3 -2
- package/src/presets/all.codegen.tsx +2 -1
- package/src/presets/recommended.codegen.tsx +2 -1
- package/src/rules/index.codegen.tsx +3 -1
- package/src/rules/use-compiled/README.md +49 -0
- package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +73 -0
- package/src/rules/use-compiled/__tests__/css.test.tsx +126 -0
- package/src/rules/use-compiled/__tests__/imports.test.tsx +139 -0
- package/src/rules/use-compiled/__tests__/keyframes.test.tsx +140 -0
- package/src/rules/use-compiled/__tests__/pragma.test.tsx +108 -0
- package/src/rules/use-compiled/__tests__/styled.test.tsx +114 -0
- package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +15 -0
- package/src/rules/use-compiled/index.tsx +275 -0
- package/src/rules/use-compiled/is-safe.tsx +163 -0
- package/src/rules/use-compiled/utils.tsx +37 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { identifier, importDeclaration, importSpecifier, literal } from 'eslint-codemod-utils';
|
|
2
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
3
|
+
import { createLintRule } from '../utils/create-rule';
|
|
4
|
+
import { getFirstImportFromSource, isImportDefaultSpecifier, isImportNamespaceSpecifier, isImportSpecifier } from './utils';
|
|
5
|
+
import { isSafeStyled, isSafeUsage } from './is-safe';
|
|
6
|
+
const SUPPORTED_EMOTION_IMPORTS = new Set(['css', 'keyframes', 'ClassNames', 'jsx']);
|
|
7
|
+
const SUPPORTED_IMPORT_MAP = {
|
|
8
|
+
'@emotion/core': SUPPORTED_EMOTION_IMPORTS,
|
|
9
|
+
'@emotion/react': SUPPORTED_EMOTION_IMPORTS,
|
|
10
|
+
'@emotion/styled': new Set(['default']),
|
|
11
|
+
'styled-components': new Set(['default', 'css', 'keyframes'])
|
|
12
|
+
};
|
|
13
|
+
function isSupportedImportSource(importSource) {
|
|
14
|
+
return importSource in SUPPORTED_IMPORT_MAP;
|
|
15
|
+
}
|
|
16
|
+
const schema = {
|
|
17
|
+
type: 'array',
|
|
18
|
+
items: [{
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
canAutoFix: {
|
|
22
|
+
type: 'boolean'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
additionalProperties: false
|
|
26
|
+
}]
|
|
27
|
+
};
|
|
28
|
+
const defaultConfig = {
|
|
29
|
+
canAutoFix: false
|
|
30
|
+
};
|
|
31
|
+
function readConfig(context) {
|
|
32
|
+
const [config] = context.options;
|
|
33
|
+
return Object.assign({}, defaultConfig, config);
|
|
34
|
+
}
|
|
35
|
+
export const rule = createLintRule({
|
|
36
|
+
meta: {
|
|
37
|
+
name: 'use-compiled',
|
|
38
|
+
type: 'problem',
|
|
39
|
+
fixable: 'code',
|
|
40
|
+
docs: {
|
|
41
|
+
description: 'Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries',
|
|
42
|
+
recommended: true,
|
|
43
|
+
severity: 'warn'
|
|
44
|
+
},
|
|
45
|
+
messages: {
|
|
46
|
+
'use-compiled': `Use '@compiled/react' instead of '{{importSource}}'`
|
|
47
|
+
},
|
|
48
|
+
schema
|
|
49
|
+
},
|
|
50
|
+
create(context) {
|
|
51
|
+
const config = readConfig(context);
|
|
52
|
+
return {
|
|
53
|
+
Program() {
|
|
54
|
+
const pragma = context.getSourceCode().getAllComments().find(n => n.value.includes('@jsxImportSource @emotion/react'));
|
|
55
|
+
if (!pragma) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
context.report({
|
|
59
|
+
messageId: 'use-compiled',
|
|
60
|
+
data: {
|
|
61
|
+
importSource: '@emotion/react'
|
|
62
|
+
},
|
|
63
|
+
loc: pragma.loc,
|
|
64
|
+
*fix(fixer) {
|
|
65
|
+
if (!config.canAutoFix) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const importDeclarations = context.getSourceCode().ast.body.filter(node => node.type === 'ImportDeclaration');
|
|
69
|
+
const hasEmotionImport = importDeclarations.some(importDeclaration => {
|
|
70
|
+
const importSource = importDeclaration.source.value;
|
|
71
|
+
return importSource.startsWith('@emotion/');
|
|
72
|
+
});
|
|
73
|
+
const hasCompiledImport = importDeclarations.some(importDeclaration => importDeclaration.source.value === '@compiled/react');
|
|
74
|
+
if (hasEmotionImport || !hasCompiledImport) {
|
|
75
|
+
// Considering this unsafe to auto-fix
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
yield fixer.replaceText(
|
|
79
|
+
// @ts-expect-error - types don't accept comment nodes
|
|
80
|
+
pragma, `/** @jsxImportSource @compiled/react */`);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
ImportDeclaration(node) {
|
|
85
|
+
const importSource = node.source.value;
|
|
86
|
+
if (!isSupportedImportSource(importSource)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
context.report({
|
|
90
|
+
node: node.source,
|
|
91
|
+
messageId: 'use-compiled',
|
|
92
|
+
data: {
|
|
93
|
+
importSource
|
|
94
|
+
},
|
|
95
|
+
*fix(fixer) {
|
|
96
|
+
if (!config.canAutoFix) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const specifiers = getSpecifiers({
|
|
100
|
+
context,
|
|
101
|
+
node,
|
|
102
|
+
importSource
|
|
103
|
+
});
|
|
104
|
+
if (!specifiers) {
|
|
105
|
+
// Cannot safely auto-fix
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
yield* fixImport({
|
|
109
|
+
context,
|
|
110
|
+
fixer,
|
|
111
|
+
node,
|
|
112
|
+
specifiers
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
export default rule;
|
|
121
|
+
function getSpecifiers({
|
|
122
|
+
context,
|
|
123
|
+
node,
|
|
124
|
+
importSource
|
|
125
|
+
}) {
|
|
126
|
+
const supportedImports = SUPPORTED_IMPORT_MAP[importSource];
|
|
127
|
+
|
|
128
|
+
// We aren't supporting namespace imports
|
|
129
|
+
if (node.specifiers.some(isImportNamespaceSpecifier)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const defaultSpecifier = node.specifiers.find(isImportDefaultSpecifier);
|
|
133
|
+
if (defaultSpecifier) {
|
|
134
|
+
if (!supportedImports.has('default')) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const styled = findVariable({
|
|
138
|
+
identifier: defaultSpecifier.local,
|
|
139
|
+
sourceCode: context.getSourceCode()
|
|
140
|
+
});
|
|
141
|
+
if (!styled || !isSafeStyled(styled)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const namedSpecifiers = node.specifiers.filter(isImportSpecifier);
|
|
146
|
+
if (!namedSpecifiers.every(specifier => isSafeUsage({
|
|
147
|
+
context,
|
|
148
|
+
specifier,
|
|
149
|
+
supportedImports
|
|
150
|
+
}))) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (defaultSpecifier) {
|
|
154
|
+
return [...namedSpecifiers,
|
|
155
|
+
// ASSUMPTION: the default import, if allowed, is for `styled`
|
|
156
|
+
importSpecifier({
|
|
157
|
+
imported: identifier('styled'),
|
|
158
|
+
local: node.specifiers[0].local
|
|
159
|
+
})];
|
|
160
|
+
}
|
|
161
|
+
return namedSpecifiers;
|
|
162
|
+
}
|
|
163
|
+
function* fixImport({
|
|
164
|
+
context,
|
|
165
|
+
fixer,
|
|
166
|
+
node,
|
|
167
|
+
specifiers
|
|
168
|
+
}) {
|
|
169
|
+
const compiledImport = getFirstImportFromSource(context, '@compiled/react');
|
|
170
|
+
if (compiledImport) {
|
|
171
|
+
yield fixer.remove(node);
|
|
172
|
+
yield fixer.replaceText(compiledImport, buildImportDeclaration(compiledImport.specifiers.concat(specifiers), '@compiled/react'));
|
|
173
|
+
} else {
|
|
174
|
+
yield fixer.replaceText(node, buildImportDeclaration(specifiers, '@compiled/react'));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function buildImportDeclaration(specifiers, source) {
|
|
178
|
+
return `${importDeclaration({
|
|
179
|
+
specifiers,
|
|
180
|
+
source: literal(source)
|
|
181
|
+
})};`;
|
|
182
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { closestOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
3
|
+
export function isSafeUsage({
|
|
4
|
+
context,
|
|
5
|
+
specifier,
|
|
6
|
+
supportedImports
|
|
7
|
+
}) {
|
|
8
|
+
const importName = specifier.imported.name;
|
|
9
|
+
if (!supportedImports.has(importName)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (importName === 'css') {
|
|
13
|
+
const css = findVariable({
|
|
14
|
+
identifier: specifier.local,
|
|
15
|
+
sourceCode: context.getSourceCode()
|
|
16
|
+
});
|
|
17
|
+
return css !== null && isSafeCss(css);
|
|
18
|
+
}
|
|
19
|
+
if (importName === 'keyframes') {
|
|
20
|
+
const keyframes = findVariable({
|
|
21
|
+
identifier: specifier.local,
|
|
22
|
+
sourceCode: context.getSourceCode()
|
|
23
|
+
});
|
|
24
|
+
return keyframes !== null && isSafeKeyframes(keyframes);
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function isSafeKeyframes(keyframes) {
|
|
29
|
+
return keyframes.references.every(ref => {
|
|
30
|
+
const callExpression = closestOfType(ref.identifier, 'CallExpression');
|
|
31
|
+
if (!callExpression) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if (callExpression.callee !== ref.identifier) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (callExpression.arguments.length !== 1) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const argument = callExpression.arguments[0];
|
|
41
|
+
if (argument.type !== 'ObjectExpression') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return argument.properties.every(property => isSafeProperty(property, isSafeKeyframePropertyValue));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function isSafeKeyframePropertyValue(value) {
|
|
48
|
+
if (value.type !== 'ObjectExpression') {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return isSafeStyleObject(value);
|
|
52
|
+
}
|
|
53
|
+
export function isSafeCss(css) {
|
|
54
|
+
return css.references.every(ref => {
|
|
55
|
+
const callExpression = closestOfType(ref.identifier, 'CallExpression');
|
|
56
|
+
if (!callExpression) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (callExpression.callee !== ref.identifier) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return callExpression.arguments.every(isSafeStyleArgument);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export function isSafeStyled(styled) {
|
|
66
|
+
return styled.references.every(ref => {
|
|
67
|
+
const callExpression = closestOfType(ref.identifier, 'CallExpression');
|
|
68
|
+
if (!callExpression) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* If it's not in the form `styled.tagName()`
|
|
74
|
+
* then we consider it unsafe to auto-fix.
|
|
75
|
+
*/
|
|
76
|
+
if (callExpression.callee.type !== 'MemberExpression' || callExpression.callee.object !== ref.identifier) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return callExpression.arguments.every(isSafeStyleArgument);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Determines if a style argument is safe for auto-fix conversion.
|
|
85
|
+
*/
|
|
86
|
+
function isSafeStyleArgument(arg) {
|
|
87
|
+
if (arg.type === 'ObjectExpression') {
|
|
88
|
+
return isSafeStyleObject(arg);
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
function isSafeStyleObject(object) {
|
|
93
|
+
return object.properties.every(property => isSafeProperty(property, isSafeStyleValue));
|
|
94
|
+
}
|
|
95
|
+
function isSafeStyleValue(value) {
|
|
96
|
+
if (value.type === 'Literal') {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
function isSafeProperty(property, isSafePropertyValue) {
|
|
102
|
+
if (property.type === 'SpreadElement') {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (property.method || property.shorthand || property.kind !== 'init') {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (!hasSafeKey(property)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return isSafePropertyValue(property.value);
|
|
112
|
+
}
|
|
113
|
+
function hasSafeKey(property) {
|
|
114
|
+
if (property.computed) {
|
|
115
|
+
return property.key.type === 'Literal';
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function getFirstImportFromSource(context, source) {
|
|
2
|
+
return context.getSourceCode().ast.body.filter(isImportDeclaration).find(node => node.source.value === source);
|
|
3
|
+
}
|
|
4
|
+
function isImportDeclaration(node) {
|
|
5
|
+
return node.type === 'ImportDeclaration';
|
|
6
|
+
}
|
|
7
|
+
export function isImportSpecifier(specifier) {
|
|
8
|
+
return specifier.type === 'ImportSpecifier';
|
|
9
|
+
}
|
|
10
|
+
export function isImportDefaultSpecifier(specifier) {
|
|
11
|
+
return specifier.type === 'ImportDefaultSpecifier';
|
|
12
|
+
}
|
|
13
|
+
export function isImportNamespaceSpecifier(specifier) {
|
|
14
|
+
return specifier.type === 'ImportNamespaceSpecifier';
|
|
15
|
+
}
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -19,6 +19,7 @@ export default {
|
|
|
19
19
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
20
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
21
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
22
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
23
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
24
|
excludeReactComponents: true
|
|
24
25
|
}],
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::752a442dd46681814ab73f7fa9c05374>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -19,6 +19,7 @@ export default {
|
|
|
19
19
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
20
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
21
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
22
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
23
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
24
|
excludeReactComponents: true
|
|
24
25
|
}],
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::be177a361ddc1a7a41d843680299e318>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
import atlaskitTheme from './atlaskit-theme';
|
|
@@ -16,6 +16,7 @@ import noImportantStyles from './no-important-styles';
|
|
|
16
16
|
import noImportedStyleValues from './no-imported-style-values';
|
|
17
17
|
import noNestedSelectors from './no-nested-selectors';
|
|
18
18
|
import noUnsafeValues from './no-unsafe-values';
|
|
19
|
+
import useCompiled from './use-compiled';
|
|
19
20
|
export default {
|
|
20
21
|
'atlaskit-theme': atlaskitTheme,
|
|
21
22
|
'convert-props-syntax': convertPropsSyntax,
|
|
@@ -29,5 +30,6 @@ export default {
|
|
|
29
30
|
'no-important-styles': noImportantStyles,
|
|
30
31
|
'no-imported-style-values': noImportedStyleValues,
|
|
31
32
|
'no-nested-selectors': noNestedSelectors,
|
|
32
|
-
'no-unsafe-values': noUnsafeValues
|
|
33
|
+
'no-unsafe-values': noUnsafeValues,
|
|
34
|
+
'use-compiled': useCompiled
|
|
33
35
|
};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
import { identifier, importDeclaration, importSpecifier, literal } from 'eslint-codemod-utils';
|
|
5
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
6
|
+
import { createLintRule } from '../utils/create-rule';
|
|
7
|
+
import { getFirstImportFromSource, isImportDefaultSpecifier, isImportNamespaceSpecifier, isImportSpecifier } from './utils';
|
|
8
|
+
import { isSafeStyled, isSafeUsage } from './is-safe';
|
|
9
|
+
var SUPPORTED_EMOTION_IMPORTS = new Set(['css', 'keyframes', 'ClassNames', 'jsx']);
|
|
10
|
+
var SUPPORTED_IMPORT_MAP = {
|
|
11
|
+
'@emotion/core': SUPPORTED_EMOTION_IMPORTS,
|
|
12
|
+
'@emotion/react': SUPPORTED_EMOTION_IMPORTS,
|
|
13
|
+
'@emotion/styled': new Set(['default']),
|
|
14
|
+
'styled-components': new Set(['default', 'css', 'keyframes'])
|
|
15
|
+
};
|
|
16
|
+
function isSupportedImportSource(importSource) {
|
|
17
|
+
return importSource in SUPPORTED_IMPORT_MAP;
|
|
18
|
+
}
|
|
19
|
+
var schema = {
|
|
20
|
+
type: 'array',
|
|
21
|
+
items: [{
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
canAutoFix: {
|
|
25
|
+
type: 'boolean'
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
additionalProperties: false
|
|
29
|
+
}]
|
|
30
|
+
};
|
|
31
|
+
var defaultConfig = {
|
|
32
|
+
canAutoFix: false
|
|
33
|
+
};
|
|
34
|
+
function readConfig(context) {
|
|
35
|
+
var _ref = context.options,
|
|
36
|
+
_ref2 = _slicedToArray(_ref, 1),
|
|
37
|
+
config = _ref2[0];
|
|
38
|
+
return Object.assign({}, defaultConfig, config);
|
|
39
|
+
}
|
|
40
|
+
export var rule = createLintRule({
|
|
41
|
+
meta: {
|
|
42
|
+
name: 'use-compiled',
|
|
43
|
+
type: 'problem',
|
|
44
|
+
fixable: 'code',
|
|
45
|
+
docs: {
|
|
46
|
+
description: 'Ensures usage of `@compiled/react` instead of other CSS-in-JS libraries',
|
|
47
|
+
recommended: true,
|
|
48
|
+
severity: 'warn'
|
|
49
|
+
},
|
|
50
|
+
messages: {
|
|
51
|
+
'use-compiled': "Use '@compiled/react' instead of '{{importSource}}'"
|
|
52
|
+
},
|
|
53
|
+
schema: schema
|
|
54
|
+
},
|
|
55
|
+
create: function create(context) {
|
|
56
|
+
var config = readConfig(context);
|
|
57
|
+
return {
|
|
58
|
+
Program: function Program() {
|
|
59
|
+
var pragma = context.getSourceCode().getAllComments().find(function (n) {
|
|
60
|
+
return n.value.includes('@jsxImportSource @emotion/react');
|
|
61
|
+
});
|
|
62
|
+
if (!pragma) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
context.report({
|
|
66
|
+
messageId: 'use-compiled',
|
|
67
|
+
data: {
|
|
68
|
+
importSource: '@emotion/react'
|
|
69
|
+
},
|
|
70
|
+
loc: pragma.loc,
|
|
71
|
+
fix: /*#__PURE__*/_regeneratorRuntime.mark(function fix(fixer) {
|
|
72
|
+
var importDeclarations, hasEmotionImport, hasCompiledImport;
|
|
73
|
+
return _regeneratorRuntime.wrap(function fix$(_context) {
|
|
74
|
+
while (1) switch (_context.prev = _context.next) {
|
|
75
|
+
case 0:
|
|
76
|
+
if (config.canAutoFix) {
|
|
77
|
+
_context.next = 2;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
return _context.abrupt("return");
|
|
81
|
+
case 2:
|
|
82
|
+
importDeclarations = context.getSourceCode().ast.body.filter(function (node) {
|
|
83
|
+
return node.type === 'ImportDeclaration';
|
|
84
|
+
});
|
|
85
|
+
hasEmotionImport = importDeclarations.some(function (importDeclaration) {
|
|
86
|
+
var importSource = importDeclaration.source.value;
|
|
87
|
+
return importSource.startsWith('@emotion/');
|
|
88
|
+
});
|
|
89
|
+
hasCompiledImport = importDeclarations.some(function (importDeclaration) {
|
|
90
|
+
return importDeclaration.source.value === '@compiled/react';
|
|
91
|
+
});
|
|
92
|
+
if (!(hasEmotionImport || !hasCompiledImport)) {
|
|
93
|
+
_context.next = 7;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
return _context.abrupt("return");
|
|
97
|
+
case 7:
|
|
98
|
+
_context.next = 9;
|
|
99
|
+
return fixer.replaceText(
|
|
100
|
+
// @ts-expect-error - types don't accept comment nodes
|
|
101
|
+
pragma, "/** @jsxImportSource @compiled/react */");
|
|
102
|
+
case 9:
|
|
103
|
+
case "end":
|
|
104
|
+
return _context.stop();
|
|
105
|
+
}
|
|
106
|
+
}, fix);
|
|
107
|
+
})
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
ImportDeclaration: function (_ImportDeclaration) {
|
|
111
|
+
function ImportDeclaration(_x) {
|
|
112
|
+
return _ImportDeclaration.apply(this, arguments);
|
|
113
|
+
}
|
|
114
|
+
ImportDeclaration.toString = function () {
|
|
115
|
+
return _ImportDeclaration.toString();
|
|
116
|
+
};
|
|
117
|
+
return ImportDeclaration;
|
|
118
|
+
}(function (node) {
|
|
119
|
+
var importSource = node.source.value;
|
|
120
|
+
if (!isSupportedImportSource(importSource)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
context.report({
|
|
124
|
+
node: node.source,
|
|
125
|
+
messageId: 'use-compiled',
|
|
126
|
+
data: {
|
|
127
|
+
importSource: importSource
|
|
128
|
+
},
|
|
129
|
+
fix: /*#__PURE__*/_regeneratorRuntime.mark(function fix(fixer) {
|
|
130
|
+
var specifiers;
|
|
131
|
+
return _regeneratorRuntime.wrap(function fix$(_context2) {
|
|
132
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
133
|
+
case 0:
|
|
134
|
+
if (config.canAutoFix) {
|
|
135
|
+
_context2.next = 2;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
return _context2.abrupt("return");
|
|
139
|
+
case 2:
|
|
140
|
+
specifiers = getSpecifiers({
|
|
141
|
+
context: context,
|
|
142
|
+
node: node,
|
|
143
|
+
importSource: importSource
|
|
144
|
+
});
|
|
145
|
+
if (specifiers) {
|
|
146
|
+
_context2.next = 5;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
return _context2.abrupt("return");
|
|
150
|
+
case 5:
|
|
151
|
+
return _context2.delegateYield(fixImport({
|
|
152
|
+
context: context,
|
|
153
|
+
fixer: fixer,
|
|
154
|
+
node: node,
|
|
155
|
+
specifiers: specifiers
|
|
156
|
+
}), "t0", 6);
|
|
157
|
+
case 6:
|
|
158
|
+
case "end":
|
|
159
|
+
return _context2.stop();
|
|
160
|
+
}
|
|
161
|
+
}, fix);
|
|
162
|
+
})
|
|
163
|
+
});
|
|
164
|
+
})
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
export default rule;
|
|
169
|
+
function getSpecifiers(_ref3) {
|
|
170
|
+
var context = _ref3.context,
|
|
171
|
+
node = _ref3.node,
|
|
172
|
+
importSource = _ref3.importSource;
|
|
173
|
+
var supportedImports = SUPPORTED_IMPORT_MAP[importSource];
|
|
174
|
+
|
|
175
|
+
// We aren't supporting namespace imports
|
|
176
|
+
if (node.specifiers.some(isImportNamespaceSpecifier)) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
var defaultSpecifier = node.specifiers.find(isImportDefaultSpecifier);
|
|
180
|
+
if (defaultSpecifier) {
|
|
181
|
+
if (!supportedImports.has('default')) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
var styled = findVariable({
|
|
185
|
+
identifier: defaultSpecifier.local,
|
|
186
|
+
sourceCode: context.getSourceCode()
|
|
187
|
+
});
|
|
188
|
+
if (!styled || !isSafeStyled(styled)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
var namedSpecifiers = node.specifiers.filter(isImportSpecifier);
|
|
193
|
+
if (!namedSpecifiers.every(function (specifier) {
|
|
194
|
+
return isSafeUsage({
|
|
195
|
+
context: context,
|
|
196
|
+
specifier: specifier,
|
|
197
|
+
supportedImports: supportedImports
|
|
198
|
+
});
|
|
199
|
+
})) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (defaultSpecifier) {
|
|
203
|
+
return [].concat(_toConsumableArray(namedSpecifiers), [
|
|
204
|
+
// ASSUMPTION: the default import, if allowed, is for `styled`
|
|
205
|
+
importSpecifier({
|
|
206
|
+
imported: identifier('styled'),
|
|
207
|
+
local: node.specifiers[0].local
|
|
208
|
+
})]);
|
|
209
|
+
}
|
|
210
|
+
return namedSpecifiers;
|
|
211
|
+
}
|
|
212
|
+
function fixImport(_ref4) {
|
|
213
|
+
var context = _ref4.context,
|
|
214
|
+
fixer = _ref4.fixer,
|
|
215
|
+
node = _ref4.node,
|
|
216
|
+
specifiers = _ref4.specifiers;
|
|
217
|
+
return /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
218
|
+
var compiledImport;
|
|
219
|
+
return _regeneratorRuntime.wrap(function _callee$(_context3) {
|
|
220
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
221
|
+
case 0:
|
|
222
|
+
compiledImport = getFirstImportFromSource(context, '@compiled/react');
|
|
223
|
+
if (!compiledImport) {
|
|
224
|
+
_context3.next = 8;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
_context3.next = 4;
|
|
228
|
+
return fixer.remove(node);
|
|
229
|
+
case 4:
|
|
230
|
+
_context3.next = 6;
|
|
231
|
+
return fixer.replaceText(compiledImport, buildImportDeclaration(compiledImport.specifiers.concat(specifiers), '@compiled/react'));
|
|
232
|
+
case 6:
|
|
233
|
+
_context3.next = 10;
|
|
234
|
+
break;
|
|
235
|
+
case 8:
|
|
236
|
+
_context3.next = 10;
|
|
237
|
+
return fixer.replaceText(node, buildImportDeclaration(specifiers, '@compiled/react'));
|
|
238
|
+
case 10:
|
|
239
|
+
case "end":
|
|
240
|
+
return _context3.stop();
|
|
241
|
+
}
|
|
242
|
+
}, _callee);
|
|
243
|
+
})();
|
|
244
|
+
}
|
|
245
|
+
function buildImportDeclaration(specifiers, source) {
|
|
246
|
+
return "".concat(importDeclaration({
|
|
247
|
+
specifiers: specifiers,
|
|
248
|
+
source: literal(source)
|
|
249
|
+
}), ";");
|
|
250
|
+
}
|