Skip to content

Commit

Permalink
Removes unused v2 default import (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 4, 2022
1 parent 316883b commit fcd1e82
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-laws-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Removes unused v2 default import
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ client.listTables({}, (err, data) => {
$ npx aws-sdk-js-codemod -t v2-to-v3 example.ts

$ cat example.ts
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
Expand Down
2 changes: 0 additions & 2 deletions src/transforms/v2-to-v3/__testfixtures__/basic.output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
Expand Down
4 changes: 3 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { API, FileInfo } from "jscodeshift";
import findImports from "jscodeshift-find-imports";

import {
addV3ClientImport,
getV2ClientNames,
getV2DefaultImportName,
getV3ClientName,
getV3ClientPackageName,
removeDefaultImportIfNotUsed,
replaceClientCreation,
} from "./utils";

Expand All @@ -32,5 +32,7 @@ export default function transformer(file: FileInfo, api: API) {
});
}

removeDefaultImportIfNotUsed(j, source, v2DefaultImportName);

return source.toSource();
}
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./getV2ClientNames";
export * from "./getV2DefaultImportName";
export * from "./getV3ClientName";
export * from "./getV3ClientPackageName";
export * from "./removeDefaultImportIfNotUsed";
export * from "./replaceClientCreation";
31 changes: 31 additions & 0 deletions src/transforms/v2-to-v3/utils/removeDefaultImportIfNotUsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Collection, JSCodeshift } from "jscodeshift";

export const removeDefaultImportIfNotUsed = (
j: JSCodeshift,
source: Collection<any>,
defaultImportName: string
) => {
const identifierUsages = source
.find(j.Identifier, { name: defaultImportName })
// Ignore identifier from import.
.filter((identifierPath) => identifierPath.parentPath.value.type !== "ImportDefaultSpecifier");

if (identifierUsages.size() === 0) {
source
.find(j.ImportDeclaration, {
specifiers: [{ type: "ImportDefaultSpecifier", local: { name: defaultImportName } }],
})
.forEach((declerationPath) => {
// Remove default import from ImportDecleration.
declerationPath.value.specifiers = declerationPath.value.specifiers.filter(
(specifier) =>
specifier.type !== "ImportDefaultSpecifier" &&
specifier.local.name !== defaultImportName
);
// Remove ImportDeclaration if there are no other imports.
if (declerationPath.value.specifiers.length === 0) {
j(declerationPath).remove();
}
});
}
};

0 comments on commit fcd1e82

Please sign in to comment.