@atlaskit/codemod-cli 0.24.1 → 0.24.3

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +335 -320
  2. package/dist/cjs/main.js +1 -1
  3. package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +126 -50
  4. package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-split-imports.js +25 -9
  5. package/dist/cjs/presets/migrate-to-new-buttons/utils/constants.js +16 -3
  6. package/dist/cjs/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +14 -5
  7. package/dist/cjs/presets/migrate-to-new-buttons/utils/get-default-imports.js +14 -0
  8. package/dist/cjs/presets/migrate-to-new-buttons/utils/get-specifier-names.js +14 -0
  9. package/dist/cjs/presets/migrate-to-new-buttons/utils/if-variant-already-imported.js +15 -7
  10. package/dist/cjs/presets/migrate-to-new-buttons/utils/rename-elements.js +18 -0
  11. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +119 -46
  12. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-split-imports.js +25 -9
  13. package/dist/es2019/presets/migrate-to-new-buttons/utils/constants.js +16 -2
  14. package/dist/es2019/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +12 -5
  15. package/dist/es2019/presets/migrate-to-new-buttons/utils/get-default-imports.js +6 -0
  16. package/dist/es2019/presets/migrate-to-new-buttons/utils/get-specifier-names.js +6 -0
  17. package/dist/es2019/presets/migrate-to-new-buttons/utils/if-variant-already-imported.js +9 -3
  18. package/dist/es2019/presets/migrate-to-new-buttons/utils/rename-elements.js +12 -0
  19. package/dist/esm/main.js +1 -1
  20. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +126 -51
  21. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-split-imports.js +25 -9
  22. package/dist/esm/presets/migrate-to-new-buttons/utils/constants.js +15 -2
  23. package/dist/esm/presets/migrate-to-new-buttons/utils/generate-new-button-element.js +14 -5
  24. package/dist/esm/presets/migrate-to-new-buttons/utils/get-default-imports.js +8 -0
  25. package/dist/esm/presets/migrate-to-new-buttons/utils/get-specifier-names.js +8 -0
  26. package/dist/esm/presets/migrate-to-new-buttons/utils/if-variant-already-imported.js +14 -6
  27. package/dist/esm/presets/migrate-to-new-buttons/utils/rename-elements.js +12 -0
  28. package/dist/types/main.d.ts +1 -1
  29. package/dist/types/presets/migrate-to-new-buttons/utils/constants.d.ts +11 -1
  30. package/dist/types/presets/migrate-to-new-buttons/utils/get-default-imports.d.ts +5 -0
  31. package/dist/types/presets/migrate-to-new-buttons/utils/get-specifier-names.d.ts +5 -0
  32. package/dist/types/presets/migrate-to-new-buttons/utils/if-variant-already-imported.d.ts +2 -1
  33. package/dist/types/presets/migrate-to-new-buttons/utils/migrate-fit-container-icon-button.d.ts +1 -1
  34. package/dist/types/presets/migrate-to-new-buttons/utils/rename-elements.d.ts +2 -0
  35. package/dist/types/presets/styled-to-emotion/styled-to-emotion.d.ts +1 -1
  36. package/dist/types/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.d.ts +1 -1
  37. package/dist/types/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.d.ts +1 -1
  38. package/dist/types/sinceRef.d.ts +1 -1
  39. package/dist/types/transforms.d.ts +2 -2
  40. package/dist/types/types.d.ts +1 -1
  41. package/dist/types-ts4.5/main.d.ts +1 -1
  42. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/constants.d.ts +11 -1
  43. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/get-default-imports.d.ts +5 -0
  44. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/get-specifier-names.d.ts +5 -0
  45. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/if-variant-already-imported.d.ts +2 -1
  46. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/migrate-fit-container-icon-button.d.ts +1 -1
  47. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/rename-elements.d.ts +2 -0
  48. package/dist/types-ts4.5/presets/styled-to-emotion/styled-to-emotion.d.ts +1 -1
  49. package/dist/types-ts4.5/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.d.ts +1 -1
  50. package/dist/types-ts4.5/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.d.ts +1 -1
  51. package/dist/types-ts4.5/sinceRef.d.ts +1 -1
  52. package/dist/types-ts4.5/transforms.d.ts +2 -2
  53. package/dist/types-ts4.5/types.d.ts +1 -1
  54. package/package.json +3 -5
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.24.1", _PACKAGE_VERSION_ = _process$env$_PACKAGE === void 0 ? '0.0.0-dev' : _process$env$_PACKAGE;
308
+ _process$env$_PACKAGE = "0.24.3", _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) {
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.default = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
7
9
  var _codemodUtils = require("@atlaskit/codemod-utils");
8
10
  var _utils = require("@hypermod/utils");
9
11
  var _constants = require("../utils/constants");
12
+ var _getDefaultImports = _interopRequireDefault(require("../utils/get-default-imports"));
13
+ var _getSpecifierNames = _interopRequireDefault(require("../utils/get-specifier-names"));
10
14
  var _generateNewButtonElement = require("../utils/generate-new-button-element");
11
15
  var _hasUnsupportedProps = require("../utils/has-unsupported-props");
12
16
  var _ifVariantAlreadyImported = require("../utils/if-variant-already-imported");
@@ -18,30 +22,44 @@ var transformer = function transformer(file, api) {
18
22
  var j = api.jscodeshift;
19
23
  var fileSource = j(file.source);
20
24
  (0, _addCommentForCustomThemeButtons.addCommentForCustomThemeButtons)(fileSource, j);
21
- var buttonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
22
- return path.node.source.value === _constants.entryPointsMapping.Button || path.node.source.value === _constants.entryPointsMapping.LoadingButton || path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT;
25
+
26
+ // Find old buttons
27
+ var oldButtonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
28
+ return path.node.source.value === _constants.entryPointsMapping.Button || path.node.source.value === _constants.entryPointsMapping.LoadingButton || path.node.source.value === _constants.OLD_BUTTON_ENTRY_POINT;
23
29
  });
24
- if (!buttonImports.length) {
30
+ if (!oldButtonImports.length) {
25
31
  return fileSource.toSource();
26
32
  }
27
- var defaultButtonImport = buttonImports.find(j.Specifier).filter(function (path) {
28
- return path.node.type === 'ImportDefaultSpecifier';
33
+ var oldDefaultDefaultImports = (0, _getDefaultImports.default)(oldButtonImports, j);
34
+ var oldDefaultImportSpecifiers = (0, _getSpecifierNames.default)(oldDefaultDefaultImports);
35
+
36
+ // Find new buttons
37
+ var newButtonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
38
+ return path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT;
29
39
  });
30
- if (defaultButtonImport.length === 0) {
31
- return fileSource.toSource();
32
- }
33
- var specifierIdentifier = defaultButtonImport.get(0).node.local.name;
34
- var hasLinkIconButton = (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.linkIcon, fileSource, j);
35
- var hasLinkButton = (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.link, fileSource, j);
36
- var hasIconButton = (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.icon, fileSource, j);
37
- var allButtons = fileSource.find(j.JSXElement).filter(function (path) {
38
- return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === specifierIdentifier;
40
+ var newDefaultDefaultImports = (0, _getDefaultImports.default)(newButtonImports, j);
41
+ var newDefaultImportSpecifiers = (0, _getSpecifierNames.default)(newDefaultDefaultImports);
42
+ var loadingButtonDirectImportName = (0, _utils.getDefaultImportSpecifierName)(j, fileSource, _constants.entryPointsMapping.LoadingButton);
43
+
44
+ /**
45
+ * Which variants should be in this file?
46
+ *
47
+ * Any variants enabled here will be added to the final import statement.
48
+ * Initial values check if the variant is already imported in the file.
49
+ */
50
+ var hasVariant = {
51
+ defaultButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.default, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j, true),
52
+ linkButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.link, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j),
53
+ iconButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.icon, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j),
54
+ linkIconButton: (0, _ifVariantAlreadyImported.checkIfVariantAlreadyImported)(_constants.NEW_BUTTON_VARIANTS.linkIcon, _constants.NEW_BUTTON_ENTRY_POINT, fileSource, j)
55
+ };
56
+ var oldButtonElements = fileSource.find(j.JSXElement).filter(function (path) {
57
+ 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));
39
58
  });
40
- var buttonsWithoutUnsupportedProps = allButtons.filter(function (path) {
59
+ var oldButtonsWithoutUnsupportedProps = oldButtonElements.filter(function (path) {
41
60
  return !(0, _hasUnsupportedProps.ifHasUnsupportedProps)(path.value.openingElement.attributes);
42
61
  });
43
- var loadingButtonImportName = (0, _utils.getDefaultImportSpecifierName)(j, fileSource, _constants.entryPointsMapping.LoadingButton);
44
- buttonsWithoutUnsupportedProps.forEach(function (element) {
62
+ oldButtonsWithoutUnsupportedProps.forEach(function (element) {
45
63
  var _element$value$childr;
46
64
  var attributes = element.value.openingElement.attributes;
47
65
  if (!attributes) {
@@ -57,9 +75,14 @@ var transformer = function transformer(file, api) {
57
75
  var isLinkIconButton = hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
58
76
  var isLinkButton = hasHref && !isLinkIconButton;
59
77
  var isIconButton = !hasHref && hasIcon && hasNoChildren && !isFitContainerIconButton;
60
- var isDefaultButtonWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon;
61
- var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonImportName;
62
- if (isDefaultButtonWithAnIcon) {
78
+ var isDefaultButton = !isLinkButton && !isLinkIconButton && !isIconButton && !isFitContainerIconButton;
79
+ var isDefaultVariantWithAnIcon = !isLinkIconButton && !isIconButton && !isFitContainerIconButton && hasIcon;
80
+ var isLoadingButton = element.value.openingElement.name.type === 'JSXIdentifier' && element.value.openingElement.name.name === loadingButtonDirectImportName;
81
+ var linkAppearanceAttribute = attributes.find(function (node) {
82
+ var _node$value, _node$name, _node$value2, _node$value3;
83
+ 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');
84
+ });
85
+ if (isDefaultVariantWithAnIcon) {
63
86
  (0, _generateNewButtonElement.handleIconAttributes)(element.value, j);
64
87
  }
65
88
  if (isFitContainerIconButton) {
@@ -68,73 +91,86 @@ var transformer = function transformer(file, api) {
68
91
  isIconButton = true;
69
92
  }
70
93
  }
71
- if (isLinkIconButton) {
72
- hasLinkIconButton = true;
94
+ if (isLinkIconButton && !isLoadingButton) {
95
+ hasVariant.linkIconButton = true;
73
96
  j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS.linkIcon, element.value, j));
74
97
  }
75
- if (isIconButton) {
76
- hasIconButton = true;
98
+ if (isIconButton && !isLoadingButton) {
99
+ hasVariant.iconButton = true;
77
100
  j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS.icon, element.value, j));
78
101
  }
79
- if (isLinkButton) {
80
- hasLinkButton = true;
102
+ if (isLinkButton && !isLoadingButton) {
103
+ hasVariant.linkButton = true;
81
104
  j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS.link, element.value, j));
82
105
  }
83
- if (isLoadingButton) {
84
- j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS.default, element.value, j));
106
+ if (isDefaultButton && !isLoadingButton) {
107
+ hasVariant.defaultButton = true;
108
+ j(element).replaceWith((0, _generateNewButtonElement.generateNewElement)(newDefaultImportSpecifiers !== null && newDefaultImportSpecifiers !== void 0 && newDefaultImportSpecifiers.length ?
109
+ // If new button already has a default import, use that incase it's aliased
110
+ newDefaultImportSpecifiers[0] : _constants.NEW_BUTTON_VARIANTS.default, element.value, j));
85
111
  }
86
- var linkAppearanceAttribute = attributes.find(function (node) {
87
- var _node$value, _node$name, _node$value2, _node$value3;
88
- 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');
89
- });
90
- if (!hasHref && linkAppearanceAttribute) {
112
+ if (isLoadingButton) {
113
+ var newElement = (0, _generateNewButtonElement.generateNewElement)(_constants.NEW_BUTTON_VARIANTS[isIconButton || isLinkIconButton ? 'icon' : 'default'], element.value, j);
114
+ if (isIconButton || isLinkIconButton) {
115
+ hasVariant.iconButton = true;
116
+ } else {
117
+ hasVariant.defaultButton = true;
118
+ }
119
+ j(element).replaceWith(newElement);
120
+ if (hasHref || linkAppearanceAttribute) {
121
+ (0, _codemodUtils.addCommentBefore)(j, j(newElement), (0, _constants.loadingButtonComment)({
122
+ hasHref: hasHref,
123
+ hasLinkAppearance: Boolean(linkAppearanceAttribute)
124
+ }), 'block');
125
+ }
126
+ } else if (!hasHref && linkAppearanceAttribute) {
91
127
  (0, _codemodUtils.addCommentBefore)(j, j(linkAppearanceAttribute), _constants.linkButtonMissingHrefComment, 'line');
92
128
  }
93
129
  });
94
130
 
95
131
  // modify import declarations
96
132
  var specifiers = [];
97
- [hasLinkButton ? 'link' : null, hasIconButton ? 'icon' : null, hasLinkIconButton ? 'linkIcon' : null].forEach(function (variant) {
133
+ [hasVariant.linkButton ? 'link' : null, hasVariant.iconButton ? 'icon' : null, hasVariant.linkIconButton ? 'linkIcon' : null].forEach(function (variant) {
98
134
  if (variant) {
99
135
  specifiers.push(j.importSpecifier(j.identifier(_constants.NEW_BUTTON_VARIANTS[variant])));
100
136
  }
101
137
  });
102
- var oldButtonImport = fileSource.find(j.ImportDeclaration).filter(function (path) {
103
- return path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT || path.node.source.value === _constants.entryPointsMapping.Button || path.node.source.value === _constants.entryPointsMapping.LoadingButton;
104
- });
105
138
  var remainingDefaultButtons = fileSource.find(j.JSXElement).filter(function (path) {
106
- return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === specifierIdentifier && !(0, _hasUnsupportedProps.ifHasUnsupportedProps)(path.value.openingElement.attributes);
139
+ return path.value.openingElement.name.type === 'JSXIdentifier' && Boolean(oldDefaultImportSpecifiers === null || oldDefaultImportSpecifiers === void 0 ? void 0 : oldDefaultImportSpecifiers.includes(path.value.openingElement.name.name)) && !(0, _hasUnsupportedProps.ifHasUnsupportedProps)(path.value.openingElement.attributes);
107
140
  }).length > 0 || fileSource.find(j.CallExpression).filter(function (path) {
108
141
  return path.node.arguments.map(function (argument) {
109
142
  return argument.type === 'Identifier' && (argument === null || argument === void 0 ? void 0 : argument.name);
110
- }).includes(specifierIdentifier);
143
+ }).some(function (name) {
144
+ return name && (oldDefaultImportSpecifiers === null || oldDefaultImportSpecifiers === void 0 ? void 0 : oldDefaultImportSpecifiers.includes(name));
145
+ });
111
146
  }).length > 0;
112
147
 
113
148
  // Loading buttons map back to default imports
114
- if (specifierIdentifier === loadingButtonImportName) {
149
+ if (loadingButtonDirectImportName && oldDefaultImportSpecifiers !== null && oldDefaultImportSpecifiers !== void 0 && oldDefaultImportSpecifiers.includes(loadingButtonDirectImportName)) {
115
150
  fileSource
116
151
  // rename LoadingButton to Button in all call expressions i.e. render(LoadingButton), find(LoadingButton)
117
152
  .find(j.CallExpression).find(j.Identifier).forEach(function (path) {
118
- if (path.node.name === loadingButtonImportName) {
153
+ if (path.node.name === loadingButtonDirectImportName) {
119
154
  path.node.name = _constants.NEW_BUTTON_VARIANTS.default;
120
155
  }
121
156
  });
122
- specifiers.push(j.importDefaultSpecifier(j.identifier(_constants.NEW_BUTTON_VARIANTS.default)));
123
157
  }
124
- if (!specifiers.find(function (specifier) {
158
+
159
+ // Only add the Button import if we found a default button, not icon only
160
+ if (hasVariant.defaultButton || !specifiers.find(function (specifier) {
125
161
  return specifier.type === 'ImportDefaultSpecifier';
126
162
  }) && remainingDefaultButtons) {
127
163
  specifiers.push(j.importDefaultSpecifier(j.identifier(_constants.NEW_BUTTON_VARIANTS.default)));
128
164
  }
129
165
 
130
166
  // update import path for types imports
131
- specifiers = (0, _importTypesFromNewEntryPoint.importTypesFromNewEntryPoint)(buttonImports, specifiers, j, fileSource);
132
- var buttonsWithUnsupportedProps = allButtons.filter(function (path) {
167
+ specifiers = (0, _importTypesFromNewEntryPoint.importTypesFromNewEntryPoint)(oldButtonImports, specifiers, j, fileSource);
168
+ var oldButtonsWithUnsupportedProps = oldButtonElements.filter(function (path) {
133
169
  return (0, _hasUnsupportedProps.ifHasUnsupportedProps)(path.value.openingElement.attributes);
134
170
  });
135
- if (buttonsWithUnsupportedProps.length) {
171
+ if (oldButtonsWithUnsupportedProps.length) {
136
172
  // add comment to all buttons with unsupported props: "component", "style", "css"
137
- buttonsWithUnsupportedProps.forEach(function (element) {
173
+ oldButtonsWithUnsupportedProps.forEach(function (element) {
138
174
  var _element$value$openin;
139
175
  var attribute = (_element$value$openin = element.value.openingElement.attributes) === null || _element$value$openin === void 0 ? void 0 : _element$value$openin.find(function (node) {
140
176
  return node.type === 'JSXAttribute' && typeof node.name.name === 'string' && _constants.unsupportedProps.includes(node.name.name);
@@ -148,16 +184,56 @@ var transformer = function transformer(file, api) {
148
184
  if (specifiers.find(function (specifier) {
149
185
  return specifier.type === 'ImportDefaultSpecifier';
150
186
  })) {
151
- (0, _renameDefaultButtonToLegacyButton.renameDefaultButtonToLegacyButtonImport)(oldButtonImport, buttonsWithUnsupportedProps, j);
187
+ (0, _renameDefaultButtonToLegacyButton.renameDefaultButtonToLegacyButtonImport)(oldButtonImports, oldButtonsWithUnsupportedProps, j);
152
188
  }
153
189
  }
154
190
  if (specifiers.length) {
191
+ var existingNewButtonImports = fileSource.find(j.ImportDeclaration).filter(function (path) {
192
+ return path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT;
193
+ });
155
194
  var newButtonImport = j.importDeclaration(specifiers, j.stringLiteral(_constants.NEW_BUTTON_ENTRY_POINT));
156
- oldButtonImport.forEach(function (path) {
195
+ oldButtonImports.forEach(function (path) {
157
196
  var _path$node;
158
197
  newButtonImport.comments = path !== null && path !== void 0 && (_path$node = path.node) !== null && _path$node !== void 0 && _path$node.comments ? path.node.comments : undefined;
159
198
  });
160
- oldButtonImport.replaceWith(newButtonImport);
199
+ if (existingNewButtonImports.length) {
200
+ existingNewButtonImports.forEach(function (path) {
201
+ // Merge specifiers from existing new button import with added specifiers
202
+ var mergedSpecifiers = [].concat((0, _toConsumableArray2.default)(specifiers.filter(function (specifier) {
203
+ var _path$node$specifiers;
204
+ if (specifier.type !== 'ImportDefaultSpecifier') {
205
+ return true;
206
+ }
207
+
208
+ // Ensure we don't add a duplicate default specifier if one is already imported
209
+ return !((_path$node$specifiers = path.node.specifiers) !== null && _path$node$specifiers !== void 0 && _path$node$specifiers.find(function (s) {
210
+ return s.type === 'ImportDefaultSpecifier';
211
+ }));
212
+ })), (0, _toConsumableArray2.default)(path.node.specifiers ? path.node.specifiers : [])) // Filter duplicates
213
+ .filter(function (specifier) {
214
+ if (specifier.type === 'ImportDefaultSpecifier') {
215
+ return true;
216
+ }
217
+ var isAlreadyImported = specifiers.find(function (s) {
218
+ var _specifier$local;
219
+ return s.type === 'ImportSpecifier' && s.imported.name === ((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name);
220
+ });
221
+ return !isAlreadyImported;
222
+ });
223
+ newButtonImport = j.importDeclaration(mergedSpecifiers, j.stringLiteral(_constants.NEW_BUTTON_ENTRY_POINT));
224
+ });
225
+ oldButtonImports.remove();
226
+ existingNewButtonImports.replaceWith(newButtonImport);
227
+ } else {
228
+ oldButtonImports.replaceWith(function (_, index) {
229
+ // Only replace the first import
230
+ if (index === 0) {
231
+ return newButtonImport;
232
+ }
233
+ // Remove the rest
234
+ return null;
235
+ });
236
+ }
161
237
  }
162
238
 
163
239
  // remove empty import declarations
@@ -7,6 +7,8 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.default = void 0;
8
8
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
9
  var _constants = require("../utils/constants");
10
+ var _renameElements = _interopRequireDefault(require("../utils/rename-elements"));
11
+ var _utils = require("@hypermod/utils");
10
12
  var transformer = function transformer(file, api) {
11
13
  var j = api.jscodeshift;
12
14
  var fileSource = j(file.source);
@@ -23,6 +25,8 @@ var transformer = function transformer(file, api) {
23
25
  var _node$node = node.node,
24
26
  specifiers = _node$node.specifiers,
25
27
  source = _node$node.source;
28
+
29
+ // Return early if the import is not a named import
26
30
  if ([].concat((0, _toConsumableArray2.default)(Object.values(_constants.entryPointsMapping)), [_constants.NEW_BUTTON_ENTRY_POINT, '@atlaskit/button/types']).includes(source.value)) {
27
31
  return fileSource.toSource(_constants.PRINT_SETTINGS);
28
32
  }
@@ -30,16 +34,22 @@ var transformer = function transformer(file, api) {
30
34
  return specifier.type === 'ImportDefaultSpecifier';
31
35
  });
32
36
  if (defaultSpecifier && defaultSpecifier.local) {
33
- var defaultButtonImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier(defaultSpecifier.local.name))], j.stringLiteral(_constants.entryPointsMapping.Button));
34
- j(node).insertAfter(defaultButtonImport);
37
+ var existingSpecifier = (0, _utils.getDefaultImportSpecifierName)(j, fileSource, _constants.entryPointsMapping.Button);
38
+ if (existingSpecifier) {
39
+ var _defaultSpecifier$loc;
40
+ (0, _renameElements.default)((_defaultSpecifier$loc = defaultSpecifier.local) === null || _defaultSpecifier$loc === void 0 ? void 0 : _defaultSpecifier$loc.name, existingSpecifier, fileSource, j);
41
+ } else {
42
+ var newImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier(defaultSpecifier.local.name))], j.stringLiteral(_constants.entryPointsMapping.Button));
43
+ j(node).insertAfter(newImport);
44
+ }
35
45
  }
36
- var valueSpecifiers = specifiers === null || specifiers === void 0 ? void 0 : specifiers.filter(function (specifier) {
46
+ var namedSpecifiers = specifiers === null || specifiers === void 0 ? void 0 : specifiers.filter(function (specifier) {
37
47
  return specifier.type === 'ImportSpecifier';
38
48
  });
39
- var newTypeSpecifier = valueSpecifiers === null || valueSpecifiers === void 0 ? void 0 : valueSpecifiers.filter(function (specifier) {
49
+ var newTypeSpecifier = namedSpecifiers === null || namedSpecifiers === void 0 ? void 0 : namedSpecifiers.filter(function (specifier) {
40
50
  return specifier.type === 'ImportSpecifier' && (specifier.imported.name === 'Appearance' || specifier.imported.name === 'Spacing');
41
51
  });
42
- var otherTypeSpecifiers = valueSpecifiers === null || valueSpecifiers === void 0 ? void 0 : valueSpecifiers.filter(function (specifier) {
52
+ var otherTypeSpecifiers = namedSpecifiers === null || namedSpecifiers === void 0 ? void 0 : namedSpecifiers.filter(function (specifier) {
43
53
  return _constants.BUTTON_TYPES.includes(specifier.imported.name);
44
54
  });
45
55
  if (newTypeSpecifier !== null && newTypeSpecifier !== void 0 && newTypeSpecifier.length) {
@@ -50,11 +60,17 @@ var transformer = function transformer(file, api) {
50
60
  var _typeImport = j.importDeclaration(otherTypeSpecifiers, j.stringLiteral('@atlaskit/button'));
51
61
  j(node).insertAfter(_typeImport);
52
62
  }
53
- if (valueSpecifiers !== null && valueSpecifiers !== void 0 && valueSpecifiers.length) {
54
- valueSpecifiers.forEach(function (specifier) {
63
+ if (namedSpecifiers !== null && namedSpecifiers !== void 0 && namedSpecifiers.length) {
64
+ namedSpecifiers.forEach(function (specifier) {
55
65
  if (specifier.local && specifier.type === 'ImportSpecifier' && specifier.local.name && _constants.entryPointsMapping[specifier.imported.name]) {
56
- var newImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier(specifier.local.name))], j.stringLiteral(_constants.entryPointsMapping[specifier.imported.name]));
57
- j(node).insertAfter(newImport);
66
+ var _existingSpecifier = (0, _utils.getDefaultImportSpecifierName)(j, fileSource, _constants.entryPointsMapping[specifier.imported.name]);
67
+ if (_existingSpecifier) {
68
+ var _specifier$local;
69
+ (0, _renameElements.default)((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name, _existingSpecifier, fileSource, j);
70
+ } else {
71
+ var _newImport = j.importDeclaration([j.importDefaultSpecifier(j.identifier(specifier.local.name))], j.stringLiteral(_constants.entryPointsMapping[specifier.imported.name]));
72
+ j(node).insertAfter(_newImport);
73
+ }
58
74
  }
59
75
  });
60
76
  }
@@ -3,17 +3,25 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.unsupportedProps = exports.migrateFitContainerButtonToIconButtonComment = exports.migrateFitContainerButtonToDefaultButtonComment = exports.linkButtonMissingHrefComment = exports.entryPointsMapping = exports.customThemeButtonComment = exports.buttonPropsNoLongerSupportedComment = exports.UNSAFE_SIZE_PROPS_MAP = exports.PRINT_SETTINGS = exports.NEW_BUTTON_VARIANTS = exports.NEW_BUTTON_ENTRY_POINT = exports.BUTTON_TYPES = void 0;
6
+ exports.unsupportedProps = exports.migrateFitContainerButtonToIconButtonComment = exports.migrateFitContainerButtonToDefaultButtonComment = exports.loadingButtonComment = exports.linkButtonMissingHrefComment = exports.entryPointsMapping = exports.customThemeButtonComment = exports.buttonPropsNoLongerSupportedComment = exports.UNSAFE_SIZE_PROPS_MAP = exports.PRINT_SETTINGS = exports.OLD_BUTTON_VARIANTS = exports.OLD_BUTTON_ENTRY_POINT = exports.NEW_BUTTON_VARIANTS = exports.NEW_BUTTON_ENTRY_POINT = exports.BUTTON_TYPES = void 0;
7
7
  var PRINT_SETTINGS = exports.PRINT_SETTINGS = {
8
8
  quote: 'single'
9
9
  };
10
+
11
+ /** NEW button **/
12
+ var NEW_BUTTON_ENTRY_POINT = exports.NEW_BUTTON_ENTRY_POINT = '@atlaskit/button/new';
10
13
  var NEW_BUTTON_VARIANTS = exports.NEW_BUTTON_VARIANTS = {
11
14
  default: 'Button',
12
15
  link: 'LinkButton',
13
16
  icon: 'IconButton',
14
17
  linkIcon: 'LinkIconButton'
15
18
  };
16
- var NEW_BUTTON_ENTRY_POINT = exports.NEW_BUTTON_ENTRY_POINT = '@atlaskit/button/new';
19
+
20
+ /** OLD button **/
21
+ var OLD_BUTTON_ENTRY_POINT = exports.OLD_BUTTON_ENTRY_POINT = '@atlaskit/button';
22
+ var OLD_BUTTON_VARIANTS = exports.OLD_BUTTON_VARIANTS = {
23
+ loading: 'LoadingButton'
24
+ };
17
25
  var entryPointsMapping = exports.entryPointsMapping = {
18
26
  Button: '@atlaskit/button/standard-button',
19
27
  LoadingButton: '@atlaskit/button/loading-button',
@@ -31,4 +39,9 @@ var linkButtonMissingHrefComment = exports.linkButtonMissingHrefComment = "\"lin
31
39
  var buttonPropsNoLongerSupportedComment = exports.buttonPropsNoLongerSupportedComment = "Buttons with \"component\", \"css\" or \"style\" prop can't be automatically migrated with codemods. Please migrate it manually.";
32
40
  var migrateFitContainerButtonToDefaultButtonComment = exports.migrateFitContainerButtonToDefaultButtonComment = "Migrated to a default button with text which is from the icon label.";
33
41
  var migrateFitContainerButtonToIconButtonComment = exports.migrateFitContainerButtonToIconButtonComment = "\"shouldFitContainer\" is not available in icon buttons, please consider using a default button with text.";
34
- var customThemeButtonComment = exports.customThemeButtonComment = "CustomThemeButton will be deprecated. Please consider migrating to Pressable or Anchor Primitives with custom styles.";
42
+ var customThemeButtonComment = exports.customThemeButtonComment = "CustomThemeButton will be deprecated. Please consider migrating to Pressable or Anchor Primitives with custom styles.";
43
+ var loadingButtonComment = exports.loadingButtonComment = function loadingButtonComment(_ref) {
44
+ var hasLinkAppearance = _ref.hasLinkAppearance,
45
+ hasHref = _ref.hasHref;
46
+ return "This should be migrated to a new Button with a `isLoading` prop. ".concat(hasLinkAppearance ? "\"link\" and \"subtle-link\" appearances are not available for new loading buttons.\"" : '').concat(hasHref ? "The `href` attribute it not compatible with new loading buttons, because links should not need loading states." : '', " Please reconsider the design or change the appearance of the button.");
47
+ };
@@ -57,7 +57,10 @@ var handleIconAttributes = exports.handleIconAttributes = function handleIconAtt
57
57
  var buttonAlreadyHasLabelProp = buttonAttributes === null || buttonAttributes === void 0 ? void 0 : buttonAttributes.find(function (buttonAttribute) {
58
58
  return buttonAttribute.type === 'JSXAttribute' && buttonAttribute.name.name === 'label';
59
59
  });
60
- if (!buttonAlreadyHasLabelProp) {
60
+ var buttonAlreadyHasAriaLabelProp = buttonAttributes === null || buttonAttributes === void 0 ? void 0 : buttonAttributes.find(function (buttonAttribute) {
61
+ return buttonAttribute.type === 'JSXAttribute' && buttonAttribute.name.name === 'aria-label';
62
+ });
63
+ if (!buttonAlreadyHasLabelProp && !buttonAlreadyHasAriaLabelProp) {
61
64
  var labelAttribute = iconAttributes.find(function (attribute) {
62
65
  return attribute.type === 'JSXAttribute' && attribute.name.name === 'label';
63
66
  });
@@ -123,12 +126,18 @@ var generateNewElement = exports.generateNewElement = function generateNewElemen
123
126
  return attribute.type === 'JSXAttribute' && attribute.name.name === 'label';
124
127
  }));
125
128
  if (hasNoLabelProp && attributes) {
126
- attributes.push(j.jsxAttribute(j.jsxIdentifier('label'), ariaLabelAttr.get().node.value));
129
+ attributes.unshift(j.jsxAttribute.from({
130
+ name: j.jsxIdentifier('label'),
131
+ value: j.literal(ariaLabelAttr.get().value.value.value)
132
+ }));
127
133
  }
128
134
  ariaLabelAttr.remove();
129
135
  }
130
136
  }
131
- return j.jsxElement(
132
- // self closing if it's an icon button or icon link button
133
- j.jsxOpeningElement(j.jsxIdentifier(variant), attributes, isIconOrLinkIcon), isIconOrLinkIcon ? null : j.jsxClosingElement(j.jsxIdentifier(variant)), element.children);
137
+ return j.jsxElement.from({
138
+ openingElement: j.jsxOpeningElement(j.jsxIdentifier(variant), attributes, isIconOrLinkIcon),
139
+ // self closing if it's an icon button or icon link button
140
+ closingElement: isIconOrLinkIcon ? null : j.jsxClosingElement(j.jsxIdentifier(variant)),
141
+ children: element.children
142
+ });
134
143
  };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getDefaultImports;
7
+ /**
8
+ * Returns default import specifiers from the given import declarations.
9
+ */
10
+ function getDefaultImports(importDeclarations, j) {
11
+ return importDeclarations.find(j.Specifier).filter(function (path) {
12
+ return path.node.type === 'ImportDefaultSpecifier';
13
+ });
14
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getSpecifierNames;
7
+ /**
8
+ * Returns a list of specifier names from the given specifiers.
9
+ */
10
+ function getSpecifierNames(specifiers) {
11
+ return specifiers.length ? specifiers.paths().map(function (path) {
12
+ return path.get().value.local.name;
13
+ }) : undefined;
14
+ }
@@ -3,12 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.checkIfVariantAlreadyImported = void 0;
7
- var _constants = require("./constants");
8
- var checkIfVariantAlreadyImported = exports.checkIfVariantAlreadyImported = function checkIfVariantAlreadyImported(variant, fileSource, j) {
9
- return fileSource.find(j.ImportDeclaration).filter(function (path) {
10
- return path.node.source.value === _constants.NEW_BUTTON_ENTRY_POINT;
11
- }).find(j.ImportSpecifier).filter(function (path) {
6
+ exports.findVariantAlreadyImported = exports.checkIfVariantAlreadyImported = void 0;
7
+ var findVariantAlreadyImported = exports.findVariantAlreadyImported = function findVariantAlreadyImported(variant, entryPoint, fileSource, j) {
8
+ var isDefaultSpecifier = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
9
+ var imports = fileSource.find(j.ImportDeclaration).filter(function (path) {
10
+ return path.node.source.value === entryPoint;
11
+ });
12
+ if (isDefaultSpecifier) {
13
+ return imports.find(j.ImportDefaultSpecifier);
14
+ }
15
+ return imports.find(j.ImportSpecifier).filter(function (path) {
12
16
  return path.node.imported.name === variant;
13
- }).length > 0;
17
+ });
18
+ };
19
+ var checkIfVariantAlreadyImported = exports.checkIfVariantAlreadyImported = function checkIfVariantAlreadyImported(variant, entryPoint, fileSource, j) {
20
+ var isDefaultSpecifier = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
21
+ return findVariantAlreadyImported(variant, entryPoint, fileSource, j, isDefaultSpecifier).length > 0;
14
22
  };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = renameElements;
7
+ function renameElements(elementName, newElementName, fileSource, j) {
8
+ var oldElements = fileSource.find(j.JSXElement).filter(function (path) {
9
+ return path.value.openingElement.name.type === 'JSXIdentifier' && path.value.openingElement.name.name === elementName;
10
+ });
11
+
12
+ // Rename elements to match existing import name
13
+ oldElements.forEach(function (element) {
14
+ var _element$value$childr, _element$value$childr2;
15
+ var newElement = j.jsxElement(j.jsxOpeningElement(j.jsxIdentifier(newElementName), element.value.openingElement.attributes, ((_element$value$childr = element.value.children) === null || _element$value$childr === void 0 ? void 0 : _element$value$childr.length) === 0), ((_element$value$childr2 = element.value.children) === null || _element$value$childr2 === void 0 ? void 0 : _element$value$childr2.length) === 0 ? null : j.jsxClosingElement(j.jsxIdentifier(newElementName)), element.value.children);
16
+ j(element).replaceWith(newElement);
17
+ });
18
+ }