diff --git a/.changeset/rotten-ears-move.md b/.changeset/rotten-ears-move.md new file mode 100644 index 000000000..9237f7f40 --- /dev/null +++ b/.changeset/rotten-ears-move.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Remove .promise() calls from APIs called from new client diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.input.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.input.js new file mode 100644 index 000000000..0108e645b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.input.js @@ -0,0 +1,3 @@ +import AWS from "aws-sdk"; + +const data = await new AWS.DynamoDB().listTables().promise(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.output.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.output.js new file mode 100644 index 000000000..ee5620547 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-client-creation.output.js @@ -0,0 +1,3 @@ +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const data = await new DynamoDB().listTables(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.input.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.input.js new file mode 100644 index 000000000..3437ef54c --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.input.js @@ -0,0 +1,3 @@ +import { DynamoDB } from "aws-sdk"; + +const data = await new DynamoDB().listTables().promise(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.output.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.output.js new file mode 100644 index 000000000..ee5620547 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-client-creation.output.js @@ -0,0 +1,3 @@ +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const data = await new DynamoDB().listTables(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/apis/removePromiseCalls.ts b/src/transforms/v2-to-v3/apis/removePromiseCalls.ts index 1c32899df..36e882806 100644 --- a/src/transforms/v2-to-v3/apis/removePromiseCalls.ts +++ b/src/transforms/v2-to-v3/apis/removePromiseCalls.ts @@ -3,12 +3,69 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift"; import { ClientIdentifier } from "../types"; import { removePromiseForCallExpression } from "./removePromiseForCallExpression"; +export interface RemovePromiseCallsOptions { + v2GlobalName?: string; + v2ClientName: string; + v2ClientLocalName: string; + clientIdentifiers: ClientIdentifier[]; +} + // Removes .promise() from client API calls. export const removePromiseCalls = ( j: JSCodeshift, source: Collection, - clientIdentifiers: ClientIdentifier[] + { v2GlobalName, v2ClientName, v2ClientLocalName, clientIdentifiers }: RemovePromiseCallsOptions ): void => { + // Remove .promise() for API calls from client creation from global name. + if (v2GlobalName) { + source + .find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: { + type: "CallExpression", + callee: { + type: "MemberExpression", + object: { + type: "NewExpression", + callee: { + type: "MemberExpression", + object: { type: "Identifier", name: v2GlobalName }, + property: { type: "Identifier", name: v2ClientName }, + }, + }, + }, + }, + property: { type: "Identifier", name: "promise" }, + }, + }) + .forEach((callExpression) => { + removePromiseForCallExpression(j, callExpression); + }); + } + + // Remove .promise() for API calls client creation from local name. + source + .find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: { + type: "CallExpression", + callee: { + type: "MemberExpression", + object: { + type: "NewExpression", + callee: { type: "Identifier", name: v2ClientLocalName }, + }, + }, + }, + property: { type: "Identifier", name: "promise" }, + }, + }) + .forEach((callExpression) => { + removePromiseForCallExpression(j, callExpression); + }); + for (const clientId of clientIdentifiers) { // Remove .promise() from client API calls. source diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index d39675b5c..70b012d32 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -107,7 +107,7 @@ const transformer = async (file: FileInfo, api: API) => { replaceS3UploadApi(j, source, clientIdentifiers); } - removePromiseCalls(j, source, clientIdentifiers); + removePromiseCalls(j, source, { ...v2Options, clientIdentifiers }); addEmptyObjectForUndefined(j, source, clientIdentifiers); renameErrorCodeWithName(j, source, clientIdentifiers);