Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transformation for client require #65

Merged
merged 7 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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();
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();
};