Skip to content

Commit fcd1e82

Browse files
authored
Removes unused v2 default import (#23)
1 parent 316883b commit fcd1e82

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

.changeset/sweet-laws-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Removes unused v2 default import

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ client.listTables({}, (err, data) => {
4444
$ npx aws-sdk-js-codemod -t v2-to-v3 example.ts
4545

4646
$ cat example.ts
47-
import AWS from "aws-sdk";
48-
4947
import { DynamoDB } from "@aws-sdk/client-dynamodb";
5048

5149
const region = "us-west-2";

src/transforms/v2-to-v3/__testfixtures__/basic.output.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import AWS from "aws-sdk";
2-
31
import { DynamoDB } from "@aws-sdk/client-dynamodb";
42

53
const region = "us-west-2";

src/transforms/v2-to-v3/transformer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { API, FileInfo } from "jscodeshift";
2-
import findImports from "jscodeshift-find-imports";
32

43
import {
54
addV3ClientImport,
65
getV2ClientNames,
76
getV2DefaultImportName,
87
getV3ClientName,
98
getV3ClientPackageName,
9+
removeDefaultImportIfNotUsed,
1010
replaceClientCreation,
1111
} from "./utils";
1212

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

35+
removeDefaultImportIfNotUsed(j, source, v2DefaultImportName);
36+
3537
return source.toSource();
3638
}

src/transforms/v2-to-v3/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from "./getV2ClientNames";
33
export * from "./getV2DefaultImportName";
44
export * from "./getV3ClientName";
55
export * from "./getV3ClientPackageName";
6+
export * from "./removeDefaultImportIfNotUsed";
67
export * from "./replaceClientCreation";
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export const removeDefaultImportIfNotUsed = (
4+
j: JSCodeshift,
5+
source: Collection<any>,
6+
defaultImportName: string
7+
) => {
8+
const identifierUsages = source
9+
.find(j.Identifier, { name: defaultImportName })
10+
// Ignore identifier from import.
11+
.filter((identifierPath) => identifierPath.parentPath.value.type !== "ImportDefaultSpecifier");
12+
13+
if (identifierUsages.size() === 0) {
14+
source
15+
.find(j.ImportDeclaration, {
16+
specifiers: [{ type: "ImportDefaultSpecifier", local: { name: defaultImportName } }],
17+
})
18+
.forEach((declerationPath) => {
19+
// Remove default import from ImportDecleration.
20+
declerationPath.value.specifiers = declerationPath.value.specifiers.filter(
21+
(specifier) =>
22+
specifier.type !== "ImportDefaultSpecifier" &&
23+
specifier.local.name !== defaultImportName
24+
);
25+
// Remove ImportDeclaration if there are no other imports.
26+
if (declerationPath.value.specifiers.length === 0) {
27+
j(declerationPath).remove();
28+
}
29+
});
30+
}
31+
};

0 commit comments

Comments
 (0)