Skip to content

Commit

Permalink
Remove .promise() from client API request stored in a variable (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Dec 23, 2022
1 parent b76ff35 commit c80233f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-hornets-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove .promise() from client API request stored in a variable.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const client = new AWS.DynamoDB();

// Variable declarator
const listTablesPromise = client.listTables().promise();

// Promise call on request in variable declarator
const listTablesRequest = client.listTables();
listTablesRequest
.promise()
.then((data) => console.log(data))
.catch((err) => console.log(err, err.stack));
}

// Promise with params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const client = new DynamoDB();

// Variable declarator
const listTablesPromise = client.listTables();

// Promise call on request in variable declarator
const listTablesRequest = client.listTables();
listTablesRequest
.then((data) => console.log(data))
.catch((err) => console.log(err, err.stack));
}

// Promise with params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const client = new DynamoDB();

// Variable declarator
const listTablesPromise = client.listTables().promise();

// Promise call on request in variable declarator
const listTablesRequest = client.listTables();
listTablesRequest
.promise()
.then((data) => console.log(data))
.catch((err) => console.log(err, err.stack));
}

// Promise with params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const client = new DynamoDB();

// Variable declarator
const listTablesPromise = client.listTables();

// Promise call on request in variable declarator
const listTablesRequest = client.listTables();
listTablesRequest
.then((data) => console.log(data))
.catch((err) => console.log(err, err.stack));
}

// Promise with params
Expand Down
63 changes: 34 additions & 29 deletions src/transforms/v2-to-v3/utils/remove/removePromiseCalls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CallExpression, Collection, JSCodeshift, MemberExpression } from "jscodeshift";
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { getV2ClientIdentifiers, getV2ClientIdThisExpressions } from "../get";
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";

export interface RemovePromiseCallsOptions {
v2ClientName: string;
Expand All @@ -20,6 +21,7 @@ export const removePromiseCalls = (
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);

for (const v2ClientId of [...v2ClientIdentifiers, ...v2ClientIdThisExpressions]) {
// Remove .promise() from client API calls.
source
.find(j.CallExpression, {
callee: {
Expand All @@ -34,34 +36,37 @@ export const removePromiseCalls = (
property: { type: "Identifier", name: "promise" },
},
})
.forEach((callExpressionPath) => {
switch (callExpressionPath.parentPath.value.type) {
case "AwaitExpression":
callExpressionPath.parentPath.value.argument = (
callExpressionPath.value.callee as MemberExpression
).object;
break;
case "MemberExpression":
callExpressionPath.parentPath.value.object = (
callExpressionPath.value.callee as MemberExpression
).object;
break;
case "VariableDeclarator":
callExpressionPath.parentPath.value.init = (
callExpressionPath.value.callee as MemberExpression
).object;
break;
case "ArrowFunctionExpression":
case "ReturnStatement":
callExpressionPath.value.callee = (
(callExpressionPath.value.callee as MemberExpression).object as CallExpression
).callee;
break;
default:
throw new Error(
`Removal of .promise() not implemented for ${callExpressionPath.parentPath.value.type}`
);
}
.forEach((callExpression) => {
removePromiseForCallExpression(callExpression);
});

// Remove .promise() from client API request stored in a variable.
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: v2ClientId,
},
},
})
.forEach((variableDeclarator) => {
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: {
type: "Identifier",
name: (variableDeclarator.value.id as Identifier).name,
},
property: { type: "Identifier", name: "promise" },
},
})
.forEach((callExpression) => {
removePromiseForCallExpression(callExpression);
});
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ASTPath, CallExpression, MemberExpression } from "jscodeshift";

export const removePromiseForCallExpression = (callExpression: ASTPath<CallExpression>) => {
switch (callExpression.parentPath.value.type) {
case "AwaitExpression":
callExpression.parentPath.value.argument = (
callExpression.value.callee as MemberExpression
).object;
break;
case "MemberExpression":
callExpression.parentPath.value.object = (
callExpression.value.callee as MemberExpression
).object;
break;
case "VariableDeclarator":
callExpression.parentPath.value.init = (
callExpression.value.callee as MemberExpression
).object;
break;
case "ArrowFunctionExpression":
case "ReturnStatement":
callExpression.value.callee = (
(callExpression.value.callee as MemberExpression).object as CallExpression
).callee;
break;
default:
throw new Error(
`Removal of .promise() not implemented for ${callExpression.parentPath.value.type}`
);
}
};

0 comments on commit c80233f

Please sign in to comment.