@atlaskit/codemod-utils 4.1.3 → 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 +6 -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 +5 -9
- package/report.api.md +31 -13
- package/dist/types-ts4.0/index.d.ts +0 -2
- package/dist/types-ts4.0/utils/index.d.ts +0 -15
- package/dist/types-ts4.0/utils/support.d.ts +0 -47
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
@@ -19,101 +17,76 @@ exports.placeholderStringMatches = placeholderStringMatches;
|
|
|
19
17
|
exports.removeImport = removeImport;
|
|
20
18
|
exports.testMethodVariantEach = exports.shiftDefaultImport = void 0;
|
|
21
19
|
exports.tryCreateImport = tryCreateImport;
|
|
22
|
-
|
|
23
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
24
|
-
|
|
25
20
|
function getNamedSpecifier(j, source, specifier, importName) {
|
|
26
|
-
|
|
27
|
-
return path.node.source.value === specifier;
|
|
28
|
-
}).find(j.ImportSpecifier).filter(function (path) {
|
|
29
|
-
return path.node.imported.name === importName;
|
|
30
|
-
});
|
|
31
|
-
|
|
21
|
+
const specifiers = source.find(j.ImportDeclaration).filter(path => path.node.source.value === specifier).find(j.ImportSpecifier).filter(path => path.node.imported.name === importName);
|
|
32
22
|
if (!specifiers.length) {
|
|
33
23
|
return null;
|
|
34
24
|
}
|
|
35
|
-
|
|
36
25
|
return specifiers.nodes()[0].local.name;
|
|
37
26
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
27
|
+
const getDynamicImportName = (j, source, importPath) => {
|
|
28
|
+
const dynamicImports = source.find(j.VariableDeclarator).filter(variableDeclaratorPath => {
|
|
29
|
+
return j(variableDeclaratorPath).find(j.CallExpression).filter(callExpressionPath => {
|
|
30
|
+
const {
|
|
31
|
+
callee,
|
|
32
|
+
arguments: callExpressionArguments
|
|
33
|
+
} = callExpressionPath.node;
|
|
45
34
|
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
|
|
46
35
|
}).length > 0;
|
|
47
36
|
});
|
|
48
|
-
|
|
49
37
|
if (!dynamicImports.length) {
|
|
50
38
|
return null;
|
|
51
39
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
40
|
+
const {
|
|
41
|
+
id
|
|
42
|
+
} = dynamicImports.nodes()[0];
|
|
55
43
|
if (id.type !== 'Identifier') {
|
|
56
44
|
return null;
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
return id.name;
|
|
60
47
|
};
|
|
61
|
-
|
|
62
48
|
exports.getDynamicImportName = getDynamicImportName;
|
|
63
|
-
|
|
64
|
-
var isCallExpressionCalleeImportType = function isCallExpressionCalleeImportType(callee) {
|
|
49
|
+
const isCallExpressionCalleeImportType = callee => {
|
|
65
50
|
return callee && callee.type === 'Import';
|
|
66
51
|
};
|
|
67
|
-
|
|
68
|
-
var isCallExpressionArgumentStringLiteralType = function isCallExpressionArgumentStringLiteralType(callExpressionArguments) {
|
|
52
|
+
const isCallExpressionArgumentStringLiteralType = callExpressionArguments => {
|
|
69
53
|
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
|
|
70
54
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return j(callExpressionArgument).some(function (path) {
|
|
74
|
-
return path.node.value === value;
|
|
75
|
-
});
|
|
55
|
+
const isCallExpressionArgumentValueMatches = (callExpressionArgument, j, value) => {
|
|
56
|
+
return j(callExpressionArgument).some(path => path.node.value === value);
|
|
76
57
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
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)]))]))]);
|
|
58
|
+
const addDynamicImport = (j, target, name, packageEndpoint) => {
|
|
59
|
+
const 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)]))]))]);
|
|
80
60
|
target.insertAfter(node);
|
|
81
61
|
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.');
|
|
82
|
-
};
|
|
83
|
-
|
|
62
|
+
};
|
|
84
63
|
|
|
64
|
+
// not replacing newlines (which \s does)
|
|
85
65
|
exports.addDynamicImport = addDynamicImport;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
66
|
+
const spacesAndTabs = /[ \t]{2,}/g;
|
|
67
|
+
const lineStartWithSpaces = /^[ \t]*/gm;
|
|
89
68
|
function clean(value) {
|
|
90
69
|
return value.replace(spacesAndTabs, ' ').replace(lineStartWithSpaces, '').trim();
|
|
91
70
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
71
|
+
const addCommentToStartOfFile = ({
|
|
72
|
+
j,
|
|
73
|
+
base,
|
|
74
|
+
message
|
|
75
|
+
}) => {
|
|
97
76
|
addCommentBefore(j, base.find(j.Program), message);
|
|
98
77
|
};
|
|
99
|
-
|
|
100
78
|
exports.addCommentToStartOfFile = addCommentToStartOfFile;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
var msg = clean(messagePrefix + message);
|
|
106
|
-
var content = commentType === 'block' ? " ".concat(msg, " ") : " ".concat(msg);
|
|
107
|
-
target.forEach(function (path) {
|
|
79
|
+
function addCommentBefore(j, target, message, commentType = 'block', messagePrefix = 'TODO: (from codemod) ') {
|
|
80
|
+
const msg = clean(messagePrefix + message);
|
|
81
|
+
const content = commentType === 'block' ? ` ${msg} ` : ` ${msg}`;
|
|
82
|
+
target.forEach(path => {
|
|
108
83
|
path.value.comments = path.value.comments || [];
|
|
109
|
-
|
|
110
|
-
return comment.value === content;
|
|
111
|
-
}); // avoiding duplicates of the same comment
|
|
84
|
+
const exists = path.value.comments.find(comment => comment.value === content);
|
|
112
85
|
|
|
86
|
+
// avoiding duplicates of the same comment
|
|
113
87
|
if (exists) {
|
|
114
88
|
return;
|
|
115
89
|
}
|
|
116
|
-
|
|
117
90
|
if (commentType === 'block') {
|
|
118
91
|
path.value.comments.push(j.commentBlock(content));
|
|
119
92
|
} else {
|
|
@@ -121,106 +94,78 @@ function addCommentBefore(j, target, message) {
|
|
|
121
94
|
}
|
|
122
95
|
});
|
|
123
96
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
var specifiers = source.find(j.ImportDeclaration).filter(function (path) {
|
|
127
|
-
return path.node.source.value === specifier;
|
|
128
|
-
}).find(j.ImportDefaultSpecifier);
|
|
129
|
-
|
|
97
|
+
const getDefaultSpecifier = (j, source, specifier) => {
|
|
98
|
+
const specifiers = source.find(j.ImportDeclaration).filter(path => path.node.source.value === specifier).find(j.ImportDefaultSpecifier);
|
|
130
99
|
if (!specifiers.length) {
|
|
131
100
|
return null;
|
|
132
101
|
}
|
|
133
|
-
|
|
134
102
|
return specifiers.nodes()[0].local.name;
|
|
135
|
-
};
|
|
136
|
-
|
|
103
|
+
};
|
|
137
104
|
|
|
105
|
+
// @ts-ignore
|
|
138
106
|
exports.getDefaultSpecifier = getDefaultSpecifier;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
var matches = j(attribute).find(j.JSXIdentifier).filter(function (identifier) {
|
|
143
|
-
return identifier.value.name === attributeName;
|
|
144
|
-
});
|
|
107
|
+
const getJSXAttributesByName = (j, element, attributeName) => {
|
|
108
|
+
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(attribute => {
|
|
109
|
+
const matches = j(attribute).find(j.JSXIdentifier).filter(identifier => identifier.value.name === attributeName);
|
|
145
110
|
return Boolean(matches.length);
|
|
146
111
|
});
|
|
147
112
|
};
|
|
148
|
-
|
|
149
113
|
exports.getJSXAttributesByName = getJSXAttributesByName;
|
|
150
|
-
|
|
151
|
-
var isEmpty = function isEmpty(string) {
|
|
152
|
-
return string && string.value !== '';
|
|
153
|
-
};
|
|
154
|
-
|
|
114
|
+
const isEmpty = string => string && string.value !== '';
|
|
155
115
|
exports.isEmpty = isEmpty;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
var imports = source.find(j.ImportDeclaration).filter(function (path) {
|
|
159
|
-
return path.node.source.value === importPath;
|
|
160
|
-
});
|
|
116
|
+
const hasImportDeclaration = (j, source, importPath) => {
|
|
117
|
+
const imports = source.find(j.ImportDeclaration).filter(path => path.node.source.value === importPath);
|
|
161
118
|
return Boolean(imports.length);
|
|
162
119
|
};
|
|
163
|
-
|
|
164
120
|
exports.hasImportDeclaration = hasImportDeclaration;
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
var imports = source.find(j.ImportDeclaration).filter(function (path) {
|
|
121
|
+
const hasImportDeclarationFromAnyPackageEntrypoint = (j, source, packageName) => {
|
|
122
|
+
const imports = source.find(j.ImportDeclaration).filter(path => {
|
|
168
123
|
var _path$node, _path$node$source;
|
|
169
|
-
|
|
170
124
|
return (// @ts-ignore
|
|
171
125
|
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)
|
|
172
126
|
);
|
|
173
127
|
});
|
|
174
128
|
return Boolean(imports.length);
|
|
175
129
|
};
|
|
176
|
-
|
|
177
130
|
exports.hasImportDeclarationFromAnyPackageEntrypoint = hasImportDeclarationFromAnyPackageEntrypoint;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
source.findJSXElements(defaultSpecifier).forEach(function (element) {
|
|
188
|
-
console.log(element); //eslint-disable-line no-console
|
|
189
|
-
});
|
|
190
|
-
};
|
|
131
|
+
const debug = component => (j, source) => {
|
|
132
|
+
const defaultSpecifier = getDefaultSpecifier(j, source, component);
|
|
133
|
+
if (!defaultSpecifier) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
source.findJSXElements(defaultSpecifier).forEach(element => {
|
|
137
|
+
console.log(element); //eslint-disable-line no-console
|
|
138
|
+
});
|
|
191
139
|
};
|
|
192
|
-
|
|
193
140
|
exports.debug = debug;
|
|
194
|
-
|
|
195
141
|
function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
196
142
|
if (placeholderStr === resolvedStr) {
|
|
197
143
|
return true;
|
|
198
144
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
partsPlaceholder.forEach(function (p, i) {
|
|
145
|
+
let value = '';
|
|
146
|
+
let offset = 0;
|
|
147
|
+
const partsPlaceholder = placeholderStr.split(' ');
|
|
148
|
+
const partsResolved = resolvedStr.split(' ');
|
|
149
|
+
partsPlaceholder.forEach((p, i) => {
|
|
205
150
|
// Placeholder
|
|
206
151
|
if (p.startsWith('%')) {
|
|
207
152
|
// Trim remaining words from current position to avoid premature matching from previous parts
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
153
|
+
const remainingWords = partsResolved.slice(i + offset);
|
|
154
|
+
const nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
155
|
+
const hasNextWord = nextWordIndex !== -1;
|
|
212
156
|
if (hasNextWord) {
|
|
213
157
|
offset += nextWordIndex - 1;
|
|
214
158
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
159
|
+
const insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
160
|
+
value += `${insert} `;
|
|
161
|
+
// Regular words
|
|
218
162
|
} else {
|
|
219
|
-
value +=
|
|
163
|
+
value += `${p} `;
|
|
220
164
|
}
|
|
221
165
|
});
|
|
222
166
|
return value.trimRight() === resolvedStr;
|
|
223
167
|
}
|
|
168
|
+
|
|
224
169
|
/**
|
|
225
170
|
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
226
171
|
*
|
|
@@ -232,11 +177,9 @@ function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
|
232
177
|
*
|
|
233
178
|
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
234
179
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
180
|
function matchesStringWithFormatSpecifier(argValue, str) {
|
|
238
|
-
|
|
239
|
-
|
|
181
|
+
const value = String(argValue);
|
|
182
|
+
// Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
240
183
|
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
241
184
|
return placeholderStringMatches(value, str);
|
|
242
185
|
} else {
|
|
@@ -244,16 +187,12 @@ function matchesStringWithFormatSpecifier(argValue, str) {
|
|
|
244
187
|
return false;
|
|
245
188
|
}
|
|
246
189
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
return quasi.value.raw.trim();
|
|
251
|
-
}).join('.*');
|
|
252
|
-
var regex = new RegExp(templateStrs);
|
|
190
|
+
const checkForTemplateLiteralsWithPlaceholders = (quasis, str) => {
|
|
191
|
+
const templateStrs = quasis.map(quasi => quasi.value.raw.trim()).join('.*');
|
|
192
|
+
let regex = new RegExp(templateStrs);
|
|
253
193
|
return regex.test(str);
|
|
254
194
|
};
|
|
255
|
-
|
|
256
|
-
var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg, str) {
|
|
195
|
+
const callExpressionArgMatchesString = (arg, str) => {
|
|
257
196
|
switch (arg.type) {
|
|
258
197
|
case 'StringLiteral':
|
|
259
198
|
case 'Literal':
|
|
@@ -265,14 +204,12 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
265
204
|
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
266
205
|
}
|
|
267
206
|
}
|
|
268
|
-
|
|
269
207
|
case 'TemplateLiteral':
|
|
270
208
|
{
|
|
271
209
|
// fuzzy match template literals, skipping expressions
|
|
272
|
-
|
|
210
|
+
const templateStrs = arg.quasis.map(quasi => {
|
|
273
211
|
return quasi.value.raw.trim();
|
|
274
212
|
}).join(' ');
|
|
275
|
-
|
|
276
213
|
if (str.trim() === templateStrs.trim()) {
|
|
277
214
|
return true;
|
|
278
215
|
} else {
|
|
@@ -280,129 +217,96 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
280
217
|
return checkForTemplateLiteralsWithPlaceholders(arg.quasis, str);
|
|
281
218
|
}
|
|
282
219
|
}
|
|
283
|
-
|
|
284
220
|
case 'BinaryExpression':
|
|
285
221
|
{
|
|
286
222
|
return false;
|
|
287
223
|
}
|
|
288
|
-
|
|
289
224
|
default:
|
|
290
225
|
{
|
|
291
226
|
return false;
|
|
292
227
|
}
|
|
293
228
|
}
|
|
294
229
|
};
|
|
295
|
-
|
|
296
230
|
exports.callExpressionArgMatchesString = callExpressionArgMatchesString;
|
|
297
|
-
|
|
298
|
-
var testMethodVariantEach = function testMethodVariantEach(path, testMethods) {
|
|
231
|
+
const testMethodVariantEach = (path, testMethods) => {
|
|
299
232
|
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;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
((_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' &&
|
|
304
|
-
|
|
233
|
+
const variants = new Set(['each']);
|
|
234
|
+
return (
|
|
235
|
+
// @ts-ignore
|
|
236
|
+
((_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' &&
|
|
237
|
+
// @ts-ignore
|
|
238
|
+
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) &&
|
|
239
|
+
// @ts-ignore
|
|
305
240
|
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)
|
|
306
241
|
);
|
|
307
242
|
};
|
|
308
|
-
|
|
309
243
|
exports.testMethodVariantEach = testMethodVariantEach;
|
|
310
|
-
|
|
311
|
-
var hasJSXAttributesByName = function hasJSXAttributesByName(j, element, attributeName) {
|
|
312
|
-
return getJSXAttributesByName(j, element, attributeName).length > 0;
|
|
313
|
-
};
|
|
314
|
-
|
|
244
|
+
const hasJSXAttributesByName = (j, element, attributeName) => getJSXAttributesByName(j, element, attributeName).length > 0;
|
|
315
245
|
exports.hasJSXAttributesByName = hasJSXAttributesByName;
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
return base.find(j.Identifier).filter(function (identifer) {
|
|
319
|
-
return identifer.value.name === name;
|
|
320
|
-
}).length > 0;
|
|
246
|
+
const doesIdentifierExist = (j, base, name) => {
|
|
247
|
+
return base.find(j.Identifier).filter(identifer => identifer.value.name === name).length > 0;
|
|
321
248
|
};
|
|
322
|
-
|
|
323
249
|
exports.doesIdentifierExist = doesIdentifierExist;
|
|
324
|
-
|
|
325
250
|
function removeImport(j, base, packageName) {
|
|
326
|
-
base.find(j.ImportDeclaration).filter(
|
|
327
|
-
return path.node.source.value === packageName;
|
|
328
|
-
}).remove();
|
|
251
|
+
base.find(j.ImportDeclaration).filter(path => path.node.source.value === packageName).remove();
|
|
329
252
|
}
|
|
330
|
-
|
|
331
253
|
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
});
|
|
335
|
-
var exists = matches.length > 0;
|
|
336
|
-
|
|
254
|
+
const matches = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName);
|
|
255
|
+
const exists = matches.length > 0;
|
|
337
256
|
if (exists) {
|
|
338
257
|
if (shouldBeTypeImport) {
|
|
339
258
|
// if the matched import declarations are not type imports
|
|
340
259
|
// but should be, we update them accordingly
|
|
341
|
-
|
|
342
|
-
return path.value.importKind === 'type';
|
|
343
|
-
});
|
|
344
|
-
|
|
260
|
+
const isTypeImports = matches.every(path => path.value.importKind === 'type');
|
|
345
261
|
if (!isTypeImports) {
|
|
346
|
-
matches.filter(
|
|
347
|
-
return path.value.importKind !== 'type';
|
|
348
|
-
}).forEach(function (path) {
|
|
262
|
+
matches.filter(path => path.value.importKind !== 'type').forEach(path => {
|
|
349
263
|
path.value.importKind = 'type';
|
|
350
264
|
});
|
|
351
265
|
return;
|
|
352
266
|
}
|
|
353
267
|
}
|
|
354
|
-
|
|
355
268
|
return;
|
|
356
|
-
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
357
272
|
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
358
273
|
// runtime exceptions during transform runs when importKind is undefined)
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
var importDeclarationArgs = [[], j.literal(packageName)];
|
|
362
|
-
|
|
274
|
+
const importDeclarationArgs = [[], j.literal(packageName)];
|
|
363
275
|
if (shouldBeTypeImport) {
|
|
364
276
|
importDeclarationArgs.push('type');
|
|
365
277
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
return path.value.source.value === relativeToPackage;
|
|
369
|
-
}) // we only take the first matching path, to avoid duplicate inserts
|
|
278
|
+
base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage)
|
|
279
|
+
// we only take the first matching path, to avoid duplicate inserts
|
|
370
280
|
// if the source contains the same import declaration more than once
|
|
371
|
-
.paths()[0].insertBefore(j.importDeclaration
|
|
281
|
+
.paths()[0].insertBefore(j.importDeclaration(...importDeclarationArgs));
|
|
372
282
|
}
|
|
373
|
-
|
|
374
283
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
375
|
-
base.find(j.ImportDeclaration).filter(
|
|
376
|
-
return
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
284
|
+
base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).at(0).replaceWith(declaration => {
|
|
285
|
+
return j.importDeclaration([
|
|
286
|
+
// we are appending to the existing specifiers
|
|
287
|
+
// We are doing a filter hear because sometimes specifiers can be removed
|
|
288
|
+
// but they hand around in the declaration
|
|
289
|
+
...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName), declaration.value.importKind);
|
|
381
290
|
});
|
|
382
291
|
}
|
|
383
|
-
|
|
384
|
-
var shiftDefaultImport = function shiftDefaultImport(j, base, defaultName, oldPackagePath, newPackagePath) {
|
|
292
|
+
const shiftDefaultImport = (j, base, defaultName, oldPackagePath, newPackagePath) => {
|
|
385
293
|
tryCreateImport(j, base, oldPackagePath, newPackagePath);
|
|
386
|
-
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
294
|
+
addToImport(j, base, j.importDefaultSpecifier(j.identifier(defaultName)), newPackagePath);
|
|
387
295
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}).remove();
|
|
296
|
+
// removing old default specifier
|
|
297
|
+
base.find(j.ImportDeclaration).filter(path => path.node.source.value === oldPackagePath).remove();
|
|
391
298
|
};
|
|
392
|
-
|
|
393
299
|
exports.shiftDefaultImport = shiftDefaultImport;
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
300
|
+
function getSafeImportName({
|
|
301
|
+
j,
|
|
302
|
+
base,
|
|
303
|
+
currentDefaultSpecifierName,
|
|
304
|
+
desiredName,
|
|
305
|
+
fallbackName
|
|
306
|
+
}) {
|
|
402
307
|
if (currentDefaultSpecifierName === desiredName) {
|
|
403
308
|
return desiredName;
|
|
404
309
|
}
|
|
405
|
-
|
|
406
|
-
var isUsed = doesIdentifierExist(j, base, desiredName);
|
|
310
|
+
const isUsed = doesIdentifierExist(j, base, desiredName);
|
|
407
311
|
return isUsed ? fallbackName : desiredName;
|
|
408
312
|
}
|
package/dist/cjs/version.json
CHANGED