@atlaskit/codemod-cli 0.25.0 → 0.25.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 +8 -0
- package/dist/cjs/main.js +1 -1
- package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.js +94 -0
- package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +28 -8
- package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.js +4 -3
- package/dist/cjs/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -1
- package/dist/cjs/presets/migrate-to-new-buttons/utils/find-attribute-with-value.js +17 -0
- package/dist/cjs/presets/migrate-to-new-buttons/utils/generate-link-element.js +40 -0
- package/dist/cjs/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +6 -1
- package/dist/cjs/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.js +30 -0
- package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.js +81 -0
- package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +28 -8
- package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.js +3 -2
- package/dist/es2019/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -1
- package/dist/es2019/presets/migrate-to-new-buttons/utils/find-attribute-with-value.js +11 -0
- package/dist/es2019/presets/migrate-to-new-buttons/utils/generate-link-element.js +36 -0
- package/dist/es2019/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +6 -1
- package/dist/es2019/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.js +24 -0
- package/dist/esm/main.js +1 -1
- package/dist/esm/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.js +87 -0
- package/dist/esm/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +28 -8
- package/dist/esm/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.js +3 -2
- package/dist/esm/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -1
- package/dist/esm/presets/migrate-to-new-buttons/utils/find-attribute-with-value.js +11 -0
- package/dist/esm/presets/migrate-to-new-buttons/utils/generate-link-element.js +34 -0
- package/dist/esm/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +6 -1
- package/dist/esm/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.js +24 -0
- package/dist/types/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.d.ts +3 -0
- package/dist/types/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.d.ts +2 -1
- package/dist/types/presets/migrate-to-new-buttons/utils/find-attribute-with-value.d.ts +2 -0
- package/dist/types/presets/migrate-to-new-buttons/utils/generate-link-element.d.ts +3 -0
- package/dist/types/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.d.ts +3 -0
- package/dist/types-ts4.5/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.d.ts +3 -0
- package/dist/types-ts4.5/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.d.ts +2 -1
- package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/find-attribute-with-value.d.ts +2 -0
- package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/generate-link-element.d.ts +3 -0
- package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.d.ts +3 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @atlaskit/codemod-cli
|
|
2
2
|
|
|
3
|
+
## 0.25.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#120049](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/120049)
|
|
8
|
+
[`77504ff274f72`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/77504ff274f72) -
|
|
9
|
+
DSP-19576: Assign names to anonymous default exports
|
|
10
|
+
|
|
3
11
|
## 0.25.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
package/dist/cjs/main.js
CHANGED
|
@@ -305,7 +305,7 @@ function _main() {
|
|
|
305
305
|
case 4:
|
|
306
306
|
_yield$parseArgs = _context5.sent;
|
|
307
307
|
packages = _yield$parseArgs.packages;
|
|
308
|
-
_process$env$_PACKAGE = "0.25.
|
|
308
|
+
_process$env$_PACKAGE = "0.25.1", _PACKAGE_VERSION_ = _process$env$_PACKAGE === void 0 ? '0.0.0-dev' : _process$env$_PACKAGE;
|
|
309
309
|
logger.log(_chalk.default.bgBlue(_chalk.default.black("\uD83D\uDCDA Atlassian-Frontend codemod library @ ".concat(_PACKAGE_VERSION_, " \uD83D\uDCDA"))));
|
|
310
310
|
if (packages && packages.length > 0) {
|
|
311
311
|
logger.log(_chalk.default.gray("Searching for codemods for newer versions of the following packages: ".concat(packages.map(function (pkg) {
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _utils = require("@hypermod/utils");
|
|
9
|
+
var _constants = require("../utils/constants");
|
|
10
|
+
var _moveIconValueFromLinkButtonToLinkChildren = _interopRequireDefault(require("../utils/move-icon-value-from-link-button-to-link-children"));
|
|
11
|
+
var _findAttributeWithValue = require("../utils/find-attribute-with-value");
|
|
12
|
+
var _generateLinkElement = require("../utils/generate-link-element");
|
|
13
|
+
function transformer(file, api) {
|
|
14
|
+
var j = api.jscodeshift;
|
|
15
|
+
var source = j(file.source);
|
|
16
|
+
|
|
17
|
+
// Find all new button imports
|
|
18
|
+
var newButtonImportDeclarations = (0, _utils.getImportDeclaration)(j, source, _constants.NEW_BUTTON_ENTRY_POINT);
|
|
19
|
+
|
|
20
|
+
// No imports for new button found, exit early
|
|
21
|
+
if (!newButtonImportDeclarations.length) {
|
|
22
|
+
return source.toSource();
|
|
23
|
+
}
|
|
24
|
+
var linkButtonSpecifier = '';
|
|
25
|
+
var linkButtonDeclaration;
|
|
26
|
+
newButtonImportDeclarations.forEach(function (newButtonImport) {
|
|
27
|
+
if (!newButtonImport.value.specifiers) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
newButtonImport.value.specifiers.map(function (specifier) {
|
|
31
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported && specifier.imported.name === _constants.NEW_BUTTON_VARIANTS['link']) {
|
|
32
|
+
var _specifier$local;
|
|
33
|
+
linkButtonDeclaration = newButtonImport;
|
|
34
|
+
linkButtonSpecifier = ((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name) || '';
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
if (!linkButtonSpecifier) {
|
|
39
|
+
return source.toSource();
|
|
40
|
+
}
|
|
41
|
+
var hasLink = false;
|
|
42
|
+
var allLinkButtons = source.find(j.JSXElement).filter(function (path) {
|
|
43
|
+
return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier;
|
|
44
|
+
});
|
|
45
|
+
allLinkButtons.filter(function (path) {
|
|
46
|
+
return (0, _findAttributeWithValue.findJSXAttributeWithValue)(path.value.openingElement, 'appearance', 'link') || (0, _findAttributeWithValue.findJSXAttributeWithValue)(path.value.openingElement, 'appearance', 'subtle-link');
|
|
47
|
+
}).forEach(function (path) {
|
|
48
|
+
var hasSpacingNone = (0, _findAttributeWithValue.findJSXAttributeWithValue)(path.value.openingElement, 'spacing', 'none');
|
|
49
|
+
if (!hasSpacingNone) {
|
|
50
|
+
j(path).find(j.JSXAttribute).filter(function (path) {
|
|
51
|
+
var _path$node$value;
|
|
52
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
53
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
54
|
+
}
|
|
55
|
+
if (hasSpacingNone) {
|
|
56
|
+
var attributes = path.node.openingElement.attributes;
|
|
57
|
+
(0, _generateLinkElement.modifyLinkAttributes)(path.node, j);
|
|
58
|
+
j(path).find(j.JSXAttribute).filter(function (attribute) {
|
|
59
|
+
var _attribute$node$value;
|
|
60
|
+
var isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
61
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
62
|
+
(0, _moveIconValueFromLinkButtonToLinkChildren.default)(j, path, attribute.node.value, attribute.node.name.name);
|
|
63
|
+
}
|
|
64
|
+
return isIconAttribute;
|
|
65
|
+
}).remove();
|
|
66
|
+
hasLink = true;
|
|
67
|
+
j(path).replaceWith(j.jsxElement.from({
|
|
68
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
69
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
70
|
+
children: path.node.children
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// add link import and remove link button import if no link button is found
|
|
76
|
+
if (hasLink && linkButtonDeclaration) {
|
|
77
|
+
var linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
78
|
+
j(linkButtonDeclaration).insertAfter(linkImport);
|
|
79
|
+
var remainingLinkButton = source.find(j.JSXElement).filter(function (path) {
|
|
80
|
+
return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier;
|
|
81
|
+
});
|
|
82
|
+
if (!remainingLinkButton.length) {
|
|
83
|
+
var _value$specifiers;
|
|
84
|
+
j(linkButtonDeclaration).find(j.ImportSpecifier).filter(function (path) {
|
|
85
|
+
return path.node.imported.name === _constants.NEW_BUTTON_VARIANTS['link'];
|
|
86
|
+
}).remove();
|
|
87
|
+
if (((_value$specifiers = linkButtonDeclaration.value.specifiers) === null || _value$specifiers === void 0 ? void 0 : _value$specifiers.length) === 0) {
|
|
88
|
+
j(linkButtonDeclaration).remove();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return source.toSource(_constants.PRINT_SETTINGS);
|
|
93
|
+
}
|
|
94
|
+
var _default = exports.default = transformer;
|
package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js
CHANGED
|
@@ -12,6 +12,7 @@ var _constants = require("../utils/constants");
|
|
|
12
12
|
var _getDefaultImports = _interopRequireDefault(require("../utils/get-default-imports"));
|
|
13
13
|
var _getSpecifierNames = _interopRequireDefault(require("../utils/get-specifier-names"));
|
|
14
14
|
var _generateNewButtonElement = require("../utils/generate-new-button-element");
|
|
15
|
+
var _generateLinkElement = require("../utils/generate-link-element");
|
|
15
16
|
var _hasUnsupportedProps = require("../utils/has-unsupported-props");
|
|
16
17
|
var _ifVariantAlreadyImported = require("../utils/if-variant-already-imported");
|
|
17
18
|
var _renameDefaultButtonToLegacyButton = require("../utils/rename-default-button-to-legacy-button");
|
|
@@ -19,6 +20,7 @@ var _migrateFitContainerIconButton = require("../utils/migrate-fit-container-ico
|
|
|
19
20
|
var _importTypesFromNewEntryPoint = require("../utils/import-types-from-new-entry-point");
|
|
20
21
|
var _addCommentForCustomThemeButtons = require("../utils/add-comment-for-custom-theme-buttons");
|
|
21
22
|
var _addCommentForOverlayProp = require("../utils/add-comment-for-overlay-prop");
|
|
23
|
+
var _findAttributeWithValue = require("../utils/find-attribute-with-value");
|
|
22
24
|
var transformer = function transformer(file, api) {
|
|
23
25
|
var j = api.jscodeshift;
|
|
24
26
|
var fileSource = j(file.source);
|
|
@@ -52,7 +54,8 @@ var transformer = function transformer(file, api) {
|
|
|
52
54
|
defaultButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.default, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j, true),
|
|
53
55
|
linkButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.link, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
54
56
|
iconButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.icon, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
55
|
-
linkIconButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.linkIcon, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j)
|
|
57
|
+
linkIconButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.linkIcon, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
58
|
+
link: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)('link', '@atlaskit/link', fileSource, j)
|
|
56
59
|
};
|
|
57
60
|
var oldButtonElements = fileSource.find(j.JSXElement).filter(function (path) {
|
|
58
61
|
return path.value.openingElement.name.type === 'JSXIdentifier' && ((oldDefaultImportSpecifiers === null || oldDefaultImportSpecifiers === void 0 ? void 0 : oldDefaultImportSpecifiers.includes(path.value.openingElement.name.name)) || Object.values(_constants.OLD_BUTTON_VARIANTS).includes(path.value.openingElement.name.name));
|
|
@@ -73,18 +76,24 @@ var transformer = function transformer(file, api) {
|
|
|
73
76
|
var hasHref = buttonAttributes.includes('href');
|
|
74
77
|
var hasIcon = buttonAttributes.includes('iconBefore') || buttonAttributes.includes('iconAfter');
|
|
75
78
|
var hasNoChildren = ((_element$value$childr = element.value.children) === null || _element$value$childr === void 0 ? void 0 : _element$value$childr.length) === 0;
|
|
79
|
+
var hasSpacing = attributes.find(function (attr) {
|
|
80
|
+
var _attr$value;
|
|
81
|
+
return attr.type === 'JSXAttribute' && attr.name.name === 'spacing' && ((_attr$value = attr.value) === null || _attr$value === void 0 ? void 0 : _attr$value.type) === 'StringLiteral' && attr.value.value !== 'none';
|
|
82
|
+
}) || !buttonAttributes.includes('spacing');
|
|
83
|
+
var hasSpacingNone = (0, _findAttributeWithValue.findJSXAttributeWithValue)(element.value.openingElement, 'spacing', 'none');
|
|
76
84
|
var isFitContainerIconButton = hasIcon && hasNoChildren && buttonAttributes.includes('shouldFitContainer');
|
|
77
|
-
var isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
78
|
-
var isLinkButton = hasHref && !isLinkIconButton;
|
|
79
|
-
var isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
80
|
-
var isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton;
|
|
81
|
-
var isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon;
|
|
82
|
-
var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
83
85
|
var linkAppearanceAttribute = attributes.find(function (node) {
|
|
84
86
|
var _node$value, _node$name, _node$value2, _node$value3;
|
|
85
87
|
return node.type === 'JSXAttribute' && ((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'StringLiteral' && (node === null || node === void 0 || (_node$name = node.name) === null || _node$name === void 0 ? void 0 : _node$name.name) === 'appearance' && ((node === null || node === void 0 || (_node$value2 = node.value) === null || _node$value2 === void 0 ? void 0 : _node$value2.value) === 'link' || (node === null || node === void 0 || (_node$value3 = node.value) === null || _node$value3 === void 0 ? void 0 : _node$value3.value) === 'subtle-link');
|
|
86
88
|
});
|
|
87
|
-
|
|
89
|
+
var isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
90
|
+
var isLinkButton = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacing;
|
|
91
|
+
var isLink = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacingNone && linkAppearanceAttribute;
|
|
92
|
+
var isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton && !isLink;
|
|
93
|
+
var isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton && !isLink;
|
|
94
|
+
var isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon && !isLink;
|
|
95
|
+
var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
96
|
+
if (isDefaultVariantWithAnIcon && !isLink) {
|
|
88
97
|
(0, _generateNewButtonElement.handleIconAttributes)(element.value, j);
|
|
89
98
|
}
|
|
90
99
|
if (isFitContainerIconButton) {
|
|
@@ -105,6 +114,10 @@ var transformer = function transformer(file, api) {
|
|
|
105
114
|
hasVariant.linkButton = true;
|
|
106
115
|
j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS.link, element.value, j));
|
|
107
116
|
}
|
|
117
|
+
if (isLink && !isLoadingButton) {
|
|
118
|
+
hasVariant.link = true;
|
|
119
|
+
j(element).replaceWith((0, _generateLinkElement.generateLinkComponent)(element.node, j));
|
|
120
|
+
}
|
|
108
121
|
if (isDefaultButton && !isLoadingButton) {
|
|
109
122
|
hasVariant.defaultButton = true;
|
|
110
123
|
j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(newDefaultImportSpecifiers !== null && newDefaultImportSpecifiers !== void 0 && newDefaultImportSpecifiers.length ?
|
|
@@ -216,6 +229,13 @@ var transformer = function transformer(file, api) {
|
|
|
216
229
|
(0, _renameDefaultButtonToLegacyButton.renameDefaultButtonToLegacyButtonImport)(oldButtonImports, oldButtonsWithUnsupportedProps, j);
|
|
217
230
|
}
|
|
218
231
|
}
|
|
232
|
+
if (hasVariant.link) {
|
|
233
|
+
var linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
234
|
+
oldButtonImports.at(0).insertBefore(linkImport);
|
|
235
|
+
if (!remainingDefaultButtons && !oldButtonsWithUnsupportedProps.length) {
|
|
236
|
+
oldButtonImports.at(0).remove();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
219
239
|
if (specifiers.length) {
|
|
220
240
|
var existingNewButtonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
|
|
221
241
|
return path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT;
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var _utils = require("@hypermod/utils");
|
|
8
8
|
var _constants = require("../utils/constants");
|
|
9
|
-
function
|
|
9
|
+
function transformer(file, api) {
|
|
10
10
|
var j = api.jscodeshift;
|
|
11
11
|
var source = j(file.source);
|
|
12
12
|
|
|
@@ -118,4 +118,5 @@ function _default(file, api) {
|
|
|
118
118
|
}
|
|
119
119
|
});
|
|
120
120
|
return source.toSource(_constants.PRINT_SETTINGS);
|
|
121
|
-
}
|
|
121
|
+
}
|
|
122
|
+
var _default = exports.default = transformer;
|
|
@@ -11,6 +11,7 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
|
|
|
11
11
|
var _nextSplitImports = _interopRequireDefault(require("./codemods/next-split-imports"));
|
|
12
12
|
var _nextMigrateToNewButtonVariants = _interopRequireDefault(require("./codemods/next-migrate-to-new-button-variants"));
|
|
13
13
|
var _nextRemoveUnsafeSize = _interopRequireDefault(require("./codemods/next-remove-unsafe-size"));
|
|
14
|
+
var _nextMigrateLinkButtonToLink = _interopRequireDefault(require("./codemods/next-migrate-link-button-to-link"));
|
|
14
15
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
16
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
17
|
function transformer(_x, _x2) {
|
|
@@ -22,7 +23,7 @@ function _transformer() {
|
|
|
22
23
|
return _regenerator.default.wrap(function _callee$(_context) {
|
|
23
24
|
while (1) switch (_context.prev = _context.next) {
|
|
24
25
|
case 0:
|
|
25
|
-
transformers = [_nextSplitImports.default, _nextMigrateToNewButtonVariants.default, _nextRemoveUnsafeSize.default];
|
|
26
|
+
transformers = [_nextSplitImports.default, _nextMigrateToNewButtonVariants.default, _nextRemoveUnsafeSize.default, _nextMigrateLinkButtonToLink.default];
|
|
26
27
|
src = file.source;
|
|
27
28
|
transformers.forEach(function (transformer) {
|
|
28
29
|
if (typeof src === 'undefined') {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findJSXAttributeWithValue = void 0;
|
|
7
|
+
var findJSXAttributeWithValue = exports.findJSXAttributeWithValue = function findJSXAttributeWithValue(path, attributeName, attributeValue) {
|
|
8
|
+
var _path$attributes;
|
|
9
|
+
if (!path || path.type === 'JSXSpreadAttribute') {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
var attribute = path === null || path === void 0 || (_path$attributes = path.attributes) === null || _path$attributes === void 0 ? void 0 : _path$attributes.find(function (attribute) {
|
|
13
|
+
var _attribute$value;
|
|
14
|
+
return attribute.type === 'JSXAttribute' && attribute.name.name === attributeName && ((_attribute$value = attribute.value) === null || _attribute$value === void 0 ? void 0 : _attribute$value.type) === 'StringLiteral' && attribute.value.value === attributeValue;
|
|
15
|
+
});
|
|
16
|
+
return Boolean(attribute);
|
|
17
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.modifyLinkAttributes = exports.generateLinkComponent = void 0;
|
|
7
|
+
var modifyLinkAttributes = exports.modifyLinkAttributes = function modifyLinkAttributes(element, j) {
|
|
8
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
9
|
+
var _path$node$value, _path$node$value2;
|
|
10
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && path.node.value.value === 'link' || path.node.name.name === 'spacing' && ((_path$node$value2 = path.node.value) === null || _path$node$value2 === void 0 ? void 0 : _path$node$value2.type) === 'StringLiteral' && path.node.value.value === 'none';
|
|
11
|
+
}).remove();
|
|
12
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
13
|
+
var _path$node$value3;
|
|
14
|
+
return path.node.name.name === 'appearance' && ((_path$node$value3 = path.node.value) === null || _path$node$value3 === void 0 ? void 0 : _path$node$value3.type) === 'StringLiteral' && path.node.value.value === 'subtle-link';
|
|
15
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
16
|
+
};
|
|
17
|
+
var generateLinkComponent = exports.generateLinkComponent = function generateLinkComponent(element, j) {
|
|
18
|
+
var attributes = element.openingElement.attributes;
|
|
19
|
+
if (!element.children) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
modifyLinkAttributes(element, j);
|
|
23
|
+
j(element).find(j.JSXAttribute).filter(function (attribute) {
|
|
24
|
+
var _attribute$node$value;
|
|
25
|
+
var isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
26
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
27
|
+
var iconNode = attribute.node.value;
|
|
28
|
+
if (iconNode && iconNode.type === 'JSXExpressionContainer' && iconNode.expression.type === 'JSXElement' && element.children) {
|
|
29
|
+
var icon = iconNode.expression;
|
|
30
|
+
attribute.node.name.name === 'iconBefore' ? element.children.unshift(icon) : element.children.push(icon);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return isIconAttribute;
|
|
34
|
+
}).remove();
|
|
35
|
+
return j.jsxElement.from({
|
|
36
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
37
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
38
|
+
children: element.children
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -38,7 +38,6 @@ var getIconElement = exports.getIconElement = function getIconElement(iconAttr)
|
|
|
38
38
|
var handleIconAttributes = exports.handleIconAttributes = function handleIconAttributes(element, j) {
|
|
39
39
|
var iconRenamed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
40
40
|
var buttonAttributes = element.openingElement.attributes;
|
|
41
|
-
|
|
42
41
|
// Get iconBefore and iconAfter attributes
|
|
43
42
|
var buttonIconAttributes = buttonAttributes && getIconAttributes(buttonAttributes);
|
|
44
43
|
buttonIconAttributes === null || buttonIconAttributes === void 0 || buttonIconAttributes.forEach(function (iconAttribute) {
|
|
@@ -113,6 +112,12 @@ var generateNewElement = exports.generateNewElement = function generateNewElemen
|
|
|
113
112
|
var attributes = element.openingElement.attributes;
|
|
114
113
|
var iconAttrs = attributes && getIconAttributes(attributes);
|
|
115
114
|
var isIconOrLinkIcon = variant === _constants.NEW_BUTTON_VARIANTS.icon || variant === _constants.NEW_BUTTON_VARIANTS.linkIcon;
|
|
115
|
+
if (variant === _constants.NEW_BUTTON_VARIANTS.link) {
|
|
116
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
117
|
+
var _path$node$value;
|
|
118
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
119
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
120
|
+
}
|
|
116
121
|
if (isIconOrLinkIcon && iconAttrs !== null && iconAttrs !== void 0 && iconAttrs.length) {
|
|
117
122
|
handleIconAttributes(element, j, true);
|
|
118
123
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var moveIcon = function moveIcon(j, path, iconNode, beforeOrAfter) {
|
|
8
|
+
var element;
|
|
9
|
+
if (!iconNode) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (iconNode.type === 'JSXExpressionContainer') {
|
|
13
|
+
// when iconBefore={(iconProps) => <AddIcon {...iconProps} />}
|
|
14
|
+
if (iconNode.expression.type === 'ArrowFunctionExpression' && iconNode.expression.body.type === 'JSXElement') {
|
|
15
|
+
// remove {...iconProps} spread attribute
|
|
16
|
+
element = iconNode.expression.body;
|
|
17
|
+
j(element).find(j.JSXSpreadAttribute).remove();
|
|
18
|
+
j(element).find(j.JSXAttribute).insertBefore(j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral('')));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// when iconBefore={AddIcon}
|
|
22
|
+
if (iconNode.expression.type === 'Identifier') {
|
|
23
|
+
element = j.jsxElement(j.jsxOpeningElement(j.jsxIdentifier(iconNode.expression.name), [j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral(''))], true));
|
|
24
|
+
}
|
|
25
|
+
if (element && path.node.children) {
|
|
26
|
+
beforeOrAfter === 'iconBefore' ? path.node.children.unshift(element) : path.node.children.push(element);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var _default = exports.default = moveIcon;
|
package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-link-button-to-link.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getImportDeclaration } from '@hypermod/utils';
|
|
2
|
+
import { PRINT_SETTINGS, NEW_BUTTON_ENTRY_POINT, NEW_BUTTON_VARIANTS } from '../utils/constants';
|
|
3
|
+
import moveIconValueFromLinkButtonPropsToLinkChildren from '../utils/move-icon-value-from-link-button-to-link-children';
|
|
4
|
+
import { findJSXAttributeWithValue } from '../utils/find-attribute-with-value';
|
|
5
|
+
import { modifyLinkAttributes } from '../utils/generate-link-element';
|
|
6
|
+
function transformer(file, api) {
|
|
7
|
+
const j = api.jscodeshift;
|
|
8
|
+
const source = j(file.source);
|
|
9
|
+
|
|
10
|
+
// Find all new button imports
|
|
11
|
+
const newButtonImportDeclarations = getImportDeclaration(j, source, NEW_BUTTON_ENTRY_POINT);
|
|
12
|
+
|
|
13
|
+
// No imports for new button found, exit early
|
|
14
|
+
if (!newButtonImportDeclarations.length) {
|
|
15
|
+
return source.toSource();
|
|
16
|
+
}
|
|
17
|
+
let linkButtonSpecifier = '';
|
|
18
|
+
let linkButtonDeclaration;
|
|
19
|
+
newButtonImportDeclarations.forEach(newButtonImport => {
|
|
20
|
+
if (!newButtonImport.value.specifiers) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
newButtonImport.value.specifiers.map(specifier => {
|
|
24
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported && specifier.imported.name === NEW_BUTTON_VARIANTS['link']) {
|
|
25
|
+
var _specifier$local;
|
|
26
|
+
linkButtonDeclaration = newButtonImport;
|
|
27
|
+
linkButtonSpecifier = ((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name) || '';
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
if (!linkButtonSpecifier) {
|
|
32
|
+
return source.toSource();
|
|
33
|
+
}
|
|
34
|
+
let hasLink = false;
|
|
35
|
+
const allLinkButtons = source.find(j.JSXElement).filter(path => path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier);
|
|
36
|
+
allLinkButtons.filter(path => findJSXAttributeWithValue(path.value.openingElement, 'appearance', 'link') || findJSXAttributeWithValue(path.value.openingElement, 'appearance', 'subtle-link')).forEach(path => {
|
|
37
|
+
const hasSpacingNone = findJSXAttributeWithValue(path.value.openingElement, 'spacing', 'none');
|
|
38
|
+
if (!hasSpacingNone) {
|
|
39
|
+
j(path).find(j.JSXAttribute).filter(path => {
|
|
40
|
+
var _path$node$value;
|
|
41
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
42
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
43
|
+
}
|
|
44
|
+
if (hasSpacingNone) {
|
|
45
|
+
const {
|
|
46
|
+
attributes
|
|
47
|
+
} = path.node.openingElement;
|
|
48
|
+
modifyLinkAttributes(path.node, j);
|
|
49
|
+
j(path).find(j.JSXAttribute).filter(attribute => {
|
|
50
|
+
var _attribute$node$value;
|
|
51
|
+
const isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
52
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
53
|
+
moveIconValueFromLinkButtonPropsToLinkChildren(j, path, attribute.node.value, attribute.node.name.name);
|
|
54
|
+
}
|
|
55
|
+
return isIconAttribute;
|
|
56
|
+
}).remove();
|
|
57
|
+
hasLink = true;
|
|
58
|
+
j(path).replaceWith(j.jsxElement.from({
|
|
59
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
60
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
61
|
+
children: path.node.children
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// add link import and remove link button import if no link button is found
|
|
67
|
+
if (hasLink && linkButtonDeclaration) {
|
|
68
|
+
const linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
69
|
+
j(linkButtonDeclaration).insertAfter(linkImport);
|
|
70
|
+
const remainingLinkButton = source.find(j.JSXElement).filter(path => path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier);
|
|
71
|
+
if (!remainingLinkButton.length) {
|
|
72
|
+
var _value$specifiers;
|
|
73
|
+
j(linkButtonDeclaration).find(j.ImportSpecifier).filter(path => path.node.imported.name === NEW_BUTTON_VARIANTS['link']).remove();
|
|
74
|
+
if (((_value$specifiers = linkButtonDeclaration.value.specifiers) === null || _value$specifiers === void 0 ? void 0 : _value$specifiers.length) === 0) {
|
|
75
|
+
j(linkButtonDeclaration).remove();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return source.toSource(PRINT_SETTINGS);
|
|
80
|
+
}
|
|
81
|
+
export default transformer;
|
package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js
CHANGED
|
@@ -4,6 +4,7 @@ import { PRINT_SETTINGS, OLD_BUTTON_VARIANTS, NEW_BUTTON_VARIANTS, entryPointsMa
|
|
|
4
4
|
import getDefaultImports from '../utils/get-default-imports';
|
|
5
5
|
import getSpecifierNames from '../utils/get-specifier-names';
|
|
6
6
|
import { generateNewElement, handleIconAttributes } from '../utils/generate-new-button-element';
|
|
7
|
+
import { generateLinkComponent } from '../utils/generate-link-element';
|
|
7
8
|
import { ifHasUnsupportedProps } from '../utils/has-unsupported-props';
|
|
8
9
|
import { checkIfVariantAlreadyImported } from '../utils/if-variant-already-imported';
|
|
9
10
|
import { renameDefaultButtonToLegacyButtonImport } from '../utils/rename-default-button-to-legacy-button';
|
|
@@ -11,6 +12,7 @@ import { migrateFitContainerIconButton } from '../utils/migrate-fit-container-ic
|
|
|
11
12
|
import { importTypesFromNewEntryPoint } from '../utils/import-types-from-new-entry-point';
|
|
12
13
|
import { addCommentForCustomThemeButtons } from '../utils/add-comment-for-custom-theme-buttons';
|
|
13
14
|
import { addCommentForOverlayProp } from '../utils/add-comment-for-overlay-prop';
|
|
15
|
+
import { findJSXAttributeWithValue } from '../utils/find-attribute-with-value';
|
|
14
16
|
const transformer = (file, api) => {
|
|
15
17
|
const j = api.jscodeshift;
|
|
16
18
|
const fileSource = j(file.source);
|
|
@@ -40,7 +42,8 @@ const transformer = (file, api) => {
|
|
|
40
42
|
defaultButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.default, NEW_BUTTON_ENTRY_POINT, fileSource, j, true),
|
|
41
43
|
linkButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.link, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
42
44
|
iconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.icon, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
43
|
-
linkIconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.linkIcon, NEW_BUTTON_ENTRY_POINT, fileSource, j)
|
|
45
|
+
linkIconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.linkIcon, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
46
|
+
link: checkIfVariantAlreadyImported('link', '@atlaskit/link', fileSource, j)
|
|
44
47
|
};
|
|
45
48
|
const oldButtonElements = fileSource.find(j.JSXElement).filter(path => {
|
|
46
49
|
return path.value.openingElement.name.type === 'JSXIdentifier' && ((oldDefaultImportSpecifiers === null || oldDefaultImportSpecifiers === void 0 ? void 0 : oldDefaultImportSpecifiers.includes(path.value.openingElement.name.name)) || Object.values(OLD_BUTTON_VARIANTS).includes(path.value.openingElement.name.name));
|
|
@@ -59,18 +62,24 @@ const transformer = (file, api) => {
|
|
|
59
62
|
const hasHref = buttonAttributes.includes('href');
|
|
60
63
|
const hasIcon = buttonAttributes.includes('iconBefore') || buttonAttributes.includes('iconAfter');
|
|
61
64
|
const hasNoChildren = ((_element$value$childr = element.value.children) === null || _element$value$childr === void 0 ? void 0 : _element$value$childr.length) === 0;
|
|
65
|
+
const hasSpacing = attributes.find(attr => {
|
|
66
|
+
var _attr$value;
|
|
67
|
+
return attr.type === 'JSXAttribute' && attr.name.name === 'spacing' && ((_attr$value = attr.value) === null || _attr$value === void 0 ? void 0 : _attr$value.type) === 'StringLiteral' && attr.value.value !== 'none';
|
|
68
|
+
}) || !buttonAttributes.includes('spacing');
|
|
69
|
+
const hasSpacingNone = findJSXAttributeWithValue(element.value.openingElement, 'spacing', 'none');
|
|
62
70
|
const isFitContainerIconButton = hasIcon && hasNoChildren && buttonAttributes.includes('shouldFitContainer');
|
|
63
|
-
const isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
64
|
-
const isLinkButton = hasHref && !isLinkIconButton;
|
|
65
|
-
let isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
66
|
-
const isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton;
|
|
67
|
-
const isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon;
|
|
68
|
-
const isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
69
71
|
const linkAppearanceAttribute = attributes.find(node => {
|
|
70
72
|
var _node$value, _node$name, _node$value2, _node$value3;
|
|
71
73
|
return node.type === 'JSXAttribute' && ((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'StringLiteral' && (node === null || node === void 0 ? void 0 : (_node$name = node.name) === null || _node$name === void 0 ? void 0 : _node$name.name) === 'appearance' && ((node === null || node === void 0 ? void 0 : (_node$value2 = node.value) === null || _node$value2 === void 0 ? void 0 : _node$value2.value) === 'link' || (node === null || node === void 0 ? void 0 : (_node$value3 = node.value) === null || _node$value3 === void 0 ? void 0 : _node$value3.value) === 'subtle-link');
|
|
72
74
|
});
|
|
73
|
-
|
|
75
|
+
const isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
76
|
+
const isLinkButton = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacing;
|
|
77
|
+
const isLink = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacingNone && linkAppearanceAttribute;
|
|
78
|
+
let isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton && !isLink;
|
|
79
|
+
const isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton && !isLink;
|
|
80
|
+
const isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon && !isLink;
|
|
81
|
+
const isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
82
|
+
if (isDefaultVariantWithAnIcon && !isLink) {
|
|
74
83
|
handleIconAttributes(element.value, j);
|
|
75
84
|
}
|
|
76
85
|
if (isFitContainerIconButton) {
|
|
@@ -91,6 +100,10 @@ const transformer = (file, api) => {
|
|
|
91
100
|
hasVariant.linkButton = true;
|
|
92
101
|
j(element).replaceWith(generateNewElement(NEW_BUTTON_VARIANTS.link, element.value, j));
|
|
93
102
|
}
|
|
103
|
+
if (isLink && !isLoadingButton) {
|
|
104
|
+
hasVariant.link = true;
|
|
105
|
+
j(element).replaceWith(generateLinkComponent(element.node, j));
|
|
106
|
+
}
|
|
94
107
|
if (isDefaultButton && !isLoadingButton) {
|
|
95
108
|
hasVariant.defaultButton = true;
|
|
96
109
|
j(element).replaceWith(generateNewElement(newDefaultImportSpecifiers !== null && newDefaultImportSpecifiers !== void 0 && newDefaultImportSpecifiers.length ?
|
|
@@ -182,6 +195,13 @@ const transformer = (file, api) => {
|
|
|
182
195
|
renameDefaultButtonToLegacyButtonImport(oldButtonImports, oldButtonsWithUnsupportedProps, j);
|
|
183
196
|
}
|
|
184
197
|
}
|
|
198
|
+
if (hasVariant.link) {
|
|
199
|
+
const linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
200
|
+
oldButtonImports.at(0).insertBefore(linkImport);
|
|
201
|
+
if (!remainingDefaultButtons && !oldButtonsWithUnsupportedProps.length) {
|
|
202
|
+
oldButtonImports.at(0).remove();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
185
205
|
if (specifiers.length) {
|
|
186
206
|
const existingNewButtonImports = fileSource.find(j.ImportDeclaration).filter(path => path.node.source.value === NEW_BUTTON_ENTRY_POINT);
|
|
187
207
|
let newButtonImport = j.importDeclaration(specifiers, j.stringLiteral(NEW_BUTTON_ENTRY_POINT));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getImportDeclaration, hasJSXAttributes } from '@hypermod/utils';
|
|
2
2
|
import { PRINT_SETTINGS, NEW_BUTTON_ENTRY_POINT, UNSAFE_SIZE_PROPS_MAP } from '../utils/constants';
|
|
3
|
-
|
|
3
|
+
function transformer(file, api) {
|
|
4
4
|
const j = api.jscodeshift;
|
|
5
5
|
const source = j(file.source);
|
|
6
6
|
|
|
@@ -91,4 +91,5 @@ export default function (file, api) {
|
|
|
91
91
|
}
|
|
92
92
|
});
|
|
93
93
|
return source.toSource(PRINT_SETTINGS);
|
|
94
|
-
}
|
|
94
|
+
}
|
|
95
|
+
export default transformer;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import splitImportsTransformer from './codemods/next-split-imports';
|
|
2
2
|
import migrateToNewButtonVariantsTransformer from './codemods/next-migrate-to-new-button-variants';
|
|
3
3
|
import removeUnsafeSizeTransformer from './codemods/next-remove-unsafe-size';
|
|
4
|
+
import migrateLinkButtonToLinkTransformer from './codemods/next-migrate-link-button-to-link';
|
|
4
5
|
export default async function transformer(file, api) {
|
|
5
|
-
const transformers = [splitImportsTransformer, migrateToNewButtonVariantsTransformer, removeUnsafeSizeTransformer];
|
|
6
|
+
const transformers = [splitImportsTransformer, migrateToNewButtonVariantsTransformer, removeUnsafeSizeTransformer, migrateLinkButtonToLinkTransformer];
|
|
6
7
|
let src = file.source;
|
|
7
8
|
transformers.forEach(transformer => {
|
|
8
9
|
if (typeof src === 'undefined') {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const findJSXAttributeWithValue = (path, attributeName, attributeValue) => {
|
|
2
|
+
var _path$attributes;
|
|
3
|
+
if (!path || path.type === 'JSXSpreadAttribute') {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
const attribute = path === null || path === void 0 ? void 0 : (_path$attributes = path.attributes) === null || _path$attributes === void 0 ? void 0 : _path$attributes.find(attribute => {
|
|
7
|
+
var _attribute$value;
|
|
8
|
+
return attribute.type === 'JSXAttribute' && attribute.name.name === attributeName && ((_attribute$value = attribute.value) === null || _attribute$value === void 0 ? void 0 : _attribute$value.type) === 'StringLiteral' && attribute.value.value === attributeValue;
|
|
9
|
+
});
|
|
10
|
+
return Boolean(attribute);
|
|
11
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const modifyLinkAttributes = (element, j) => {
|
|
2
|
+
j(element).find(j.JSXAttribute).filter(path => {
|
|
3
|
+
var _path$node$value, _path$node$value2;
|
|
4
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && path.node.value.value === 'link' || path.node.name.name === 'spacing' && ((_path$node$value2 = path.node.value) === null || _path$node$value2 === void 0 ? void 0 : _path$node$value2.type) === 'StringLiteral' && path.node.value.value === 'none';
|
|
5
|
+
}).remove();
|
|
6
|
+
j(element).find(j.JSXAttribute).filter(path => {
|
|
7
|
+
var _path$node$value3;
|
|
8
|
+
return path.node.name.name === 'appearance' && ((_path$node$value3 = path.node.value) === null || _path$node$value3 === void 0 ? void 0 : _path$node$value3.type) === 'StringLiteral' && path.node.value.value === 'subtle-link';
|
|
9
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
10
|
+
};
|
|
11
|
+
export const generateLinkComponent = (element, j) => {
|
|
12
|
+
const {
|
|
13
|
+
attributes
|
|
14
|
+
} = element.openingElement;
|
|
15
|
+
if (!element.children) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
modifyLinkAttributes(element, j);
|
|
19
|
+
j(element).find(j.JSXAttribute).filter(attribute => {
|
|
20
|
+
var _attribute$node$value;
|
|
21
|
+
const isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
22
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
23
|
+
const iconNode = attribute.node.value;
|
|
24
|
+
if (iconNode && iconNode.type === 'JSXExpressionContainer' && iconNode.expression.type === 'JSXElement' && element.children) {
|
|
25
|
+
const icon = iconNode.expression;
|
|
26
|
+
attribute.node.name.name === 'iconBefore' ? element.children.unshift(icon) : element.children.push(icon);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return isIconAttribute;
|
|
30
|
+
}).remove();
|
|
31
|
+
return j.jsxElement.from({
|
|
32
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
33
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
34
|
+
children: element.children
|
|
35
|
+
});
|
|
36
|
+
};
|
|
@@ -31,7 +31,6 @@ export const handleIconAttributes = (element, j, iconRenamed = false) => {
|
|
|
31
31
|
const {
|
|
32
32
|
attributes: buttonAttributes
|
|
33
33
|
} = element.openingElement;
|
|
34
|
-
|
|
35
34
|
// Get iconBefore and iconAfter attributes
|
|
36
35
|
const buttonIconAttributes = buttonAttributes && getIconAttributes(buttonAttributes);
|
|
37
36
|
buttonIconAttributes === null || buttonIconAttributes === void 0 ? void 0 : buttonIconAttributes.forEach(iconAttribute => {
|
|
@@ -100,6 +99,12 @@ export const generateNewElement = (variant, element, j) => {
|
|
|
100
99
|
} = element.openingElement;
|
|
101
100
|
const iconAttrs = attributes && getIconAttributes(attributes);
|
|
102
101
|
const isIconOrLinkIcon = variant === NEW_BUTTON_VARIANTS.icon || variant === NEW_BUTTON_VARIANTS.linkIcon;
|
|
102
|
+
if (variant === NEW_BUTTON_VARIANTS.link) {
|
|
103
|
+
j(element).find(j.JSXAttribute).filter(path => {
|
|
104
|
+
var _path$node$value;
|
|
105
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
106
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
107
|
+
}
|
|
103
108
|
if (isIconOrLinkIcon && iconAttrs !== null && iconAttrs !== void 0 && iconAttrs.length) {
|
|
104
109
|
handleIconAttributes(element, j, true);
|
|
105
110
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const moveIcon = (j, path, iconNode, beforeOrAfter) => {
|
|
2
|
+
let element;
|
|
3
|
+
if (!iconNode) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
if (iconNode.type === 'JSXExpressionContainer') {
|
|
7
|
+
// when iconBefore={(iconProps) => <AddIcon {...iconProps} />}
|
|
8
|
+
if (iconNode.expression.type === 'ArrowFunctionExpression' && iconNode.expression.body.type === 'JSXElement') {
|
|
9
|
+
// remove {...iconProps} spread attribute
|
|
10
|
+
element = iconNode.expression.body;
|
|
11
|
+
j(element).find(j.JSXSpreadAttribute).remove();
|
|
12
|
+
j(element).find(j.JSXAttribute).insertBefore(j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral('')));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// when iconBefore={AddIcon}
|
|
16
|
+
if (iconNode.expression.type === 'Identifier') {
|
|
17
|
+
element = j.jsxElement(j.jsxOpeningElement(j.jsxIdentifier(iconNode.expression.name), [j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral(''))], true));
|
|
18
|
+
}
|
|
19
|
+
if (element && path.node.children) {
|
|
20
|
+
beforeOrAfter === 'iconBefore' ? path.node.children.unshift(element) : path.node.children.push(element);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export default moveIcon;
|
package/dist/esm/main.js
CHANGED
|
@@ -298,7 +298,7 @@ function _main() {
|
|
|
298
298
|
case 4:
|
|
299
299
|
_yield$parseArgs = _context5.sent;
|
|
300
300
|
packages = _yield$parseArgs.packages;
|
|
301
|
-
_process$env$_PACKAGE = "0.25.
|
|
301
|
+
_process$env$_PACKAGE = "0.25.1", _PACKAGE_VERSION_ = _process$env$_PACKAGE === void 0 ? '0.0.0-dev' : _process$env$_PACKAGE;
|
|
302
302
|
logger.log(chalk.bgBlue(chalk.black("\uD83D\uDCDA Atlassian-Frontend codemod library @ ".concat(_PACKAGE_VERSION_, " \uD83D\uDCDA"))));
|
|
303
303
|
if (packages && packages.length > 0) {
|
|
304
304
|
logger.log(chalk.gray("Searching for codemods for newer versions of the following packages: ".concat(packages.map(function (pkg) {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { getImportDeclaration } from '@hypermod/utils';
|
|
2
|
+
import { PRINT_SETTINGS, NEW_BUTTON_ENTRY_POINT, NEW_BUTTON_VARIANTS } from '../utils/constants';
|
|
3
|
+
import moveIconValueFromLinkButtonPropsToLinkChildren from '../utils/move-icon-value-from-link-button-to-link-children';
|
|
4
|
+
import { findJSXAttributeWithValue } from '../utils/find-attribute-with-value';
|
|
5
|
+
import { modifyLinkAttributes } from '../utils/generate-link-element';
|
|
6
|
+
function transformer(file, api) {
|
|
7
|
+
var j = api.jscodeshift;
|
|
8
|
+
var source = j(file.source);
|
|
9
|
+
|
|
10
|
+
// Find all new button imports
|
|
11
|
+
var newButtonImportDeclarations = getImportDeclaration(j, source, NEW_BUTTON_ENTRY_POINT);
|
|
12
|
+
|
|
13
|
+
// No imports for new button found, exit early
|
|
14
|
+
if (!newButtonImportDeclarations.length) {
|
|
15
|
+
return source.toSource();
|
|
16
|
+
}
|
|
17
|
+
var linkButtonSpecifier = '';
|
|
18
|
+
var linkButtonDeclaration;
|
|
19
|
+
newButtonImportDeclarations.forEach(function (newButtonImport) {
|
|
20
|
+
if (!newButtonImport.value.specifiers) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
newButtonImport.value.specifiers.map(function (specifier) {
|
|
24
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported && specifier.imported.name === NEW_BUTTON_VARIANTS['link']) {
|
|
25
|
+
var _specifier$local;
|
|
26
|
+
linkButtonDeclaration = newButtonImport;
|
|
27
|
+
linkButtonSpecifier = ((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name) || '';
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
if (!linkButtonSpecifier) {
|
|
32
|
+
return source.toSource();
|
|
33
|
+
}
|
|
34
|
+
var hasLink = false;
|
|
35
|
+
var allLinkButtons = source.find(j.JSXElement).filter(function (path) {
|
|
36
|
+
return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier;
|
|
37
|
+
});
|
|
38
|
+
allLinkButtons.filter(function (path) {
|
|
39
|
+
return findJSXAttributeWithValue(path.value.openingElement, 'appearance', 'link') || findJSXAttributeWithValue(path.value.openingElement, 'appearance', 'subtle-link');
|
|
40
|
+
}).forEach(function (path) {
|
|
41
|
+
var hasSpacingNone = findJSXAttributeWithValue(path.value.openingElement, 'spacing', 'none');
|
|
42
|
+
if (!hasSpacingNone) {
|
|
43
|
+
j(path).find(j.JSXAttribute).filter(function (path) {
|
|
44
|
+
var _path$node$value;
|
|
45
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
46
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
47
|
+
}
|
|
48
|
+
if (hasSpacingNone) {
|
|
49
|
+
var attributes = path.node.openingElement.attributes;
|
|
50
|
+
modifyLinkAttributes(path.node, j);
|
|
51
|
+
j(path).find(j.JSXAttribute).filter(function (attribute) {
|
|
52
|
+
var _attribute$node$value;
|
|
53
|
+
var isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
54
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
55
|
+
moveIconValueFromLinkButtonPropsToLinkChildren(j, path, attribute.node.value, attribute.node.name.name);
|
|
56
|
+
}
|
|
57
|
+
return isIconAttribute;
|
|
58
|
+
}).remove();
|
|
59
|
+
hasLink = true;
|
|
60
|
+
j(path).replaceWith(j.jsxElement.from({
|
|
61
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
62
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
63
|
+
children: path.node.children
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// add link import and remove link button import if no link button is found
|
|
69
|
+
if (hasLink && linkButtonDeclaration) {
|
|
70
|
+
var linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
71
|
+
j(linkButtonDeclaration).insertAfter(linkImport);
|
|
72
|
+
var remainingLinkButton = source.find(j.JSXElement).filter(function (path) {
|
|
73
|
+
return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === linkButtonSpecifier;
|
|
74
|
+
});
|
|
75
|
+
if (!remainingLinkButton.length) {
|
|
76
|
+
var _value$specifiers;
|
|
77
|
+
j(linkButtonDeclaration).find(j.ImportSpecifier).filter(function (path) {
|
|
78
|
+
return path.node.imported.name === NEW_BUTTON_VARIANTS['link'];
|
|
79
|
+
}).remove();
|
|
80
|
+
if (((_value$specifiers = linkButtonDeclaration.value.specifiers) === null || _value$specifiers === void 0 ? void 0 : _value$specifiers.length) === 0) {
|
|
81
|
+
j(linkButtonDeclaration).remove();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return source.toSource(PRINT_SETTINGS);
|
|
86
|
+
}
|
|
87
|
+
export default transformer;
|
package/dist/esm/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js
CHANGED
|
@@ -5,6 +5,7 @@ import { PRINT_SETTINGS, OLD_BUTTON_VARIANTS, NEW_BUTTON_VARIANTS, entryPointsMa
|
|
|
5
5
|
import getDefaultImports from '../utils/get-default-imports';
|
|
6
6
|
import getSpecifierNames from '../utils/get-specifier-names';
|
|
7
7
|
import { generateNewElement, handleIconAttributes } from '../utils/generate-new-button-element';
|
|
8
|
+
import { generateLinkComponent } from '../utils/generate-link-element';
|
|
8
9
|
import { ifHasUnsupportedProps } from '../utils/has-unsupported-props';
|
|
9
10
|
import { checkIfVariantAlreadyImported } from '../utils/if-variant-already-imported';
|
|
10
11
|
import { renameDefaultButtonToLegacyButtonImport } from '../utils/rename-default-button-to-legacy-button';
|
|
@@ -12,6 +13,7 @@ import { migrateFitContainerIconButton } from '../utils/migrate-fit-container-ic
|
|
|
12
13
|
import { importTypesFromNewEntryPoint } from '../utils/import-types-from-new-entry-point';
|
|
13
14
|
import { addCommentForCustomThemeButtons } from '../utils/add-comment-for-custom-theme-buttons';
|
|
14
15
|
import { addCommentForOverlayProp } from '../utils/add-comment-for-overlay-prop';
|
|
16
|
+
import { findJSXAttributeWithValue } from '../utils/find-attribute-with-value';
|
|
15
17
|
var transformer = function transformer(file, api) {
|
|
16
18
|
var j = api.jscodeshift;
|
|
17
19
|
var fileSource = j(file.source);
|
|
@@ -45,7 +47,8 @@ var transformer = function transformer(file, api) {
|
|
|
45
47
|
defaultButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.default, NEW_BUTTON_ENTRY_POINT, fileSource, j, true),
|
|
46
48
|
linkButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.link, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
47
49
|
iconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.icon, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
48
|
-
linkIconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.linkIcon, NEW_BUTTON_ENTRY_POINT, fileSource, j)
|
|
50
|
+
linkIconButton: checkIfVariantAlreadyImported(NEW_BUTTON_VARIANTS.linkIcon, NEW_BUTTON_ENTRY_POINT, fileSource, j),
|
|
51
|
+
link: checkIfVariantAlreadyImported('link', '@atlaskit/link', fileSource, j)
|
|
49
52
|
};
|
|
50
53
|
var oldButtonElements = fileSource.find(j.JSXElement).filter(function (path) {
|
|
51
54
|
return path.value.openingElement.name.type === 'JSXIdentifier' && ((oldDefaultImportSpecifiers === null || oldDefaultImportSpecifiers === void 0 ? void 0 : oldDefaultImportSpecifiers.includes(path.value.openingElement.name.name)) || Object.values(OLD_BUTTON_VARIANTS).includes(path.value.openingElement.name.name));
|
|
@@ -66,18 +69,24 @@ var transformer = function transformer(file, api) {
|
|
|
66
69
|
var hasHref = buttonAttributes.includes('href');
|
|
67
70
|
var hasIcon = buttonAttributes.includes('iconBefore') || buttonAttributes.includes('iconAfter');
|
|
68
71
|
var hasNoChildren = ((_element$value$childr = element.value.children) === null || _element$value$childr === void 0 ? void 0 : _element$value$childr.length) === 0;
|
|
72
|
+
var hasSpacing = attributes.find(function (attr) {
|
|
73
|
+
var _attr$value;
|
|
74
|
+
return attr.type === 'JSXAttribute' && attr.name.name === 'spacing' && ((_attr$value = attr.value) === null || _attr$value === void 0 ? void 0 : _attr$value.type) === 'StringLiteral' && attr.value.value !== 'none';
|
|
75
|
+
}) || !buttonAttributes.includes('spacing');
|
|
76
|
+
var hasSpacingNone = findJSXAttributeWithValue(element.value.openingElement, 'spacing', 'none');
|
|
69
77
|
var isFitContainerIconButton = hasIcon && hasNoChildren && buttonAttributes.includes('shouldFitContainer');
|
|
70
|
-
var isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
71
|
-
var isLinkButton = hasHref && !isLinkIconButton;
|
|
72
|
-
var isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
73
|
-
var isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton;
|
|
74
|
-
var isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon;
|
|
75
|
-
var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
76
78
|
var linkAppearanceAttribute = attributes.find(function (node) {
|
|
77
79
|
var _node$value, _node$name, _node$value2, _node$value3;
|
|
78
80
|
return node.type === 'JSXAttribute' && ((_node$value = node.value) === null || _node$value === void 0 ? void 0 : _node$value.type) === 'StringLiteral' && (node === null || node === void 0 || (_node$name = node.name) === null || _node$name === void 0 ? void 0 : _node$name.name) === 'appearance' && ((node === null || node === void 0 || (_node$value2 = node.value) === null || _node$value2 === void 0 ? void 0 : _node$value2.value) === 'link' || (node === null || node === void 0 || (_node$value3 = node.value) === null || _node$value3 === void 0 ? void 0 : _node$value3.value) === 'subtle-link');
|
|
79
81
|
});
|
|
80
|
-
|
|
82
|
+
var isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
|
|
83
|
+
var isLinkButton = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacing;
|
|
84
|
+
var isLink = hasHref && !isLinkIconButton && !hasNoChildren && hasSpacingNone && linkAppearanceAttribute;
|
|
85
|
+
var isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton && !isLink;
|
|
86
|
+
var isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton && !isLink;
|
|
87
|
+
var isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon && !isLink;
|
|
88
|
+
var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
|
|
89
|
+
if (isDefaultVariantWithAnIcon && !isLink) {
|
|
81
90
|
handleIconAttributes(element.value, j);
|
|
82
91
|
}
|
|
83
92
|
if (isFitContainerIconButton) {
|
|
@@ -98,6 +107,10 @@ var transformer = function transformer(file, api) {
|
|
|
98
107
|
hasVariant.linkButton = true;
|
|
99
108
|
j(element).replaceWith(generateNewElement(NEW_BUTTON_VARIANTS.link, element.value, j));
|
|
100
109
|
}
|
|
110
|
+
if (isLink && !isLoadingButton) {
|
|
111
|
+
hasVariant.link = true;
|
|
112
|
+
j(element).replaceWith(generateLinkComponent(element.node, j));
|
|
113
|
+
}
|
|
101
114
|
if (isDefaultButton && !isLoadingButton) {
|
|
102
115
|
hasVariant.defaultButton = true;
|
|
103
116
|
j(element).replaceWith(generateNewElement(newDefaultImportSpecifiers !== null && newDefaultImportSpecifiers !== void 0 && newDefaultImportSpecifiers.length ?
|
|
@@ -209,6 +222,13 @@ var transformer = function transformer(file, api) {
|
|
|
209
222
|
renameDefaultButtonToLegacyButtonImport(oldButtonImports, oldButtonsWithUnsupportedProps, j);
|
|
210
223
|
}
|
|
211
224
|
}
|
|
225
|
+
if (hasVariant.link) {
|
|
226
|
+
var linkImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier('Link'))], j.stringLiteral('@atlaskit/link'));
|
|
227
|
+
oldButtonImports.at(0).insertBefore(linkImport);
|
|
228
|
+
if (!remainingDefaultButtons && !oldButtonsWithUnsupportedProps.length) {
|
|
229
|
+
oldButtonImports.at(0).remove();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
212
232
|
if (specifiers.length) {
|
|
213
233
|
var existingNewButtonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
|
|
214
234
|
return path.node.source.value === NEW_BUTTON_ENTRY_POINT;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getImportDeclaration, hasJSXAttributes } from '@hypermod/utils';
|
|
2
2
|
import { PRINT_SETTINGS, NEW_BUTTON_ENTRY_POINT, UNSAFE_SIZE_PROPS_MAP } from '../utils/constants';
|
|
3
|
-
|
|
3
|
+
function transformer(file, api) {
|
|
4
4
|
var j = api.jscodeshift;
|
|
5
5
|
var source = j(file.source);
|
|
6
6
|
|
|
@@ -112,4 +112,5 @@ export default function (file, api) {
|
|
|
112
112
|
}
|
|
113
113
|
});
|
|
114
114
|
return source.toSource(PRINT_SETTINGS);
|
|
115
|
-
}
|
|
115
|
+
}
|
|
116
|
+
export default transformer;
|
|
@@ -6,6 +6,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
|
|
6
6
|
import splitImportsTransformer from './codemods/next-split-imports';
|
|
7
7
|
import migrateToNewButtonVariantsTransformer from './codemods/next-migrate-to-new-button-variants';
|
|
8
8
|
import removeUnsafeSizeTransformer from './codemods/next-remove-unsafe-size';
|
|
9
|
+
import migrateLinkButtonToLinkTransformer from './codemods/next-migrate-link-button-to-link';
|
|
9
10
|
export default function transformer(_x, _x2) {
|
|
10
11
|
return _transformer.apply(this, arguments);
|
|
11
12
|
}
|
|
@@ -15,7 +16,7 @@ function _transformer() {
|
|
|
15
16
|
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
16
17
|
while (1) switch (_context.prev = _context.next) {
|
|
17
18
|
case 0:
|
|
18
|
-
transformers = [splitImportsTransformer, migrateToNewButtonVariantsTransformer, removeUnsafeSizeTransformer];
|
|
19
|
+
transformers = [splitImportsTransformer, migrateToNewButtonVariantsTransformer, removeUnsafeSizeTransformer, migrateLinkButtonToLinkTransformer];
|
|
19
20
|
src = file.source;
|
|
20
21
|
transformers.forEach(function (transformer) {
|
|
21
22
|
if (typeof src === 'undefined') {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export var findJSXAttributeWithValue = function findJSXAttributeWithValue(path, attributeName, attributeValue) {
|
|
2
|
+
var _path$attributes;
|
|
3
|
+
if (!path || path.type === 'JSXSpreadAttribute') {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
var attribute = path === null || path === void 0 || (_path$attributes = path.attributes) === null || _path$attributes === void 0 ? void 0 : _path$attributes.find(function (attribute) {
|
|
7
|
+
var _attribute$value;
|
|
8
|
+
return attribute.type === 'JSXAttribute' && attribute.name.name === attributeName && ((_attribute$value = attribute.value) === null || _attribute$value === void 0 ? void 0 : _attribute$value.type) === 'StringLiteral' && attribute.value.value === attributeValue;
|
|
9
|
+
});
|
|
10
|
+
return Boolean(attribute);
|
|
11
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export var modifyLinkAttributes = function modifyLinkAttributes(element, j) {
|
|
2
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
3
|
+
var _path$node$value, _path$node$value2;
|
|
4
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && path.node.value.value === 'link' || path.node.name.name === 'spacing' && ((_path$node$value2 = path.node.value) === null || _path$node$value2 === void 0 ? void 0 : _path$node$value2.type) === 'StringLiteral' && path.node.value.value === 'none';
|
|
5
|
+
}).remove();
|
|
6
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
7
|
+
var _path$node$value3;
|
|
8
|
+
return path.node.name.name === 'appearance' && ((_path$node$value3 = path.node.value) === null || _path$node$value3 === void 0 ? void 0 : _path$node$value3.type) === 'StringLiteral' && path.node.value.value === 'subtle-link';
|
|
9
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
10
|
+
};
|
|
11
|
+
export var generateLinkComponent = function generateLinkComponent(element, j) {
|
|
12
|
+
var attributes = element.openingElement.attributes;
|
|
13
|
+
if (!element.children) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
modifyLinkAttributes(element, j);
|
|
17
|
+
j(element).find(j.JSXAttribute).filter(function (attribute) {
|
|
18
|
+
var _attribute$node$value;
|
|
19
|
+
var isIconAttribute = attribute.node.name.name === 'iconBefore' || attribute.node.name.name === 'iconAfter';
|
|
20
|
+
if (((_attribute$node$value = attribute.node.value) === null || _attribute$node$value === void 0 ? void 0 : _attribute$node$value.type) === 'JSXExpressionContainer' && isIconAttribute) {
|
|
21
|
+
var iconNode = attribute.node.value;
|
|
22
|
+
if (iconNode && iconNode.type === 'JSXExpressionContainer' && iconNode.expression.type === 'JSXElement' && element.children) {
|
|
23
|
+
var icon = iconNode.expression;
|
|
24
|
+
attribute.node.name.name === 'iconBefore' ? element.children.unshift(icon) : element.children.push(icon);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return isIconAttribute;
|
|
28
|
+
}).remove();
|
|
29
|
+
return j.jsxElement.from({
|
|
30
|
+
openingElement: j.jsxOpeningElement(j.jsxIdentifier('Link'), attributes, false),
|
|
31
|
+
closingElement: j.jsxClosingElement(j.jsxIdentifier('Link')),
|
|
32
|
+
children: element.children
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -32,7 +32,6 @@ export var getIconElement = function getIconElement(iconAttr) {
|
|
|
32
32
|
export var handleIconAttributes = function handleIconAttributes(element, j) {
|
|
33
33
|
var iconRenamed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
34
34
|
var buttonAttributes = element.openingElement.attributes;
|
|
35
|
-
|
|
36
35
|
// Get iconBefore and iconAfter attributes
|
|
37
36
|
var buttonIconAttributes = buttonAttributes && getIconAttributes(buttonAttributes);
|
|
38
37
|
buttonIconAttributes === null || buttonIconAttributes === void 0 || buttonIconAttributes.forEach(function (iconAttribute) {
|
|
@@ -107,6 +106,12 @@ export var generateNewElement = function generateNewElement(variant, element, j)
|
|
|
107
106
|
var attributes = element.openingElement.attributes;
|
|
108
107
|
var iconAttrs = attributes && getIconAttributes(attributes);
|
|
109
108
|
var isIconOrLinkIcon = variant === NEW_BUTTON_VARIANTS.icon || variant === NEW_BUTTON_VARIANTS.linkIcon;
|
|
109
|
+
if (variant === NEW_BUTTON_VARIANTS.link) {
|
|
110
|
+
j(element).find(j.JSXAttribute).filter(function (path) {
|
|
111
|
+
var _path$node$value;
|
|
112
|
+
return path.node.name.name === 'appearance' && ((_path$node$value = path.node.value) === null || _path$node$value === void 0 ? void 0 : _path$node$value.type) === 'StringLiteral' && (path.node.value.value === 'subtle-link' || path.node.value.value === 'link');
|
|
113
|
+
}).replaceWith(j.jsxAttribute(j.jsxIdentifier('appearance'), j.stringLiteral('subtle')));
|
|
114
|
+
}
|
|
110
115
|
if (isIconOrLinkIcon && iconAttrs !== null && iconAttrs !== void 0 && iconAttrs.length) {
|
|
111
116
|
handleIconAttributes(element, j, true);
|
|
112
117
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var moveIcon = function moveIcon(j, path, iconNode, beforeOrAfter) {
|
|
2
|
+
var element;
|
|
3
|
+
if (!iconNode) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
if (iconNode.type === 'JSXExpressionContainer') {
|
|
7
|
+
// when iconBefore={(iconProps) => <AddIcon {...iconProps} />}
|
|
8
|
+
if (iconNode.expression.type === 'ArrowFunctionExpression' && iconNode.expression.body.type === 'JSXElement') {
|
|
9
|
+
// remove {...iconProps} spread attribute
|
|
10
|
+
element = iconNode.expression.body;
|
|
11
|
+
j(element).find(j.JSXSpreadAttribute).remove();
|
|
12
|
+
j(element).find(j.JSXAttribute).insertBefore(j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral('')));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// when iconBefore={AddIcon}
|
|
16
|
+
if (iconNode.expression.type === 'Identifier') {
|
|
17
|
+
element = j.jsxElement(j.jsxOpeningElement(j.jsxIdentifier(iconNode.expression.name), [j.jsxAttribute(j.jsxIdentifier('label'), j.stringLiteral(''))], true));
|
|
18
|
+
}
|
|
19
|
+
if (element && path.node.children) {
|
|
20
|
+
beforeOrAfter === 'iconBefore' ? path.node.children.unshift(element) : path.node.children.push(element);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export default moveIcon;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type JSXElement, type API, type JSXExpressionContainer, type ASTPath } from 'jscodeshift';
|
|
2
|
+
declare const moveIcon: (j: API['jscodeshift'], path: ASTPath<JSXElement>, iconNode: JSXExpressionContainer | undefined | null, beforeOrAfter: 'iconBefore' | 'iconAfter') => void;
|
|
3
|
+
export default moveIcon;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type JSXElement, type API, type JSXExpressionContainer, type ASTPath } from 'jscodeshift';
|
|
2
|
+
declare const moveIcon: (j: API['jscodeshift'], path: ASTPath<JSXElement>, iconNode: JSXExpressionContainer | undefined | null, beforeOrAfter: 'iconBefore' | 'iconAfter') => void;
|
|
3
|
+
export default moveIcon;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/codemod-cli",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.1",
|
|
4
4
|
"description": "A cli for distributing codemods for atlassian-frontend components and services",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"bin": "./bin/codemod-cli.js",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@atlaskit/codemod-utils": "^4.2.0",
|
|
43
|
-
"@atlaskit/tokens": "^1.
|
|
43
|
+
"@atlaskit/tokens": "^1.54.0",
|
|
44
44
|
"@babel/runtime": "^7.0.0",
|
|
45
45
|
"@codeshift/utils": "^0.2.4",
|
|
46
46
|
"@hypermod/utils": "^0.4.2",
|