From 03c00b6375c5977d83e3e30c662ef2d93ac6f18a Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Fri, 22 Mar 2024 09:15:38 -0700 Subject: [PATCH] Add an empty param if value is not passed for optional params in callback (#806) --- .changeset/odd-insects-learn.md | 5 +++ .../__fixtures__/misc/api-callback.input.js | 2 +- .../apis/addEmptyObjectForUndefined.ts | 33 +++++++++++++++++++ src/transforms/v2-to-v3/apis/index.ts | 1 + src/transforms/v2-to-v3/transformer.ts | 2 ++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .changeset/odd-insects-learn.md create mode 100644 src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts diff --git a/.changeset/odd-insects-learn.md b/.changeset/odd-insects-learn.md new file mode 100644 index 000000000..1eb6e2c62 --- /dev/null +++ b/.changeset/odd-insects-learn.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Add an empty param if value is not passed for optional params in callback diff --git a/src/transforms/v2-to-v3/__fixtures__/misc/api-callback.input.js b/src/transforms/v2-to-v3/__fixtures__/misc/api-callback.input.js index d6bf0d50a..24d66150f 100644 --- a/src/transforms/v2-to-v3/__fixtures__/misc/api-callback.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/misc/api-callback.input.js @@ -2,7 +2,7 @@ import AWS from "aws-sdk"; const client = new AWS.DynamoDB(); -client.listTables({}, (err, data) => { +client.listTables((err, data) => { if (err) console.log(err, err.stack); else console.log(data); }); diff --git a/src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts b/src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts new file mode 100644 index 000000000..ccf441494 --- /dev/null +++ b/src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts @@ -0,0 +1,33 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { ClientIdentifier } from "../types"; + +// Adds an empty object, if undefined is passed for optional parameters. +export const addEmptyObjectForUndefined = ( + j: JSCodeshift, + source: Collection, + clientIdentifiers: ClientIdentifier[] +): void => { + for (const clientId of clientIdentifiers) { + // Remove .promise() from client API calls. + source + .find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: clientId, + }, + }) + .filter((callExpression) => { + return ( + callExpression.value.arguments.length === 1 && + ["FunctionExpression", "ArrowFunctionExpression"].includes( + callExpression.value.arguments[0].type + ) + ); + }) + .replaceWith((callExpression) => { + callExpression.value.arguments.unshift(j.objectExpression([])); + return callExpression.value; + }); + } +}; diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index 66f4874d2..a7d0b9974 100644 --- a/src/transforms/v2-to-v3/apis/index.ts +++ b/src/transforms/v2-to-v3/apis/index.ts @@ -1,3 +1,4 @@ +export * from "./addEmptyObjectForUndefined"; export * from "./addNotSupportedComments"; export * from "./addNotSupportedClientComments"; export * from "./getClientWaiterStates"; diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 8d08536ae..c80fe0cce 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -10,6 +10,7 @@ import { getClientIdentifiersRecord, replaceAwsIdentity, replaceAwsError, + addEmptyObjectForUndefined, } from "./apis"; import { replaceAwsUtilFunctions } from "./aws-util"; import { @@ -98,6 +99,7 @@ const transformer = async (file: FileInfo, api: API) => { } removePromiseCalls(j, source, clientIdentifiers); + addEmptyObjectForUndefined(j, source, clientIdentifiers); if (v2ClientName === S3) { replaceS3GetSignedUrlApi(j, source, clientIdentifiers);