Skip to content

Commit 1008a5b

Browse files
authored
Support transformation for client require (#65)
1 parent 154ae00 commit 1008a5b

File tree

8 files changed

+63
-4
lines changed

8 files changed

+63
-4
lines changed

.changeset/chatty-walls-fold.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+
Support transformation for client require
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const DynamoDB = require("aws-sdk/clients/dynamodb");
2+
3+
const client = new DynamoDB();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const {
2+
DynamoDB
3+
} = require("@aws-sdk/client-dynamodb");
4+
5+
const client = new DynamoDB();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getV2DefaultImportName,
99
removeDefaultModuleIfNotUsed,
1010
removePromiseCalls,
11-
removeV2ClientImport,
11+
removeV2ClientModule,
1212
replaceClientCreation,
1313
} from "./utils";
1414

@@ -28,7 +28,7 @@ export default function transformer(file: FileInfo, api: API) {
2828
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadata).reverse()) {
2929
const { v3ClientName, v3ClientPackageName } = v3ClientMetadata;
3030
addV3ClientModule(j, source, { v2ClientName, v3ClientName, v3ClientPackageName });
31-
removeV2ClientImport(j, source, v2ClientName);
31+
removeV2ClientModule(j, source, v2ClientName);
3232
removePromiseCalls(j, source, { v2DefaultImportName, v2ClientName });
3333
replaceClientCreation(j, source, { v2DefaultImportName, v2ClientName, v3ClientName });
3434
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
22

33
import { CLIENT_NAMES } from "./config";
44

@@ -21,6 +21,20 @@ export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>):
2121
}
2222
});
2323
});
24+
25+
// Add specifier name to v2ClientImportNames if it is required in the source.
26+
source
27+
.find(j.VariableDeclarator, {
28+
id: { type: "Identifier" },
29+
init: {
30+
type: "CallExpression",
31+
callee: { type: "Identifier", name: "require" },
32+
arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }],
33+
},
34+
})
35+
.forEach((declerationPath) => {
36+
v2ClientImportNames.push((declerationPath.value.id as Identifier).name);
37+
});
2438
}
2539

2640
return v2ClientImportNames;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export * from "./getV2ClientNames";
66
export * from "./getV2DefaultImportName";
77
export * from "./removeDefaultModuleIfNotUsed";
88
export * from "./removePromiseCalls";
9-
export * from "./removeV2ClientImport";
9+
export * from "./removeV2ClientModule";
1010
export * from "./replaceClientCreation";
1111
export * from "./types";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { containsRequire } from "./containsRequire";
4+
import { removeV2ClientImport } from "./removeV2ClientImport";
5+
import { removeV2ClientRequire } from "./removeV2ClientRequire";
6+
7+
export const removeV2ClientModule = (
8+
j: JSCodeshift,
9+
source: Collection<any>,
10+
v2ClientName: string
11+
) =>
12+
containsRequire(j, source)
13+
? removeV2ClientRequire(j, source, v2ClientName)
14+
: removeV2ClientImport(j, source, v2ClientName);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";
2+
3+
import { getRequireVariableDeclaration } from "./getRequireVariableDeclaration";
4+
5+
export const removeV2ClientRequire = (
6+
j: JSCodeshift,
7+
source: Collection<any>,
8+
v2ClientName: string
9+
) => {
10+
const importSourceName = `aws-sdk/clients/${v2ClientName.toLowerCase()}`;
11+
getRequireVariableDeclaration(j, source, importSourceName)
12+
.filter(
13+
(nodePath) =>
14+
((nodePath.value.declarations[0] as VariableDeclarator).id as Identifier).name ===
15+
v2ClientName
16+
)
17+
.remove();
18+
};

0 commit comments

Comments
 (0)