@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.
- package/CHANGELOG.md +12 -0
- package/dist/cjs/index.js +29 -31
- package/dist/cjs/utils/index.js +185 -279
- package/dist/cjs/utils/support.js +113 -209
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/utils/index.js +12 -44
- package/dist/es2019/utils/support.js +26 -68
- package/dist/es2019/version.json +1 -1
- package/dist/esm/utils/index.js +12 -44
- package/dist/esm/utils/support.js +32 -77
- package/dist/esm/version.json +1 -1
- package/package.json +6 -3
- package/report.api.md +92 -41
package/dist/esm/utils/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { addCommentToStartOfFile, addToImport, getDefaultSpecifier, getJSXAttributesByName, getNamedSpecifier, getSafeImportName, tryCreateImport } from './support';
|
|
2
|
-
|
|
3
2
|
var createRemoveFuncFor = function createRemoveFuncFor(component, importName, prop) {
|
|
4
3
|
var predicate = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
|
|
5
4
|
return true;
|
|
@@ -7,11 +6,9 @@ var createRemoveFuncFor = function createRemoveFuncFor(component, importName, pr
|
|
|
7
6
|
var comment = arguments.length > 4 ? arguments[4] : undefined;
|
|
8
7
|
return function (j, source) {
|
|
9
8
|
var specifier = getNamedSpecifier(j, source, component, importName);
|
|
10
|
-
|
|
11
9
|
if (!specifier) {
|
|
12
10
|
return;
|
|
13
11
|
}
|
|
14
|
-
|
|
15
12
|
source.findJSXElements(specifier).forEach(function (element) {
|
|
16
13
|
if (predicate(j, element) && comment) {
|
|
17
14
|
addCommentToStartOfFile({
|
|
@@ -27,15 +24,12 @@ var createRemoveFuncFor = function createRemoveFuncFor(component, importName, pr
|
|
|
27
24
|
});
|
|
28
25
|
};
|
|
29
26
|
};
|
|
30
|
-
|
|
31
27
|
var createRenameFuncFor = function createRenameFuncFor(component, from, to) {
|
|
32
28
|
return function (j, source) {
|
|
33
29
|
var defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
34
|
-
|
|
35
30
|
if (!defaultSpecifier) {
|
|
36
31
|
return;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
40
34
|
getJSXAttributesByName(j, element, from).forEach(function (attribute) {
|
|
41
35
|
j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(to), attribute.node.value));
|
|
@@ -43,20 +37,16 @@ var createRenameFuncFor = function createRenameFuncFor(component, from, to) {
|
|
|
43
37
|
});
|
|
44
38
|
};
|
|
45
39
|
};
|
|
46
|
-
|
|
47
40
|
var createConvertFuncFor = function createConvertFuncFor(component, from, to, predicate) {
|
|
48
41
|
return function (j, source) {
|
|
49
42
|
var defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
50
|
-
|
|
51
43
|
if (!defaultSpecifier) {
|
|
52
44
|
return;
|
|
53
45
|
}
|
|
54
|
-
|
|
55
46
|
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
56
47
|
getJSXAttributesByName(j, element, from).forEach(function (attribute) {
|
|
57
48
|
var shouldConvert = predicate && predicate(attribute.node.value) || false;
|
|
58
49
|
var node = j.jsxAttribute(j.jsxIdentifier(to));
|
|
59
|
-
|
|
60
50
|
if (shouldConvert) {
|
|
61
51
|
j(attribute).insertBefore(node);
|
|
62
52
|
}
|
|
@@ -64,7 +54,6 @@ var createConvertFuncFor = function createConvertFuncFor(component, from, to, pr
|
|
|
64
54
|
});
|
|
65
55
|
};
|
|
66
56
|
};
|
|
67
|
-
|
|
68
57
|
var replaceImportStatementFor = function replaceImportStatementFor(pkg, convertMap) {
|
|
69
58
|
return function (j, root) {
|
|
70
59
|
root.find(j.ImportDeclaration).filter(function (path) {
|
|
@@ -82,7 +71,6 @@ var replaceImportStatementFor = function replaceImportStatementFor(pkg, convertM
|
|
|
82
71
|
j(path).replaceWith(defaultDeclarations);
|
|
83
72
|
var otherDeclarations = otherSpecifier.map(function (s) {
|
|
84
73
|
var localName = s.local.name;
|
|
85
|
-
|
|
86
74
|
if (convertMap[localName]) {
|
|
87
75
|
return j.importDeclaration([j.importDefaultSpecifier(j.identifier(localName))], j.literal(convertMap[localName]));
|
|
88
76
|
} else {
|
|
@@ -93,7 +81,6 @@ var replaceImportStatementFor = function replaceImportStatementFor(pkg, convertM
|
|
|
93
81
|
});
|
|
94
82
|
};
|
|
95
83
|
};
|
|
96
|
-
|
|
97
84
|
var createRenameImportFor = function createRenameImportFor(component, from, to) {
|
|
98
85
|
return function (j, source) {
|
|
99
86
|
source.find(j.ImportDeclaration).filter(function (path) {
|
|
@@ -103,23 +90,21 @@ var createRenameImportFor = function createRenameImportFor(component, from, to)
|
|
|
103
90
|
});
|
|
104
91
|
};
|
|
105
92
|
};
|
|
106
|
-
|
|
107
93
|
var createTransformer = function createTransformer(migrates, shouldApplyTransform) {
|
|
108
94
|
return function (fileInfo, _ref, options) {
|
|
109
95
|
var j = _ref.jscodeshift;
|
|
110
|
-
var source = j(fileInfo.source);
|
|
96
|
+
var source = j(fileInfo.source);
|
|
111
97
|
|
|
98
|
+
// If shouldApplyTransform not provided then perform old behaviour
|
|
112
99
|
if (!shouldApplyTransform || shouldApplyTransform(j, source)) {
|
|
113
100
|
migrates.forEach(function (tf) {
|
|
114
101
|
return tf(j, source);
|
|
115
102
|
});
|
|
116
103
|
return source.toSource(options.printOptions);
|
|
117
104
|
}
|
|
118
|
-
|
|
119
105
|
return fileInfo.source;
|
|
120
106
|
};
|
|
121
107
|
};
|
|
122
|
-
|
|
123
108
|
var elevateComponentToNewEntryPoint = function elevateComponentToNewEntryPoint(pkg, toPkg, innerElementName) {
|
|
124
109
|
return function (j, root) {
|
|
125
110
|
var importDeclarations = root.find(j.ImportDeclaration).filter(function (path) {
|
|
@@ -143,15 +128,12 @@ var elevateComponentToNewEntryPoint = function elevateComponentToNewEntryPoint(p
|
|
|
143
128
|
});
|
|
144
129
|
};
|
|
145
130
|
};
|
|
146
|
-
|
|
147
131
|
var flattenCertainChildPropsAsProp = function flattenCertainChildPropsAsProp(component, propName, childProps) {
|
|
148
132
|
return function (j, source) {
|
|
149
133
|
var defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
150
|
-
|
|
151
134
|
if (!defaultSpecifier) {
|
|
152
135
|
return;
|
|
153
136
|
}
|
|
154
|
-
|
|
155
137
|
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
156
138
|
getJSXAttributesByName(j, element, propName).forEach(function (attribute) {
|
|
157
139
|
j(attribute).find(j.JSXExpressionContainer).find(j.ObjectExpression).forEach(function (objectExpression) {
|
|
@@ -159,7 +141,6 @@ var flattenCertainChildPropsAsProp = function flattenCertainChildPropsAsProp(com
|
|
|
159
141
|
childProps.forEach(function (childProp) {
|
|
160
142
|
if ((property.type === 'Property' || property.type === 'ObjectProperty') && property.key.type === 'Identifier' && property.key.name === childProp) {
|
|
161
143
|
var _element$node$opening;
|
|
162
|
-
|
|
163
144
|
(_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)));
|
|
164
145
|
}
|
|
165
146
|
});
|
|
@@ -169,7 +150,6 @@ var flattenCertainChildPropsAsProp = function flattenCertainChildPropsAsProp(com
|
|
|
169
150
|
});
|
|
170
151
|
};
|
|
171
152
|
};
|
|
172
|
-
|
|
173
153
|
var createRenameJSXFunc = function createRenameJSXFunc(packagePath, from, to) {
|
|
174
154
|
var fallback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
|
|
175
155
|
return function (j, source) {
|
|
@@ -186,41 +166,33 @@ var createRenameJSXFunc = function createRenameJSXFunc(packagePath, from, to) {
|
|
|
186
166
|
}).find(j.ImportSpecifier).nodes().map(function (specifier) {
|
|
187
167
|
if (from !== specifier.imported.name) {
|
|
188
168
|
return null;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
169
|
+
}
|
|
170
|
+
// If aliased: return the alias
|
|
192
171
|
if (specifier.local && from !== specifier.local.name) {
|
|
193
172
|
return specifier.local.name;
|
|
194
173
|
}
|
|
195
|
-
|
|
196
174
|
return null;
|
|
197
175
|
}).filter(Boolean)[0] || null;
|
|
198
176
|
source.find(j.ImportDeclaration).filter(function (path) {
|
|
199
177
|
return path.node.source.value === packagePath;
|
|
200
178
|
}).find(j.ImportSpecifier).filter(function (importSpecifier) {
|
|
201
179
|
var identifier = j(importSpecifier).find(j.Identifier).get();
|
|
202
|
-
|
|
203
180
|
if (from === identifier.value.name || existingAlias === identifier.value.name) {
|
|
204
181
|
return true;
|
|
205
182
|
}
|
|
206
|
-
|
|
207
183
|
return false;
|
|
208
184
|
}).replaceWith([j.importSpecifier(j.identifier(toName), existingAlias ? j.identifier(existingAlias) : null)], j.literal(packagePath));
|
|
209
185
|
};
|
|
210
186
|
};
|
|
211
|
-
|
|
212
187
|
var createRemoveFuncAddCommentFor = function createRemoveFuncAddCommentFor(component, prop, comment) {
|
|
213
188
|
return function (j, source) {
|
|
214
189
|
var defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
215
|
-
|
|
216
190
|
if (!defaultSpecifier) {
|
|
217
191
|
return;
|
|
218
192
|
}
|
|
219
|
-
|
|
220
193
|
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
221
194
|
getJSXAttributesByName(j, element, prop).forEach(function (attribute) {
|
|
222
195
|
j(attribute).remove();
|
|
223
|
-
|
|
224
196
|
if (comment) {
|
|
225
197
|
addCommentToStartOfFile({
|
|
226
198
|
j: j,
|
|
@@ -232,7 +204,6 @@ var createRemoveFuncAddCommentFor = function createRemoveFuncAddCommentFor(compo
|
|
|
232
204
|
});
|
|
233
205
|
};
|
|
234
206
|
};
|
|
235
|
-
|
|
236
207
|
var renameNamedImportWithAliasName = function renameNamedImportWithAliasName(component, from, to) {
|
|
237
208
|
return function (j, source) {
|
|
238
209
|
source.find(j.ImportDeclaration).filter(function (path) {
|
|
@@ -245,7 +216,6 @@ var renameNamedImportWithAliasName = function renameNamedImportWithAliasName(com
|
|
|
245
216
|
});
|
|
246
217
|
};
|
|
247
218
|
};
|
|
248
|
-
|
|
249
219
|
var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) {
|
|
250
220
|
return function (j, root) {
|
|
251
221
|
root.find(j.ImportDeclaration, {
|
|
@@ -254,46 +224,44 @@ var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, imp
|
|
|
254
224
|
}
|
|
255
225
|
}).forEach(function (path) {
|
|
256
226
|
var _currentImportSpecifi;
|
|
257
|
-
|
|
258
227
|
var currentImportSpecifier = (path.value.specifiers || []).find(function (specifier) {
|
|
259
228
|
if (specifier.type === 'ImportSpecifier') {
|
|
260
229
|
return specifier.imported.name === importToConvert;
|
|
261
230
|
}
|
|
262
|
-
|
|
263
231
|
return false;
|
|
264
232
|
});
|
|
265
|
-
|
|
266
233
|
if (!currentImportSpecifier) {
|
|
267
234
|
return;
|
|
268
235
|
}
|
|
269
|
-
|
|
270
236
|
var importedSpecifierName = currentImportSpecifier.imported.name;
|
|
271
237
|
var localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
|
|
272
|
-
var newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined);
|
|
238
|
+
var newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined);
|
|
273
239
|
|
|
274
|
-
|
|
240
|
+
// check if new import exists, if not create it
|
|
241
|
+
tryCreateImport(j, root, oldPackageName, newPackageName, shouldBeTypeImport);
|
|
275
242
|
|
|
243
|
+
// remove the old import specifier, but NOT the whole package
|
|
276
244
|
root.find(j.ImportDeclaration, {
|
|
277
245
|
source: {
|
|
278
246
|
value: oldPackageName
|
|
279
247
|
}
|
|
280
248
|
}).find(j.ImportSpecifier).filter(function (path) {
|
|
281
249
|
return path.value.imported.name === importToConvert;
|
|
282
|
-
}).remove();
|
|
250
|
+
}).remove();
|
|
283
251
|
|
|
284
|
-
|
|
252
|
+
// adds import specifier to new import
|
|
253
|
+
addToImport(j, root, newIdentifier, newPackageName);
|
|
285
254
|
|
|
255
|
+
// if there are any imports with no specifiers, remove the whole import
|
|
286
256
|
root.find(j.ImportDeclaration, {
|
|
287
257
|
source: {
|
|
288
258
|
value: oldPackageName
|
|
289
259
|
}
|
|
290
260
|
}).filter(function (path) {
|
|
291
261
|
var _path$value$specifier;
|
|
292
|
-
|
|
293
262
|
return !((_path$value$specifier = path.value.specifiers) !== null && _path$value$specifier !== void 0 && _path$value$specifier.length);
|
|
294
263
|
}).remove();
|
|
295
264
|
});
|
|
296
265
|
};
|
|
297
266
|
};
|
|
298
|
-
|
|
299
267
|
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor, changeImportEntryPoint };
|
|
@@ -1,77 +1,62 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
|
|
3
2
|
function getNamedSpecifier(j, source, specifier, importName) {
|
|
4
3
|
var specifiers = source.find(j.ImportDeclaration).filter(function (path) {
|
|
5
4
|
return path.node.source.value === specifier;
|
|
6
5
|
}).find(j.ImportSpecifier).filter(function (path) {
|
|
7
6
|
return path.node.imported.name === importName;
|
|
8
7
|
});
|
|
9
|
-
|
|
10
8
|
if (!specifiers.length) {
|
|
11
9
|
return null;
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
return specifiers.nodes()[0].local.name;
|
|
15
12
|
}
|
|
16
|
-
|
|
17
13
|
var getDynamicImportName = function getDynamicImportName(j, source, importPath) {
|
|
18
14
|
var dynamicImports = source.find(j.VariableDeclarator).filter(function (variableDeclaratorPath) {
|
|
19
15
|
return j(variableDeclaratorPath).find(j.CallExpression).filter(function (callExpressionPath) {
|
|
20
16
|
var _callExpressionPath$n = callExpressionPath.node,
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
callee = _callExpressionPath$n.callee,
|
|
18
|
+
callExpressionArguments = _callExpressionPath$n.arguments;
|
|
23
19
|
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
|
|
24
20
|
}).length > 0;
|
|
25
21
|
});
|
|
26
|
-
|
|
27
22
|
if (!dynamicImports.length) {
|
|
28
23
|
return null;
|
|
29
24
|
}
|
|
30
|
-
|
|
31
25
|
var id = dynamicImports.nodes()[0].id;
|
|
32
|
-
|
|
33
26
|
if (id.type !== 'Identifier') {
|
|
34
27
|
return null;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
29
|
return id.name;
|
|
38
30
|
};
|
|
39
|
-
|
|
40
31
|
var isCallExpressionCalleeImportType = function isCallExpressionCalleeImportType(callee) {
|
|
41
32
|
return callee && callee.type === 'Import';
|
|
42
33
|
};
|
|
43
|
-
|
|
44
34
|
var isCallExpressionArgumentStringLiteralType = function isCallExpressionArgumentStringLiteralType(callExpressionArguments) {
|
|
45
35
|
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
|
|
46
36
|
};
|
|
47
|
-
|
|
48
37
|
var isCallExpressionArgumentValueMatches = function isCallExpressionArgumentValueMatches(callExpressionArgument, j, value) {
|
|
49
38
|
return j(callExpressionArgument).some(function (path) {
|
|
50
39
|
return path.node.value === value;
|
|
51
40
|
});
|
|
52
41
|
};
|
|
53
|
-
|
|
54
42
|
var addDynamicImport = function addDynamicImport(j, target, name, packageEndpoint) {
|
|
55
43
|
var node = j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [j.arrowFunctionExpression([], j.callExpression(j.import(), [j.stringLiteral(packageEndpoint)]))]))]);
|
|
56
44
|
target.insertAfter(node);
|
|
57
45
|
addCommentBefore(j, j(node), 'We have added "React.lazy" here. Feel free to change it to "lazy" or other named import depending upon how you imported.');
|
|
58
|
-
};
|
|
59
|
-
|
|
46
|
+
};
|
|
60
47
|
|
|
48
|
+
// not replacing newlines (which \s does)
|
|
61
49
|
var spacesAndTabs = /[ \t]{2,}/g;
|
|
62
50
|
var lineStartWithSpaces = /^[ \t]*/gm;
|
|
63
|
-
|
|
64
51
|
function clean(value) {
|
|
65
52
|
return value.replace(spacesAndTabs, ' ').replace(lineStartWithSpaces, '').trim();
|
|
66
53
|
}
|
|
67
|
-
|
|
68
54
|
var addCommentToStartOfFile = function addCommentToStartOfFile(_ref) {
|
|
69
55
|
var j = _ref.j,
|
|
70
|
-
|
|
71
|
-
|
|
56
|
+
base = _ref.base,
|
|
57
|
+
message = _ref.message;
|
|
72
58
|
addCommentBefore(j, base.find(j.Program), message);
|
|
73
59
|
};
|
|
74
|
-
|
|
75
60
|
function addCommentBefore(j, target, message) {
|
|
76
61
|
var commentType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'block';
|
|
77
62
|
var messagePrefix = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'TODO: (from codemod) ';
|
|
@@ -81,12 +66,12 @@ function addCommentBefore(j, target, message) {
|
|
|
81
66
|
path.value.comments = path.value.comments || [];
|
|
82
67
|
var exists = path.value.comments.find(function (comment) {
|
|
83
68
|
return comment.value === content;
|
|
84
|
-
});
|
|
69
|
+
});
|
|
85
70
|
|
|
71
|
+
// avoiding duplicates of the same comment
|
|
86
72
|
if (exists) {
|
|
87
73
|
return;
|
|
88
74
|
}
|
|
89
|
-
|
|
90
75
|
if (commentType === 'block') {
|
|
91
76
|
path.value.comments.push(j.commentBlock(content));
|
|
92
77
|
} else {
|
|
@@ -94,20 +79,17 @@ function addCommentBefore(j, target, message) {
|
|
|
94
79
|
}
|
|
95
80
|
});
|
|
96
81
|
}
|
|
97
|
-
|
|
98
82
|
var getDefaultSpecifier = function getDefaultSpecifier(j, source, specifier) {
|
|
99
83
|
var specifiers = source.find(j.ImportDeclaration).filter(function (path) {
|
|
100
84
|
return path.node.source.value === specifier;
|
|
101
85
|
}).find(j.ImportDefaultSpecifier);
|
|
102
|
-
|
|
103
86
|
if (!specifiers.length) {
|
|
104
87
|
return null;
|
|
105
88
|
}
|
|
106
|
-
|
|
107
89
|
return specifiers.nodes()[0].local.name;
|
|
108
|
-
};
|
|
109
|
-
|
|
90
|
+
};
|
|
110
91
|
|
|
92
|
+
// @ts-ignore
|
|
111
93
|
var getJSXAttributesByName = function getJSXAttributesByName(j, element, attributeName) {
|
|
112
94
|
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(function (attribute) {
|
|
113
95
|
var matches = j(attribute).find(j.JSXIdentifier).filter(function (identifier) {
|
|
@@ -116,37 +98,30 @@ var getJSXAttributesByName = function getJSXAttributesByName(j, element, attribu
|
|
|
116
98
|
return Boolean(matches.length);
|
|
117
99
|
});
|
|
118
100
|
};
|
|
119
|
-
|
|
120
101
|
var isEmpty = function isEmpty(string) {
|
|
121
102
|
return string && string.value !== '';
|
|
122
103
|
};
|
|
123
|
-
|
|
124
104
|
var hasImportDeclaration = function hasImportDeclaration(j, source, importPath) {
|
|
125
105
|
var imports = source.find(j.ImportDeclaration).filter(function (path) {
|
|
126
106
|
return path.node.source.value === importPath;
|
|
127
107
|
});
|
|
128
108
|
return Boolean(imports.length);
|
|
129
109
|
};
|
|
130
|
-
|
|
131
110
|
var hasImportDeclarationFromAnyPackageEntrypoint = function hasImportDeclarationFromAnyPackageEntrypoint(j, source, packageName) {
|
|
132
111
|
var imports = source.find(j.ImportDeclaration).filter(function (path) {
|
|
133
112
|
var _path$node, _path$node$source;
|
|
134
|
-
|
|
135
113
|
return (// @ts-ignore
|
|
136
114
|
path === null || path === void 0 ? void 0 : (_path$node = path.node) === null || _path$node === void 0 ? void 0 : (_path$node$source = _path$node.source) === null || _path$node$source === void 0 ? void 0 : _path$node$source.value.toString().startsWith(packageName)
|
|
137
115
|
);
|
|
138
116
|
});
|
|
139
117
|
return Boolean(imports.length);
|
|
140
118
|
};
|
|
141
|
-
|
|
142
119
|
var debug = function debug(component) {
|
|
143
120
|
return function (j, source) {
|
|
144
121
|
var defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
145
|
-
|
|
146
122
|
if (!defaultSpecifier) {
|
|
147
123
|
return;
|
|
148
124
|
}
|
|
149
|
-
|
|
150
125
|
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
151
126
|
console.log(element); //eslint-disable-line no-console
|
|
152
127
|
});
|
|
@@ -157,7 +132,6 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
157
132
|
if (placeholderStr === resolvedStr) {
|
|
158
133
|
return true;
|
|
159
134
|
}
|
|
160
|
-
|
|
161
135
|
var value = '';
|
|
162
136
|
var offset = 0;
|
|
163
137
|
var partsPlaceholder = placeholderStr.split(' ');
|
|
@@ -169,19 +143,19 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
169
143
|
var remainingWords = partsResolved.slice(i + offset);
|
|
170
144
|
var nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
171
145
|
var hasNextWord = nextWordIndex !== -1;
|
|
172
|
-
|
|
173
146
|
if (hasNextWord) {
|
|
174
147
|
offset += nextWordIndex - 1;
|
|
175
148
|
}
|
|
176
|
-
|
|
177
149
|
var insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
178
|
-
value += "".concat(insert, " ");
|
|
150
|
+
value += "".concat(insert, " ");
|
|
151
|
+
// Regular words
|
|
179
152
|
} else {
|
|
180
153
|
value += "".concat(p, " ");
|
|
181
154
|
}
|
|
182
155
|
});
|
|
183
156
|
return value.trimRight() === resolvedStr;
|
|
184
157
|
}
|
|
158
|
+
|
|
185
159
|
/**
|
|
186
160
|
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
187
161
|
*
|
|
@@ -193,10 +167,9 @@ export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
193
167
|
*
|
|
194
168
|
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
195
169
|
*/
|
|
196
|
-
|
|
197
170
|
export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
198
|
-
var value = String(argValue);
|
|
199
|
-
|
|
171
|
+
var value = String(argValue);
|
|
172
|
+
// Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
200
173
|
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
201
174
|
return placeholderStringMatches(value, str);
|
|
202
175
|
} else {
|
|
@@ -204,7 +177,6 @@ export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
|
204
177
|
return false;
|
|
205
178
|
}
|
|
206
179
|
}
|
|
207
|
-
|
|
208
180
|
var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiteralsWithPlaceholders(quasis, str) {
|
|
209
181
|
var templateStrs = quasis.map(function (quasi) {
|
|
210
182
|
return quasi.value.raw.trim();
|
|
@@ -212,7 +184,6 @@ var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiterals
|
|
|
212
184
|
var regex = new RegExp(templateStrs);
|
|
213
185
|
return regex.test(str);
|
|
214
186
|
};
|
|
215
|
-
|
|
216
187
|
var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg, str) {
|
|
217
188
|
switch (arg.type) {
|
|
218
189
|
case 'StringLiteral':
|
|
@@ -225,14 +196,12 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
225
196
|
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
226
197
|
}
|
|
227
198
|
}
|
|
228
|
-
|
|
229
199
|
case 'TemplateLiteral':
|
|
230
200
|
{
|
|
231
201
|
// fuzzy match template literals, skipping expressions
|
|
232
202
|
var templateStrs = arg.quasis.map(function (quasi) {
|
|
233
203
|
return quasi.value.raw.trim();
|
|
234
204
|
}).join(' ');
|
|
235
|
-
|
|
236
205
|
if (str.trim() === templateStrs.trim()) {
|
|
237
206
|
return true;
|
|
238
207
|
} else {
|
|
@@ -240,52 +209,46 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
240
209
|
return checkForTemplateLiteralsWithPlaceholders(arg.quasis, str);
|
|
241
210
|
}
|
|
242
211
|
}
|
|
243
|
-
|
|
244
212
|
case 'BinaryExpression':
|
|
245
213
|
{
|
|
246
214
|
return false;
|
|
247
215
|
}
|
|
248
|
-
|
|
249
216
|
default:
|
|
250
217
|
{
|
|
251
218
|
return false;
|
|
252
219
|
}
|
|
253
220
|
}
|
|
254
221
|
};
|
|
255
|
-
|
|
256
222
|
var testMethodVariantEach = function testMethodVariantEach(path, testMethods) {
|
|
257
223
|
var _path$value, _path$value$callee, _path$value$callee$ca, _path$value2, _path$value2$callee, _path$value2$callee$c, _path$value2$callee$c2, _path$value3, _path$value3$callee, _path$value3$callee$c, _path$value3$callee$c2;
|
|
258
|
-
|
|
259
224
|
var variants = new Set(['each']);
|
|
260
|
-
return (
|
|
261
|
-
|
|
262
|
-
|
|
225
|
+
return (
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
((_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$callee = _path$value.callee) === null || _path$value$callee === void 0 ? void 0 : (_path$value$callee$ca = _path$value$callee.callee) === null || _path$value$callee$ca === void 0 ? void 0 : _path$value$callee$ca.type) === 'MemberExpression' &&
|
|
228
|
+
// @ts-ignore
|
|
229
|
+
testMethods.has((_path$value2 = path.value) === null || _path$value2 === void 0 ? void 0 : (_path$value2$callee = _path$value2.callee) === null || _path$value2$callee === void 0 ? void 0 : (_path$value2$callee$c = _path$value2$callee.callee) === null || _path$value2$callee$c === void 0 ? void 0 : (_path$value2$callee$c2 = _path$value2$callee$c.object) === null || _path$value2$callee$c2 === void 0 ? void 0 : _path$value2$callee$c2.name) &&
|
|
230
|
+
// @ts-ignore
|
|
263
231
|
variants.has((_path$value3 = path.value) === null || _path$value3 === void 0 ? void 0 : (_path$value3$callee = _path$value3.callee) === null || _path$value3$callee === void 0 ? void 0 : (_path$value3$callee$c = _path$value3$callee.callee) === null || _path$value3$callee$c === void 0 ? void 0 : (_path$value3$callee$c2 = _path$value3$callee$c.property) === null || _path$value3$callee$c2 === void 0 ? void 0 : _path$value3$callee$c2.name)
|
|
264
232
|
);
|
|
265
233
|
};
|
|
266
|
-
|
|
267
234
|
var hasJSXAttributesByName = function hasJSXAttributesByName(j, element, attributeName) {
|
|
268
235
|
return getJSXAttributesByName(j, element, attributeName).length > 0;
|
|
269
236
|
};
|
|
270
|
-
|
|
271
237
|
var doesIdentifierExist = function doesIdentifierExist(j, base, name) {
|
|
272
238
|
return base.find(j.Identifier).filter(function (identifer) {
|
|
273
239
|
return identifer.value.name === name;
|
|
274
240
|
}).length > 0;
|
|
275
241
|
};
|
|
276
|
-
|
|
277
242
|
function removeImport(j, base, packageName) {
|
|
278
243
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
279
244
|
return path.node.source.value === packageName;
|
|
280
245
|
}).remove();
|
|
281
246
|
}
|
|
282
|
-
|
|
283
247
|
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
284
248
|
var matches = base.find(j.ImportDeclaration).filter(function (path) {
|
|
285
249
|
return path.value.source.value === packageName;
|
|
286
250
|
});
|
|
287
251
|
var exists = matches.length > 0;
|
|
288
|
-
|
|
289
252
|
if (exists) {
|
|
290
253
|
if (shouldBeTypeImport) {
|
|
291
254
|
// if the matched import declarations are not type imports
|
|
@@ -293,7 +256,6 @@ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeIm
|
|
|
293
256
|
var isTypeImports = matches.every(function (path) {
|
|
294
257
|
return path.value.importKind === 'type';
|
|
295
258
|
});
|
|
296
|
-
|
|
297
259
|
if (!isTypeImports) {
|
|
298
260
|
matches.filter(function (path) {
|
|
299
261
|
return path.value.importKind !== 'type';
|
|
@@ -303,26 +265,23 @@ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeIm
|
|
|
303
265
|
return;
|
|
304
266
|
}
|
|
305
267
|
}
|
|
306
|
-
|
|
307
268
|
return;
|
|
308
|
-
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
309
272
|
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
310
273
|
// runtime exceptions during transform runs when importKind is undefined)
|
|
311
|
-
|
|
312
|
-
|
|
313
274
|
var importDeclarationArgs = [[], j.literal(packageName)];
|
|
314
|
-
|
|
315
275
|
if (shouldBeTypeImport) {
|
|
316
276
|
importDeclarationArgs.push('type');
|
|
317
277
|
}
|
|
318
|
-
|
|
319
278
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
320
279
|
return path.value.source.value === relativeToPackage;
|
|
321
|
-
})
|
|
280
|
+
})
|
|
281
|
+
// we only take the first matching path, to avoid duplicate inserts
|
|
322
282
|
// if the source contains the same import declaration more than once
|
|
323
283
|
.paths()[0].insertBefore(j.importDeclaration.apply(j, importDeclarationArgs));
|
|
324
284
|
}
|
|
325
|
-
|
|
326
285
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
327
286
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
328
287
|
return path.value.source.value === packageName;
|
|
@@ -332,29 +291,25 @@ function addToImport(j, base, importSpecifier, packageName) {
|
|
|
332
291
|
})), [importSpecifier]), j.literal(packageName), declaration.value.importKind);
|
|
333
292
|
});
|
|
334
293
|
}
|
|
335
|
-
|
|
336
294
|
var shiftDefaultImport = function shiftDefaultImport(j, base, defaultName, oldPackagePath, newPackagePath) {
|
|
337
295
|
tryCreateImport(j, base, oldPackagePath, newPackagePath);
|
|
338
|
-
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
296
|
+
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
339
297
|
|
|
298
|
+
// removing old default specifier
|
|
340
299
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
341
300
|
return path.node.source.value === oldPackagePath;
|
|
342
301
|
}).remove();
|
|
343
302
|
};
|
|
344
|
-
|
|
345
303
|
function getSafeImportName(_ref2) {
|
|
346
304
|
var j = _ref2.j,
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
305
|
+
base = _ref2.base,
|
|
306
|
+
currentDefaultSpecifierName = _ref2.currentDefaultSpecifierName,
|
|
307
|
+
desiredName = _ref2.desiredName,
|
|
308
|
+
fallbackName = _ref2.fallbackName;
|
|
352
309
|
if (currentDefaultSpecifierName === desiredName) {
|
|
353
310
|
return desiredName;
|
|
354
311
|
}
|
|
355
|
-
|
|
356
312
|
var isUsed = doesIdentifierExist(j, base, desiredName);
|
|
357
313
|
return isUsed ? fallbackName : desiredName;
|
|
358
314
|
}
|
|
359
|
-
|
|
360
315
|
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, hasImportDeclarationFromAnyPackageEntrypoint, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, getDynamicImportName, addDynamicImport };
|
package/dist/esm/version.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/codemod-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"author": "Atlassian Pty Ltd",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": " jscodeshift-compatible codemod utilities",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"registry": "https://registry.npmjs.org/"
|
|
9
9
|
},
|
|
10
10
|
"atlassian": {
|
|
11
|
-
"team": "UIP:
|
|
11
|
+
"team": "UIP: Frontend Delivery",
|
|
12
12
|
"inPublicMirror": false,
|
|
13
13
|
"releaseModel": "continuous"
|
|
14
14
|
},
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@atlassian/atlassian-frontend-prettier-config-1.0.0": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.0",
|
|
31
31
|
"@types/jscodeshift": "^0.11.0",
|
|
32
32
|
"jscodeshift": "^0.13.0",
|
|
33
|
-
"typescript": "4.
|
|
33
|
+
"typescript": "4.5.5"
|
|
34
34
|
},
|
|
35
35
|
"techstack": {
|
|
36
36
|
"@atlassian/frontend": {
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"analytics": [
|
|
46
46
|
"analytics-next"
|
|
47
47
|
],
|
|
48
|
+
"design-tokens": [
|
|
49
|
+
"color"
|
|
50
|
+
],
|
|
48
51
|
"theming": [
|
|
49
52
|
"react-context"
|
|
50
53
|
],
|