@atlaskit/codemod-utils 4.1.2 → 4.2.0

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.
@@ -4,325 +4,231 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.replaceImportStatementFor = exports.renameNamedImportWithAliasName = exports.flattenCertainChildPropsAsProp = exports.elevateComponentToNewEntryPoint = exports.createTransformer = exports.createRenameJSXFunc = exports.createRenameImportFor = exports.createRenameFuncFor = exports.createRemoveFuncFor = exports.createRemoveFuncAddCommentFor = exports.createConvertFuncFor = exports.changeImportEntryPoint = void 0;
7
-
8
7
  var _support = require("./support");
9
-
10
- var createRemoveFuncFor = function createRemoveFuncFor(component, importName, prop) {
11
- var predicate = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
12
- return true;
13
- };
14
- var comment = arguments.length > 4 ? arguments[4] : undefined;
15
- return function (j, source) {
16
- var specifier = (0, _support.getNamedSpecifier)(j, source, component, importName);
17
-
18
- if (!specifier) {
19
- return;
8
+ const createRemoveFuncFor = (component, importName, prop, predicate = () => true, comment) => (j, source) => {
9
+ const specifier = (0, _support.getNamedSpecifier)(j, source, component, importName);
10
+ if (!specifier) {
11
+ return;
12
+ }
13
+ source.findJSXElements(specifier).forEach(element => {
14
+ if (predicate(j, element) && comment) {
15
+ (0, _support.addCommentToStartOfFile)({
16
+ j,
17
+ base: source,
18
+ message: comment
19
+ });
20
+ } else {
21
+ (0, _support.getJSXAttributesByName)(j, element, prop).forEach(attribute => {
22
+ j(attribute).remove();
23
+ });
20
24
  }
21
-
22
- source.findJSXElements(specifier).forEach(function (element) {
23
- if (predicate(j, element) && comment) {
24
- (0, _support.addCommentToStartOfFile)({
25
- j: j,
26
- base: source,
27
- message: comment
28
- });
29
- } else {
30
- (0, _support.getJSXAttributesByName)(j, element, prop).forEach(function (attribute) {
31
- j(attribute).remove();
32
- });
33
- }
34
- });
35
- };
25
+ });
36
26
  };
37
-
38
27
  exports.createRemoveFuncFor = createRemoveFuncFor;
39
-
40
- var createRenameFuncFor = function createRenameFuncFor(component, from, to) {
41
- return function (j, source) {
42
- var defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
43
-
44
- if (!defaultSpecifier) {
45
- return;
46
- }
47
-
48
- source.findJSXElements(defaultSpecifier).forEach(function (element) {
49
- (0, _support.getJSXAttributesByName)(j, element, from).forEach(function (attribute) {
50
- j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(to), attribute.node.value));
51
- });
28
+ const createRenameFuncFor = (component, from, to) => (j, source) => {
29
+ const defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
30
+ if (!defaultSpecifier) {
31
+ return;
32
+ }
33
+ source.findJSXElements(defaultSpecifier).forEach(element => {
34
+ (0, _support.getJSXAttributesByName)(j, element, from).forEach(attribute => {
35
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(to), attribute.node.value));
52
36
  });
53
- };
37
+ });
54
38
  };
55
-
56
39
  exports.createRenameFuncFor = createRenameFuncFor;
57
-
58
- var createConvertFuncFor = function createConvertFuncFor(component, from, to, predicate) {
59
- return function (j, source) {
60
- var defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
61
-
62
- if (!defaultSpecifier) {
63
- return;
64
- }
65
-
66
- source.findJSXElements(defaultSpecifier).forEach(function (element) {
67
- (0, _support.getJSXAttributesByName)(j, element, from).forEach(function (attribute) {
68
- var shouldConvert = predicate && predicate(attribute.node.value) || false;
69
- var node = j.jsxAttribute(j.jsxIdentifier(to));
70
-
71
- if (shouldConvert) {
72
- j(attribute).insertBefore(node);
73
- }
74
- });
40
+ const createConvertFuncFor = (component, from, to, predicate) => (j, source) => {
41
+ const defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
42
+ if (!defaultSpecifier) {
43
+ return;
44
+ }
45
+ source.findJSXElements(defaultSpecifier).forEach(element => {
46
+ (0, _support.getJSXAttributesByName)(j, element, from).forEach(attribute => {
47
+ const shouldConvert = predicate && predicate(attribute.node.value) || false;
48
+ const node = j.jsxAttribute(j.jsxIdentifier(to));
49
+ if (shouldConvert) {
50
+ j(attribute).insertBefore(node);
51
+ }
75
52
  });
76
- };
53
+ });
77
54
  };
78
-
79
55
  exports.createConvertFuncFor = createConvertFuncFor;
80
-
81
- var replaceImportStatementFor = function replaceImportStatementFor(pkg, convertMap) {
82
- return function (j, root) {
83
- root.find(j.ImportDeclaration).filter(function (path) {
84
- return path.node.source.value === pkg;
85
- }).forEach(function (path) {
86
- var defaultSpecifier = (path.value.specifiers || []).filter(function (specifier) {
87
- return specifier.type === 'ImportDefaultSpecifier';
88
- });
89
- var defaultDeclarations = defaultSpecifier.map(function (s) {
90
- return j.importDeclaration([s], j.literal(convertMap['default']));
91
- });
92
- var otherSpecifier = (path.value.specifiers || []).filter(function (specifier) {
93
- return specifier.type === 'ImportSpecifier';
94
- });
95
- j(path).replaceWith(defaultDeclarations);
96
- var otherDeclarations = otherSpecifier.map(function (s) {
97
- var localName = s.local.name;
98
-
99
- if (convertMap[localName]) {
100
- return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap[localName]));
101
- } else {
102
- return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap['*']));
103
- }
104
- });
105
- j(path).insertAfter(otherDeclarations);
56
+ const replaceImportStatementFor = (pkg, convertMap) => (j, root) => {
57
+ root.find(j.ImportDeclaration).filter(path => path.node.source.value === pkg).forEach(path => {
58
+ const defaultSpecifier = (path.value.specifiers || []).filter(specifier => specifier.type === 'ImportDefaultSpecifier');
59
+ const defaultDeclarations = defaultSpecifier.map(s => {
60
+ return j.importDeclaration([s], j.literal(convertMap['default']));
61
+ });
62
+ const otherSpecifier = (path.value.specifiers || []).filter(specifier => specifier.type === 'ImportSpecifier');
63
+ j(path).replaceWith(defaultDeclarations);
64
+ const otherDeclarations = otherSpecifier.map(s => {
65
+ const localName = s.local.name;
66
+ if (convertMap[localName]) {
67
+ return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap[localName]));
68
+ } else {
69
+ return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap['*']));
70
+ }
106
71
  });
107
- };
72
+ j(path).insertAfter(otherDeclarations);
73
+ });
108
74
  };
109
-
110
75
  exports.replaceImportStatementFor = replaceImportStatementFor;
111
-
112
- var createRenameImportFor = function createRenameImportFor(component, from, to) {
113
- return function (j, source) {
114
- source.find(j.ImportDeclaration).filter(function (path) {
115
- return path.node.source.value === component;
116
- }).forEach(function (path) {
117
- j(path).replaceWith(j.importDeclaration(path.value.specifiers, j.literal(to)));
118
- });
119
- };
76
+ const createRenameImportFor = (component, from, to) => (j, source) => {
77
+ source.find(j.ImportDeclaration).filter(path => path.node.source.value === component).forEach(path => {
78
+ j(path).replaceWith(j.importDeclaration(path.value.specifiers, j.literal(to)));
79
+ });
120
80
  };
121
-
122
81
  exports.createRenameImportFor = createRenameImportFor;
123
-
124
- var createTransformer = function createTransformer(migrates, shouldApplyTransform) {
125
- return function (fileInfo, _ref, options) {
126
- var j = _ref.jscodeshift;
127
- var source = j(fileInfo.source); // If shouldApplyTransform not provided then perform old behaviour
128
-
129
- if (!shouldApplyTransform || shouldApplyTransform(j, source)) {
130
- migrates.forEach(function (tf) {
131
- return tf(j, source);
132
- });
133
- return source.toSource(options.printOptions);
134
- }
135
-
136
- return fileInfo.source;
137
- };
82
+ const createTransformer = (migrates, shouldApplyTransform) => (fileInfo, {
83
+ jscodeshift: j
84
+ }, options) => {
85
+ const source = j(fileInfo.source);
86
+
87
+ // If shouldApplyTransform not provided then perform old behaviour
88
+ if (!shouldApplyTransform || shouldApplyTransform(j, source)) {
89
+ migrates.forEach(tf => tf(j, source));
90
+ return source.toSource(options.printOptions);
91
+ }
92
+ return fileInfo.source;
138
93
  };
139
-
140
94
  exports.createTransformer = createTransformer;
141
-
142
- var elevateComponentToNewEntryPoint = function elevateComponentToNewEntryPoint(pkg, toPkg, innerElementName) {
143
- return function (j, root) {
144
- var importDeclarations = root.find(j.ImportDeclaration).filter(function (path) {
145
- return path.node.source.value === pkg;
146
- });
147
- var defaultSpecifier = importDeclarations.find(j.ImportDefaultSpecifier).nodes();
148
- var otherSpecifier = importDeclarations.find(j.ImportSpecifier).nodes();
149
- var newDefaultSpecifier = defaultSpecifier.map(function (s) {
150
- return j.importDeclaration([j.importDefaultSpecifier(s.local)], j.literal(pkg));
151
- });
152
- var newOtherSpecifiers = otherSpecifier.map(function (s) {
153
- if (s.imported.name === innerElementName) {
154
- return j.importDeclaration([j.importDefaultSpecifier(s.local)], j.literal(toPkg));
155
- } else {
156
- return j.importDeclaration([s], j.literal(pkg));
157
- }
158
- });
159
- importDeclarations.forEach(function (path) {
160
- j(path).replaceWith(newDefaultSpecifier);
161
- j(path).insertBefore(newOtherSpecifiers);
162
- });
163
- };
95
+ const elevateComponentToNewEntryPoint = (pkg, toPkg, innerElementName) => (j, root) => {
96
+ const importDeclarations = root.find(j.ImportDeclaration).filter(path => path.node.source.value === pkg);
97
+ const defaultSpecifier = importDeclarations.find(j.ImportDefaultSpecifier).nodes();
98
+ const otherSpecifier = importDeclarations.find(j.ImportSpecifier).nodes();
99
+ const newDefaultSpecifier = defaultSpecifier.map(s => {
100
+ return j.importDeclaration([j.importDefaultSpecifier(s.local)], j.literal(pkg));
101
+ });
102
+ const newOtherSpecifiers = otherSpecifier.map(s => {
103
+ if (s.imported.name === innerElementName) {
104
+ return j.importDeclaration([j.importDefaultSpecifier(s.local)], j.literal(toPkg));
105
+ } else {
106
+ return j.importDeclaration([s], j.literal(pkg));
107
+ }
108
+ });
109
+ importDeclarations.forEach(path => {
110
+ j(path).replaceWith(newDefaultSpecifier);
111
+ j(path).insertBefore(newOtherSpecifiers);
112
+ });
164
113
  };
165
-
166
114
  exports.elevateComponentToNewEntryPoint = elevateComponentToNewEntryPoint;
167
-
168
- var flattenCertainChildPropsAsProp = function flattenCertainChildPropsAsProp(component, propName, childProps) {
169
- return function (j, source) {
170
- var defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
171
-
172
- if (!defaultSpecifier) {
173
- return;
174
- }
175
-
176
- source.findJSXElements(defaultSpecifier).forEach(function (element) {
177
- (0, _support.getJSXAttributesByName)(j, element, propName).forEach(function (attribute) {
178
- j(attribute).find(j.JSXExpressionContainer).find(j.ObjectExpression).forEach(function (objectExpression) {
179
- objectExpression.node.properties.forEach(function (property) {
180
- childProps.forEach(function (childProp) {
181
- if ((property.type === 'Property' || property.type === 'ObjectProperty') && property.key.type === 'Identifier' && property.key.name === childProp) {
182
- var _element$node$opening;
183
-
184
- (_element$node$opening = element.node.openingElement.attributes) === null || _element$node$opening === void 0 ? void 0 : _element$node$opening.push(j.jsxAttribute(j.jsxIdentifier(childProp), j.jsxExpressionContainer(property.value)));
185
- }
186
- });
115
+ const flattenCertainChildPropsAsProp = (component, propName, childProps) => (j, source) => {
116
+ const defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
117
+ if (!defaultSpecifier) {
118
+ return;
119
+ }
120
+ source.findJSXElements(defaultSpecifier).forEach(element => {
121
+ (0, _support.getJSXAttributesByName)(j, element, propName).forEach(attribute => {
122
+ j(attribute).find(j.JSXExpressionContainer).find(j.ObjectExpression).forEach(objectExpression => {
123
+ objectExpression.node.properties.forEach(property => {
124
+ childProps.forEach(childProp => {
125
+ if ((property.type === 'Property' || property.type === 'ObjectProperty') && property.key.type === 'Identifier' && property.key.name === childProp) {
126
+ var _element$node$opening;
127
+ (_element$node$opening = element.node.openingElement.attributes) === null || _element$node$opening === void 0 ? void 0 : _element$node$opening.push(j.jsxAttribute(j.jsxIdentifier(childProp), j.jsxExpressionContainer(property.value)));
128
+ }
187
129
  });
188
130
  });
189
131
  });
190
132
  });
191
- };
133
+ });
192
134
  };
193
-
194
135
  exports.flattenCertainChildPropsAsProp = flattenCertainChildPropsAsProp;
195
-
196
- var createRenameJSXFunc = function createRenameJSXFunc(packagePath, from, to) {
197
- var fallback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
198
- return function (j, source) {
199
- var namedSpecifier = (0, _support.getNamedSpecifier)(j, source, packagePath, from);
200
- var toName = fallback ? (0, _support.getSafeImportName)({
201
- j: j,
202
- base: source,
203
- currentDefaultSpecifierName: namedSpecifier,
204
- desiredName: to,
205
- fallbackName: fallback
206
- }) : to;
207
- var existingAlias = source.find(j.ImportDeclaration).filter(function (path) {
208
- return path.node.source.value === packagePath;
209
- }).find(j.ImportSpecifier).nodes().map(function (specifier) {
210
- if (from !== specifier.imported.name) {
211
- return null;
212
- } // If aliased: return the alias
213
-
214
-
215
- if (specifier.local && from !== specifier.local.name) {
216
- return specifier.local.name;
217
- }
218
-
136
+ const createRenameJSXFunc = (packagePath, from, to, fallback = undefined) => (j, source) => {
137
+ const namedSpecifier = (0, _support.getNamedSpecifier)(j, source, packagePath, from);
138
+ const toName = fallback ? (0, _support.getSafeImportName)({
139
+ j,
140
+ base: source,
141
+ currentDefaultSpecifierName: namedSpecifier,
142
+ desiredName: to,
143
+ fallbackName: fallback
144
+ }) : to;
145
+ const existingAlias = source.find(j.ImportDeclaration).filter(path => path.node.source.value === packagePath).find(j.ImportSpecifier).nodes().map(specifier => {
146
+ if (from !== specifier.imported.name) {
219
147
  return null;
220
- }).filter(Boolean)[0] || null;
221
- source.find(j.ImportDeclaration).filter(function (path) {
222
- return path.node.source.value === packagePath;
223
- }).find(j.ImportSpecifier).filter(function (importSpecifier) {
224
- var identifier = j(importSpecifier).find(j.Identifier).get();
225
-
226
- if (from === identifier.value.name || existingAlias === identifier.value.name) {
227
- return true;
228
- }
229
-
230
- return false;
231
- }).replaceWith([j.importSpecifier(j.identifier(toName), existingAlias ? j.identifier(existingAlias) : null)], j.literal(packagePath));
232
- };
148
+ }
149
+ // If aliased: return the alias
150
+ if (specifier.local && from !== specifier.local.name) {
151
+ return specifier.local.name;
152
+ }
153
+ return null;
154
+ }).filter(Boolean)[0] || null;
155
+ source.find(j.ImportDeclaration).filter(path => path.node.source.value === packagePath).find(j.ImportSpecifier).filter(importSpecifier => {
156
+ const identifier = j(importSpecifier).find(j.Identifier).get();
157
+ if (from === identifier.value.name || existingAlias === identifier.value.name) {
158
+ return true;
159
+ }
160
+ return false;
161
+ }).replaceWith([j.importSpecifier(j.identifier(toName), existingAlias ? j.identifier(existingAlias) : null)], j.literal(packagePath));
233
162
  };
234
-
235
163
  exports.createRenameJSXFunc = createRenameJSXFunc;
236
-
237
- var createRemoveFuncAddCommentFor = function createRemoveFuncAddCommentFor(component, prop, comment) {
238
- return function (j, source) {
239
- var defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
240
-
241
- if (!defaultSpecifier) {
242
- return;
243
- }
244
-
245
- source.findJSXElements(defaultSpecifier).forEach(function (element) {
246
- (0, _support.getJSXAttributesByName)(j, element, prop).forEach(function (attribute) {
247
- j(attribute).remove();
248
-
249
- if (comment) {
250
- (0, _support.addCommentToStartOfFile)({
251
- j: j,
252
- base: source,
253
- message: comment
254
- });
255
- }
256
- });
164
+ const createRemoveFuncAddCommentFor = (component, prop, comment) => (j, source) => {
165
+ const defaultSpecifier = (0, _support.getDefaultSpecifier)(j, source, component);
166
+ if (!defaultSpecifier) {
167
+ return;
168
+ }
169
+ source.findJSXElements(defaultSpecifier).forEach(element => {
170
+ (0, _support.getJSXAttributesByName)(j, element, prop).forEach(attribute => {
171
+ j(attribute).remove();
172
+ if (comment) {
173
+ (0, _support.addCommentToStartOfFile)({
174
+ j,
175
+ base: source,
176
+ message: comment
177
+ });
178
+ }
257
179
  });
258
- };
180
+ });
259
181
  };
260
-
261
182
  exports.createRemoveFuncAddCommentFor = createRemoveFuncAddCommentFor;
262
-
263
- var renameNamedImportWithAliasName = function renameNamedImportWithAliasName(component, from, to) {
264
- return function (j, source) {
265
- source.find(j.ImportDeclaration).filter(function (path) {
266
- return path.node.source.value === component;
267
- }).find(j.ImportSpecifier).filter(function (path) {
268
- return path.node.imported.name === from;
269
- }).forEach(function (path) {
270
- var localName = path.node.local.name;
271
- j(path).replaceWith(j.importSpecifier(j.identifier(to), j.identifier(localName)));
272
- });
273
- };
183
+ const renameNamedImportWithAliasName = (component, from, to) => (j, source) => {
184
+ source.find(j.ImportDeclaration).filter(path => path.node.source.value === component).find(j.ImportSpecifier).filter(path => path.node.imported.name === from).forEach(path => {
185
+ const localName = path.node.local.name;
186
+ j(path).replaceWith(j.importSpecifier(j.identifier(to), j.identifier(localName)));
187
+ });
274
188
  };
275
-
276
189
  exports.renameNamedImportWithAliasName = renameNamedImportWithAliasName;
190
+ const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) => (j, root) => {
191
+ root.find(j.ImportDeclaration, {
192
+ source: {
193
+ value: oldPackageName
194
+ }
195
+ }).forEach(path => {
196
+ var _currentImportSpecifi;
197
+ const currentImportSpecifier = (path.value.specifiers || []).find(specifier => {
198
+ if (specifier.type === 'ImportSpecifier') {
199
+ return specifier.imported.name === importToConvert;
200
+ }
201
+ return false;
202
+ });
203
+ if (!currentImportSpecifier) {
204
+ return;
205
+ }
206
+ const importedSpecifierName = currentImportSpecifier.imported.name;
207
+ const localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
208
+ const newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined);
277
209
 
278
- var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) {
279
- return function (j, root) {
210
+ // check if new import exists, if not create it
211
+ (0, _support.tryCreateImport)(j, root, oldPackageName, newPackageName, shouldBeTypeImport);
212
+
213
+ // remove the old import specifier, but NOT the whole package
280
214
  root.find(j.ImportDeclaration, {
281
215
  source: {
282
216
  value: oldPackageName
283
217
  }
284
- }).forEach(function (path) {
285
- var _currentImportSpecifi;
286
-
287
- var currentImportSpecifier = (path.value.specifiers || []).find(function (specifier) {
288
- if (specifier.type === 'ImportSpecifier') {
289
- return specifier.imported.name === importToConvert;
290
- }
218
+ }).find(j.ImportSpecifier).filter(path => path.value.imported.name === importToConvert).remove();
291
219
 
292
- return false;
293
- });
220
+ // adds import specifier to new import
221
+ (0, _support.addToImport)(j, root, newIdentifier, newPackageName);
294
222
 
295
- if (!currentImportSpecifier) {
296
- return;
223
+ // if there are any imports with no specifiers, remove the whole import
224
+ root.find(j.ImportDeclaration, {
225
+ source: {
226
+ value: oldPackageName
297
227
  }
298
-
299
- var importedSpecifierName = currentImportSpecifier.imported.name;
300
- var localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
301
- var newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined); // check if new import exists, if not create it
302
-
303
- (0, _support.tryCreateImport)(j, root, oldPackageName, newPackageName, shouldBeTypeImport); // remove the old import specifier, but NOT the whole package
304
-
305
- root.find(j.ImportDeclaration, {
306
- source: {
307
- value: oldPackageName
308
- }
309
- }).find(j.ImportSpecifier).filter(function (path) {
310
- return path.value.imported.name === importToConvert;
311
- }).remove(); // adds import specifier to new import
312
-
313
- (0, _support.addToImport)(j, root, newIdentifier, newPackageName); // if there are any imports with no specifiers, remove the whole import
314
-
315
- root.find(j.ImportDeclaration, {
316
- source: {
317
- value: oldPackageName
318
- }
319
- }).filter(function (path) {
320
- var _path$value$specifier;
321
-
322
- return !((_path$value$specifier = path.value.specifiers) !== null && _path$value$specifier !== void 0 && _path$value$specifier.length);
323
- }).remove();
324
- });
325
- };
228
+ }).filter(path => {
229
+ var _path$value$specifier;
230
+ return !((_path$value$specifier = path.value.specifiers) !== null && _path$value$specifier !== void 0 && _path$value$specifier.length);
231
+ }).remove();
232
+ });
326
233
  };
327
-
328
234
  exports.changeImportEntryPoint = changeImportEntryPoint;