Skip to content

Commit

Permalink
Retain only top level comments while transforming (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jul 26, 2023
1 parent f4d2497 commit 83832c8
Show file tree
Hide file tree
Showing 25 changed files with 16 additions and 127 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-trees-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Retain only top level comments while transforming

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 8 additions & 8 deletions src/transforms/v2-to-v3/modules/removeImportDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ASTPath, ImportDeclaration, JSCodeshift } from "jscodeshift";
import { ASTPath, Collection, ImportDeclaration, JSCodeshift } from "jscodeshift";

/**
* Removes import declaration, but preserves comments by adding them to next sibling.
* Removes import declaration, but preserves comments if they're top level comments.
*/
export const removeImportDeclaration = (
j: JSCodeshift,
source: Collection<unknown>,
declarationPath: ASTPath<ImportDeclaration>
) => {
const { comments } = declarationPath.value;
if (comments?.length) {
const siblings = declarationPath.parent?.value.body;
if (siblings?.length) {
const nextSibling = siblings[siblings.indexOf(declarationPath.value) + 1];
nextSibling.comments = [...comments, ...(nextSibling.comments || [])];
const firstNode = source.find(j.Program).get("body", 0).node;
if (firstNode === declarationPath.node) {
const { comments } = declarationPath.value;
if (comments?.length) {
declarationPath.insertBefore(j.emptyStatement.from({ comments }));
}
}
j(declarationPath).remove();
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/modules/removeImportDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const removeImportDefault = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
removeImportDeclaration(j, declarationPath);
removeImportDeclaration(j, source, declarationPath);
}
});
};
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/modules/removeImportEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export const removeImportEquals = (
},
});
if (importEqualsDeclaration.length) {
removeImportDeclaration(j, importEqualsDeclaration.get());
removeImportDeclaration(j, source, importEqualsDeclaration.get());
}
};
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/modules/removeImportNamed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const removeImportNamed = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
removeImportDeclaration(j, declarationPath);
removeImportDeclaration(j, source, declarationPath);
}
});
};

0 comments on commit 83832c8

Please sign in to comment.