@atlaskit/codemod-utils 4.0.0 → 4.1.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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # @atlaskit/codemod-utils
2
2
 
3
+ ## 4.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`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
8
+
9
+ ## 4.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [`9358d42eeaa`](https://bitbucket.org/atlassian/atlassian-frontend/commits/9358d42eeaa) - NO-ISSUE: fixed codemod utils tryCreateImport() and addToImport() causing multiple insert in certain scenarios
14
+
15
+ ## 4.0.1
16
+
17
+ ### Patch Changes
18
+
19
+ - [`cb2392f6d33`](https://bitbucket.org/atlassian/atlassian-frontend/commits/cb2392f6d33) - Upgrade to TypeScript 4.2.4
20
+
3
21
  ## 4.0.0
4
22
 
5
23
  ### Major 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: {
@@ -286,27 +286,56 @@ function removeImport(j, base, packageName) {
286
286
  }).remove();
287
287
  }
288
288
 
289
- function tryCreateImport(j, base, relativeToPackage, packageName) {
290
- var exists = base.find(j.ImportDeclaration).filter(function (path) {
289
+ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
290
+ var matches = base.find(j.ImportDeclaration).filter(function (path) {
291
291
  return path.value.source.value === packageName;
292
- }).length > 0;
292
+ });
293
+ var exists = matches.length > 0;
293
294
 
294
295
  if (exists) {
296
+ if (shouldBeTypeImport) {
297
+ // if the matched import declarations are not type imports
298
+ // but should be, we update them accordingly
299
+ var isTypeImports = matches.every(function (path) {
300
+ return path.value.importKind === 'type';
301
+ });
302
+
303
+ if (!isTypeImports) {
304
+ matches.filter(function (path) {
305
+ return path.value.importKind !== 'type';
306
+ }).forEach(function (path) {
307
+ path.value.importKind = 'type';
308
+ });
309
+ return;
310
+ }
311
+ }
312
+
295
313
  return;
314
+ } // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
315
+ // is falsy, it is never passed explicitly as an importKind parameter (avoiding
316
+ // runtime exceptions during transform runs when importKind is undefined)
317
+
318
+
319
+ var importDeclarationArgs = [[], j.literal(packageName)];
320
+
321
+ if (shouldBeTypeImport) {
322
+ importDeclarationArgs.push('type');
296
323
  }
297
324
 
298
325
  base.find(j.ImportDeclaration).filter(function (path) {
299
326
  return path.value.source.value === relativeToPackage;
300
- }).insertBefore(j.importDeclaration([], j.literal(packageName)));
327
+ }) // we only take the first matching path, to avoid duplicate inserts
328
+ // if the source contains the same import declaration more than once
329
+ .paths()[0].insertBefore(j.importDeclaration.apply(j, importDeclarationArgs));
301
330
  }
302
331
 
303
332
  function addToImport(j, base, importSpecifier, packageName) {
304
333
  base.find(j.ImportDeclaration).filter(function (path) {
305
334
  return path.value.source.value === packageName;
306
- }).replaceWith(function (declaration) {
335
+ }).at(0).replaceWith(function (declaration) {
307
336
  return j.importDeclaration([].concat((0, _toConsumableArray2.default)((declaration.value.specifiers || []).filter(function (item) {
308
337
  return item.type === 'ImportSpecifier' && item.imported != null;
309
- })), [importSpecifier]), j.literal(packageName));
338
+ })), [importSpecifier]), j.literal(packageName), declaration.value.importKind);
310
339
  });
311
340
  }
312
341
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
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: {
@@ -215,22 +215,47 @@ function removeImport(j, base, packageName) {
215
215
  base.find(j.ImportDeclaration).filter(path => path.node.source.value === packageName).remove();
216
216
  }
217
217
 
218
- function tryCreateImport(j, base, relativeToPackage, packageName) {
219
- const exists = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).length > 0;
218
+ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
219
+ const matches = base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName);
220
+ const exists = matches.length > 0;
220
221
 
221
222
  if (exists) {
223
+ if (shouldBeTypeImport) {
224
+ // if the matched import declarations are not type imports
225
+ // but should be, we update them accordingly
226
+ const isTypeImports = matches.every(path => path.value.importKind === 'type');
227
+
228
+ if (!isTypeImports) {
229
+ matches.filter(path => path.value.importKind !== 'type').forEach(path => {
230
+ path.value.importKind = 'type';
231
+ });
232
+ return;
233
+ }
234
+ }
235
+
222
236
  return;
237
+ } // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
238
+ // is falsy, it is never passed explicitly as an importKind parameter (avoiding
239
+ // runtime exceptions during transform runs when importKind is undefined)
240
+
241
+
242
+ const importDeclarationArgs = [[], j.literal(packageName)];
243
+
244
+ if (shouldBeTypeImport) {
245
+ importDeclarationArgs.push('type');
223
246
  }
224
247
 
225
- base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage).insertBefore(j.importDeclaration([], j.literal(packageName)));
248
+ base.find(j.ImportDeclaration).filter(path => path.value.source.value === relativeToPackage) // we only take the first matching path, to avoid duplicate inserts
249
+ // if the source contains the same import declaration more than once
250
+ .paths()[0].insertBefore(j.importDeclaration(...importDeclarationArgs));
226
251
  }
227
252
 
228
253
  function addToImport(j, base, importSpecifier, packageName) {
229
- base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).replaceWith(declaration => {
254
+ base.find(j.ImportDeclaration).filter(path => path.value.source.value === packageName).at(0).replaceWith(declaration => {
230
255
  return j.importDeclaration([// we are appending to the existing specifiers
231
256
  // We are doing a filter hear because sometimes specifiers can be removed
232
257
  // but they hand around in the declaration
233
- ...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName));
258
+ ...(declaration.value.specifiers || []).filter(item => item.type === 'ImportSpecifier' && item.imported != null), importSpecifier], j.literal(packageName), declaration.value.importKind);
234
259
  });
235
260
  }
236
261
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
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: {
@@ -241,27 +241,56 @@ function removeImport(j, base, packageName) {
241
241
  }).remove();
242
242
  }
243
243
 
244
- function tryCreateImport(j, base, relativeToPackage, packageName) {
245
- var exists = base.find(j.ImportDeclaration).filter(function (path) {
244
+ function tryCreateImport(j, base, relativeToPackage, packageName, shouldBeTypeImport) {
245
+ var matches = base.find(j.ImportDeclaration).filter(function (path) {
246
246
  return path.value.source.value === packageName;
247
- }).length > 0;
247
+ });
248
+ var exists = matches.length > 0;
248
249
 
249
250
  if (exists) {
251
+ if (shouldBeTypeImport) {
252
+ // if the matched import declarations are not type imports
253
+ // but should be, we update them accordingly
254
+ var isTypeImports = matches.every(function (path) {
255
+ return path.value.importKind === 'type';
256
+ });
257
+
258
+ if (!isTypeImports) {
259
+ matches.filter(function (path) {
260
+ return path.value.importKind !== 'type';
261
+ }).forEach(function (path) {
262
+ path.value.importKind = 'type';
263
+ });
264
+ return;
265
+ }
266
+ }
267
+
250
268
  return;
269
+ } // we dynamically build up importDeclaration args, so that if shouldBeTypeImport
270
+ // is falsy, it is never passed explicitly as an importKind parameter (avoiding
271
+ // runtime exceptions during transform runs when importKind is undefined)
272
+
273
+
274
+ var importDeclarationArgs = [[], j.literal(packageName)];
275
+
276
+ if (shouldBeTypeImport) {
277
+ importDeclarationArgs.push('type');
251
278
  }
252
279
 
253
280
  base.find(j.ImportDeclaration).filter(function (path) {
254
281
  return path.value.source.value === relativeToPackage;
255
- }).insertBefore(j.importDeclaration([], j.literal(packageName)));
282
+ }) // we only take the first matching path, to avoid duplicate inserts
283
+ // if the source contains the same import declaration more than once
284
+ .paths()[0].insertBefore(j.importDeclaration.apply(j, importDeclarationArgs));
256
285
  }
257
286
 
258
287
  function addToImport(j, base, importSpecifier, packageName) {
259
288
  base.find(j.ImportDeclaration).filter(function (path) {
260
289
  return path.value.source.value === packageName;
261
- }).replaceWith(function (declaration) {
290
+ }).at(0).replaceWith(function (declaration) {
262
291
  return j.importDeclaration([].concat(_toConsumableArray((declaration.value.specifiers || []).filter(function (item) {
263
292
  return item.type === 'ImportSpecifier' && item.imported != null;
264
- })), [importSpecifier]), j.literal(packageName));
293
+ })), [importSpecifier]), j.literal(packageName), declaration.value.importKind);
265
294
  });
266
295
  }
267
296
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/codemod-utils",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
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, };
@@ -21,7 +21,7 @@ declare const testMethodVariantEach: (path: ASTPath<CallExpression>, testMethods
21
21
  declare const hasJSXAttributesByName: (j: core.JSCodeshift, element: ASTPath<any>, attributeName: string) => boolean;
22
22
  declare const doesIdentifierExist: (j: core.JSCodeshift, base: Collection<any>, name: string) => boolean;
23
23
  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;
24
+ declare function tryCreateImport(j: core.JSCodeshift, base: Collection<any>, relativeToPackage: string, packageName: string, shouldBeTypeImport?: boolean): void;
25
25
  declare function addToImport(j: core.JSCodeshift, base: Collection<any>, importSpecifier: ImportSpecifier | ImportDefaultSpecifier, packageName: string): void;
26
26
  declare const shiftDefaultImport: (j: core.JSCodeshift, base: Collection<any>, defaultName: string, oldPackagePath: string, newPackagePath: string) => void;
27
27
  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.0",
3
+ "version": "4.1.0",
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": "3.9.10"
33
+ "typescript": "4.2.4"
34
34
  },
35
35
  "techstack": {
36
36
  "@atlassian/frontend": {