Skip to content

Commit

Permalink
Support transformation for client require (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 8, 2022
1 parent 154ae00 commit 1008a5b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-walls-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Support transformation for client require
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/client-require.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const DynamoDB = require("aws-sdk/clients/dynamodb");

const client = new DynamoDB();
5 changes: 5 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/client-require.output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getV2DefaultImportName,
removeDefaultModuleIfNotUsed,
removePromiseCalls,
removeV2ClientImport,
removeV2ClientModule,
replaceClientCreation,
} from "./utils";

Expand All @@ -28,7 +28,7 @@ export default function transformer(file: FileInfo, api: API) {
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadata).reverse()) {
const { v3ClientName, v3ClientPackageName } = v3ClientMetadata;
addV3ClientModule(j, source, { v2ClientName, v3ClientName, v3ClientPackageName });
removeV2ClientImport(j, source, v2ClientName);
removeV2ClientModule(j, source, v2ClientName);
removePromiseCalls(j, source, { v2DefaultImportName, v2ClientName });
replaceClientCreation(j, source, { v2DefaultImportName, v2ClientName, v3ClientName });
}
Expand Down
16 changes: 15 additions & 1 deletion src/transforms/v2-to-v3/utils/getV2ClientImportNames.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";

Expand All @@ -21,6 +21,20 @@ export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>):
}
});
});

// Add specifier name to v2ClientImportNames if it is required in the source.
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }],
},
})
.forEach((declerationPath) => {
v2ClientImportNames.push((declerationPath.value.id as Identifier).name);
});
}

return v2ClientImportNames;
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export * from "./getV2ClientNames";
export * from "./getV2DefaultImportName";
export * from "./removeDefaultModuleIfNotUsed";
export * from "./removePromiseCalls";
export * from "./removeV2ClientImport";
export * from "./removeV2ClientModule";
export * from "./replaceClientCreation";
export * from "./types";
14 changes: 14 additions & 0 deletions src/transforms/v2-to-v3/utils/removeV2ClientModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { containsRequire } from "./containsRequire";
import { removeV2ClientImport } from "./removeV2ClientImport";
import { removeV2ClientRequire } from "./removeV2ClientRequire";

export const removeV2ClientModule = (
j: JSCodeshift,
source: Collection<any>,
v2ClientName: string
) =>
containsRequire(j, source)
? removeV2ClientRequire(j, source, v2ClientName)
: removeV2ClientImport(j, source, v2ClientName);
18 changes: 18 additions & 0 deletions src/transforms/v2-to-v3/utils/removeV2ClientRequire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";

import { getRequireVariableDeclaration } from "./getRequireVariableDeclaration";

export const removeV2ClientRequire = (
j: JSCodeshift,
source: Collection<any>,
v2ClientName: string
) => {
const importSourceName = `aws-sdk/clients/${v2ClientName.toLowerCase()}`;
getRequireVariableDeclaration(j, source, importSourceName)
.filter(
(nodePath) =>
((nodePath.value.declarations[0] as VariableDeclarator).id as Identifier).name ===
v2ClientName
)
.remove();
};

0 comments on commit 1008a5b

Please sign in to comment.