@atlaskit/codemod-utils 4.0.1 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/cjs/utils/index.js +2 -2
- package/dist/cjs/utils/support.js +86 -15
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/utils/index.js +2 -2
- package/dist/es2019/utils/support.js +79 -15
- package/dist/es2019/version.json +1 -1
- package/dist/esm/utils/index.js +2 -2
- package/dist/esm/utils/support.js +84 -16
- package/dist/esm/version.json +1 -1
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/support.d.ts +14 -1
- package/package.json +1 -1
- package/report.api.md +225 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @atlaskit/codemod-utils
|
|
2
2
|
|
|
3
|
+
## 4.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`edc6fef0c8f`](https://bitbucket.org/atlassian/atlassian-frontend/commits/edc6fef0c8f) - Fix printf format specifier matching within `matchesStringWithFormatSpecifier` for fuzzy matching interpolated placeholder values
|
|
8
|
+
|
|
9
|
+
## 4.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [`e4624adcb2f`](https://bitbucket.org/atlassian/atlassian-frontend/commits/e4624adcb2f) - ED-14608: Update codemod-utils to support type import entry point changes in changeImportEntryPoint, remove adf-utils from repo stricter lint exclusion lists
|
|
14
|
+
|
|
15
|
+
## 4.0.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [`9358d42eeaa`](https://bitbucket.org/atlassian/atlassian-frontend/commits/9358d42eeaa) - NO-ISSUE: fixed codemod utils tryCreateImport() and addToImport() causing multiple insert in certain scenarios
|
|
20
|
+
|
|
3
21
|
## 4.0.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -275,7 +275,7 @@ var renameNamedImportWithAliasName = function renameNamedImportWithAliasName(com
|
|
|
275
275
|
|
|
276
276
|
exports.renameNamedImportWithAliasName = renameNamedImportWithAliasName;
|
|
277
277
|
|
|
278
|
-
var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName) {
|
|
278
|
+
var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) {
|
|
279
279
|
return function (j, root) {
|
|
280
280
|
root.find(j.ImportDeclaration, {
|
|
281
281
|
source: {
|
|
@@ -300,7 +300,7 @@ var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, imp
|
|
|
300
300
|
var localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
|
|
301
301
|
var newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined); // check if new import exists, if not create it
|
|
302
302
|
|
|
303
|
-
(0, _support.tryCreateImport)(j, root, oldPackageName, newPackageName); // remove the old import specifier, but NOT the whole package
|
|
303
|
+
(0, _support.tryCreateImport)(j, root, oldPackageName, newPackageName, shouldBeTypeImport); // remove the old import specifier, but NOT the whole package
|
|
304
304
|
|
|
305
305
|
root.find(j.ImportDeclaration, {
|
|
306
306
|
source: {
|
|
@@ -14,6 +14,8 @@ exports.getJSXAttributesByName = exports.getDynamicImportName = exports.getDefau
|
|
|
14
14
|
exports.getNamedSpecifier = getNamedSpecifier;
|
|
15
15
|
exports.getSafeImportName = getSafeImportName;
|
|
16
16
|
exports.isEmpty = exports.hasJSXAttributesByName = exports.hasImportDeclarationFromAnyPackageEntrypoint = exports.hasImportDeclaration = void 0;
|
|
17
|
+
exports.matchesStringWithFormatSpecifier = matchesStringWithFormatSpecifier;
|
|
18
|
+
exports.placeholderStringMatches = placeholderStringMatches;
|
|
17
19
|
exports.removeImport = removeImport;
|
|
18
20
|
exports.testMethodVariantEach = exports.shiftDefaultImport = void 0;
|
|
19
21
|
exports.tryCreateImport = tryCreateImport;
|
|
@@ -190,18 +192,58 @@ var debug = function debug(component) {
|
|
|
190
192
|
|
|
191
193
|
exports.debug = debug;
|
|
192
194
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
196
|
+
if (placeholderStr === resolvedStr) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var value = '';
|
|
201
|
+
var offset = 0;
|
|
202
|
+
var partsPlaceholder = placeholderStr.split(' ');
|
|
203
|
+
var partsResolved = resolvedStr.split(' ');
|
|
204
|
+
partsPlaceholder.forEach(function (p, i) {
|
|
205
|
+
// Placeholder
|
|
206
|
+
if (p.startsWith('%')) {
|
|
207
|
+
// Trim remaining words from current position to avoid premature matching from previous parts
|
|
208
|
+
var remainingWords = partsResolved.slice(i + offset);
|
|
209
|
+
var nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
210
|
+
var hasNextWord = nextWordIndex !== -1;
|
|
211
|
+
|
|
212
|
+
if (hasNextWord) {
|
|
213
|
+
offset += nextWordIndex - 1;
|
|
214
|
+
}
|
|
196
215
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
216
|
+
var insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
217
|
+
value += "".concat(insert, " "); // Regular words
|
|
218
|
+
} else {
|
|
219
|
+
value += "".concat(p, " ");
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
return value.trimRight() === resolvedStr;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
226
|
+
*
|
|
227
|
+
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout
|
|
228
|
+
* @see https://nodejs.org/api/util.html#utilformatformat-args
|
|
229
|
+
*
|
|
230
|
+
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s')
|
|
231
|
+
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`)
|
|
232
|
+
*
|
|
233
|
+
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
function matchesStringWithFormatSpecifier(argValue, str) {
|
|
238
|
+
var value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
239
|
+
|
|
240
|
+
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
241
|
+
return placeholderStringMatches(value, str);
|
|
201
242
|
} else {
|
|
243
|
+
// No format specifier placeholder
|
|
202
244
|
return false;
|
|
203
245
|
}
|
|
204
|
-
}
|
|
246
|
+
}
|
|
205
247
|
|
|
206
248
|
var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiteralsWithPlaceholders(quasis, str) {
|
|
207
249
|
var templateStrs = quasis.map(function (quasi) {
|
|
@@ -220,7 +262,7 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
220
262
|
return true;
|
|
221
263
|
} else {
|
|
222
264
|
// Eg: 'should contain %s'
|
|
223
|
-
return
|
|
265
|
+
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
224
266
|
}
|
|
225
267
|
}
|
|
226
268
|
|
|
@@ -286,27 +328,56 @@ function removeImport(j, base, packageName) {
|
|
|
286
328
|
}).remove();
|
|
287
329
|
}
|
|
288
330
|
|
|
289
|
-
function tryCreateImport(j, base, relativeToPackage, packageName) {
|
|
290
|
-
var
|
|
331
|
+
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
332
|
+
var matches = base.find(j.ImportDeclaration).filter(function (path) {
|
|
291
333
|
return path.value.source.value === packageName;
|
|
292
|
-
})
|
|
334
|
+
});
|
|
335
|
+
var exists = matches.length > 0;
|
|
293
336
|
|
|
294
337
|
if (exists) {
|
|
338
|
+
if (shouldBeTypeImport) {
|
|
339
|
+
// if the matched import declarations are not type imports
|
|
340
|
+
// but should be, we update them accordingly
|
|
341
|
+
var isTypeImports = matches.every(function (path) {
|
|
342
|
+
return path.value.importKind === 'type';
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
if (!isTypeImports) {
|
|
346
|
+
matches.filter(function (path) {
|
|
347
|
+
return path.value.importKind !== 'type';
|
|
348
|
+
}).forEach(function (path) {
|
|
349
|
+
path.value.importKind = 'type';
|
|
350
|
+
});
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
295
355
|
return;
|
|
356
|
+
} // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
357
|
+
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
358
|
+
// runtime exceptions during transform runs when importKind is undefined)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
var importDeclarationArgs = [[], j.literal(packageName)];
|
|
362
|
+
|
|
363
|
+
if (shouldBeTypeImport) {
|
|
364
|
+
importDeclarationArgs.push('type');
|
|
296
365
|
}
|
|
297
366
|
|
|
298
367
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
299
368
|
return path.value.source.value === relativeToPackage;
|
|
300
|
-
})
|
|
369
|
+
}) // we only take the first matching path, to avoid duplicate inserts
|
|
370
|
+
// if the source contains the same import declaration more than once
|
|
371
|
+
.paths()[0].insertBefore(j.importDeclaration.apply(j, importDeclarationArgs));
|
|
301
372
|
}
|
|
302
373
|
|
|
303
374
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
304
375
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
305
376
|
return path.value.source.value === packageName;
|
|
306
|
-
}).replaceWith(function (declaration) {
|
|
377
|
+
}).at(0).replaceWith(function (declaration) {
|
|
307
378
|
return j.importDeclaration([].concat((0, _toConsumableArray2.default)((declaration.value.specifiers || []).filter(function (item) {
|
|
308
379
|
return item.type === 'ImportSpecifier' && item.imported != null;
|
|
309
|
-
})), [importSpecifier]), j.literal(packageName));
|
|
380
|
+
})), [importSpecifier]), j.literal(packageName), declaration.value.importKind);
|
|
310
381
|
});
|
|
311
382
|
}
|
|
312
383
|
|
package/dist/cjs/version.json
CHANGED
|
@@ -200,7 +200,7 @@ const renameNamedImportWithAliasName = (component, from, to) => (j, source) => {
|
|
|
200
200
|
});
|
|
201
201
|
};
|
|
202
202
|
|
|
203
|
-
const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName) => (j, root) => {
|
|
203
|
+
const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) => (j, root) => {
|
|
204
204
|
root.find(j.ImportDeclaration, {
|
|
205
205
|
source: {
|
|
206
206
|
value: oldPackageName
|
|
@@ -224,7 +224,7 @@ const changeImportEntryPoint = (oldPackageName, importToConvert, newPackageName)
|
|
|
224
224
|
const localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
|
|
225
225
|
const newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined); // check if new import exists, if not create it
|
|
226
226
|
|
|
227
|
-
tryCreateImport(j, root, oldPackageName, newPackageName); // remove the old import specifier, but NOT the whole package
|
|
227
|
+
tryCreateImport(j, root, oldPackageName, newPackageName, shouldBeTypeImport); // remove the old import specifier, but NOT the whole package
|
|
228
228
|
|
|
229
229
|
root.find(j.ImportDeclaration, {
|
|
230
230
|
source: {
|
|
@@ -135,18 +135,57 @@ const debug = component => (j, source) => {
|
|
|
135
135
|
});
|
|
136
136
|
};
|
|
137
137
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
138
|
+
export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
139
|
+
if (placeholderStr === resolvedStr) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let value = '';
|
|
144
|
+
let offset = 0;
|
|
145
|
+
const partsPlaceholder = placeholderStr.split(' ');
|
|
146
|
+
const partsResolved = resolvedStr.split(' ');
|
|
147
|
+
partsPlaceholder.forEach((p, i) => {
|
|
148
|
+
// Placeholder
|
|
149
|
+
if (p.startsWith('%')) {
|
|
150
|
+
// Trim remaining words from current position to avoid premature matching from previous parts
|
|
151
|
+
const remainingWords = partsResolved.slice(i + offset);
|
|
152
|
+
const nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
153
|
+
const hasNextWord = nextWordIndex !== -1;
|
|
154
|
+
|
|
155
|
+
if (hasNextWord) {
|
|
156
|
+
offset += nextWordIndex - 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
160
|
+
value += `${insert} `; // Regular words
|
|
161
|
+
} else {
|
|
162
|
+
value += `${p} `;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return value.trimRight() === resolvedStr;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
169
|
+
*
|
|
170
|
+
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout
|
|
171
|
+
* @see https://nodejs.org/api/util.html#utilformatformat-args
|
|
172
|
+
*
|
|
173
|
+
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s')
|
|
174
|
+
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`)
|
|
175
|
+
*
|
|
176
|
+
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
180
|
+
const value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
181
|
+
|
|
182
|
+
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
183
|
+
return placeholderStringMatches(value, str);
|
|
146
184
|
} else {
|
|
185
|
+
// No format specifier placeholder
|
|
147
186
|
return false;
|
|
148
187
|
}
|
|
149
|
-
}
|
|
188
|
+
}
|
|
150
189
|
|
|
151
190
|
const checkForTemplateLiteralsWithPlaceholders = (quasis, str) => {
|
|
152
191
|
const templateStrs = quasis.map(quasi => quasi.value.raw.trim()).join('.*');
|
|
@@ -163,7 +202,7 @@ const callExpressionArgMatchesString = (arg, str) => {
|
|
|
163
202
|
return true;
|
|
164
203
|
} else {
|
|
165
204
|
// Eg: 'should contain %s'
|
|
166
|
-
return
|
|
205
|
+
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
167
206
|
}
|
|
168
207
|
}
|
|
169
208
|
|
|
@@ -215,22 +254,47 @@ function removeImport(j, base, packageName) {
|
|
|
215
254
|
base.find(j.ImportDeclaration).filter(path => path.node.source.value === packageName).remove();
|
|
216
255
|
}
|
|
217
256
|
|
|
218
|
-
function tryCreateImport(j, base, relativeToPackage, packageName) {
|
|
219
|
-
const
|
|
257
|
+
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
258
|
+
const matches = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName);
|
|
259
|
+
const exists = matches.length > 0;
|
|
220
260
|
|
|
221
261
|
if (exists) {
|
|
262
|
+
if (shouldBeTypeImport) {
|
|
263
|
+
// if the matched import declarations are not type imports
|
|
264
|
+
// but should be, we update them accordingly
|
|
265
|
+
const isTypeImports = matches.every(path => path.value.importKind === 'type');
|
|
266
|
+
|
|
267
|
+
if (!isTypeImports) {
|
|
268
|
+
matches.filter(path => path.value.importKind !== 'type').forEach(path => {
|
|
269
|
+
path.value.importKind = 'type';
|
|
270
|
+
});
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
222
275
|
return;
|
|
276
|
+
} // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
277
|
+
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
278
|
+
// runtime exceptions during transform runs when importKind is undefined)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
const importDeclarationArgs = [[], j.literal(packageName)];
|
|
282
|
+
|
|
283
|
+
if (shouldBeTypeImport) {
|
|
284
|
+
importDeclarationArgs.push('type');
|
|
223
285
|
}
|
|
224
286
|
|
|
225
|
-
base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage)
|
|
287
|
+
base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage) // we only take the first matching path, to avoid duplicate inserts
|
|
288
|
+
// if the source contains the same import declaration more than once
|
|
289
|
+
.paths()[0].insertBefore(j.importDeclaration(...importDeclarationArgs));
|
|
226
290
|
}
|
|
227
291
|
|
|
228
292
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
229
|
-
base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).replaceWith(declaration => {
|
|
293
|
+
base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).at(0).replaceWith(declaration => {
|
|
230
294
|
return j.importDeclaration([// we are appending to the existing specifiers
|
|
231
295
|
// We are doing a filter hear because sometimes specifiers can be removed
|
|
232
296
|
// but they hand around in the declaration
|
|
233
|
-
...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName));
|
|
297
|
+
...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName), declaration.value.importKind);
|
|
234
298
|
});
|
|
235
299
|
}
|
|
236
300
|
|
package/dist/es2019/version.json
CHANGED
package/dist/esm/utils/index.js
CHANGED
|
@@ -246,7 +246,7 @@ var renameNamedImportWithAliasName = function renameNamedImportWithAliasName(com
|
|
|
246
246
|
};
|
|
247
247
|
};
|
|
248
248
|
|
|
249
|
-
var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName) {
|
|
249
|
+
var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, importToConvert, newPackageName, shouldBeTypeImport) {
|
|
250
250
|
return function (j, root) {
|
|
251
251
|
root.find(j.ImportDeclaration, {
|
|
252
252
|
source: {
|
|
@@ -271,7 +271,7 @@ var changeImportEntryPoint = function changeImportEntryPoint(oldPackageName, imp
|
|
|
271
271
|
var localSpecifierName = (_currentImportSpecifi = currentImportSpecifier.local) === null || _currentImportSpecifi === void 0 ? void 0 : _currentImportSpecifi.name;
|
|
272
272
|
var newIdentifier = j.importSpecifier(j.identifier(importedSpecifierName), localSpecifierName ? j.identifier(localSpecifierName) : undefined); // check if new import exists, if not create it
|
|
273
273
|
|
|
274
|
-
tryCreateImport(j, root, oldPackageName, newPackageName); // remove the old import specifier, but NOT the whole package
|
|
274
|
+
tryCreateImport(j, root, oldPackageName, newPackageName, shouldBeTypeImport); // remove the old import specifier, but NOT the whole package
|
|
275
275
|
|
|
276
276
|
root.find(j.ImportDeclaration, {
|
|
277
277
|
source: {
|
|
@@ -153,18 +153,57 @@ var debug = function debug(component) {
|
|
|
153
153
|
};
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
156
|
+
export function placeholderStringMatches(placeholderStr, resolvedStr) {
|
|
157
|
+
if (placeholderStr === resolvedStr) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
var value = '';
|
|
162
|
+
var offset = 0;
|
|
163
|
+
var partsPlaceholder = placeholderStr.split(' ');
|
|
164
|
+
var partsResolved = resolvedStr.split(' ');
|
|
165
|
+
partsPlaceholder.forEach(function (p, i) {
|
|
166
|
+
// Placeholder
|
|
167
|
+
if (p.startsWith('%')) {
|
|
168
|
+
// Trim remaining words from current position to avoid premature matching from previous parts
|
|
169
|
+
var remainingWords = partsResolved.slice(i + offset);
|
|
170
|
+
var nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]);
|
|
171
|
+
var hasNextWord = nextWordIndex !== -1;
|
|
172
|
+
|
|
173
|
+
if (hasNextWord) {
|
|
174
|
+
offset += nextWordIndex - 1;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
var insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' ');
|
|
178
|
+
value += "".concat(insert, " "); // Regular words
|
|
179
|
+
} else {
|
|
180
|
+
value += "".concat(p, " ");
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return value.trimRight() === resolvedStr;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
187
|
+
*
|
|
188
|
+
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout
|
|
189
|
+
* @see https://nodejs.org/api/util.html#utilformatformat-args
|
|
190
|
+
*
|
|
191
|
+
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s')
|
|
192
|
+
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`)
|
|
193
|
+
*
|
|
194
|
+
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
export function matchesStringWithFormatSpecifier(argValue, str) {
|
|
198
|
+
var value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc
|
|
199
|
+
|
|
200
|
+
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) {
|
|
201
|
+
return placeholderStringMatches(value, str);
|
|
164
202
|
} else {
|
|
203
|
+
// No format specifier placeholder
|
|
165
204
|
return false;
|
|
166
205
|
}
|
|
167
|
-
}
|
|
206
|
+
}
|
|
168
207
|
|
|
169
208
|
var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiteralsWithPlaceholders(quasis, str) {
|
|
170
209
|
var templateStrs = quasis.map(function (quasi) {
|
|
@@ -183,7 +222,7 @@ var callExpressionArgMatchesString = function callExpressionArgMatchesString(arg
|
|
|
183
222
|
return true;
|
|
184
223
|
} else {
|
|
185
224
|
// Eg: 'should contain %s'
|
|
186
|
-
return
|
|
225
|
+
return matchesStringWithFormatSpecifier(arg.value, str);
|
|
187
226
|
}
|
|
188
227
|
}
|
|
189
228
|
|
|
@@ -241,27 +280,56 @@ function removeImport(j, base, packageName) {
|
|
|
241
280
|
}).remove();
|
|
242
281
|
}
|
|
243
282
|
|
|
244
|
-
function tryCreateImport(j, base, relativeToPackage, packageName) {
|
|
245
|
-
var
|
|
283
|
+
function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
|
|
284
|
+
var matches = base.find(j.ImportDeclaration).filter(function (path) {
|
|
246
285
|
return path.value.source.value === packageName;
|
|
247
|
-
})
|
|
286
|
+
});
|
|
287
|
+
var exists = matches.length > 0;
|
|
248
288
|
|
|
249
289
|
if (exists) {
|
|
290
|
+
if (shouldBeTypeImport) {
|
|
291
|
+
// if the matched import declarations are not type imports
|
|
292
|
+
// but should be, we update them accordingly
|
|
293
|
+
var isTypeImports = matches.every(function (path) {
|
|
294
|
+
return path.value.importKind === 'type';
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
if (!isTypeImports) {
|
|
298
|
+
matches.filter(function (path) {
|
|
299
|
+
return path.value.importKind !== 'type';
|
|
300
|
+
}).forEach(function (path) {
|
|
301
|
+
path.value.importKind = 'type';
|
|
302
|
+
});
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
250
307
|
return;
|
|
308
|
+
} // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
|
|
309
|
+
// is falsy, it is never passed explicitly as an importKind parameter (avoiding
|
|
310
|
+
// runtime exceptions during transform runs when importKind is undefined)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
var importDeclarationArgs = [[], j.literal(packageName)];
|
|
314
|
+
|
|
315
|
+
if (shouldBeTypeImport) {
|
|
316
|
+
importDeclarationArgs.push('type');
|
|
251
317
|
}
|
|
252
318
|
|
|
253
319
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
254
320
|
return path.value.source.value === relativeToPackage;
|
|
255
|
-
})
|
|
321
|
+
}) // we only take the first matching path, to avoid duplicate inserts
|
|
322
|
+
// if the source contains the same import declaration more than once
|
|
323
|
+
.paths()[0].insertBefore(j.importDeclaration.apply(j, importDeclarationArgs));
|
|
256
324
|
}
|
|
257
325
|
|
|
258
326
|
function addToImport(j, base, importSpecifier, packageName) {
|
|
259
327
|
base.find(j.ImportDeclaration).filter(function (path) {
|
|
260
328
|
return path.value.source.value === packageName;
|
|
261
|
-
}).replaceWith(function (declaration) {
|
|
329
|
+
}).at(0).replaceWith(function (declaration) {
|
|
262
330
|
return j.importDeclaration([].concat(_toConsumableArray((declaration.value.specifiers || []).filter(function (item) {
|
|
263
331
|
return item.type === 'ImportSpecifier' && item.imported != null;
|
|
264
|
-
})), [importSpecifier]), j.literal(packageName));
|
|
332
|
+
})), [importSpecifier]), j.literal(packageName), declaration.value.importKind);
|
|
265
333
|
});
|
|
266
334
|
}
|
|
267
335
|
|
package/dist/esm/version.json
CHANGED
|
@@ -11,5 +11,5 @@ declare const flattenCertainChildPropsAsProp: (component: string, propName: stri
|
|
|
11
11
|
declare const createRenameJSXFunc: (packagePath: string, from: string, to: string, fallback?: string | undefined) => (j: core.JSCodeshift, source: any) => void;
|
|
12
12
|
declare const createRemoveFuncAddCommentFor: (component: string, prop: string, comment?: string | undefined) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
13
13
|
declare const renameNamedImportWithAliasName: (component: string, from: string, to: string) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
14
|
-
declare const changeImportEntryPoint: (oldPackageName: string, importToConvert: string, newPackageName: string) => (j: core.JSCodeshift, root: Collection<Node>) => void;
|
|
14
|
+
declare const changeImportEntryPoint: (oldPackageName: string, importToConvert: string, newPackageName: string, shouldBeTypeImport?: boolean | undefined) => (j: core.JSCodeshift, root: Collection<Node>) => void;
|
|
15
15
|
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor, changeImportEntryPoint, };
|
|
@@ -16,12 +16,25 @@ declare const isEmpty: any;
|
|
|
16
16
|
declare const hasImportDeclaration: (j: core.JSCodeshift, source: Collection<Node>, importPath: string) => boolean;
|
|
17
17
|
declare const hasImportDeclarationFromAnyPackageEntrypoint: (j: core.JSCodeshift, source: Collection<Node>, packageName: string) => boolean;
|
|
18
18
|
declare const debug: (component: string) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
19
|
+
export declare function placeholderStringMatches(placeholderStr: string, resolvedStr: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Check whether a value contains a Format Specifier (printf placeholder)
|
|
22
|
+
*
|
|
23
|
+
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout
|
|
24
|
+
* @see https://nodejs.org/api/util.html#utilformatformat-args
|
|
25
|
+
*
|
|
26
|
+
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s')
|
|
27
|
+
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`)
|
|
28
|
+
*
|
|
29
|
+
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not.
|
|
30
|
+
*/
|
|
31
|
+
export declare function matchesStringWithFormatSpecifier(argValue: string | number | boolean | RegExp | null, str: string): boolean;
|
|
19
32
|
declare const callExpressionArgMatchesString: (arg: CallExpression['arguments'][number], str: string) => boolean;
|
|
20
33
|
declare const testMethodVariantEach: (path: ASTPath<CallExpression>, testMethods: Set<string>) => boolean;
|
|
21
34
|
declare const hasJSXAttributesByName: (j: core.JSCodeshift, element: ASTPath<any>, attributeName: string) => boolean;
|
|
22
35
|
declare const doesIdentifierExist: (j: core.JSCodeshift, base: Collection<any>, name: string) => boolean;
|
|
23
36
|
declare function removeImport(j: core.JSCodeshift, base: Collection<any>, packageName: string): void;
|
|
24
|
-
declare function tryCreateImport(j: core.JSCodeshift, base: Collection<any>, relativeToPackage: string, packageName: string): void;
|
|
37
|
+
declare function tryCreateImport(j: core.JSCodeshift, base: Collection<any>, relativeToPackage: string, packageName: string, shouldBeTypeImport?: boolean): void;
|
|
25
38
|
declare function addToImport(j: core.JSCodeshift, base: Collection<any>, importSpecifier: ImportSpecifier | ImportDefaultSpecifier, packageName: string): void;
|
|
26
39
|
declare const shiftDefaultImport: (j: core.JSCodeshift, base: Collection<any>, defaultName: string, oldPackagePath: string, newPackagePath: string) => void;
|
|
27
40
|
declare function getSafeImportName({ j, base, currentDefaultSpecifierName, desiredName, fallbackName, }: {
|
package/package.json
CHANGED
package/report.api.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
## API Report File for "@atlaskit/codemod-utils"
|
|
2
|
+
|
|
3
|
+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { API } from 'jscodeshift';
|
|
7
|
+
import { ASTPath } from 'jscodeshift';
|
|
8
|
+
import { CallExpression } from 'jscodeshift';
|
|
9
|
+
import { Collection } from 'jscodeshift/src/Collection';
|
|
10
|
+
import core from 'jscodeshift';
|
|
11
|
+
import { FileInfo } from 'jscodeshift';
|
|
12
|
+
import { ImportDeclaration } from 'jscodeshift';
|
|
13
|
+
import { ImportDefaultSpecifier } from 'jscodeshift';
|
|
14
|
+
import { ImportSpecifier } from 'jscodeshift';
|
|
15
|
+
import { JSXAttribute } from 'jscodeshift';
|
|
16
|
+
import { JSXElement } from 'jscodeshift';
|
|
17
|
+
import { Options } from 'jscodeshift';
|
|
18
|
+
import { Program } from 'jscodeshift';
|
|
19
|
+
import { VariableDeclaration } from 'jscodeshift';
|
|
20
|
+
import { VariableDeclarator } from 'jscodeshift';
|
|
21
|
+
|
|
22
|
+
export declare function addCommentBefore(
|
|
23
|
+
j: core.JSCodeshift,
|
|
24
|
+
target:
|
|
25
|
+
| Collection<Program>
|
|
26
|
+
| Collection<ImportDeclaration>
|
|
27
|
+
| Collection<JSXElement>
|
|
28
|
+
| Collection<JSXAttribute>
|
|
29
|
+
| Collection<CallExpression>
|
|
30
|
+
| Collection<VariableDeclarator>,
|
|
31
|
+
message: string,
|
|
32
|
+
commentType?: 'block' | 'line',
|
|
33
|
+
messagePrefix?: string,
|
|
34
|
+
): void;
|
|
35
|
+
|
|
36
|
+
export declare const addCommentToStartOfFile: ({
|
|
37
|
+
j,
|
|
38
|
+
base,
|
|
39
|
+
message,
|
|
40
|
+
}: {
|
|
41
|
+
j: core.JSCodeshift;
|
|
42
|
+
base: Collection<Node>;
|
|
43
|
+
message: string;
|
|
44
|
+
}) => void;
|
|
45
|
+
|
|
46
|
+
export declare const addDynamicImport: (
|
|
47
|
+
j: core.JSCodeshift,
|
|
48
|
+
target: Collection<VariableDeclaration>,
|
|
49
|
+
name: string,
|
|
50
|
+
packageEndpoint: string,
|
|
51
|
+
) => void;
|
|
52
|
+
|
|
53
|
+
export declare function addToImport(
|
|
54
|
+
j: core.JSCodeshift,
|
|
55
|
+
base: Collection<any>,
|
|
56
|
+
importSpecifier: ImportSpecifier | ImportDefaultSpecifier,
|
|
57
|
+
packageName: string,
|
|
58
|
+
): void;
|
|
59
|
+
|
|
60
|
+
export declare const callExpressionArgMatchesString: (
|
|
61
|
+
arg: CallExpression['arguments'][number],
|
|
62
|
+
str: string,
|
|
63
|
+
) => boolean;
|
|
64
|
+
|
|
65
|
+
export declare const changeImportEntryPoint: (
|
|
66
|
+
oldPackageName: string,
|
|
67
|
+
importToConvert: string,
|
|
68
|
+
newPackageName: string,
|
|
69
|
+
shouldBeTypeImport?: boolean | undefined,
|
|
70
|
+
) => (j: core.JSCodeshift, root: Collection<Node>) => void;
|
|
71
|
+
|
|
72
|
+
export declare const createConvertFuncFor: (
|
|
73
|
+
component: string,
|
|
74
|
+
from: string,
|
|
75
|
+
to: string,
|
|
76
|
+
predicate?: ((value: any) => boolean) | undefined,
|
|
77
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
78
|
+
|
|
79
|
+
export declare const createRemoveFuncAddCommentFor: (
|
|
80
|
+
component: string,
|
|
81
|
+
prop: string,
|
|
82
|
+
comment?: string | undefined,
|
|
83
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
84
|
+
|
|
85
|
+
export declare const createRemoveFuncFor: (
|
|
86
|
+
component: string,
|
|
87
|
+
importName: string,
|
|
88
|
+
prop: string,
|
|
89
|
+
predicate?: (j: core.JSCodeshift, element: ASTPath<any>) => boolean,
|
|
90
|
+
comment?: string | undefined,
|
|
91
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
92
|
+
|
|
93
|
+
export declare const createRenameFuncFor: (
|
|
94
|
+
component: string,
|
|
95
|
+
from: string,
|
|
96
|
+
to: string,
|
|
97
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
98
|
+
|
|
99
|
+
export declare const createRenameImportFor: (
|
|
100
|
+
component: string,
|
|
101
|
+
from: string,
|
|
102
|
+
to: string,
|
|
103
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
104
|
+
|
|
105
|
+
export declare const createRenameJSXFunc: (
|
|
106
|
+
packagePath: string,
|
|
107
|
+
from: string,
|
|
108
|
+
to: string,
|
|
109
|
+
fallback?: string | undefined,
|
|
110
|
+
) => (j: core.JSCodeshift, source: any) => void;
|
|
111
|
+
|
|
112
|
+
export declare const createTransformer: (
|
|
113
|
+
migrates: ((j: core.JSCodeshift, source: Collection<Node>) => void)[],
|
|
114
|
+
shouldApplyTransform?:
|
|
115
|
+
| ((j: core.JSCodeshift, source: Collection<Node>) => boolean)
|
|
116
|
+
| undefined,
|
|
117
|
+
) => (fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) => string;
|
|
118
|
+
|
|
119
|
+
export declare const doesIdentifierExist: (
|
|
120
|
+
j: core.JSCodeshift,
|
|
121
|
+
base: Collection<any>,
|
|
122
|
+
name: string,
|
|
123
|
+
) => boolean;
|
|
124
|
+
|
|
125
|
+
export declare const elevateComponentToNewEntryPoint: (
|
|
126
|
+
pkg: string,
|
|
127
|
+
toPkg: string,
|
|
128
|
+
innerElementName: string,
|
|
129
|
+
) => (j: core.JSCodeshift, root: any) => void;
|
|
130
|
+
|
|
131
|
+
export declare const flattenCertainChildPropsAsProp: (
|
|
132
|
+
component: string,
|
|
133
|
+
propName: string,
|
|
134
|
+
childProps: string[],
|
|
135
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
136
|
+
|
|
137
|
+
export declare const getDefaultSpecifier: (
|
|
138
|
+
j: core.JSCodeshift,
|
|
139
|
+
source: Collection<Node>,
|
|
140
|
+
specifier: string,
|
|
141
|
+
) => string | null;
|
|
142
|
+
|
|
143
|
+
export declare const getDynamicImportName: (
|
|
144
|
+
j: core.JSCodeshift,
|
|
145
|
+
source: Collection<any>,
|
|
146
|
+
importPath: string,
|
|
147
|
+
) => string | null;
|
|
148
|
+
|
|
149
|
+
export declare const getJSXAttributesByName: (
|
|
150
|
+
j: core.JSCodeshift,
|
|
151
|
+
element: ASTPath<any>,
|
|
152
|
+
attributeName: string,
|
|
153
|
+
) => Collection<JSXAttribute>;
|
|
154
|
+
|
|
155
|
+
export declare function getNamedSpecifier(
|
|
156
|
+
j: core.JSCodeshift,
|
|
157
|
+
source: Collection<Node>,
|
|
158
|
+
specifier: string,
|
|
159
|
+
importName: string,
|
|
160
|
+
): string | null;
|
|
161
|
+
|
|
162
|
+
export declare function getSafeImportName({
|
|
163
|
+
j,
|
|
164
|
+
base,
|
|
165
|
+
currentDefaultSpecifierName,
|
|
166
|
+
desiredName,
|
|
167
|
+
fallbackName,
|
|
168
|
+
}: {
|
|
169
|
+
j: core.JSCodeshift;
|
|
170
|
+
base: Collection<any>;
|
|
171
|
+
currentDefaultSpecifierName: string | null;
|
|
172
|
+
desiredName: string;
|
|
173
|
+
fallbackName: string;
|
|
174
|
+
}): string;
|
|
175
|
+
|
|
176
|
+
export declare const hasImportDeclaration: (
|
|
177
|
+
j: core.JSCodeshift,
|
|
178
|
+
source: Collection<Node>,
|
|
179
|
+
importPath: string,
|
|
180
|
+
) => boolean;
|
|
181
|
+
|
|
182
|
+
export declare const hasImportDeclarationFromAnyPackageEntrypoint: (
|
|
183
|
+
j: core.JSCodeshift,
|
|
184
|
+
source: Collection<Node>,
|
|
185
|
+
packageName: string,
|
|
186
|
+
) => boolean;
|
|
187
|
+
|
|
188
|
+
export declare const hasJSXAttributesByName: (
|
|
189
|
+
j: core.JSCodeshift,
|
|
190
|
+
element: ASTPath<any>,
|
|
191
|
+
attributeName: string,
|
|
192
|
+
) => boolean;
|
|
193
|
+
|
|
194
|
+
export declare function removeImport(
|
|
195
|
+
j: core.JSCodeshift,
|
|
196
|
+
base: Collection<any>,
|
|
197
|
+
packageName: string,
|
|
198
|
+
): void;
|
|
199
|
+
|
|
200
|
+
export declare const renameNamedImportWithAliasName: (
|
|
201
|
+
component: string,
|
|
202
|
+
from: string,
|
|
203
|
+
to: string,
|
|
204
|
+
) => (j: core.JSCodeshift, source: Collection<Node>) => void;
|
|
205
|
+
|
|
206
|
+
export declare const replaceImportStatementFor: (
|
|
207
|
+
pkg: string,
|
|
208
|
+
convertMap: any,
|
|
209
|
+
) => (j: core.JSCodeshift, root: Collection<Node>) => void;
|
|
210
|
+
|
|
211
|
+
export declare const testMethodVariantEach: (
|
|
212
|
+
path: ASTPath<CallExpression>,
|
|
213
|
+
testMethods: Set<string>,
|
|
214
|
+
) => boolean;
|
|
215
|
+
|
|
216
|
+
export declare function tryCreateImport(
|
|
217
|
+
j: core.JSCodeshift,
|
|
218
|
+
base: Collection<any>,
|
|
219
|
+
relativeToPackage: string,
|
|
220
|
+
packageName: string,
|
|
221
|
+
shouldBeTypeImport?: boolean,
|
|
222
|
+
): void;
|
|
223
|
+
|
|
224
|
+
export {};
|
|
225
|
+
```
|