@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.
- package/CHANGELOG.md +22 -0
- package/README.md +2 -0
- package/constellation/index/overview.mdx +3 -1
- package/constellation/no-global-styles/usage.mdx +59 -0
- package/constellation/use-compiled/usage.mdx +54 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/index.codegen.js +6 -2
- package/dist/cjs/rules/no-global-styles/index.js +100 -0
- 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 +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/index.codegen.js +6 -2
- package/dist/es2019/rules/no-global-styles/index.js +67 -0
- 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 +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/index.codegen.js +6 -2
- package/dist/esm/rules/no-global-styles/index.js +94 -0
- 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 +4 -0
- package/dist/types/presets/all.codegen.d.ts +3 -1
- package/dist/types/presets/recommended.codegen.d.ts +3 -1
- package/dist/types/rules/index.codegen.d.ts +2 -0
- package/dist/types/rules/no-global-styles/index.d.ts +2 -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 +4 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-global-styles/index.d.ts +2 -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 +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/index.codegen.tsx +5 -1
- package/src/rules/no-global-styles/README.md +56 -0
- package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +33 -0
- package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +46 -0
- package/src/rules/no-global-styles/index.tsx +85 -0
- 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 +163 -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,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isSafeCss = isSafeCss;
|
|
7
|
+
exports.isSafeKeyframes = isSafeKeyframes;
|
|
8
|
+
exports.isSafeStyled = isSafeStyled;
|
|
9
|
+
exports.isSafeUsage = isSafeUsage;
|
|
10
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
11
|
+
var _findVariable = require("@atlaskit/eslint-utils/find-variable");
|
|
12
|
+
function isSafeUsage(_ref) {
|
|
13
|
+
var context = _ref.context,
|
|
14
|
+
specifier = _ref.specifier,
|
|
15
|
+
supportedImports = _ref.supportedImports;
|
|
16
|
+
var importName = specifier.imported.name;
|
|
17
|
+
if (!supportedImports.has(importName)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (importName === 'css') {
|
|
21
|
+
var css = (0, _findVariable.findVariable)({
|
|
22
|
+
identifier: specifier.local,
|
|
23
|
+
sourceCode: context.getSourceCode()
|
|
24
|
+
});
|
|
25
|
+
return css !== null && isSafeCss(css);
|
|
26
|
+
}
|
|
27
|
+
if (importName === 'keyframes') {
|
|
28
|
+
var keyframes = (0, _findVariable.findVariable)({
|
|
29
|
+
identifier: specifier.local,
|
|
30
|
+
sourceCode: context.getSourceCode()
|
|
31
|
+
});
|
|
32
|
+
return keyframes !== null && isSafeKeyframes(keyframes);
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
function isSafeKeyframes(keyframes) {
|
|
37
|
+
return keyframes.references.every(function (ref) {
|
|
38
|
+
var callExpression = (0, _eslintCodemodUtils.closestOfType)(ref.identifier, 'CallExpression');
|
|
39
|
+
if (!callExpression) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (callExpression.callee !== ref.identifier) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (callExpression.arguments.length !== 1) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
var argument = callExpression.arguments[0];
|
|
49
|
+
if (argument.type !== 'ObjectExpression') {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return argument.properties.every(function (property) {
|
|
53
|
+
return isSafeProperty(property, isSafeKeyframePropertyValue);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function isSafeKeyframePropertyValue(value) {
|
|
58
|
+
if (value.type !== 'ObjectExpression') {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return isSafeStyleObject(value);
|
|
62
|
+
}
|
|
63
|
+
function isSafeCss(css) {
|
|
64
|
+
return css.references.every(function (ref) {
|
|
65
|
+
var callExpression = (0, _eslintCodemodUtils.closestOfType)(ref.identifier, 'CallExpression');
|
|
66
|
+
if (!callExpression) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (callExpression.callee !== ref.identifier) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return callExpression.arguments.every(isSafeStyleArgument);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function isSafeStyled(styled) {
|
|
76
|
+
return styled.references.every(function (ref) {
|
|
77
|
+
var callExpression = (0, _eslintCodemodUtils.closestOfType)(ref.identifier, 'CallExpression');
|
|
78
|
+
if (!callExpression) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* If it's not in the form `styled.tagName()`
|
|
84
|
+
* then we consider it unsafe to auto-fix.
|
|
85
|
+
*/
|
|
86
|
+
if (callExpression.callee.type !== 'MemberExpression' || callExpression.callee.object !== ref.identifier) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return callExpression.arguments.every(isSafeStyleArgument);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Determines if a style argument is safe for auto-fix conversion.
|
|
95
|
+
*/
|
|
96
|
+
function isSafeStyleArgument(arg) {
|
|
97
|
+
if (arg.type === 'ObjectExpression') {
|
|
98
|
+
return isSafeStyleObject(arg);
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function isSafeStyleObject(object) {
|
|
103
|
+
return object.properties.every(function (property) {
|
|
104
|
+
return isSafeProperty(property, isSafeStyleValue);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function isSafeStyleValue(value) {
|
|
108
|
+
if (value.type === 'Literal') {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
function isSafeProperty(property, isSafePropertyValue) {
|
|
114
|
+
if (property.type === 'SpreadElement') {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
if (property.method || property.shorthand || property.kind !== 'init') {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (!hasSafeKey(property)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return isSafePropertyValue(property.value);
|
|
124
|
+
}
|
|
125
|
+
function hasSafeKey(property) {
|
|
126
|
+
if (property.computed) {
|
|
127
|
+
return property.key.type === 'Literal';
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getFirstImportFromSource = getFirstImportFromSource;
|
|
7
|
+
exports.isImportDefaultSpecifier = isImportDefaultSpecifier;
|
|
8
|
+
exports.isImportNamespaceSpecifier = isImportNamespaceSpecifier;
|
|
9
|
+
exports.isImportSpecifier = isImportSpecifier;
|
|
10
|
+
function getFirstImportFromSource(context, source) {
|
|
11
|
+
return context.getSourceCode().ast.body.filter(isImportDeclaration).find(function (node) {
|
|
12
|
+
return node.source.value === source;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function isImportDeclaration(node) {
|
|
16
|
+
return node.type === 'ImportDeclaration';
|
|
17
|
+
}
|
|
18
|
+
function isImportSpecifier(specifier) {
|
|
19
|
+
return specifier.type === 'ImportSpecifier';
|
|
20
|
+
}
|
|
21
|
+
function isImportDefaultSpecifier(specifier) {
|
|
22
|
+
return specifier.type === 'ImportDefaultSpecifier';
|
|
23
|
+
}
|
|
24
|
+
function isImportNamespaceSpecifier(specifier) {
|
|
25
|
+
return specifier.type === 'ImportNamespaceSpecifier';
|
|
26
|
+
}
|
|
@@ -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::9ac05c3cbd30f6209164b7a24f7a7a19>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -15,10 +15,12 @@ export default {
|
|
|
15
15
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-global-styles': 'warn',
|
|
18
19
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
23
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
24
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
25
|
excludeReactComponents: true
|
|
24
26
|
}],
|
|
@@ -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::9ac05c3cbd30f6209164b7a24f7a7a19>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -15,10 +15,12 @@ export default {
|
|
|
15
15
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-global-styles': 'warn',
|
|
18
19
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
23
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
24
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
25
|
excludeReactComponents: true
|
|
24
26
|
}],
|
|
@@ -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::3cea683df6e31f816db56abf1fd65c1e>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
import atlaskitTheme from './atlaskit-theme';
|
|
@@ -12,10 +12,12 @@ import noClassnameProp from './no-classname-prop';
|
|
|
12
12
|
import noContainerQueries from './no-container-queries';
|
|
13
13
|
import noDynamicStyles from './no-dynamic-styles';
|
|
14
14
|
import noExportedStyles from './no-exported-styles';
|
|
15
|
+
import noGlobalStyles from './no-global-styles';
|
|
15
16
|
import noImportantStyles from './no-important-styles';
|
|
16
17
|
import noImportedStyleValues from './no-imported-style-values';
|
|
17
18
|
import noNestedSelectors from './no-nested-selectors';
|
|
18
19
|
import noUnsafeValues from './no-unsafe-values';
|
|
20
|
+
import useCompiled from './use-compiled';
|
|
19
21
|
export default {
|
|
20
22
|
'atlaskit-theme': atlaskitTheme,
|
|
21
23
|
'convert-props-syntax': convertPropsSyntax,
|
|
@@ -26,8 +28,10 @@ export default {
|
|
|
26
28
|
'no-container-queries': noContainerQueries,
|
|
27
29
|
'no-dynamic-styles': noDynamicStyles,
|
|
28
30
|
'no-exported-styles': noExportedStyles,
|
|
31
|
+
'no-global-styles': noGlobalStyles,
|
|
29
32
|
'no-important-styles': noImportantStyles,
|
|
30
33
|
'no-imported-style-values': noImportedStyleValues,
|
|
31
34
|
'no-nested-selectors': noNestedSelectors,
|
|
32
|
-
'no-unsafe-values': noUnsafeValues
|
|
35
|
+
'no-unsafe-values': noUnsafeValues,
|
|
36
|
+
'use-compiled': useCompiled
|
|
33
37
|
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createLintRule } from '../utils/create-rule';
|
|
2
|
+
const restrictedFileTypes = ['.css', '.less', '.sass', '.scss'];
|
|
3
|
+
const restrictedImports = {
|
|
4
|
+
'@emotion/core': ['Global'],
|
|
5
|
+
'@emotion/react': ['Global'],
|
|
6
|
+
'styled-components': ['createGlobalStyle', 'injectGlobal']
|
|
7
|
+
};
|
|
8
|
+
function isRestrictedImportSource(importSource) {
|
|
9
|
+
return importSource in restrictedImports;
|
|
10
|
+
}
|
|
11
|
+
export const rule = createLintRule({
|
|
12
|
+
meta: {
|
|
13
|
+
name: 'no-global-styles',
|
|
14
|
+
docs: {
|
|
15
|
+
description: 'Prevents global styles through CSS-in-JS or CSS module imports',
|
|
16
|
+
recommended: true,
|
|
17
|
+
severity: 'warn'
|
|
18
|
+
},
|
|
19
|
+
messages: {
|
|
20
|
+
'no-global-styles': 'Global styles are not allowed.',
|
|
21
|
+
'no-css-modules': 'Importing CSS files is not allowed.'
|
|
22
|
+
},
|
|
23
|
+
type: 'problem'
|
|
24
|
+
},
|
|
25
|
+
create(context) {
|
|
26
|
+
function checkFileType(node) {
|
|
27
|
+
const importSource = node.source.value;
|
|
28
|
+
if (restrictedFileTypes.some(suffix => importSource.endsWith(suffix))) {
|
|
29
|
+
context.report({
|
|
30
|
+
node: node.source,
|
|
31
|
+
messageId: 'no-css-modules'
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function checkImports(node) {
|
|
36
|
+
const importSource = node.source.value;
|
|
37
|
+
if (!isRestrictedImportSource(importSource)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
for (const importName of restrictedImports[importSource]) {
|
|
41
|
+
const specifier = node.specifiers.filter(isImportSpecifier).find(specifier => specifier.imported.name === importName);
|
|
42
|
+
if (specifier) {
|
|
43
|
+
context.report({
|
|
44
|
+
node: specifier,
|
|
45
|
+
messageId: 'no-global-styles'
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
ImportDeclaration(node) {
|
|
52
|
+
checkFileType(node);
|
|
53
|
+
checkImports(node);
|
|
54
|
+
},
|
|
55
|
+
'JSXOpeningElement[name.name="style"]'(node) {
|
|
56
|
+
context.report({
|
|
57
|
+
node,
|
|
58
|
+
messageId: 'no-global-styles'
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
export default rule;
|
|
65
|
+
function isImportSpecifier(node) {
|
|
66
|
+
return node.type === 'ImportSpecifier';
|
|
67
|
+
}
|
|
@@ -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::9ac05c3cbd30f6209164b7a24f7a7a19>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -15,10 +15,12 @@ export default {
|
|
|
15
15
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-global-styles': 'warn',
|
|
18
19
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
23
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
24
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
25
|
excludeReactComponents: true
|
|
24
26
|
}],
|
|
@@ -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::9ac05c3cbd30f6209164b7a24f7a7a19>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -15,10 +15,12 @@ export default {
|
|
|
15
15
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
16
16
|
'@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
|
|
17
17
|
'@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-global-styles': 'warn',
|
|
18
19
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
19
20
|
'@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
|
|
20
21
|
'@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
|
|
21
22
|
'@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
|
|
23
|
+
'@atlaskit/ui-styling-standard/use-compiled': 'warn',
|
|
22
24
|
'@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
|
|
23
25
|
excludeReactComponents: true
|
|
24
26
|
}],
|