@atlaskit/eslint-plugin-design-system 8.34.0 → 8.36.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 +28 -0
- package/constellation/use-primitives-text/usage.mdx +13 -3
- package/dist/cjs/rules/ensure-design-token-usage/color.js +5 -0
- package/dist/cjs/rules/use-primitives/transformers/compiled-styled/find-valid-jsx-usage-to-transform.js +24 -1
- package/dist/cjs/rules/use-primitives/transformers/compiled-styled/index.js +2 -1
- package/dist/cjs/rules/use-primitives-text/config/index.js +2 -1
- package/dist/cjs/rules/use-primitives-text/transformers/common.js +10 -1
- package/dist/cjs/rules/use-primitives-text/transformers/emphasis-elements.js +6 -3
- package/dist/cjs/rules/use-primitives-text/transformers/paragraph-elements.js +19 -4
- package/dist/cjs/rules/use-primitives-text/transformers/span-elements.js +6 -4
- package/dist/cjs/rules/use-primitives-text/transformers/strong-elements.js +6 -3
- package/dist/cjs/rules/utils/is-node.js +18 -1
- package/dist/es2019/rules/ensure-design-token-usage/color.js +6 -1
- package/dist/es2019/rules/use-primitives/transformers/compiled-styled/find-valid-jsx-usage-to-transform.js +17 -3
- package/dist/es2019/rules/use-primitives/transformers/compiled-styled/index.js +2 -1
- package/dist/es2019/rules/use-primitives-text/config/index.js +2 -1
- package/dist/es2019/rules/use-primitives-text/transformers/common.js +9 -2
- package/dist/es2019/rules/use-primitives-text/transformers/emphasis-elements.js +7 -4
- package/dist/es2019/rules/use-primitives-text/transformers/paragraph-elements.js +20 -5
- package/dist/es2019/rules/use-primitives-text/transformers/span-elements.js +7 -5
- package/dist/es2019/rules/use-primitives-text/transformers/strong-elements.js +7 -4
- package/dist/es2019/rules/utils/is-node.js +17 -0
- package/dist/esm/rules/ensure-design-token-usage/color.js +6 -1
- package/dist/esm/rules/use-primitives/transformers/compiled-styled/find-valid-jsx-usage-to-transform.js +23 -2
- package/dist/esm/rules/use-primitives/transformers/compiled-styled/index.js +2 -1
- package/dist/esm/rules/use-primitives-text/config/index.js +2 -1
- package/dist/esm/rules/use-primitives-text/transformers/common.js +9 -2
- package/dist/esm/rules/use-primitives-text/transformers/emphasis-elements.js +7 -4
- package/dist/esm/rules/use-primitives-text/transformers/paragraph-elements.js +20 -5
- package/dist/esm/rules/use-primitives-text/transformers/span-elements.js +7 -5
- package/dist/esm/rules/use-primitives-text/transformers/strong-elements.js +7 -4
- package/dist/esm/rules/utils/is-node.js +17 -0
- package/dist/types/rules/use-primitives/config/index.d.ts +1 -1
- package/dist/types/rules/use-primitives/transformers/compiled-styled/find-valid-jsx-usage-to-transform.d.ts +3 -1
- package/dist/types/rules/use-primitives/transformers/compiled-styled/index.d.ts +1 -1
- package/dist/types/rules/use-primitives/transformers/emotion-css/index.d.ts +1 -2
- package/dist/types/rules/use-primitives-text/config/index.d.ts +1 -0
- package/dist/types/rules/use-primitives-text/transformers/common.d.ts +6 -0
- package/dist/types/rules/use-primitives-text/transformers/emphasis-elements.d.ts +2 -10
- package/dist/types/rules/use-primitives-text/transformers/paragraph-elements.d.ts +4 -12
- package/dist/types/rules/use-primitives-text/transformers/span-elements.d.ts +2 -10
- package/dist/types/rules/use-primitives-text/transformers/strong-elements.d.ts +2 -10
- package/dist/types/rules/use-tokens-typography/utils.d.ts +33 -0
- package/dist/types/rules/utils/is-node.d.ts +1 -0
- package/dist/types-ts4.5/rules/use-primitives/config/index.d.ts +1 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/find-valid-jsx-usage-to-transform.d.ts +3 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/compiled-styled/index.d.ts +1 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/index.d.ts +1 -2
- package/dist/types-ts4.5/rules/use-primitives-text/config/index.d.ts +1 -0
- package/dist/types-ts4.5/rules/use-primitives-text/transformers/common.d.ts +6 -0
- package/dist/types-ts4.5/rules/use-primitives-text/transformers/emphasis-elements.d.ts +2 -10
- package/dist/types-ts4.5/rules/use-primitives-text/transformers/paragraph-elements.d.ts +4 -12
- package/dist/types-ts4.5/rules/use-primitives-text/transformers/span-elements.d.ts +2 -10
- package/dist/types-ts4.5/rules/use-primitives-text/transformers/strong-elements.d.ts +2 -10
- package/dist/types-ts4.5/rules/use-tokens-typography/utils.d.ts +33 -0
- package/dist/types-ts4.5/rules/utils/is-node.d.ts +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-design-system
|
|
2
2
|
|
|
3
|
+
## 8.36.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#79810](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/79810) [`8c6e96aa3cf0`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8c6e96aa3cf0) - Fixed an issue with `ensure-design-token-usage` where color props on primitive components could report an error.
|
|
8
|
+
|
|
9
|
+
## 8.36.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#78346](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/78346) [`d20b2626a3b0`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/d20b2626a3b0) - Adds support for use-primitives linting rule matching JSX elements declared before styles.
|
|
14
|
+
|
|
15
|
+
## 8.35.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#78282](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/78282) [`e19154833d5f`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/e19154833d5f) - Missing allowed props check for single paragraph elements
|
|
20
|
+
|
|
21
|
+
## 8.35.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- [#77589](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/77589) [`744ea21e3367`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/744ea21e3367) - Update use-primitives-text rule for new defaults in Text primitive component
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- Updated dependencies
|
|
30
|
+
|
|
3
31
|
## 8.34.0
|
|
4
32
|
|
|
5
33
|
### Minor Changes
|
|
@@ -9,13 +9,23 @@ This rule marks code as violations when it can be replaced 1:1 with one or multi
|
|
|
9
9
|
### Incorrect
|
|
10
10
|
|
|
11
11
|
```jsx
|
|
12
|
-
<span
|
|
12
|
+
<span>text</span>
|
|
13
|
+
^^^^^^
|
|
14
|
+
<p>text</p>
|
|
15
|
+
^^^
|
|
16
|
+
<strong>text</strong>
|
|
17
|
+
^^^^^^^^
|
|
18
|
+
<em>text</em>
|
|
19
|
+
^^^^
|
|
13
20
|
```
|
|
14
21
|
|
|
15
22
|
### Correct
|
|
16
23
|
|
|
17
24
|
```jsx
|
|
18
|
-
<Text
|
|
25
|
+
<Text>text</Text>
|
|
26
|
+
<Text as="p">text</Text>
|
|
27
|
+
<Text as="strong">text</Text>
|
|
28
|
+
<Text as="em">text</Text>
|
|
19
29
|
```
|
|
20
30
|
|
|
21
|
-
Currently, the rule is extremely defensive, only reporting on
|
|
31
|
+
Currently, the rule is extremely defensive, only reporting on `span`, `p`, `strong` and `em` elements that don't have any props outside of `key`, `id` and `data-testid`. For `span` elements we're only targeting instances that almost certainly only have text as children.
|
|
@@ -143,6 +143,11 @@ var lintJSXLiteralForColor = exports.lintJSXLiteralForColor = function lintJSXLi
|
|
|
143
143
|
if ((0, _isNode.isDecendantOfSvgElement)(node.parent)) {
|
|
144
144
|
return;
|
|
145
145
|
}
|
|
146
|
+
|
|
147
|
+
// Box backgroundColor prop accepts token names directly - don't lint against this
|
|
148
|
+
if ((0, _isNode.isDecendantOfPrimitive)(node.parent, context)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
146
151
|
if (['alt', 'src', 'label', 'key'].includes(node.parent.name.name)) {
|
|
147
152
|
return;
|
|
148
153
|
}
|
|
@@ -1,15 +1,22 @@
|
|
|
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.findValidJsxUsageToTransform = void 0;
|
|
8
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
7
9
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
10
|
+
var JSX_IDENTIFIER = 'JSXIdentifier';
|
|
8
11
|
/**
|
|
9
12
|
* Given a component name finds its JSX usages and performs some
|
|
10
13
|
* additional validations to ensure transformation can be done correctly
|
|
14
|
+
*
|
|
15
|
+
* anyOrder: if true, the order of the references doesn't matter (JSX or style declaration)
|
|
11
16
|
*/
|
|
12
17
|
var findValidJsxUsageToTransform = exports.findValidJsxUsageToTransform = function findValidJsxUsageToTransform(componentName, scope) {
|
|
18
|
+
var _variableDeclaration$;
|
|
19
|
+
var anyOrder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
13
20
|
var variableDeclaration = scope.variables.find(function (v) {
|
|
14
21
|
return v.name === componentName;
|
|
15
22
|
});
|
|
@@ -25,7 +32,23 @@ var findValidJsxUsageToTransform = exports.findValidJsxUsageToTransform = functi
|
|
|
25
32
|
if (variableDeclaration.references.length !== 2) {
|
|
26
33
|
return;
|
|
27
34
|
}
|
|
28
|
-
var jsxUsage = variableDeclaration.references[1]
|
|
35
|
+
var jsxUsage = (_variableDeclaration$ = variableDeclaration.references[1]) === null || _variableDeclaration$ === void 0 ? void 0 : _variableDeclaration$.identifier;
|
|
36
|
+
if (anyOrder) {
|
|
37
|
+
var _variableDeclaration$2 = variableDeclaration.references.map(function (ref) {
|
|
38
|
+
return ref === null || ref === void 0 ? void 0 : ref.identifier;
|
|
39
|
+
}),
|
|
40
|
+
_variableDeclaration$3 = (0, _slicedToArray2.default)(_variableDeclaration$2, 2),
|
|
41
|
+
firstIdentifier = _variableDeclaration$3[0],
|
|
42
|
+
secondIdentifier = _variableDeclaration$3[1];
|
|
43
|
+
// Check if the first reference is a JSXOpeningElement and the second is not or vice versa
|
|
44
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(firstIdentifier, JSX_IDENTIFIER) && !(0, _eslintCodemodUtils.isNodeOfType)(secondIdentifier, JSX_IDENTIFIER)) {
|
|
45
|
+
jsxUsage = firstIdentifier;
|
|
46
|
+
} else if ((0, _eslintCodemodUtils.isNodeOfType)(secondIdentifier, JSX_IDENTIFIER) && !(0, _eslintCodemodUtils.isNodeOfType)(firstIdentifier, JSX_IDENTIFIER)) {
|
|
47
|
+
jsxUsage = secondIdentifier;
|
|
48
|
+
} else {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
29
52
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxUsage, 'JSXIdentifier')) {
|
|
30
53
|
return;
|
|
31
54
|
}
|
|
@@ -65,7 +65,8 @@ var CompiledStyled = exports.CompiledStyled = {
|
|
|
65
65
|
success: false
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
|
-
var
|
|
68
|
+
var anyOrder = config.patterns.includes('jsx-order-fix');
|
|
69
|
+
var styledComponentJsxRef = (0, _findValidJsxUsageToTransform.findValidJsxUsageToTransform)(styledComponentVariableRef.id.name, context.getScope(), anyOrder);
|
|
69
70
|
if (!styledComponentJsxRef) {
|
|
70
71
|
return {
|
|
71
72
|
success: false
|
|
@@ -5,7 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getConfig = void 0;
|
|
7
7
|
var defaults = {
|
|
8
|
-
patterns: ['paragraph-elements', 'span-elements', 'strong-elements', 'emphasis-elements']
|
|
8
|
+
patterns: ['paragraph-elements', 'span-elements', 'strong-elements', 'emphasis-elements'],
|
|
9
|
+
inheritColor: false
|
|
9
10
|
};
|
|
10
11
|
var getConfig = exports.getConfig = function getConfig(overrides) {
|
|
11
12
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
@@ -4,6 +4,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
+
exports.addColorInheritAttributeFix = addColorInheritAttributeFix;
|
|
7
8
|
exports.allowedAttrs = void 0;
|
|
8
9
|
exports.hasTextChildrenOnly = hasTextChildrenOnly;
|
|
9
10
|
exports.updateTestIdAttributeFix = updateTestIdAttributeFix;
|
|
@@ -18,10 +19,18 @@ function updateTestIdAttributeFix(node, fixer) {
|
|
|
18
19
|
return ast.JSXAttribute.updateName(testIdAttr, 'testId', fixer);
|
|
19
20
|
}
|
|
20
21
|
}
|
|
22
|
+
|
|
23
|
+
// Add color="inherit" prop depending on config
|
|
24
|
+
function addColorInheritAttributeFix(node, config, fixer) {
|
|
25
|
+
if (!config.inheritColor) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
return ast.JSXElement.addAttribute(node, 'color', 'inherit', fixer);
|
|
29
|
+
}
|
|
21
30
|
var allowedAttrs = exports.allowedAttrs = ['id', 'data-testid', 'key'];
|
|
22
31
|
|
|
23
32
|
// Only allow elements with strings as children
|
|
24
|
-
// The use of
|
|
33
|
+
// The use of `<FormattedMessage ... />` component and `{formatMessage(...)}` are allowed as these are used for i18n
|
|
25
34
|
function hasTextChildrenOnly(node) {
|
|
26
35
|
var _node$children;
|
|
27
36
|
return (_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.every(function (child) {
|
|
@@ -36,7 +36,8 @@ var EmphasisElements = exports.EmphasisElements = {
|
|
|
36
36
|
suggest: [{
|
|
37
37
|
desc: "Convert to Text",
|
|
38
38
|
fix: EmphasisElements._fix(node, {
|
|
39
|
-
context: context
|
|
39
|
+
context: context,
|
|
40
|
+
config: config
|
|
40
41
|
})
|
|
41
42
|
}]
|
|
42
43
|
});
|
|
@@ -65,7 +66,8 @@ var EmphasisElements = exports.EmphasisElements = {
|
|
|
65
66
|
return true;
|
|
66
67
|
},
|
|
67
68
|
_fix: function _fix(node, _ref3) {
|
|
68
|
-
var context = _ref3.context
|
|
69
|
+
var context = _ref3.context,
|
|
70
|
+
config = _ref3.config;
|
|
69
71
|
return function (fixer) {
|
|
70
72
|
var importFix = (0, _upsertImportDeclaration.upsertImportDeclaration)({
|
|
71
73
|
module: '@atlaskit/primitives',
|
|
@@ -73,8 +75,9 @@ var EmphasisElements = exports.EmphasisElements = {
|
|
|
73
75
|
}, context, fixer);
|
|
74
76
|
var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
75
77
|
var asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'em', fixer);
|
|
78
|
+
var colorAttributeFix = (0, _common.addColorInheritAttributeFix)(node, config, fixer);
|
|
76
79
|
var testAttributeFix = (0, _common.updateTestIdAttributeFix)(node, fixer);
|
|
77
|
-
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
80
|
+
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, colorAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
78
81
|
return Boolean(fix);
|
|
79
82
|
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
80
83
|
};
|
|
@@ -53,6 +53,7 @@ var ParagraphElements = exports.ParagraphElements = {
|
|
|
53
53
|
desc: 'Convert to Text and Stack',
|
|
54
54
|
fix: ParagraphElements._fixMultiple(node, {
|
|
55
55
|
context: context,
|
|
56
|
+
config: config,
|
|
56
57
|
refs: refs
|
|
57
58
|
})
|
|
58
59
|
}]
|
|
@@ -64,7 +65,8 @@ var ParagraphElements = exports.ParagraphElements = {
|
|
|
64
65
|
suggest: [{
|
|
65
66
|
desc: 'Convert to Text',
|
|
66
67
|
fix: ParagraphElements._fixSingle(node, {
|
|
67
|
-
context: context
|
|
68
|
+
context: context,
|
|
69
|
+
config: config
|
|
68
70
|
})
|
|
69
71
|
}]
|
|
70
72
|
});
|
|
@@ -130,6 +132,13 @@ var ParagraphElements = exports.ParagraphElements = {
|
|
|
130
132
|
}
|
|
131
133
|
};
|
|
132
134
|
}
|
|
135
|
+
} else if (!ast.JSXElement.hasAllowedAttrsOnly(node, _common.allowedAttrs)) {
|
|
136
|
+
return {
|
|
137
|
+
success: false,
|
|
138
|
+
refs: {
|
|
139
|
+
siblings: siblings
|
|
140
|
+
}
|
|
141
|
+
};
|
|
133
142
|
}
|
|
134
143
|
var importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
|
|
135
144
|
|
|
@@ -150,21 +159,25 @@ var ParagraphElements = exports.ParagraphElements = {
|
|
|
150
159
|
};
|
|
151
160
|
},
|
|
152
161
|
_fixSingle: function _fixSingle(node, _ref3) {
|
|
153
|
-
var context = _ref3.context
|
|
162
|
+
var context = _ref3.context,
|
|
163
|
+
config = _ref3.config;
|
|
154
164
|
return function (fixer) {
|
|
155
165
|
var importFix = (0, _upsertImportDeclaration.upsertImportDeclaration)({
|
|
156
166
|
module: '@atlaskit/primitives',
|
|
157
167
|
specifiers: ['Text']
|
|
158
168
|
}, context, fixer);
|
|
159
169
|
var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
170
|
+
var asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'p', fixer);
|
|
171
|
+
var colorAttributeFix = (0, _common.addColorInheritAttributeFix)(node, config, fixer);
|
|
160
172
|
var testAttributeFix = (0, _common.updateTestIdAttributeFix)(node, fixer);
|
|
161
|
-
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [testAttributeFix]).filter(function (fix) {
|
|
173
|
+
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, colorAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
162
174
|
return Boolean(fix);
|
|
163
175
|
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
164
176
|
};
|
|
165
177
|
},
|
|
166
178
|
_fixMultiple: function _fixMultiple(node, _ref4) {
|
|
167
179
|
var context = _ref4.context,
|
|
180
|
+
config = _ref4.config,
|
|
168
181
|
refs = _ref4.refs;
|
|
169
182
|
return function (fixer) {
|
|
170
183
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.parent, 'JSXElement') || !node.parent.closingElement) {
|
|
@@ -179,8 +192,10 @@ var ParagraphElements = exports.ParagraphElements = {
|
|
|
179
192
|
var siblingFixes = refs.siblings.map(function (sibling) {
|
|
180
193
|
if ((0, _eslintCodemodUtils.isNodeOfType)(sibling, 'JSXElement')) {
|
|
181
194
|
var elementNameFixes = ast.JSXElement.updateName(sibling, 'Text', fixer);
|
|
195
|
+
var asAttributeFix = ast.JSXElement.addAttribute(sibling, 'as', 'p', fixer);
|
|
196
|
+
var colorAttributeFix = (0, _common.addColorInheritAttributeFix)(sibling, config, fixer);
|
|
182
197
|
var testAttributeFix = (0, _common.updateTestIdAttributeFix)(sibling, fixer);
|
|
183
|
-
return [].concat((0, _toConsumableArray2.default)(elementNameFixes), [testAttributeFix]);
|
|
198
|
+
return [].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, colorAttributeFix, testAttributeFix]);
|
|
184
199
|
}
|
|
185
200
|
return undefined;
|
|
186
201
|
}).flat();
|
|
@@ -36,7 +36,8 @@ var SpanElements = exports.SpanElements = {
|
|
|
36
36
|
suggest: [{
|
|
37
37
|
desc: "Convert to Text",
|
|
38
38
|
fix: SpanElements._fix(node, {
|
|
39
|
-
context: context
|
|
39
|
+
context: context,
|
|
40
|
+
config: config
|
|
40
41
|
})
|
|
41
42
|
}]
|
|
42
43
|
});
|
|
@@ -70,16 +71,17 @@ var SpanElements = exports.SpanElements = {
|
|
|
70
71
|
return true;
|
|
71
72
|
},
|
|
72
73
|
_fix: function _fix(node, _ref3) {
|
|
73
|
-
var context = _ref3.context
|
|
74
|
+
var context = _ref3.context,
|
|
75
|
+
config = _ref3.config;
|
|
74
76
|
return function (fixer) {
|
|
75
77
|
var importFix = (0, _upsertImportDeclaration.upsertImportDeclaration)({
|
|
76
78
|
module: '@atlaskit/primitives',
|
|
77
79
|
specifiers: ['Text']
|
|
78
80
|
}, context, fixer);
|
|
79
81
|
var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
80
|
-
var
|
|
82
|
+
var colorAttributeFix = (0, _common.addColorInheritAttributeFix)(node, config, fixer);
|
|
81
83
|
var testAttributeFix = (0, _common.updateTestIdAttributeFix)(node, fixer);
|
|
82
|
-
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [
|
|
84
|
+
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [colorAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
83
85
|
return Boolean(fix);
|
|
84
86
|
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
85
87
|
};
|
|
@@ -36,7 +36,8 @@ var StrongElements = exports.StrongElements = {
|
|
|
36
36
|
suggest: [{
|
|
37
37
|
desc: "Convert to Text",
|
|
38
38
|
fix: StrongElements._fix(node, {
|
|
39
|
-
context: context
|
|
39
|
+
context: context,
|
|
40
|
+
config: config
|
|
40
41
|
})
|
|
41
42
|
}]
|
|
42
43
|
});
|
|
@@ -65,7 +66,8 @@ var StrongElements = exports.StrongElements = {
|
|
|
65
66
|
return true;
|
|
66
67
|
},
|
|
67
68
|
_fix: function _fix(node, _ref3) {
|
|
68
|
-
var context = _ref3.context
|
|
69
|
+
var context = _ref3.context,
|
|
70
|
+
config = _ref3.config;
|
|
69
71
|
return function (fixer) {
|
|
70
72
|
var importFix = (0, _upsertImportDeclaration.upsertImportDeclaration)({
|
|
71
73
|
module: '@atlaskit/primitives',
|
|
@@ -73,8 +75,9 @@ var StrongElements = exports.StrongElements = {
|
|
|
73
75
|
}, context, fixer);
|
|
74
76
|
var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
75
77
|
var asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'strong', fixer);
|
|
78
|
+
var colorAttributeFix = (0, _common.addColorInheritAttributeFix)(node, config, fixer);
|
|
76
79
|
var testAttributeFix = (0, _common.updateTestIdAttributeFix)(node, fixer);
|
|
77
|
-
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
80
|
+
return [importFix].concat((0, _toConsumableArray2.default)(elementNameFixes), [asAttributeFix, colorAttributeFix, testAttributeFix]).filter(function (fix) {
|
|
78
81
|
return Boolean(fix);
|
|
79
82
|
}); // Some of the transformers can return arrays with undefined, so filter them out
|
|
80
83
|
};
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isPropertyKey = exports.isDecendantOfType = exports.isDecendantOfSvgElement = exports.isDecendantOfStyleJsxAttribute = exports.isDecendantOfStyleBlock = exports.isDecendantOfGlobalToken = exports.isCssInJsTemplateNode = exports.isCssInJsObjectNode = exports.isCssInJsCallNode = exports.isChildOfType = void 0;
|
|
6
|
+
exports.isPropertyKey = exports.isDecendantOfType = exports.isDecendantOfSvgElement = exports.isDecendantOfStyleJsxAttribute = exports.isDecendantOfStyleBlock = exports.isDecendantOfPrimitive = exports.isDecendantOfGlobalToken = exports.isCssInJsTemplateNode = exports.isCssInJsObjectNode = exports.isCssInJsCallNode = exports.isChildOfType = void 0;
|
|
7
7
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var _astNodes = require("../../ast-nodes");
|
|
8
9
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
9
10
|
|
|
10
11
|
var isDecendantOfGlobalToken = exports.isDecendantOfGlobalToken = function isDecendantOfGlobalToken(node) {
|
|
@@ -54,6 +55,22 @@ var isDecendantOfSvgElement = exports.isDecendantOfSvgElement = function isDecen
|
|
|
54
55
|
}
|
|
55
56
|
return false;
|
|
56
57
|
};
|
|
58
|
+
var isDecendantOfPrimitive = exports.isDecendantOfPrimitive = function isDecendantOfPrimitive(node, context) {
|
|
59
|
+
var primitivesToCheck = ['Box', 'Text'];
|
|
60
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
if (primitivesToCheck.includes(node.openingElement.name.name)) {
|
|
63
|
+
var importDeclaration = _astNodes.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
|
|
64
|
+
if (importDeclaration.length) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (node.parent) {
|
|
70
|
+
return isDecendantOfPrimitive(node.parent, context);
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
};
|
|
57
74
|
var cssInJsCallees = ['css', 'styled', 'styled2'];
|
|
58
75
|
var isCssInJsTemplateNode = exports.isCssInJsTemplateNode = function isCssInJsTemplateNode(node) {
|
|
59
76
|
return (node === null || node === void 0 ? void 0 : node.type) === 'TaggedTemplateExpression' && node.tag.type === 'MemberExpression' && node.tag.object.type === 'Identifier' && node.tag.object.name === 'styled';
|
|
@@ -4,7 +4,7 @@ import { node as generate, isNodeOfType } from 'eslint-codemod-utils';
|
|
|
4
4
|
import { getIsException } from '../utils/get-is-exception';
|
|
5
5
|
import { includesHardCodedColor, isHardCodedColor, isLegacyColor, isLegacyNamedColor } from '../utils/is-color';
|
|
6
6
|
import { isLegacyElevation } from '../utils/is-elevation';
|
|
7
|
-
import { isChildOfType, isDecendantOfGlobalToken, isDecendantOfStyleBlock, isDecendantOfSvgElement } from '../utils/is-node';
|
|
7
|
+
import { isChildOfType, isDecendantOfGlobalToken, isDecendantOfPrimitive, isDecendantOfStyleBlock, isDecendantOfSvgElement } from '../utils/is-node';
|
|
8
8
|
// TemplateLiteral > Identifier
|
|
9
9
|
export const lintTemplateIdentifierForColor = (node, context, config) => {
|
|
10
10
|
if (node.type !== 'Identifier') {
|
|
@@ -138,6 +138,11 @@ export const lintJSXLiteralForColor = (node, context, config) => {
|
|
|
138
138
|
if (isDecendantOfSvgElement(node.parent)) {
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
|
+
|
|
142
|
+
// Box backgroundColor prop accepts token names directly - don't lint against this
|
|
143
|
+
if (isDecendantOfPrimitive(node.parent, context)) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
141
146
|
if (['alt', 'src', 'label', 'key'].includes(node.parent.name.name)) {
|
|
142
147
|
return;
|
|
143
148
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
-
|
|
2
|
+
const JSX_IDENTIFIER = 'JSXIdentifier';
|
|
3
3
|
/**
|
|
4
4
|
* Given a component name finds its JSX usages and performs some
|
|
5
5
|
* additional validations to ensure transformation can be done correctly
|
|
6
|
+
*
|
|
7
|
+
* anyOrder: if true, the order of the references doesn't matter (JSX or style declaration)
|
|
6
8
|
*/
|
|
7
|
-
export const findValidJsxUsageToTransform = (componentName, scope) => {
|
|
9
|
+
export const findValidJsxUsageToTransform = (componentName, scope, anyOrder = false) => {
|
|
10
|
+
var _variableDeclaration$;
|
|
8
11
|
const variableDeclaration = scope.variables.find(v => v.name === componentName);
|
|
9
12
|
if (!variableDeclaration) {
|
|
10
13
|
return;
|
|
@@ -18,7 +21,18 @@ export const findValidJsxUsageToTransform = (componentName, scope) => {
|
|
|
18
21
|
if (variableDeclaration.references.length !== 2) {
|
|
19
22
|
return;
|
|
20
23
|
}
|
|
21
|
-
|
|
24
|
+
let jsxUsage = (_variableDeclaration$ = variableDeclaration.references[1]) === null || _variableDeclaration$ === void 0 ? void 0 : _variableDeclaration$.identifier;
|
|
25
|
+
if (anyOrder) {
|
|
26
|
+
const [firstIdentifier, secondIdentifier] = variableDeclaration.references.map(ref => ref === null || ref === void 0 ? void 0 : ref.identifier);
|
|
27
|
+
// Check if the first reference is a JSXOpeningElement and the second is not or vice versa
|
|
28
|
+
if (isNodeOfType(firstIdentifier, JSX_IDENTIFIER) && !isNodeOfType(secondIdentifier, JSX_IDENTIFIER)) {
|
|
29
|
+
jsxUsage = firstIdentifier;
|
|
30
|
+
} else if (isNodeOfType(secondIdentifier, JSX_IDENTIFIER) && !isNodeOfType(firstIdentifier, JSX_IDENTIFIER)) {
|
|
31
|
+
jsxUsage = secondIdentifier;
|
|
32
|
+
} else {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
22
36
|
if (!isNodeOfType(jsxUsage, 'JSXIdentifier')) {
|
|
23
37
|
return;
|
|
24
38
|
}
|
|
@@ -57,7 +57,8 @@ export const CompiledStyled = {
|
|
|
57
57
|
success: false
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
-
const
|
|
60
|
+
const anyOrder = config.patterns.includes('jsx-order-fix');
|
|
61
|
+
const styledComponentJsxRef = findValidJsxUsageToTransform(styledComponentVariableRef.id.name, context.getScope(), anyOrder);
|
|
61
62
|
if (!styledComponentJsxRef) {
|
|
62
63
|
return {
|
|
63
64
|
success: false
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const defaults = {
|
|
2
|
-
patterns: ['paragraph-elements', 'span-elements', 'strong-elements', 'emphasis-elements']
|
|
2
|
+
patterns: ['paragraph-elements', 'span-elements', 'strong-elements', 'emphasis-elements'],
|
|
3
|
+
inheritColor: false
|
|
3
4
|
};
|
|
4
5
|
export const getConfig = overrides => {
|
|
5
6
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
2
|
import * as ast from '../../../ast-nodes';
|
|
3
|
-
|
|
4
3
|
// Rename data-testid prop to testId if present
|
|
5
4
|
export function updateTestIdAttributeFix(node, fixer) {
|
|
6
5
|
const testIdAttr = ast.JSXElement.getAttributeByName(node, 'data-testid');
|
|
@@ -8,10 +7,18 @@ export function updateTestIdAttributeFix(node, fixer) {
|
|
|
8
7
|
return ast.JSXAttribute.updateName(testIdAttr, 'testId', fixer);
|
|
9
8
|
}
|
|
10
9
|
}
|
|
10
|
+
|
|
11
|
+
// Add color="inherit" prop depending on config
|
|
12
|
+
export function addColorInheritAttributeFix(node, config, fixer) {
|
|
13
|
+
if (!config.inheritColor) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
return ast.JSXElement.addAttribute(node, 'color', 'inherit', fixer);
|
|
17
|
+
}
|
|
11
18
|
export const allowedAttrs = ['id', 'data-testid', 'key'];
|
|
12
19
|
|
|
13
20
|
// Only allow elements with strings as children
|
|
14
|
-
// The use of
|
|
21
|
+
// The use of `<FormattedMessage ... />` component and `{formatMessage(...)}` are allowed as these are used for i18n
|
|
15
22
|
export function hasTextChildrenOnly(node) {
|
|
16
23
|
var _node$children;
|
|
17
24
|
return (_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.every(child => {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
4
|
import * as ast from '../../../ast-nodes';
|
|
5
5
|
import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
|
|
6
|
-
import { allowedAttrs, updateTestIdAttributeFix } from './common';
|
|
6
|
+
import { addColorInheritAttributeFix, allowedAttrs, updateTestIdAttributeFix } from './common';
|
|
7
7
|
export const EmphasisElements = {
|
|
8
8
|
lint(node, {
|
|
9
9
|
context,
|
|
@@ -26,7 +26,8 @@ export const EmphasisElements = {
|
|
|
26
26
|
suggest: [{
|
|
27
27
|
desc: `Convert to Text`,
|
|
28
28
|
fix: EmphasisElements._fix(node, {
|
|
29
|
-
context
|
|
29
|
+
context,
|
|
30
|
+
config
|
|
30
31
|
})
|
|
31
32
|
}]
|
|
32
33
|
});
|
|
@@ -56,7 +57,8 @@ export const EmphasisElements = {
|
|
|
56
57
|
return true;
|
|
57
58
|
},
|
|
58
59
|
_fix(node, {
|
|
59
|
-
context
|
|
60
|
+
context,
|
|
61
|
+
config
|
|
60
62
|
}) {
|
|
61
63
|
return fixer => {
|
|
62
64
|
const importFix = upsertImportDeclaration({
|
|
@@ -65,8 +67,9 @@ export const EmphasisElements = {
|
|
|
65
67
|
}, context, fixer);
|
|
66
68
|
const elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
67
69
|
const asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'em', fixer);
|
|
70
|
+
const colorAttributeFix = addColorInheritAttributeFix(node, config, fixer);
|
|
68
71
|
const testAttributeFix = updateTestIdAttributeFix(node, fixer);
|
|
69
|
-
return [importFix, ...elementNameFixes, asAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
|
|
72
|
+
return [importFix, ...elementNameFixes, asAttributeFix, colorAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
75
|
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
4
|
import * as ast from '../../../ast-nodes';
|
|
5
5
|
import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
|
|
6
|
-
import { allowedAttrs, updateTestIdAttributeFix } from './common';
|
|
6
|
+
import { addColorInheritAttributeFix, allowedAttrs, updateTestIdAttributeFix } from './common';
|
|
7
7
|
export const ParagraphElements = {
|
|
8
8
|
lint(node, {
|
|
9
9
|
context,
|
|
@@ -44,6 +44,7 @@ export const ParagraphElements = {
|
|
|
44
44
|
desc: 'Convert to Text and Stack',
|
|
45
45
|
fix: ParagraphElements._fixMultiple(node, {
|
|
46
46
|
context,
|
|
47
|
+
config,
|
|
47
48
|
refs
|
|
48
49
|
})
|
|
49
50
|
}]
|
|
@@ -55,7 +56,8 @@ export const ParagraphElements = {
|
|
|
55
56
|
suggest: [{
|
|
56
57
|
desc: 'Convert to Text',
|
|
57
58
|
fix: ParagraphElements._fixSingle(node, {
|
|
58
|
-
context
|
|
59
|
+
context,
|
|
60
|
+
config
|
|
59
61
|
})
|
|
60
62
|
}]
|
|
61
63
|
});
|
|
@@ -122,6 +124,13 @@ export const ParagraphElements = {
|
|
|
122
124
|
}
|
|
123
125
|
};
|
|
124
126
|
}
|
|
127
|
+
} else if (!ast.JSXElement.hasAllowedAttrsOnly(node, allowedAttrs)) {
|
|
128
|
+
return {
|
|
129
|
+
success: false,
|
|
130
|
+
refs: {
|
|
131
|
+
siblings
|
|
132
|
+
}
|
|
133
|
+
};
|
|
125
134
|
}
|
|
126
135
|
const importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
|
|
127
136
|
|
|
@@ -142,7 +151,8 @@ export const ParagraphElements = {
|
|
|
142
151
|
};
|
|
143
152
|
},
|
|
144
153
|
_fixSingle(node, {
|
|
145
|
-
context
|
|
154
|
+
context,
|
|
155
|
+
config
|
|
146
156
|
}) {
|
|
147
157
|
return fixer => {
|
|
148
158
|
const importFix = upsertImportDeclaration({
|
|
@@ -150,13 +160,16 @@ export const ParagraphElements = {
|
|
|
150
160
|
specifiers: ['Text']
|
|
151
161
|
}, context, fixer);
|
|
152
162
|
const elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
163
|
+
const asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'p', fixer);
|
|
164
|
+
const colorAttributeFix = addColorInheritAttributeFix(node, config, fixer);
|
|
153
165
|
const testAttributeFix = updateTestIdAttributeFix(node, fixer);
|
|
154
|
-
return [importFix, ...elementNameFixes, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
|
|
166
|
+
return [importFix, ...elementNameFixes, asAttributeFix, colorAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
|
|
155
167
|
};
|
|
156
168
|
},
|
|
157
169
|
|
|
158
170
|
_fixMultiple(node, {
|
|
159
171
|
context,
|
|
172
|
+
config,
|
|
160
173
|
refs
|
|
161
174
|
}) {
|
|
162
175
|
return fixer => {
|
|
@@ -172,8 +185,10 @@ export const ParagraphElements = {
|
|
|
172
185
|
const siblingFixes = refs.siblings.map(sibling => {
|
|
173
186
|
if (isNodeOfType(sibling, 'JSXElement')) {
|
|
174
187
|
const elementNameFixes = ast.JSXElement.updateName(sibling, 'Text', fixer);
|
|
188
|
+
const asAttributeFix = ast.JSXElement.addAttribute(sibling, 'as', 'p', fixer);
|
|
189
|
+
const colorAttributeFix = addColorInheritAttributeFix(sibling, config, fixer);
|
|
175
190
|
const testAttributeFix = updateTestIdAttributeFix(sibling, fixer);
|
|
176
|
-
return [...elementNameFixes, testAttributeFix];
|
|
191
|
+
return [...elementNameFixes, asAttributeFix, colorAttributeFix, testAttributeFix];
|
|
177
192
|
}
|
|
178
193
|
return undefined;
|
|
179
194
|
}).flat();
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
4
|
import * as ast from '../../../ast-nodes';
|
|
5
5
|
import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
|
|
6
|
-
import { allowedAttrs, hasTextChildrenOnly, updateTestIdAttributeFix } from './common';
|
|
6
|
+
import { addColorInheritAttributeFix, allowedAttrs, hasTextChildrenOnly, updateTestIdAttributeFix } from './common';
|
|
7
7
|
export const SpanElements = {
|
|
8
8
|
lint(node, {
|
|
9
9
|
context,
|
|
@@ -26,7 +26,8 @@ export const SpanElements = {
|
|
|
26
26
|
suggest: [{
|
|
27
27
|
desc: `Convert to Text`,
|
|
28
28
|
fix: SpanElements._fix(node, {
|
|
29
|
-
context
|
|
29
|
+
context,
|
|
30
|
+
config
|
|
30
31
|
})
|
|
31
32
|
}]
|
|
32
33
|
});
|
|
@@ -61,7 +62,8 @@ export const SpanElements = {
|
|
|
61
62
|
return true;
|
|
62
63
|
},
|
|
63
64
|
_fix(node, {
|
|
64
|
-
context
|
|
65
|
+
context,
|
|
66
|
+
config
|
|
65
67
|
}) {
|
|
66
68
|
return fixer => {
|
|
67
69
|
const importFix = upsertImportDeclaration({
|
|
@@ -69,9 +71,9 @@ export const SpanElements = {
|
|
|
69
71
|
specifiers: ['Text']
|
|
70
72
|
}, context, fixer);
|
|
71
73
|
const elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
|
|
72
|
-
const
|
|
74
|
+
const colorAttributeFix = addColorInheritAttributeFix(node, config, fixer);
|
|
73
75
|
const testAttributeFix = updateTestIdAttributeFix(node, fixer);
|
|
74
|
-
return [importFix, ...elementNameFixes,
|
|
76
|
+
return [importFix, ...elementNameFixes, colorAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
|
|
75
77
|
};
|
|
76
78
|
}
|
|
77
79
|
};
|