Skip to content

Commit

Permalink
Add an empty param if value is not passed for optional params in call…
Browse files Browse the repository at this point in the history
…back (#806)
  • Loading branch information
trivikr authored Mar 22, 2024
1 parent 93857b4 commit 03c00b6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/odd-insects-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add an empty param if value is not passed for optional params in callback
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
33 changes: 33 additions & 0 deletions src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>,
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;
});
}
};
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./addEmptyObjectForUndefined";
export * from "./addNotSupportedComments";
export * from "./addNotSupportedClientComments";
export * from "./getClientWaiterStates";
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getClientIdentifiersRecord,
replaceAwsIdentity,
replaceAwsError,
addEmptyObjectForUndefined,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 03c00b6

Please sign in to comment.