Skip to content

Commit 03c00b6

Browse files
authored
Add an empty param if value is not passed for optional params in callback (#806)
1 parent 93857b4 commit 03c00b6

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.changeset/odd-insects-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Add an empty param if value is not passed for optional params in callback

src/transforms/v2-to-v3/__fixtures__/misc/api-callback.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import AWS from "aws-sdk";
22

33
const client = new AWS.DynamoDB();
44

5-
client.listTables({}, (err, data) => {
5+
client.listTables((err, data) => {
66
if (err) console.log(err, err.stack);
77
else console.log(data);
88
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { ClientIdentifier } from "../types";
4+
5+
// Adds an empty object, if undefined is passed for optional parameters.
6+
export const addEmptyObjectForUndefined = (
7+
j: JSCodeshift,
8+
source: Collection<unknown>,
9+
clientIdentifiers: ClientIdentifier[]
10+
): void => {
11+
for (const clientId of clientIdentifiers) {
12+
// Remove .promise() from client API calls.
13+
source
14+
.find(j.CallExpression, {
15+
callee: {
16+
type: "MemberExpression",
17+
object: clientId,
18+
},
19+
})
20+
.filter((callExpression) => {
21+
return (
22+
callExpression.value.arguments.length === 1 &&
23+
["FunctionExpression", "ArrowFunctionExpression"].includes(
24+
callExpression.value.arguments[0].type
25+
)
26+
);
27+
})
28+
.replaceWith((callExpression) => {
29+
callExpression.value.arguments.unshift(j.objectExpression([]));
30+
return callExpression.value;
31+
});
32+
}
33+
};

src/transforms/v2-to-v3/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./addEmptyObjectForUndefined";
12
export * from "./addNotSupportedComments";
23
export * from "./addNotSupportedClientComments";
34
export * from "./getClientWaiterStates";

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getClientIdentifiersRecord,
1111
replaceAwsIdentity,
1212
replaceAwsError,
13+
addEmptyObjectForUndefined,
1314
} from "./apis";
1415
import { replaceAwsUtilFunctions } from "./aws-util";
1516
import {
@@ -98,6 +99,7 @@ const transformer = async (file: FileInfo, api: API) => {
9899
}
99100

100101
removePromiseCalls(j, source, clientIdentifiers);
102+
addEmptyObjectForUndefined(j, source, clientIdentifiers);
101103

102104
if (v2ClientName === S3) {
103105
replaceS3GetSignedUrlApi(j, source, clientIdentifiers);

0 commit comments

Comments
 (0)