@atlaskit/eslint-plugin-design-system 8.23.1 → 8.23.2
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 +6 -0
- package/dist/cjs/ast-nodes/function-call.js +48 -0
- package/dist/cjs/ast-nodes/import.js +49 -0
- package/dist/cjs/ast-nodes/index.js +40 -0
- package/dist/cjs/ast-nodes/jsx-attribute.js +64 -0
- package/dist/cjs/ast-nodes/jsx-element.js +55 -0
- package/dist/cjs/ast-nodes/root.js +34 -0
- package/dist/cjs/rules/use-primitives/index.js +6 -76
- package/dist/cjs/rules/use-primitives/transformers/emotion-css/index.js +168 -0
- package/dist/cjs/rules/use-primitives/transformers/emotion-css/supported.js +52 -0
- package/dist/cjs/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +3 -0
- package/dist/es2019/ast-nodes/function-call.js +42 -0
- package/dist/es2019/ast-nodes/import.js +42 -0
- package/dist/es2019/ast-nodes/index.js +5 -0
- package/dist/es2019/ast-nodes/jsx-attribute.js +59 -0
- package/dist/es2019/ast-nodes/jsx-element.js +50 -0
- package/dist/es2019/ast-nodes/root.js +28 -0
- package/dist/es2019/rules/use-primitives/index.js +9 -79
- package/dist/es2019/rules/use-primitives/transformers/emotion-css/index.js +159 -0
- package/dist/es2019/rules/use-primitives/transformers/emotion-css/supported.js +46 -0
- package/dist/es2019/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +3 -0
- package/dist/esm/ast-nodes/function-call.js +42 -0
- package/dist/esm/ast-nodes/import.js +43 -0
- package/dist/esm/ast-nodes/index.js +5 -0
- package/dist/esm/ast-nodes/jsx-attribute.js +59 -0
- package/dist/esm/ast-nodes/jsx-element.js +50 -0
- package/dist/esm/ast-nodes/root.js +28 -0
- package/dist/esm/rules/use-primitives/index.js +9 -79
- package/dist/esm/rules/use-primitives/transformers/emotion-css/index.js +158 -0
- package/dist/esm/rules/use-primitives/transformers/emotion-css/supported.js +46 -0
- package/dist/esm/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +3 -0
- package/dist/types/ast-nodes/function-call.d.ts +21 -0
- package/dist/types/ast-nodes/import.d.ts +16 -0
- package/dist/types/ast-nodes/index.d.ts +5 -0
- package/dist/types/ast-nodes/jsx-attribute.d.ts +26 -0
- package/dist/types/ast-nodes/jsx-element.d.ts +21 -0
- package/dist/types/ast-nodes/root.d.ts +19 -0
- package/dist/types/rules/use-primitives/transformers/emotion-css/index.d.ts +35 -0
- package/dist/types/rules/use-primitives/transformers/emotion-css/supported.d.ts +9 -0
- package/dist/types-ts4.5/ast-nodes/function-call.d.ts +21 -0
- package/dist/types-ts4.5/ast-nodes/import.d.ts +16 -0
- package/dist/types-ts4.5/ast-nodes/index.d.ts +5 -0
- package/dist/types-ts4.5/ast-nodes/jsx-attribute.d.ts +26 -0
- package/dist/types-ts4.5/ast-nodes/jsx-element.d.ts +21 -0
- package/dist/types-ts4.5/ast-nodes/root.d.ts +19 -0
- package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/index.d.ts +35 -0
- package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/supported.d.ts +9 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-design-system
|
|
2
2
|
|
|
3
|
+
## 8.23.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#65758](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/65758) [`16e6a0fbe125`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/16e6a0fbe125) - Internal refactor of `use-primitves` rule. There should be no change to consumers of the rule.
|
|
8
|
+
|
|
3
9
|
## 8.23.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.FunctionCall = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
9
|
+
|
|
10
|
+
var FunctionCall = exports.FunctionCall = {
|
|
11
|
+
getName: function getName(node) {
|
|
12
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'CallExpression')) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.callee, 'Identifier')) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return node.callee.name;
|
|
19
|
+
},
|
|
20
|
+
updateName: function updateName(node, newName, fixer) {
|
|
21
|
+
return fixer.replaceText(node.callee, newName);
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* Function arguments can be many things:
|
|
25
|
+
* `css(myStyles, () => {}, undefined, 'literal', ...rest) // etc`
|
|
26
|
+
* They all need slightly different treatment.
|
|
27
|
+
*
|
|
28
|
+
* Currently 'getArgumentAtPos' only implements strategies for Literals and ObjectExpressions.
|
|
29
|
+
* If you need to support another type of arg, add it, and update the type.
|
|
30
|
+
*/
|
|
31
|
+
getArgumentAtPos: function getArgumentAtPos(node, pos) {
|
|
32
|
+
var argument = node.arguments[pos];
|
|
33
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(argument, 'Literal') && argument.value) {
|
|
34
|
+
var _argument$value;
|
|
35
|
+
return {
|
|
36
|
+
type: 'Literal',
|
|
37
|
+
value: (_argument$value = argument.value) === null || _argument$value === void 0 ? void 0 : _argument$value.toString()
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(argument, 'ObjectExpression')) {
|
|
41
|
+
argument;
|
|
42
|
+
return {
|
|
43
|
+
type: 'ObjectExpression',
|
|
44
|
+
value: argument
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Import = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
9
|
+
|
|
10
|
+
var Import = exports.Import = {
|
|
11
|
+
/**
|
|
12
|
+
* Note: fixes can't overlap, which means this will fail:
|
|
13
|
+
* ```
|
|
14
|
+
* const importNode = Root.findImportByModule('@atlaskit/primitives')
|
|
15
|
+
* Import.insertNamedSpecifier(importNode, 'Box')
|
|
16
|
+
* Import.insertNamedSpecifier(importNode, 'xcss')
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* For this reason `insertNamedSpecifiers` accepts a `specifiers` array, so you can group all inserts together.
|
|
20
|
+
*/
|
|
21
|
+
insertNamedSpecifiers: function insertNamedSpecifiers(node, specifiers, fixer) {
|
|
22
|
+
var _this = this;
|
|
23
|
+
/**
|
|
24
|
+
* `insertImportSpecifier()` has the unfortunate implementation detail of naively adding duplicate specifiers.
|
|
25
|
+
* e.g. calling
|
|
26
|
+
* `insertImportSpecifier(importDecl, 'xcss')`
|
|
27
|
+
* on
|
|
28
|
+
* `import { Inline, xcss } from '@atlaskit/primitives'`
|
|
29
|
+
* will result in:
|
|
30
|
+
* `import { Inline, xcss, xcss } from '@atlaskit/primitives'`.
|
|
31
|
+
* So, we need to filter out specifiers that are already imported.
|
|
32
|
+
*/
|
|
33
|
+
var uniqueSpecifiers = specifiers.filter(function (specifier) {
|
|
34
|
+
return !_this.containsNamedSpecifier(node, specifier);
|
|
35
|
+
});
|
|
36
|
+
if (uniqueSpecifiers.length === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return fixer.replaceText(node, "".concat((0, _eslintCodemodUtils.insertImportSpecifier)(node, uniqueSpecifiers.join(', ')), ";\n"));
|
|
40
|
+
},
|
|
41
|
+
containsNamedSpecifier: function containsNamedSpecifier(node, name) {
|
|
42
|
+
return node.specifiers.some(function (specifier) {
|
|
43
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(specifier, 'ImportSpecifier')) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return specifier.imported.name === name;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "FunctionCall", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _functionCall.FunctionCall;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "Import", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _import.Import;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "JSXAttribute", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _jsxAttribute.JSXAttribute;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "JSXElement", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _jsxElement.JSXElement;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "Root", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function get() {
|
|
33
|
+
return _root.Root;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
var _functionCall = require("./function-call");
|
|
37
|
+
var _import = require("./import");
|
|
38
|
+
var _jsxAttribute = require("./jsx-attribute");
|
|
39
|
+
var _jsxElement = require("./jsx-element");
|
|
40
|
+
var _root = require("./root");
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.JSXAttribute = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var HelperJSXAttribute = exports.JSXAttribute = {
|
|
9
|
+
getName: function getName(node) {
|
|
10
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXAttribute')) {
|
|
11
|
+
throw new Error('Not a JSXAttribute');
|
|
12
|
+
}
|
|
13
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.name, 'JSXIdentifier')) {
|
|
14
|
+
throw new Error('name is not a JSXIdentifier');
|
|
15
|
+
}
|
|
16
|
+
return node.name.name;
|
|
17
|
+
},
|
|
18
|
+
updateName: function updateName(node, name, fixer) {
|
|
19
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXAttribute')) {
|
|
20
|
+
throw new Error('Not a JSXAttribute');
|
|
21
|
+
}
|
|
22
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.name, 'JSXIdentifier')) {
|
|
23
|
+
throw new Error('name is not a JSXIdentifier');
|
|
24
|
+
}
|
|
25
|
+
return fixer.replaceText(node.name, (0, _eslintCodemodUtils.literal)(name).toString());
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* A JSXAttribute value can be many things:
|
|
29
|
+
* - css='myStyles'
|
|
30
|
+
* - css={myStyles}
|
|
31
|
+
* - css={[styles1, styles2]}
|
|
32
|
+
* - header={<></>}
|
|
33
|
+
* - css={styleMap.header}
|
|
34
|
+
* - css={...styles}
|
|
35
|
+
*
|
|
36
|
+
* Currently, `getValue` has only implemented strategies for when the value is a string, or an ExpressionStatement
|
|
37
|
+
* If you need additional functionality add it, and set the correct `type` on the returned object
|
|
38
|
+
*/
|
|
39
|
+
getValue: function getValue(node) {
|
|
40
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXAttribute')) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (!node.value) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// handle `css={myStyles}`
|
|
48
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'JSXExpressionContainer') && (0, _eslintCodemodUtils.isNodeOfType)(node.value.expression, 'Identifier')) {
|
|
49
|
+
return {
|
|
50
|
+
type: 'ExpressionStatement',
|
|
51
|
+
value: node.value.expression.name
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// handle `css='myStyles'`
|
|
56
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'Literal') && node.value.value) {
|
|
57
|
+
var _node$value$value;
|
|
58
|
+
return {
|
|
59
|
+
type: 'Literal',
|
|
60
|
+
value: (_node$value$value = node.value.value) === null || _node$value$value === void 0 ? void 0 : _node$value$value.toString()
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.JSXElementHelper = exports.JSXElement = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var JSXElementHelper = exports.JSXElement = exports.JSXElementHelper = {
|
|
9
|
+
/**
|
|
10
|
+
* Names of JSXElements can be any of:
|
|
11
|
+
* `<Component></Component>` - (JSXIdentifier)
|
|
12
|
+
* `<MyComponents.Component></MyComponents.Component>` - `MyComponents` is a namespace (JSXNamespacedName)
|
|
13
|
+
* `<MyComponents.Component></MyComponents.Component>` - `MyComponents` is an object (JSXMemberExpression)
|
|
14
|
+
*
|
|
15
|
+
* Getting the name of a JSXMemberExpression is difficult, because object can contain objects, which is recursively defined in the AST.
|
|
16
|
+
* e.g. getting the name of `<MyComponents.PresentationLayer.LeftSideBar.Header />` would require `getName` to recursively resolve all parts of the name.
|
|
17
|
+
* `getName` does not currently have this functionality. Add it if you need it.
|
|
18
|
+
*/
|
|
19
|
+
getName: function getName(node) {
|
|
20
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.openingElement.name, 'JSXIdentifier')) {
|
|
21
|
+
// TODO: We may want to log this
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
return node.openingElement.name.name;
|
|
25
|
+
},
|
|
26
|
+
updateName: function updateName(node, newName, fixer) {
|
|
27
|
+
var isSelfClosing = JSXElementHelper.isSelfClosing(node);
|
|
28
|
+
var openingElementFix = fixer.replaceText(node.openingElement.name, (0, _eslintCodemodUtils.jsxIdentifier)(newName).toString());
|
|
29
|
+
if (isSelfClosing || !node.closingElement) {
|
|
30
|
+
return [openingElementFix];
|
|
31
|
+
}
|
|
32
|
+
var closingElementFix = fixer.replaceText(node.closingElement.name, (0, _eslintCodemodUtils.jsxIdentifier)(newName).toString());
|
|
33
|
+
return [openingElementFix, closingElementFix];
|
|
34
|
+
},
|
|
35
|
+
isSelfClosing: function isSelfClosing(node) {
|
|
36
|
+
return node.openingElement.selfClosing;
|
|
37
|
+
},
|
|
38
|
+
getAttributes: function getAttributes(node) {
|
|
39
|
+
return node.openingElement.attributes;
|
|
40
|
+
},
|
|
41
|
+
getAttributeByName: function getAttributeByName(node, name) {
|
|
42
|
+
return node.openingElement.attributes.find(function (attr) {
|
|
43
|
+
// Ignore anything other than JSXAttribute
|
|
44
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute')) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return attr.name.name === name;
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
containsSpreadAttributes: function containsSpreadAttributes(node) {
|
|
51
|
+
return node.openingElement.attributes.some(function (attr) {
|
|
52
|
+
return (0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXSpreadAttribute');
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Root = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
9
|
+
|
|
10
|
+
// Little bit unreadable, but better than duplicating the type
|
|
11
|
+
|
|
12
|
+
var Root = exports.Root = {
|
|
13
|
+
/**
|
|
14
|
+
* Note: This can return multiple ImportDeclarations for cases like:
|
|
15
|
+
* ```
|
|
16
|
+
* import { Stack } from '@atlaskit/primitives'
|
|
17
|
+
* import type { StackProps } from '@atlaskit/primitives'
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
findImportsByModule: function findImportsByModule(root, name) {
|
|
21
|
+
return root.filter(function (node) {
|
|
22
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'ImportDeclaration')) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (!(0, _eslintCodemodUtils.hasImportDeclaration)(node, name)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
insertImport: function insertImport(root, data, fixer) {
|
|
32
|
+
return fixer.insertTextBefore(root[0], "".concat((0, _eslintCodemodUtils.insertImportDeclaration)(data.module, data.specifiers), ";\n"));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
@@ -8,6 +8,7 @@ var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
|
8
8
|
var _createRule = require("../utils/create-rule");
|
|
9
9
|
var _config = require("./config");
|
|
10
10
|
var _transformers = require("./transformers");
|
|
11
|
+
var _emotionCss = require("./transformers/emotion-css");
|
|
11
12
|
var _utils = require("./utils");
|
|
12
13
|
var boxDocsUrl = 'https://atlassian.design/components/primitives/box';
|
|
13
14
|
var rule = (0, _createRule.createLintRule)({
|
|
@@ -59,84 +60,13 @@ var rule = (0, _createRule.createLintRule)({
|
|
|
59
60
|
});
|
|
60
61
|
},
|
|
61
62
|
// transforms <div css={...}> usages
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.name, 'JSXIdentifier')) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.parent, 'JSXElement')) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
var suggestBox = shouldSuggestBox(node.parent, context, config);
|
|
76
|
-
if (suggestBox) {
|
|
77
|
-
context.report({
|
|
78
|
-
node: node,
|
|
79
|
-
messageId: 'preferPrimitivesBox',
|
|
80
|
-
data: {
|
|
81
|
-
element: node.name.name
|
|
82
|
-
},
|
|
83
|
-
suggest: [{
|
|
84
|
-
desc: "Convert to Box",
|
|
85
|
-
fix: (0, _transformers.jsxElementToBoxTransformer)(node.parent, context)
|
|
86
|
-
}]
|
|
87
|
-
});
|
|
88
|
-
}
|
|
63
|
+
JSXElement: function JSXElement(node) {
|
|
64
|
+
_emotionCss.EmotionCSS.lint(node, {
|
|
65
|
+
context: context,
|
|
66
|
+
config: config
|
|
67
|
+
});
|
|
89
68
|
}
|
|
90
69
|
};
|
|
91
70
|
}
|
|
92
71
|
});
|
|
93
|
-
var shouldSuggestBox = function shouldSuggestBox(node, context, config
|
|
94
|
-
// scope: Scope.Scope,
|
|
95
|
-
) {
|
|
96
|
-
if (!node) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Currently we only support `div`, but possibly more in future
|
|
101
|
-
if (!(0, _utils.isValidTagName)(node)) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Currently we only support elements that contain only a `css` attr, possibly more in future
|
|
106
|
-
if (!(0, _utils.containsOnlySupportedAttrs)(node)) {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Get the `css` attr
|
|
111
|
-
var cssAttr = (0, _utils.getJSXAttributeByName)(node.openingElement, 'css');
|
|
112
|
-
|
|
113
|
-
// Get the prop value, e.g. `myStyles` in `css={myStyles}`
|
|
114
|
-
var cssVariableName = (0, _utils.getAttributeValueIdentifier)(cssAttr);
|
|
115
|
-
|
|
116
|
-
// `cssVariableName` could be undefined if the maker has
|
|
117
|
-
// done something like `css={[styles1, styles2]}`, `css={...styles}`, etc
|
|
118
|
-
if (!cssVariableName) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Make sure the variable is not used in multiple JSX elements:
|
|
124
|
-
* ```
|
|
125
|
-
* <div css={paddingStyles}></div>
|
|
126
|
-
* <input css={paddingStyles}></input>
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
if ((0, _utils.getVariableUsagesCount)(cssVariableName, context) !== 1) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Find where `cssVariableName` is defined. We're looking for `const myStyles = css({...})`
|
|
134
|
-
var cssVariableDefinition = (0, _eslintCodemodUtils.getIdentifierInParentScope)(context.getScope(), cssVariableName);
|
|
135
|
-
var cssVariableValue = (0, _utils.getVariableDefinitionValue)(cssVariableDefinition);
|
|
136
|
-
// Check if `cssVariableValue` is a function called `css()`
|
|
137
|
-
if (!cssVariableValue || !(0, _utils.isFunctionNamed)(cssVariableValue, 'css')) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
return (0, _utils.isValidCssPropertiesToTransform)(cssVariableValue.node.init, config);
|
|
141
|
-
};
|
|
142
72
|
var _default = exports.default = rule;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.EmotionCSS = void 0;
|
|
9
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
10
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
11
|
+
var ast = _interopRequireWildcard(require("../../../../ast-nodes"));
|
|
12
|
+
var _utils = require("../../utils");
|
|
13
|
+
var _cssToXcss = require("../css-to-xcss");
|
|
14
|
+
var supported = _interopRequireWildcard(require("./supported"));
|
|
15
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
18
|
+
|
|
19
|
+
var EmotionCSS = exports.EmotionCSS = {
|
|
20
|
+
lint: function lint(node, _ref) {
|
|
21
|
+
var context = _ref.context,
|
|
22
|
+
config = _ref.config;
|
|
23
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Check whether all criteria needed to make a transformation are met
|
|
28
|
+
if (!EmotionCSS._check(node, {
|
|
29
|
+
context: context,
|
|
30
|
+
config: config
|
|
31
|
+
})) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
context.report({
|
|
35
|
+
node: node.openingElement,
|
|
36
|
+
messageId: 'preferPrimitivesBox',
|
|
37
|
+
suggest: [{
|
|
38
|
+
desc: "Convert to Box",
|
|
39
|
+
fix: EmotionCSS._fix(node, {
|
|
40
|
+
context: context
|
|
41
|
+
})
|
|
42
|
+
}]
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
_check: function _check(node, _ref2) {
|
|
46
|
+
var _cssVariableValue$nod, _cssVariableValue$nod2;
|
|
47
|
+
var context = _ref2.context,
|
|
48
|
+
config = _ref2.config;
|
|
49
|
+
if (!config.patterns.includes('compiled-css-function')) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
var elementName = ast.JSXElement.getName(node);
|
|
56
|
+
if (!elementName) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Currently we only support `div`. This may change in future.
|
|
61
|
+
if (!supported.elements.includes(elementName)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Ignore elements that contain dangerous attributes like `id`.
|
|
66
|
+
if (!this._containsOnlySupportedAttributes(node)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Currently we don't transform anything to `Box` unless it defines styles
|
|
71
|
+
var cssAttr = ast.JSXElement.getAttributeByName(node, 'css');
|
|
72
|
+
if (!cssAttr) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Get `myStyles` in `css={myStyles}` as a string so we can search for the `const myStyles` VariableDefinition
|
|
77
|
+
var cssAttrValue = ast.JSXAttribute.getValue(cssAttr);
|
|
78
|
+
if ((cssAttrValue === null || cssAttrValue === void 0 ? void 0 : cssAttrValue.type) !== 'ExpressionStatement') {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// TODO: Everything below this line could be refactored to use `ast-nodes`.
|
|
83
|
+
|
|
84
|
+
// Bail if the styles are used on multiple JSXElements
|
|
85
|
+
if ((0, _utils.getVariableUsagesCount)(cssAttrValue.value, context) !== 1) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Find where `myStyles` is defined. We're looking for `const myStyles = css({...})`
|
|
90
|
+
var cssVariableDefinition = (0, _eslintCodemodUtils.getIdentifierInParentScope)(context.getScope(), cssAttrValue.value);
|
|
91
|
+
var cssVariableValue = (0, _utils.getVariableDefinitionValue)(cssVariableDefinition);
|
|
92
|
+
// Check if `cssVariableValue` is a function called `css()`
|
|
93
|
+
if (ast.FunctionCall.getName(cssVariableValue === null || cssVariableValue === void 0 || (_cssVariableValue$nod = cssVariableValue.node) === null || _cssVariableValue$nod === void 0 ? void 0 : _cssVariableValue$nod.init) !== 'css') {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (!(0, _utils.isValidCssPropertiesToTransform)(cssVariableValue === null || cssVariableValue === void 0 || (_cssVariableValue$nod2 = cssVariableValue.node) === null || _cssVariableValue$nod2 === void 0 ? void 0 : _cssVariableValue$nod2.init, config)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
var importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
|
|
100
|
+
|
|
101
|
+
// If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
|
|
102
|
+
if (importDeclaration.length > 1) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
},
|
|
107
|
+
_fix: function _fix(node, _ref3) {
|
|
108
|
+
var _this = this;
|
|
109
|
+
var context = _ref3.context;
|
|
110
|
+
return function (fixer) {
|
|
111
|
+
var importFix = _this._upsertImportDeclaration({
|
|
112
|
+
module: '@atlaskit/primitives',
|
|
113
|
+
specifiers: ['Box', 'xcss']
|
|
114
|
+
}, context, fixer);
|
|
115
|
+
var cssAttr = ast.JSXElement.getAttributeByName(node, 'css'); // Can strongly assert the type here, because we validated it exists in `check()`.
|
|
116
|
+
var attributeFix = ast.JSXAttribute.updateName(cssAttr, 'xcss', fixer);
|
|
117
|
+
var elementNameFixes = ast.JSXElement.updateName(node, 'Box', fixer);
|
|
118
|
+
var cssToXcssTransform = (0, _cssToXcss.cssToXcssTransformer)(node, context, fixer);
|
|
119
|
+
return [importFix, attributeFix].concat((0, _toConsumableArray2.default)(elementNameFixes), (0, _toConsumableArray2.default)(cssToXcssTransform)).filter(function (fix) {
|
|
120
|
+
return Boolean(fix);
|
|
121
|
+
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
/**
|
|
125
|
+
* Check that every attribute in the JSXElement is something we support.
|
|
126
|
+
* We do this via a whitelist in `this.attributes`. The result is we exclude
|
|
127
|
+
* dangerous attrs like `id` and `style`.
|
|
128
|
+
*/
|
|
129
|
+
_containsOnlySupportedAttributes: function _containsOnlySupportedAttributes(node) {
|
|
130
|
+
var attrs = ast.JSXElement.getAttributes(node);
|
|
131
|
+
return attrs.every(function (attr) {
|
|
132
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute')) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(attr.name, 'JSXIdentifier')) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
return supported.attributes.includes(attr.name.name);
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
/**
|
|
142
|
+
* Currently this is defined here because it's not very general purpose.
|
|
143
|
+
* If we were to move this to `ast-nodes`, half the implementation would be in `Root`,
|
|
144
|
+
* and the other half would be in `Import`.
|
|
145
|
+
*
|
|
146
|
+
* TODO: Refactor and move to `ast-nodes`
|
|
147
|
+
*
|
|
148
|
+
* Note: It does not handle default imports, namespace imports, or aliased imports.
|
|
149
|
+
*/
|
|
150
|
+
_upsertImportDeclaration: function _upsertImportDeclaration(_ref4, context, fixer) {
|
|
151
|
+
var module = _ref4.module,
|
|
152
|
+
specifiers = _ref4.specifiers;
|
|
153
|
+
// Find any imports that match the packageName
|
|
154
|
+
var root = context.getSourceCode().ast.body;
|
|
155
|
+
var importDeclarations = ast.Root.findImportsByModule(root, module);
|
|
156
|
+
|
|
157
|
+
// The import doesn't exist yet, we can just insert a whole new one
|
|
158
|
+
if (importDeclarations.length === 0) {
|
|
159
|
+
return ast.Root.insertImport(root, {
|
|
160
|
+
module: module,
|
|
161
|
+
specifiers: specifiers
|
|
162
|
+
}, fixer);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The import exists so, modify the existing one
|
|
166
|
+
return ast.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.styles = exports.elements = exports.attributes = void 0;
|
|
7
|
+
var elements = exports.elements = ['div'];
|
|
8
|
+
var attributes = exports.attributes = ['css'
|
|
9
|
+
// 'data-testid'
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
// TODO: https://product-fabric.atlassian.net/browse/DSP-16054
|
|
13
|
+
var spaceTokenMap = {
|
|
14
|
+
'0px': 'space.0',
|
|
15
|
+
'2px': 'space.025',
|
|
16
|
+
'4px': 'space.050',
|
|
17
|
+
'6px': 'space.075',
|
|
18
|
+
'8px': 'space.100',
|
|
19
|
+
'12px': 'space.150',
|
|
20
|
+
'16px': 'space.200',
|
|
21
|
+
'20px': 'space.250',
|
|
22
|
+
'24px': 'space.300',
|
|
23
|
+
'32px': 'space.400',
|
|
24
|
+
'40px': 'space.500',
|
|
25
|
+
'48px': 'space.600',
|
|
26
|
+
'64px': 'space.800',
|
|
27
|
+
'80px': 'space.1000'
|
|
28
|
+
};
|
|
29
|
+
var styles = exports.styles = {
|
|
30
|
+
padding: spaceTokenMap,
|
|
31
|
+
paddingBlock: spaceTokenMap,
|
|
32
|
+
paddingBlockEnd: spaceTokenMap,
|
|
33
|
+
paddingBlockStart: spaceTokenMap,
|
|
34
|
+
paddingBottom: spaceTokenMap,
|
|
35
|
+
paddingInline: spaceTokenMap,
|
|
36
|
+
paddingInlineEnd: spaceTokenMap,
|
|
37
|
+
paddingInlineStart: spaceTokenMap,
|
|
38
|
+
paddingLeft: spaceTokenMap,
|
|
39
|
+
paddingRight: spaceTokenMap,
|
|
40
|
+
paddingTop: spaceTokenMap,
|
|
41
|
+
margin: spaceTokenMap,
|
|
42
|
+
marginBlock: spaceTokenMap,
|
|
43
|
+
marginBlockEnd: spaceTokenMap,
|
|
44
|
+
marginBlockStart: spaceTokenMap,
|
|
45
|
+
marginBottom: spaceTokenMap,
|
|
46
|
+
marginInline: spaceTokenMap,
|
|
47
|
+
marginInlineEnd: spaceTokenMap,
|
|
48
|
+
marginInlineStart: spaceTokenMap,
|
|
49
|
+
marginLeft: spaceTokenMap,
|
|
50
|
+
marginRight: spaceTokenMap,
|
|
51
|
+
marginTop: spaceTokenMap
|
|
52
|
+
};
|
|
@@ -12,6 +12,9 @@ var _cssToXcss = require("../transformers/css-to-xcss");
|
|
|
12
12
|
var _convertAstObjectExpressionToJsObject = require("./convert-ast-object-expression-to-js-object");
|
|
13
13
|
var _excluded = ["unsupported"];
|
|
14
14
|
var isValidCssPropertiesToTransform = exports.isValidCssPropertiesToTransform = function isValidCssPropertiesToTransform(node, config) {
|
|
15
|
+
if (!node) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
15
18
|
var cssObjectExpression = node.arguments[0];
|
|
16
19
|
// Bail on empty object calls
|
|
17
20
|
if (!cssObjectExpression || !(0, _eslintCodemodUtils.isNodeOfType)(cssObjectExpression, 'ObjectExpression')) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
2
|
+
|
|
3
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
|
+
export const FunctionCall = {
|
|
5
|
+
getName(node) {
|
|
6
|
+
if (!isNodeOfType(node, 'CallExpression')) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
if (!isNodeOfType(node.callee, 'Identifier')) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return node.callee.name;
|
|
13
|
+
},
|
|
14
|
+
updateName(node, newName, fixer) {
|
|
15
|
+
return fixer.replaceText(node.callee, newName);
|
|
16
|
+
},
|
|
17
|
+
/**
|
|
18
|
+
* Function arguments can be many things:
|
|
19
|
+
* `css(myStyles, () => {}, undefined, 'literal', ...rest) // etc`
|
|
20
|
+
* They all need slightly different treatment.
|
|
21
|
+
*
|
|
22
|
+
* Currently 'getArgumentAtPos' only implements strategies for Literals and ObjectExpressions.
|
|
23
|
+
* If you need to support another type of arg, add it, and update the type.
|
|
24
|
+
*/
|
|
25
|
+
getArgumentAtPos(node, pos) {
|
|
26
|
+
const argument = node.arguments[pos];
|
|
27
|
+
if (isNodeOfType(argument, 'Literal') && argument.value) {
|
|
28
|
+
var _argument$value;
|
|
29
|
+
return {
|
|
30
|
+
type: 'Literal',
|
|
31
|
+
value: (_argument$value = argument.value) === null || _argument$value === void 0 ? void 0 : _argument$value.toString()
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (isNodeOfType(argument, 'ObjectExpression')) {
|
|
35
|
+
argument;
|
|
36
|
+
return {
|
|
37
|
+
type: 'ObjectExpression',
|
|
38
|
+
value: argument
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|