Skip to content

Commit

Permalink
Transform promise() on APIs called on client created inside construct…
Browse files Browse the repository at this point in the history
…or (#843)
  • Loading branch information
trivikr committed Apr 11, 2024
1 parent 01cc448 commit d440d3d
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-pugs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform promise() on APIs called on client created inside constructor
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ class ClientClassMember {
async listTagsOfResource() {
return await this.clientInClass.listTagsOfResource({ ResourceArn: "STRING_VALUE" }).promise();
}
}

// Client as class member with creation inside constructor
class ClientClassMemberConstructor {
constructor() {
this.clientInClassCtr = new AWS.DynamoDB();
}

async listTables() {
return this.clientInClassCtr.listTables().promise();
}

async listTagsOfResource() {
return this.clientInClassCtr.listTagsOfResource({ ResourceArn: "STRING_VALUE" }).promise();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ class ClientClassMember {
async listTagsOfResource() {
return this.clientInClass.listTagsOfResource({ ResourceArn: "STRING_VALUE" }).promise();
}
}

// Client as class member with creation inside constructor
class ClientClassMemberConstructor {
private clientInClassCtr: AWS.DynamoDB;

constructor() {
this.clientInClassCtr = new AWS.DynamoDB();
}

async listTables() {
return this.clientInClassCtr.listTables().promise();
}

async listTagsOfResource() {
return this.clientInClassCtr.listTagsOfResource({ ResourceArn: "STRING_VALUE" }).promise();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ class ClientClassMember {
async listTagsOfResource() {
return await this.clientInClass.listTagsOfResource({ ResourceArn: "STRING_VALUE" });
}
}

// Client as class member with creation inside constructor
class ClientClassMemberConstructor {
constructor() {
this.clientInClassCtr = new DynamoDB();
}

async listTables() {
return this.clientInClassCtr.listTables();
}

async listTagsOfResource() {
return this.clientInClassCtr.listTagsOfResource({ ResourceArn: "STRING_VALUE" });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ class ClientClassMember {
async listTagsOfResource() {
return this.clientInClass.listTagsOfResource({ ResourceArn: "STRING_VALUE" });
}
}

// Client as class member with creation inside constructor
class ClientClassMemberConstructor {
private clientInClassCtr: DynamoDB;

constructor() {
this.clientInClassCtr = new DynamoDB();
}

async listTables() {
return this.clientInClassCtr.listTables();
}

async listTagsOfResource() {
return this.clientInClassCtr.listTagsOfResource({ ResourceArn: "STRING_VALUE" });
}
}
36 changes: 27 additions & 9 deletions src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, Identifier, JSCodeshift, NewExpression } from "jscodeshift";
import { Collection, Identifier, JSCodeshift, MemberExpression, NewExpression } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
Expand All @@ -15,10 +15,7 @@ const getNamesFromVariableDeclarator = (
newExpression: NewExpression
) =>
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: newExpression,
})
.find(j.VariableDeclarator, { id: { type: "Identifier" }, init: newExpression })
.nodes()
.map((variableDeclarator) => (variableDeclarator.id as Identifier).name);

Expand All @@ -28,12 +25,29 @@ const getNamesFromAssignmentPattern = (
newExpression: NewExpression
) =>
source
.find(j.AssignmentPattern, {
left: { type: "Identifier" },
.find(j.AssignmentPattern, { left: { type: "Identifier" }, right: newExpression })
.nodes()
.map((assignmentPattern) => (assignmentPattern.left as Identifier).name);

const getNamesFromThisMemberExpression = (
j: JSCodeshift,
source: Collection<unknown>,
newExpression: NewExpression
) =>
source
.find(j.AssignmentExpression, {
left: {
type: "MemberExpression",
object: { type: "ThisExpression" },
property: { type: "Identifier" },
},
right: newExpression,
})
.nodes()
.map((assignmentPattern) => (assignmentPattern.left as Identifier).name);
.map(
(assignmentExpression) =>
((assignmentExpression.left as MemberExpression).property as Identifier).name
);

export const getClientIdNamesFromNewExpr = (
j: JSCodeshift,
Expand All @@ -43,7 +57,11 @@ export const getClientIdNamesFromNewExpr = (
const namesFromGlobalModule = [];
const namesFromServiceModule = [];

for (const getNames of [getNamesFromVariableDeclarator, getNamesFromAssignmentPattern]) {
for (const getNames of [
getNamesFromVariableDeclarator,
getNamesFromAssignmentPattern,
getNamesFromThisMemberExpression,
]) {
if (v2GlobalName) {
namesFromGlobalModule.push(
...getNames(j, source, getClientNewExpression({ v2GlobalName, v2ClientName }))
Expand Down
28 changes: 24 additions & 4 deletions src/transforms/v2-to-v3/apis/getClientIdThisExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const getClientIdThisExpressions = (
source: Collection<unknown>,
clientIdentifiers: Identifier[]
): ThisMemberExpression[] =>
clientIdentifiers.flatMap((clientIdentifier) =>
source
clientIdentifiers.flatMap((clientIdentifier) => {
const clientIdsFromThisExpressionAssignment = source
.find(j.AssignmentExpression, {
left: thisMemberExpression as MemberExpression,
right: clientIdentifier,
Expand All @@ -24,5 +24,25 @@ export const getClientIdThisExpressions = (
name: ((assignmentExpression.left as MemberExpression).property as Identifier).name,
},
}) as ThisMemberExpression
)
);
);

const clientIdsFromThisExpression = source
.find(j.MemberExpression, {
type: "MemberExpression",
object: { type: "ThisExpression" },
property: clientIdentifier,
})
.nodes()
.map(
(memberExpression) =>
({
...thisMemberExpression,
property: {
type: "Identifier",
name: (memberExpression.property as Identifier).name,
},
}) as ThisMemberExpression
)[0];

return [...clientIdsFromThisExpressionAssignment, clientIdsFromThisExpression];
});

0 comments on commit d440d3d

Please sign in to comment.