@atlaskit/codemod-utils 4.0.2 → 4.1.2

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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # @atlaskit/codemod-utils
2
2
 
3
+ ## 4.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [`8d4228767b0`](https://bitbucket.org/atlassian/atlassian-frontend/commits/8d4228767b0) - Upgrade Typescript from `4.2.4` to `4.3.5`.
8
+
9
+ ## 4.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [`edc6fef0c8f`](https://bitbucket.org/atlassian/atlassian-frontend/commits/edc6fef0c8f) - Fix printf format specifier matching within `matchesStringWithFormatSpecifier` for fuzzy matching interpolated placeholder values
14
+
15
+ ## 4.1.0
16
+
17
+ ### Minor Changes
18
+
19
+ - [`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
20
+
3
21
  ## 4.0.2
4
22
 
5
23
  ### Patch Changes
@@ -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
- var checkForStringWithFormatSpecifier = function checkForStringWithFormatSpecifier(argValue, str) {
194
- var value = String(argValue);
195
- var formatSpecifierRegex = /%[a-z]/g;
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
- if (value && value.match(formatSpecifierRegex)) {
198
- var formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*');
199
- var regex = new RegExp(formatSpecifierReplacedStr);
200
- return regex.test(str);
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 checkForStringWithFormatSpecifier(arg.value, str);
265
+ return matchesStringWithFormatSpecifier(arg.value, str);
224
266
  }
225
267
  }
226
268
 
@@ -286,18 +328,47 @@ function removeImport(j, base, packageName) {
286
328
  }).remove();
287
329
  }
288
330
 
289
- function tryCreateImport(j, base, relativeToPackage, packageName) {
290
- var exists = base.find(j.ImportDeclaration).filter(function (path) {
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
- }).length > 0;
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
- }).at(0).insertBefore(j.importDeclaration([], j.literal(packageName)));
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) {
@@ -306,7 +377,7 @@ function addToImport(j, base, importSpecifier, packageName) {
306
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
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.2",
3
+ "version": "4.1.2",
4
4
  "sideEffects": false
5
5
  }
@@ -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
- const checkForStringWithFormatSpecifier = (argValue, str) => {
139
- const value = String(argValue);
140
- const formatSpecifierRegex = /%[a-z]/g;
141
-
142
- if (value && value.match(formatSpecifierRegex)) {
143
- const formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*');
144
- let regex = new RegExp(formatSpecifierReplacedStr);
145
- return regex.test(str);
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 checkForStringWithFormatSpecifier(arg.value, str);
205
+ return matchesStringWithFormatSpecifier(arg.value, str);
167
206
  }
168
207
  }
169
208
 
@@ -215,14 +254,39 @@ 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 exists = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).length > 0;
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).at(0).insertBefore(j.importDeclaration([], j.literal(packageName)));
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) {
@@ -230,7 +294,7 @@ function addToImport(j, base, importSpecifier, packageName) {
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
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.2",
3
+ "version": "4.1.2",
4
4
  "sideEffects": false
5
5
  }
@@ -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
- var checkForStringWithFormatSpecifier = function checkForStringWithFormatSpecifier(argValue, str) {
157
- var value = String(argValue);
158
- var formatSpecifierRegex = /%[a-z]/g;
159
-
160
- if (value && value.match(formatSpecifierRegex)) {
161
- var formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*');
162
- var regex = new RegExp(formatSpecifierReplacedStr);
163
- return regex.test(str);
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 checkForStringWithFormatSpecifier(arg.value, str);
225
+ return matchesStringWithFormatSpecifier(arg.value, str);
187
226
  }
188
227
  }
189
228
 
@@ -241,18 +280,47 @@ function removeImport(j, base, packageName) {
241
280
  }).remove();
242
281
  }
243
282
 
244
- function tryCreateImport(j, base, relativeToPackage, packageName) {
245
- var exists = base.find(j.ImportDeclaration).filter(function (path) {
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
- }).length > 0;
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
- }).at(0).insertBefore(j.importDeclaration([], j.literal(packageName)));
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) {
@@ -261,7 +329,7 @@ function addToImport(j, base, importSpecifier, packageName) {
261
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
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.2",
3
+ "version": "4.1.2",
4
4
  "sideEffects": false
5
5
  }
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.2",
3
+ "version": "4.1.2",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "license": "Apache-2.0",
6
6
  "description": " jscodeshift-compatible codemod utilities",
@@ -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.2.4"
33
+ "typescript": "4.3.5"
34
34
  },
35
35
  "techstack": {
36
36
  "@atlassian/frontend": {
package/report.api.md ADDED
@@ -0,0 +1,227 @@
1
+ ## API Report File for "@atlaskit/codemod-utils".
2
+
3
+ > Do not edit this file. This report is auto-generated by [API Extractor](https://api-extractor.com/).
4
+
5
+ [Learn more about API reports](https://hello.atlassian.net/wiki/spaces/UR/pages/1825484529/Package+API+Reports)
6
+
7
+ ```ts
8
+ import { API } from 'jscodeshift';
9
+ import { ASTPath } from 'jscodeshift';
10
+ import { CallExpression } from 'jscodeshift';
11
+ import { Collection } from 'jscodeshift/src/Collection';
12
+ import core from 'jscodeshift';
13
+ import { FileInfo } from 'jscodeshift';
14
+ import { ImportDeclaration } from 'jscodeshift';
15
+ import { ImportDefaultSpecifier } from 'jscodeshift';
16
+ import { ImportSpecifier } from 'jscodeshift';
17
+ import { JSXAttribute } from 'jscodeshift';
18
+ import { JSXElement } from 'jscodeshift';
19
+ import { Options } from 'jscodeshift';
20
+ import { Program } from 'jscodeshift';
21
+ import { VariableDeclaration } from 'jscodeshift';
22
+ import { VariableDeclarator } from 'jscodeshift';
23
+
24
+ export declare function addCommentBefore(
25
+ j: core.JSCodeshift,
26
+ target:
27
+ | Collection<Program>
28
+ | Collection<ImportDeclaration>
29
+ | Collection<JSXElement>
30
+ | Collection<JSXAttribute>
31
+ | Collection<CallExpression>
32
+ | Collection<VariableDeclarator>,
33
+ message: string,
34
+ commentType?: 'block' | 'line',
35
+ messagePrefix?: string,
36
+ ): void;
37
+
38
+ export declare const addCommentToStartOfFile: ({
39
+ j,
40
+ base,
41
+ message,
42
+ }: {
43
+ j: core.JSCodeshift;
44
+ base: Collection<Node>;
45
+ message: string;
46
+ }) => void;
47
+
48
+ export declare const addDynamicImport: (
49
+ j: core.JSCodeshift,
50
+ target: Collection<VariableDeclaration>,
51
+ name: string,
52
+ packageEndpoint: string,
53
+ ) => void;
54
+
55
+ export declare function addToImport(
56
+ j: core.JSCodeshift,
57
+ base: Collection<any>,
58
+ importSpecifier: ImportSpecifier | ImportDefaultSpecifier,
59
+ packageName: string,
60
+ ): void;
61
+
62
+ export declare const callExpressionArgMatchesString: (
63
+ arg: CallExpression['arguments'][number],
64
+ str: string,
65
+ ) => boolean;
66
+
67
+ export declare const changeImportEntryPoint: (
68
+ oldPackageName: string,
69
+ importToConvert: string,
70
+ newPackageName: string,
71
+ shouldBeTypeImport?: boolean | undefined,
72
+ ) => (j: core.JSCodeshift, root: Collection<Node>) => void;
73
+
74
+ export declare const createConvertFuncFor: (
75
+ component: string,
76
+ from: string,
77
+ to: string,
78
+ predicate?: ((value: any) => boolean) | undefined,
79
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
80
+
81
+ export declare const createRemoveFuncAddCommentFor: (
82
+ component: string,
83
+ prop: string,
84
+ comment?: string | undefined,
85
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
86
+
87
+ export declare const createRemoveFuncFor: (
88
+ component: string,
89
+ importName: string,
90
+ prop: string,
91
+ predicate?: (j: core.JSCodeshift, element: ASTPath<any>) => boolean,
92
+ comment?: string | undefined,
93
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
94
+
95
+ export declare const createRenameFuncFor: (
96
+ component: string,
97
+ from: string,
98
+ to: string,
99
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
100
+
101
+ export declare const createRenameImportFor: (
102
+ component: string,
103
+ from: string,
104
+ to: string,
105
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
106
+
107
+ export declare const createRenameJSXFunc: (
108
+ packagePath: string,
109
+ from: string,
110
+ to: string,
111
+ fallback?: string | undefined,
112
+ ) => (j: core.JSCodeshift, source: any) => void;
113
+
114
+ export declare const createTransformer: (
115
+ migrates: ((j: core.JSCodeshift, source: Collection<Node>) => void)[],
116
+ shouldApplyTransform?:
117
+ | ((j: core.JSCodeshift, source: Collection<Node>) => boolean)
118
+ | undefined,
119
+ ) => (fileInfo: FileInfo, { jscodeshift: j }: API, options: Options) => string;
120
+
121
+ export declare const doesIdentifierExist: (
122
+ j: core.JSCodeshift,
123
+ base: Collection<any>,
124
+ name: string,
125
+ ) => boolean;
126
+
127
+ export declare const elevateComponentToNewEntryPoint: (
128
+ pkg: string,
129
+ toPkg: string,
130
+ innerElementName: string,
131
+ ) => (j: core.JSCodeshift, root: any) => void;
132
+
133
+ export declare const flattenCertainChildPropsAsProp: (
134
+ component: string,
135
+ propName: string,
136
+ childProps: string[],
137
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
138
+
139
+ export declare const getDefaultSpecifier: (
140
+ j: core.JSCodeshift,
141
+ source: Collection<Node>,
142
+ specifier: string,
143
+ ) => string | null;
144
+
145
+ export declare const getDynamicImportName: (
146
+ j: core.JSCodeshift,
147
+ source: Collection<any>,
148
+ importPath: string,
149
+ ) => string | null;
150
+
151
+ export declare const getJSXAttributesByName: (
152
+ j: core.JSCodeshift,
153
+ element: ASTPath<any>,
154
+ attributeName: string,
155
+ ) => Collection<JSXAttribute>;
156
+
157
+ export declare function getNamedSpecifier(
158
+ j: core.JSCodeshift,
159
+ source: Collection<Node>,
160
+ specifier: string,
161
+ importName: string,
162
+ ): string | null;
163
+
164
+ export declare function getSafeImportName({
165
+ j,
166
+ base,
167
+ currentDefaultSpecifierName,
168
+ desiredName,
169
+ fallbackName,
170
+ }: {
171
+ j: core.JSCodeshift;
172
+ base: Collection<any>;
173
+ currentDefaultSpecifierName: string | null;
174
+ desiredName: string;
175
+ fallbackName: string;
176
+ }): string;
177
+
178
+ export declare const hasImportDeclaration: (
179
+ j: core.JSCodeshift,
180
+ source: Collection<Node>,
181
+ importPath: string,
182
+ ) => boolean;
183
+
184
+ export declare const hasImportDeclarationFromAnyPackageEntrypoint: (
185
+ j: core.JSCodeshift,
186
+ source: Collection<Node>,
187
+ packageName: string,
188
+ ) => boolean;
189
+
190
+ export declare const hasJSXAttributesByName: (
191
+ j: core.JSCodeshift,
192
+ element: ASTPath<any>,
193
+ attributeName: string,
194
+ ) => boolean;
195
+
196
+ export declare function removeImport(
197
+ j: core.JSCodeshift,
198
+ base: Collection<any>,
199
+ packageName: string,
200
+ ): void;
201
+
202
+ export declare const renameNamedImportWithAliasName: (
203
+ component: string,
204
+ from: string,
205
+ to: string,
206
+ ) => (j: core.JSCodeshift, source: Collection<Node>) => void;
207
+
208
+ export declare const replaceImportStatementFor: (
209
+ pkg: string,
210
+ convertMap: any,
211
+ ) => (j: core.JSCodeshift, root: Collection<Node>) => void;
212
+
213
+ export declare const testMethodVariantEach: (
214
+ path: ASTPath<CallExpression>,
215
+ testMethods: Set<string>,
216
+ ) => boolean;
217
+
218
+ export declare function tryCreateImport(
219
+ j: core.JSCodeshift,
220
+ base: Collection<any>,
221
+ relativeToPackage: string,
222
+ packageName: string,
223
+ shouldBeTypeImport?: boolean,
224
+ ): void;
225
+
226
+ export {};
227
+ ```