@atlaskit/eslint-plugin-design-system 8.18.0 → 8.18.1
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/rules/use-primitives/index.js +40 -35
- package/dist/cjs/rules/use-primitives/transformers/css-to-xcss.js +10 -3
- package/dist/cjs/rules/use-primitives/transformers/index.js +8 -1
- package/dist/cjs/rules/use-primitives/transformers/styled-component-to-primitive.js +68 -0
- package/dist/cjs/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +16 -0
- package/dist/cjs/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +39 -0
- package/dist/cjs/rules/use-primitives/utils/find-valid-styled-component-call.js +52 -0
- package/dist/cjs/rules/use-primitives/utils/index.js +21 -0
- package/dist/cjs/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +38 -0
- package/dist/cjs/rules/use-primitives/utils/is-valid-tag-name.js +2 -2
- package/dist/cjs/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
- package/dist/es2019/rules/use-primitives/index.js +41 -37
- package/dist/es2019/rules/use-primitives/transformers/css-to-xcss.js +9 -6
- package/dist/es2019/rules/use-primitives/transformers/index.js +2 -1
- package/dist/es2019/rules/use-primitives/transformers/styled-component-to-primitive.js +59 -0
- package/dist/es2019/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +14 -0
- package/dist/es2019/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +32 -0
- package/dist/es2019/rules/use-primitives/utils/find-valid-styled-component-call.js +47 -0
- package/dist/es2019/rules/use-primitives/utils/index.js +3 -0
- package/dist/es2019/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +32 -0
- package/dist/es2019/rules/use-primitives/utils/is-valid-tag-name.js +1 -1
- package/dist/es2019/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
- package/dist/esm/rules/use-primitives/index.js +41 -37
- package/dist/esm/rules/use-primitives/transformers/css-to-xcss.js +9 -2
- package/dist/esm/rules/use-primitives/transformers/index.js +2 -1
- package/dist/esm/rules/use-primitives/transformers/styled-component-to-primitive.js +61 -0
- package/dist/esm/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +16 -0
- package/dist/esm/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.js +34 -0
- package/dist/esm/rules/use-primitives/utils/find-valid-styled-component-call.js +47 -0
- package/dist/esm/rules/use-primitives/utils/index.js +3 -0
- package/dist/esm/rules/use-primitives/utils/is-valid-css-properties-to-transform.js +32 -0
- package/dist/esm/rules/use-primitives/utils/is-valid-tag-name.js +1 -1
- package/dist/esm/rules/use-primitives/utils/update-jsx-element-name.js +1 -1
- package/dist/types/rules/use-primitives/transformers/css-to-xcss.d.ts +2 -1
- package/dist/types/rules/use-primitives/transformers/index.d.ts +1 -0
- package/dist/types/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +13 -0
- package/dist/types/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +1 -1
- package/dist/types/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.d.ts +7 -0
- package/dist/types/rules/use-primitives/utils/find-valid-styled-component-call.d.ts +7 -0
- package/dist/types/rules/use-primitives/utils/index.d.ts +3 -0
- package/dist/types/rules/use-primitives/utils/is-valid-css-properties-to-transform.d.ts +3 -0
- package/dist/types/rules/use-primitives/utils/is-valid-tag-name.d.ts +1 -0
- package/dist/types/rules/use-primitives/utils/update-jsx-element-name.d.ts +1 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/css-to-xcss.d.ts +2 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/index.d.ts +1 -0
- package/dist/types-ts4.5/rules/use-primitives/transformers/styled-component-to-primitive.d.ts +13 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +1 -1
- package/dist/types-ts4.5/rules/use-primitives/utils/find-valid-jsx-usage-to-transform.d.ts +7 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/find-valid-styled-component-call.d.ts +7 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/index.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-css-properties-to-transform.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-tag-name.d.ts +1 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/update-jsx-element-name.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-design-system
|
|
2
2
|
|
|
3
|
+
## 8.18.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#64857](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/64857) [`1be24644c029`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/1be24644c029) - `use-primitives` is now capable of converting styled components. This is guarded by a config flag so this release is only a patch.
|
|
8
|
+
|
|
3
9
|
## 8.18.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
6
7
|
exports.default = void 0;
|
|
7
8
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
9
|
+
var _assign = _interopRequireDefault(require("lodash/assign"));
|
|
8
10
|
var _createRule = require("../utils/create-rule");
|
|
9
11
|
var _transformers = require("./transformers");
|
|
10
12
|
var _utils = require("./utils");
|
|
11
13
|
var boxDocsUrl = 'https://atlassian.design/components/primitives/box';
|
|
14
|
+
var defaultConfig = {
|
|
15
|
+
preview: false
|
|
16
|
+
};
|
|
12
17
|
var rule = (0, _createRule.createLintRule)({
|
|
13
18
|
meta: {
|
|
14
19
|
name: 'use-primitives',
|
|
@@ -21,11 +26,43 @@ var rule = (0, _createRule.createLintRule)({
|
|
|
21
26
|
severity: 'warn'
|
|
22
27
|
},
|
|
23
28
|
messages: {
|
|
24
|
-
preferPrimitivesBox: "This
|
|
29
|
+
preferPrimitivesBox: "This element can be replaced with a \"Box\" primitive. See ".concat(boxDocsUrl, " for additional guidance.")
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
32
|
create: function create(context) {
|
|
33
|
+
var mergedConfig = (0, _assign.default)({}, defaultConfig, context.options[0]);
|
|
28
34
|
return {
|
|
35
|
+
// transforms styled.<html>(...) usages
|
|
36
|
+
CallExpression: function CallExpression(node) {
|
|
37
|
+
if (!mergedConfig.preview) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'CallExpression')) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
var styledComponentVariableRef = (0, _utils.findValidStyledComponentCall)(node);
|
|
44
|
+
if (!styledComponentVariableRef || !(0, _eslintCodemodUtils.isNodeOfType)(styledComponentVariableRef.id, 'Identifier') || !(0, _utils.isValidCssPropertiesToTransform)(node)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
var styledComponentJsxRef = (0, _utils.findValidJsxUsageToTransform)(styledComponentVariableRef.id.name, context.getScope());
|
|
48
|
+
if (!styledComponentJsxRef) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// if we have both references at this point then we can offer a fix \o/
|
|
53
|
+
context.report({
|
|
54
|
+
node: styledComponentVariableRef,
|
|
55
|
+
messageId: 'preferPrimitivesBox',
|
|
56
|
+
suggest: [{
|
|
57
|
+
desc: "Convert ".concat(styledComponentVariableRef.id.name, " to Box"),
|
|
58
|
+
fix: (0, _transformers.styledComponentToPrimitive)({
|
|
59
|
+
stylesRef: styledComponentVariableRef,
|
|
60
|
+
jsxRef: styledComponentJsxRef
|
|
61
|
+
}, context)
|
|
62
|
+
}]
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
// transforms <div css={...}> usages
|
|
29
66
|
JSXOpeningElement: function JSXOpeningElement(node) {
|
|
30
67
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXOpeningElement')) {
|
|
31
68
|
return;
|
|
@@ -97,42 +134,10 @@ var shouldSuggestBox = function shouldSuggestBox(node, context
|
|
|
97
134
|
// Find where `cssVariableName` is defined. We're looking for `const myStyles = css({...})`
|
|
98
135
|
var cssVariableDefinition = (0, _eslintCodemodUtils.getIdentifierInParentScope)(context.getScope(), cssVariableName);
|
|
99
136
|
var cssVariableValue = (0, _utils.getVariableDefinitionValue)(cssVariableDefinition);
|
|
100
|
-
|
|
101
137
|
// Check if `cssVariableValue` is a function called `css()`
|
|
102
|
-
if (!(0, _utils.isFunctionNamed)(cssVariableValue, 'css')) {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// get the `{}` in `css({})`
|
|
107
|
-
// Zero indexed
|
|
108
|
-
var cssObjectExpression = (0, _utils.getFunctionArgumentAtPos)(cssVariableValue, 0);
|
|
109
|
-
|
|
110
|
-
// Bail on empty `css()` calls
|
|
111
|
-
if (!cssObjectExpression) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
var cssObject = (0, _utils.convertASTObjectExpressionToJSObject)(cssObjectExpression);
|
|
115
|
-
|
|
116
|
-
// Bail if there are less or more than 1 styles defined
|
|
117
|
-
if (Object.keys(cssObject).length !== 1) {
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// NOTE: Our approach with this lint rule is to strictly whitelist css properties we can map.
|
|
122
|
-
// It means we have to provide mappings for everything (e.g. `display: block`).
|
|
123
|
-
// However, from a maker's experience, it's much better that the rule doesn't report (if we miss a mapping)
|
|
124
|
-
// than the rule reporting on things that can't be mapped.
|
|
125
|
-
var containsOnlyValidStyles = Object.keys(cssObject).every(function (styleProperty) {
|
|
126
|
-
var styleValue = cssObject[styleProperty];
|
|
127
|
-
return _transformers.supportedStylesMap[styleProperty] &&
|
|
128
|
-
// Is the key something we can map
|
|
129
|
-
_transformers.supportedStylesMap[styleProperty][styleValue] // Is the value something we can map
|
|
130
|
-
;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (!containsOnlyValidStyles) {
|
|
138
|
+
if (!cssVariableValue || !(0, _utils.isFunctionNamed)(cssVariableValue, 'css')) {
|
|
134
139
|
return false;
|
|
135
140
|
}
|
|
136
|
-
return
|
|
141
|
+
return (0, _utils.isValidCssPropertiesToTransform)(cssVariableValue.node.init);
|
|
137
142
|
};
|
|
138
143
|
var _default = exports.default = rule;
|
|
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports.supportedStylesMap = exports.spaceTokenMap = exports.cssToXcssTransformer = void 0;
|
|
7
|
+
exports.supportedStylesMap = exports.styledObjectToXcssTokens = exports.spaceTokenMap = exports.cssToXcssTransformer = void 0;
|
|
8
8
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
9
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
10
10
|
var _utils = require("../utils");
|
|
@@ -36,7 +36,14 @@ var cssToXcssTransformer = exports.cssToXcssTransformer = function cssToXcssTran
|
|
|
36
36
|
var cssObjectExpression = (0, _utils.getFunctionArgumentAtPos)(cssVariableValue, 0);
|
|
37
37
|
return [
|
|
38
38
|
// Update `css` function name to `xcss`.
|
|
39
|
-
fixer.replaceText(cssVariableValue.node.init.callee, (0, _eslintCodemodUtils.identifier)('xcss').toString())].concat((0, _toConsumableArray2.default)(cssObjectExpression
|
|
39
|
+
fixer.replaceText(cssVariableValue.node.init.callee, (0, _eslintCodemodUtils.identifier)('xcss').toString())].concat((0, _toConsumableArray2.default)(styledObjectToXcssTokens(cssObjectExpression, fixer)));
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Update css object values to xcss values. e.g. `'8px'` -> `'space.100'`
|
|
43
|
+
// Note: `properties` in this context is a group of AST nodes that make up a key/value pair in an object.
|
|
44
|
+
// e.g. `padding: '8px'`. For clarity, it's renamed to `entry` inside the `.map()`.
|
|
45
|
+
var styledObjectToXcssTokens = exports.styledObjectToXcssTokens = function styledObjectToXcssTokens(styles, fixer) {
|
|
46
|
+
return styles.properties.map(function (entry) {
|
|
40
47
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(entry, 'Property')) {
|
|
41
48
|
return;
|
|
42
49
|
}
|
|
@@ -51,7 +58,7 @@ var cssToXcssTransformer = exports.cssToXcssTransformer = function cssToXcssTran
|
|
|
51
58
|
return;
|
|
52
59
|
}
|
|
53
60
|
return fixer.replaceText(entry.value, (0, _eslintCodemodUtils.literal)("'".concat(supportedStylesMap[entry.key.name][value], "'")).toString());
|
|
54
|
-
})
|
|
61
|
+
});
|
|
55
62
|
};
|
|
56
63
|
var spaceTokenMap = exports.spaceTokenMap = {
|
|
57
64
|
'0px': 'space.0',
|
|
@@ -21,6 +21,12 @@ Object.defineProperty(exports, "spaceTokenMap", {
|
|
|
21
21
|
return _cssToXcss.spaceTokenMap;
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
|
+
Object.defineProperty(exports, "styledComponentToPrimitive", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _styledComponentToPrimitive.styledComponentToPrimitive;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
24
30
|
Object.defineProperty(exports, "supportedStylesMap", {
|
|
25
31
|
enumerable: true,
|
|
26
32
|
get: function get() {
|
|
@@ -28,4 +34,5 @@ Object.defineProperty(exports, "supportedStylesMap", {
|
|
|
28
34
|
}
|
|
29
35
|
});
|
|
30
36
|
var _cssToXcss = require("./css-to-xcss");
|
|
31
|
-
var _jsxElementToBox = require("./jsx-element-to-box");
|
|
37
|
+
var _jsxElementToBox = require("./jsx-element-to-box");
|
|
38
|
+
var _styledComponentToPrimitive = require("./styled-component-to-primitive");
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.styledComponentToPrimitive = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
10
|
+
var _utils = require("../utils");
|
|
11
|
+
var _cssToXcss = require("./css-to-xcss");
|
|
12
|
+
/**
|
|
13
|
+
* All required validation steps have been taken care of before this
|
|
14
|
+
* transformer is called, so it just goes ahead providing all necessary fixes
|
|
15
|
+
*/
|
|
16
|
+
var styledComponentToPrimitive = exports.styledComponentToPrimitive = function styledComponentToPrimitive(_ref, context) {
|
|
17
|
+
var stylesRef = _ref.stylesRef,
|
|
18
|
+
jsxRef = _ref.jsxRef;
|
|
19
|
+
return function (fixer) {
|
|
20
|
+
// generates the new variable name: MyComponent -> myComponentStyles
|
|
21
|
+
var calculatedStylesVariableName = (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.id, 'Identifier') && "".concat(stylesRef.id.name.replace(stylesRef.id.name[0], stylesRef.id.name[0].toLowerCase()), "Styles");
|
|
22
|
+
if (!calculatedStylesVariableName) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
var importFixes = (0, _utils.upsertImportDeclaration)({
|
|
26
|
+
packageName: '@atlaskit/primitives',
|
|
27
|
+
specifiers: ['Box', 'xcss']
|
|
28
|
+
}, context, fixer);
|
|
29
|
+
var stylesFixes = convertStyledComponentToXcss(stylesRef, calculatedStylesVariableName, fixer);
|
|
30
|
+
var jsxFixes = convertJsxCallSite(jsxRef, calculatedStylesVariableName, fixer);
|
|
31
|
+
return [importFixes].concat(stylesFixes, jsxFixes).filter(function (fix) {
|
|
32
|
+
return Boolean(fix);
|
|
33
|
+
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
var convertStyledComponentToXcss = function convertStyledComponentToXcss(stylesRef, newStylesVariableName, fixer) {
|
|
38
|
+
var fixes = [];
|
|
39
|
+
|
|
40
|
+
// renames the variable from MyComponent to myComponentStyles
|
|
41
|
+
fixes.push(fixer.replaceText(stylesRef.id, newStylesVariableName));
|
|
42
|
+
|
|
43
|
+
// renames the function call from styled.<tag> to xcss
|
|
44
|
+
if (stylesRef.init && (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.init, 'CallExpression')) {
|
|
45
|
+
fixes.push(fixer.replaceText(stylesRef.init.callee, 'xcss'));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// converts CSS values to XCSS-compatible tokens
|
|
49
|
+
if (stylesRef.init && (0, _eslintCodemodUtils.isNodeOfType)(stylesRef.init, 'CallExpression')) {
|
|
50
|
+
var objectExpression = stylesRef.init.arguments[0];
|
|
51
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(objectExpression, 'ObjectExpression')) {
|
|
52
|
+
fixes.push.apply(fixes, (0, _toConsumableArray2.default)((0, _cssToXcss.styledObjectToXcssTokens)(objectExpression, fixer)));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return fixes;
|
|
56
|
+
};
|
|
57
|
+
var convertJsxCallSite = function convertJsxCallSite(jsxRef, newStylesVariableName, fixer) {
|
|
58
|
+
var fixes = [];
|
|
59
|
+
|
|
60
|
+
// renames the JSX call site
|
|
61
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(jsxRef.parent, 'JSXElement')) {
|
|
62
|
+
fixes.push.apply(fixes, (0, _toConsumableArray2.default)((0, _utils.updateJSXElementName)(jsxRef.parent, 'Box', fixer)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// adds xcss prop
|
|
66
|
+
fixes.push(fixer.insertTextAfter(jsxRef.name, " xcss={".concat(newStylesVariableName, "}")));
|
|
67
|
+
return fixes;
|
|
68
|
+
};
|
|
@@ -12,6 +12,22 @@ var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
|
12
12
|
*/
|
|
13
13
|
var convertASTObjectExpressionToJSObject = exports.convertASTObjectExpressionToJSObject = function convertASTObjectExpressionToJSObject(styles) {
|
|
14
14
|
var styleObj = {};
|
|
15
|
+
|
|
16
|
+
// if we see any spread props we stop and return false to indicate this is unsupported
|
|
17
|
+
if (!styles.properties.every(function (prop) {
|
|
18
|
+
return (0, _eslintCodemodUtils.isNodeOfType)(prop, 'Property');
|
|
19
|
+
})) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// TODO: We need to harden this logic.
|
|
24
|
+
// It currently generates a false positive for:
|
|
25
|
+
// styled.div({
|
|
26
|
+
// marginTop: "0px",
|
|
27
|
+
// marginBottom: token("space.100", "8px"),
|
|
28
|
+
// })
|
|
29
|
+
// as the value for `marginBottom` is not a string, so it is just skipped
|
|
30
|
+
// from the resulting map and this causes the rule to trigger when it shouldn't
|
|
15
31
|
styles.properties.forEach(function (prop) {
|
|
16
32
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(prop, 'Property')) {
|
|
17
33
|
return;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findValidJsxUsageToTransform = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
/**
|
|
9
|
+
* Given a component name finds its JSX usages and performs some
|
|
10
|
+
* additional validations to ensure transformation can be done correctly
|
|
11
|
+
*/
|
|
12
|
+
var findValidJsxUsageToTransform = exports.findValidJsxUsageToTransform = function findValidJsxUsageToTransform(componentName, scope) {
|
|
13
|
+
var variableDeclaration = scope.variables.find(function (v) {
|
|
14
|
+
return v.name === componentName;
|
|
15
|
+
});
|
|
16
|
+
if (!variableDeclaration) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// length here should be exactly 2 to indicate only two references:
|
|
21
|
+
// one being the variable declaration itself
|
|
22
|
+
// second being the JSX call site
|
|
23
|
+
// we might consider handling multiple local JSX call sites in the future
|
|
24
|
+
// but "this is good enough for now"™️
|
|
25
|
+
if (variableDeclaration.references.length !== 2) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
var jsxUsage = variableDeclaration.references[1].identifier;
|
|
29
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxUsage, 'JSXIdentifier')) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
var jsxOpeningElement = jsxUsage.parent;
|
|
33
|
+
// we could relatively easily support some safe attributes like
|
|
34
|
+
// "id" or "testId" but support will be expanded as we go
|
|
35
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxOpeningElement, 'JSXOpeningElement') || jsxOpeningElement.attributes.length > 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return jsxOpeningElement;
|
|
39
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findValidStyledComponentCall = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var _isValidTagName = require("./is-valid-tag-name");
|
|
9
|
+
/**
|
|
10
|
+
* returns a variable reference if preconditions are favourable for
|
|
11
|
+
* the transformation to proceed, undefined otherwise.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
var findValidStyledComponentCall = exports.findValidStyledComponentCall = function findValidStyledComponentCall(node) {
|
|
15
|
+
// halts unless we are dealing with a styled component
|
|
16
|
+
if (!isStyledCallExpression(node)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
// halts if the component is being exported directly
|
|
20
|
+
if ((0, _eslintCodemodUtils.closestOfType)(node, 'ExportNamedDeclaration')) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
var styledComponentVariableRef = node.parent;
|
|
24
|
+
// halts if the styled component is not assigned to a variable immediately
|
|
25
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(styledComponentVariableRef, 'VariableDeclarator')) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
return styledComponentVariableRef;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Some verbose precondition checks but all it does is check
|
|
33
|
+
* a call expression is of form `styled.div` or `styled2.div`
|
|
34
|
+
*
|
|
35
|
+
* In the future it could be enhanced to double check `styled` and `styled2`
|
|
36
|
+
* are Compiled imports but as is should work for the majority of use cases
|
|
37
|
+
*/
|
|
38
|
+
var isStyledCallExpression = function isStyledCallExpression(call) {
|
|
39
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(call, 'CallExpression')) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee, 'MemberExpression')) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee.object, 'Identifier') || !(0, _eslintCodemodUtils.isNodeOfType)(call.callee.property, 'Identifier')) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (/^styled2?$/.test(call.callee.object.name) && _isValidTagName.validPrimitiveElements.has(call.callee.property.name)) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
};
|
|
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "convertASTObjectExpressionToJSObject", {
|
|
|
15
15
|
return _convertAstObjectExpressionToJsObject.convertASTObjectExpressionToJSObject;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(exports, "findValidJsxUsageToTransform", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _findValidJsxUsageToTransform.findValidJsxUsageToTransform;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "findValidStyledComponentCall", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _findValidStyledComponentCall.findValidStyledComponentCall;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
18
30
|
Object.defineProperty(exports, "getAttributeValueIdentifier", {
|
|
19
31
|
enumerable: true,
|
|
20
32
|
get: function get() {
|
|
@@ -51,6 +63,12 @@ Object.defineProperty(exports, "isFunctionNamed", {
|
|
|
51
63
|
return _isFunctionNamed.isFunctionNamed;
|
|
52
64
|
}
|
|
53
65
|
});
|
|
66
|
+
Object.defineProperty(exports, "isValidCssPropertiesToTransform", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function get() {
|
|
69
|
+
return _isValidCssPropertiesToTransform.isValidCssPropertiesToTransform;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
54
72
|
Object.defineProperty(exports, "isValidTagName", {
|
|
55
73
|
enumerable: true,
|
|
56
74
|
get: function get() {
|
|
@@ -77,12 +95,15 @@ Object.defineProperty(exports, "upsertImportDeclaration", {
|
|
|
77
95
|
});
|
|
78
96
|
var _containsOnlySupportedAttrs = require("./contains-only-supported-attrs");
|
|
79
97
|
var _convertAstObjectExpressionToJsObject = require("./convert-ast-object-expression-to-js-object");
|
|
98
|
+
var _findValidJsxUsageToTransform = require("./find-valid-jsx-usage-to-transform");
|
|
99
|
+
var _findValidStyledComponentCall = require("./find-valid-styled-component-call");
|
|
80
100
|
var _getAttributeValueIdentifier = require("./get-attribute-value-identifier");
|
|
81
101
|
var _getFunctionArgumentAtPos = require("./get-function-argument-at-pos");
|
|
82
102
|
var _getJsxAttributeByName = require("./get-jsx-attribute-by-name");
|
|
83
103
|
var _getVariableDefinitionValue = require("./get-variable-definition-value");
|
|
84
104
|
var _getVariableUsageCount = require("./get-variable-usage-count");
|
|
85
105
|
var _isFunctionNamed = require("./is-function-named");
|
|
106
|
+
var _isValidCssPropertiesToTransform = require("./is-valid-css-properties-to-transform");
|
|
86
107
|
var _isValidTagName = require("./is-valid-tag-name");
|
|
87
108
|
var _updateJsxAttributeByName = require("./update-jsx-attribute-by-name");
|
|
88
109
|
var _updateJsxElementName = require("./update-jsx-element-name");
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isValidCssPropertiesToTransform = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var _cssToXcss = require("../transformers/css-to-xcss");
|
|
9
|
+
var _convertAstObjectExpressionToJsObject = require("./convert-ast-object-expression-to-js-object");
|
|
10
|
+
var isValidCssPropertiesToTransform = exports.isValidCssPropertiesToTransform = function isValidCssPropertiesToTransform(node) {
|
|
11
|
+
var cssObjectExpression = node.arguments[0];
|
|
12
|
+
// Bail on empty object calls
|
|
13
|
+
if (!cssObjectExpression || !(0, _eslintCodemodUtils.isNodeOfType)(cssObjectExpression, 'ObjectExpression')) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
var cssObject = (0, _convertAstObjectExpressionToJsObject.convertASTObjectExpressionToJSObject)(cssObjectExpression);
|
|
17
|
+
// Bail if there are less or more than 1 styles defined
|
|
18
|
+
if (!cssObject || Object.keys(cssObject).length !== 1) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// NOTE: Our approach with this lint rule is to strictly whitelist css properties we can map.
|
|
23
|
+
// It means we have to provide mappings for everything (e.g. `display: block`).
|
|
24
|
+
// However, from a maker's experience, it's much better that the rule doesn't report (if we miss a mapping)
|
|
25
|
+
// than the rule reporting on things that can't be mapped.
|
|
26
|
+
var containsOnlyValidStyles = Object.keys(cssObject).every(function (styleProperty) {
|
|
27
|
+
var styleValue = cssObject[styleProperty];
|
|
28
|
+
return _cssToXcss.supportedStylesMap[styleProperty] &&
|
|
29
|
+
// Is the key something we can map
|
|
30
|
+
_cssToXcss.supportedStylesMap[styleProperty][styleValue] // Is the value something we can map
|
|
31
|
+
;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (!containsOnlyValidStyles) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isValidTagName = void 0;
|
|
7
|
-
var validPrimitiveElements = new Set(['div']);
|
|
6
|
+
exports.validPrimitiveElements = exports.isValidTagName = void 0;
|
|
7
|
+
var validPrimitiveElements = exports.validPrimitiveElements = new Set(['div']);
|
|
8
8
|
var isValidTagName = exports.isValidTagName = function isValidTagName(node) {
|
|
9
9
|
if (node.openingElement.name.type !== 'JSXIdentifier') {
|
|
10
10
|
return false;
|
|
@@ -12,5 +12,5 @@ var updateJSXElementName = exports.updateJSXElementName = function updateJSXElem
|
|
|
12
12
|
var newClosingElement = closingElement &&
|
|
13
13
|
// Self closing tags, like `<div />` don't need to have the closing tag updated
|
|
14
14
|
fixer.replaceText(closingElement.name, (0, _eslintCodemodUtils.jsxIdentifier)(newName).toString());
|
|
15
|
-
return [newOpeningElement, newClosingElement];
|
|
15
|
+
return [newOpeningElement, newClosingElement || undefined];
|
|
16
16
|
};
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { getIdentifierInParentScope, isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import assign from 'lodash/assign';
|
|
2
3
|
import { createLintRule } from '../utils/create-rule';
|
|
3
|
-
import { jsxElementToBoxTransformer,
|
|
4
|
-
import { containsOnlySupportedAttrs,
|
|
4
|
+
import { jsxElementToBoxTransformer, styledComponentToPrimitive } from './transformers';
|
|
5
|
+
import { containsOnlySupportedAttrs, findValidJsxUsageToTransform, findValidStyledComponentCall, getAttributeValueIdentifier, getJSXAttributeByName, getVariableDefinitionValue, getVariableUsagesCount, isFunctionNamed, isValidCssPropertiesToTransform, isValidTagName } from './utils';
|
|
5
6
|
const boxDocsUrl = 'https://atlassian.design/components/primitives/box';
|
|
7
|
+
const defaultConfig = {
|
|
8
|
+
preview: false
|
|
9
|
+
};
|
|
6
10
|
const rule = createLintRule({
|
|
7
11
|
meta: {
|
|
8
12
|
name: 'use-primitives',
|
|
@@ -15,11 +19,43 @@ const rule = createLintRule({
|
|
|
15
19
|
severity: 'warn'
|
|
16
20
|
},
|
|
17
21
|
messages: {
|
|
18
|
-
preferPrimitivesBox: `This
|
|
22
|
+
preferPrimitivesBox: `This element can be replaced with a "Box" primitive. See ${boxDocsUrl} for additional guidance.`
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
25
|
create(context) {
|
|
26
|
+
const mergedConfig = assign({}, defaultConfig, context.options[0]);
|
|
22
27
|
return {
|
|
28
|
+
// transforms styled.<html>(...) usages
|
|
29
|
+
CallExpression(node) {
|
|
30
|
+
if (!mergedConfig.preview) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!isNodeOfType(node, 'CallExpression')) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const styledComponentVariableRef = findValidStyledComponentCall(node);
|
|
37
|
+
if (!styledComponentVariableRef || !isNodeOfType(styledComponentVariableRef.id, 'Identifier') || !isValidCssPropertiesToTransform(node)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const styledComponentJsxRef = findValidJsxUsageToTransform(styledComponentVariableRef.id.name, context.getScope());
|
|
41
|
+
if (!styledComponentJsxRef) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// if we have both references at this point then we can offer a fix \o/
|
|
46
|
+
context.report({
|
|
47
|
+
node: styledComponentVariableRef,
|
|
48
|
+
messageId: 'preferPrimitivesBox',
|
|
49
|
+
suggest: [{
|
|
50
|
+
desc: `Convert ${styledComponentVariableRef.id.name} to Box`,
|
|
51
|
+
fix: styledComponentToPrimitive({
|
|
52
|
+
stylesRef: styledComponentVariableRef,
|
|
53
|
+
jsxRef: styledComponentJsxRef
|
|
54
|
+
}, context)
|
|
55
|
+
}]
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
// transforms <div css={...}> usages
|
|
23
59
|
JSXOpeningElement(node) {
|
|
24
60
|
if (!isNodeOfType(node, 'JSXOpeningElement')) {
|
|
25
61
|
return;
|
|
@@ -91,42 +127,10 @@ const shouldSuggestBox = (node, context
|
|
|
91
127
|
// Find where `cssVariableName` is defined. We're looking for `const myStyles = css({...})`
|
|
92
128
|
const cssVariableDefinition = getIdentifierInParentScope(context.getScope(), cssVariableName);
|
|
93
129
|
const cssVariableValue = getVariableDefinitionValue(cssVariableDefinition);
|
|
94
|
-
|
|
95
130
|
// Check if `cssVariableValue` is a function called `css()`
|
|
96
|
-
if (!isFunctionNamed(cssVariableValue, 'css')) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// get the `{}` in `css({})`
|
|
101
|
-
// Zero indexed
|
|
102
|
-
const cssObjectExpression = getFunctionArgumentAtPos(cssVariableValue, 0);
|
|
103
|
-
|
|
104
|
-
// Bail on empty `css()` calls
|
|
105
|
-
if (!cssObjectExpression) {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
const cssObject = convertASTObjectExpressionToJSObject(cssObjectExpression);
|
|
109
|
-
|
|
110
|
-
// Bail if there are less or more than 1 styles defined
|
|
111
|
-
if (Object.keys(cssObject).length !== 1) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// NOTE: Our approach with this lint rule is to strictly whitelist css properties we can map.
|
|
116
|
-
// It means we have to provide mappings for everything (e.g. `display: block`).
|
|
117
|
-
// However, from a maker's experience, it's much better that the rule doesn't report (if we miss a mapping)
|
|
118
|
-
// than the rule reporting on things that can't be mapped.
|
|
119
|
-
const containsOnlyValidStyles = Object.keys(cssObject).every(styleProperty => {
|
|
120
|
-
const styleValue = cssObject[styleProperty];
|
|
121
|
-
return supportedStylesMap[styleProperty] &&
|
|
122
|
-
// Is the key something we can map
|
|
123
|
-
supportedStylesMap[styleProperty][styleValue] // Is the value something we can map
|
|
124
|
-
;
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
if (!containsOnlyValidStyles) {
|
|
131
|
+
if (!cssVariableValue || !isFunctionNamed(cssVariableValue, 'css')) {
|
|
128
132
|
return false;
|
|
129
133
|
}
|
|
130
|
-
return
|
|
134
|
+
return isValidCssPropertiesToTransform(cssVariableValue.node.init);
|
|
131
135
|
};
|
|
132
136
|
export default rule;
|
|
@@ -28,11 +28,14 @@ export const cssToXcssTransformer = (node, context, fixer) => {
|
|
|
28
28
|
const cssObjectExpression = getFunctionArgumentAtPos(cssVariableValue, 0);
|
|
29
29
|
return [
|
|
30
30
|
// Update `css` function name to `xcss`.
|
|
31
|
-
fixer.replaceText(cssVariableValue.node.init.callee, identifier('xcss').toString()),
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
fixer.replaceText(cssVariableValue.node.init.callee, identifier('xcss').toString()), ...styledObjectToXcssTokens(cssObjectExpression, fixer)];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Update css object values to xcss values. e.g. `'8px'` -> `'space.100'`
|
|
35
|
+
// Note: `properties` in this context is a group of AST nodes that make up a key/value pair in an object.
|
|
36
|
+
// e.g. `padding: '8px'`. For clarity, it's renamed to `entry` inside the `.map()`.
|
|
37
|
+
export const styledObjectToXcssTokens = (styles, fixer) => {
|
|
38
|
+
return styles.properties.map(entry => {
|
|
36
39
|
if (!isNodeOfType(entry, 'Property')) {
|
|
37
40
|
return;
|
|
38
41
|
}
|
|
@@ -47,7 +50,7 @@ export const cssToXcssTransformer = (node, context, fixer) => {
|
|
|
47
50
|
return;
|
|
48
51
|
}
|
|
49
52
|
return fixer.replaceText(entry.value, literal(`'${supportedStylesMap[entry.key.name][value]}'`).toString());
|
|
50
|
-
})
|
|
53
|
+
});
|
|
51
54
|
};
|
|
52
55
|
export const spaceTokenMap = {
|
|
53
56
|
'0px': 'space.0',
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { cssToXcssTransformer, supportedStylesMap, spaceTokenMap } from './css-to-xcss';
|
|
2
|
-
export { jsxElementToBoxTransformer } from './jsx-element-to-box';
|
|
2
|
+
export { jsxElementToBoxTransformer } from './jsx-element-to-box';
|
|
3
|
+
export { styledComponentToPrimitive } from './styled-component-to-primitive';
|