Skip to content

Commit 5b77175

Browse files
authored
Remove promise() calls from clients created from service imports (#100)
1 parent c6cbab7 commit 5b77175

9 files changed

+128
-51
lines changed

.changeset/wet-timers-itch.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+
Remove promise() calls from clients created from service imports
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import DynamoDB from "aws-sdk/clients/dynamodb";
2+
3+
const client = new DynamoDB();
4+
const data = await client.listTables().promise();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB();
4+
const data = await client.listTables();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import DynamoDB from "aws-sdk/clients/dynamodb";
2+
3+
const client = new DynamoDB();
4+
const listTablesPromise = client.listTables().promise();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB();
4+
const listTablesPromise = client.listTables();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import DynamoDB from "aws-sdk/clients/dynamodb";
2+
3+
const client = new DynamoDB();
4+
5+
client
6+
.listTables()
7+
.promise()
8+
.then((data) => console.log(data))
9+
.catch((err) => console.log(err, err.stack));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB();
4+
5+
client
6+
.listTables()
7+
.then((data) => console.log(data))
8+
.catch((err) => console.log(err, err.stack));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
2+
3+
import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicates";
4+
5+
export interface GetClientIdentifierNamesOptions {
6+
v2ClientName: string;
7+
v2DefaultModuleName: string;
8+
}
9+
10+
export const getClientIdentifierNames = (
11+
j: JSCodeshift,
12+
source: Collection<unknown>,
13+
{ v2DefaultModuleName, v2ClientName }: GetClientIdentifierNamesOptions
14+
): string[] => {
15+
const clientIdentifierNamesFromDefaultImport = source
16+
.find(j.VariableDeclarator, {
17+
id: { type: "Identifier" },
18+
init: {
19+
type: "NewExpression",
20+
callee: {
21+
object: { type: "Identifier", name: v2DefaultModuleName },
22+
property: { type: "Identifier", name: v2ClientName },
23+
},
24+
},
25+
})
26+
.nodes()
27+
.map((variableDeclarator) => (variableDeclarator.id as Identifier).name);
28+
29+
const clientIdentifierNamesFromClientImport = source
30+
.find(j.VariableDeclarator, {
31+
id: { type: "Identifier" },
32+
init: {
33+
type: "NewExpression",
34+
callee: { type: "Identifier", name: v2ClientName },
35+
},
36+
})
37+
.nodes()
38+
.map((variableDeclarator) => (variableDeclarator.id as Identifier).name);
39+
40+
return getMergedArrayWithoutDuplicates(
41+
clientIdentifierNamesFromDefaultImport,
42+
clientIdentifierNamesFromClientImport
43+
);
44+
};
Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";
1+
import { Collection, JSCodeshift, MemberExpression } from "jscodeshift";
2+
3+
import { getClientIdentifierNames } from "./getClientIdentifierNames";
24

35
export interface RemovePromiseCallsOptions {
46
v2ClientName: string;
@@ -11,58 +13,51 @@ export const removePromiseCalls = (
1113
source: Collection<unknown>,
1214
{ v2DefaultModuleName, v2ClientName }: RemovePromiseCallsOptions
1315
): void => {
14-
source
15-
.find(j.VariableDeclarator, {
16-
id: { type: "Identifier" },
17-
init: {
18-
type: "NewExpression",
16+
const clientIdentifierNames = getClientIdentifierNames(j, source, {
17+
v2DefaultModuleName,
18+
v2ClientName,
19+
});
20+
21+
for (const clientIdentifierName of clientIdentifierNames) {
22+
source
23+
.find(j.CallExpression, {
1924
callee: {
20-
object: { type: "Identifier", name: v2DefaultModuleName },
21-
property: { type: "Identifier", name: v2ClientName },
22-
},
23-
},
24-
})
25-
.forEach((nodePath) => {
26-
const name = (nodePath.value.id as Identifier).name;
27-
source
28-
.find(j.CallExpression, {
29-
callee: {
30-
type: "MemberExpression",
31-
object: {
32-
type: "CallExpression",
33-
callee: {
34-
type: "MemberExpression",
35-
object: {
36-
type: "Identifier",
37-
name,
38-
},
25+
type: "MemberExpression",
26+
object: {
27+
type: "CallExpression",
28+
callee: {
29+
type: "MemberExpression",
30+
object: {
31+
type: "Identifier",
32+
name: clientIdentifierName,
3933
},
4034
},
41-
property: { type: "Identifier", name: "promise" },
4235
},
43-
})
44-
.forEach((callExpressionPath) => {
45-
switch (callExpressionPath.parentPath.value.type) {
46-
case "AwaitExpression":
47-
callExpressionPath.parentPath.value.argument = (
48-
callExpressionPath.value.callee as MemberExpression
49-
).object;
50-
break;
51-
case "MemberExpression":
52-
callExpressionPath.parentPath.value.object = (
53-
callExpressionPath.value.callee as MemberExpression
54-
).object;
55-
break;
56-
case "VariableDeclarator":
57-
callExpressionPath.parentPath.value.init = (
58-
callExpressionPath.value.callee as MemberExpression
59-
).object;
60-
break;
61-
default:
62-
throw new Error(
63-
`Removal of .promise() not implemented for ${callExpressionPath.parentPath.value.type}`
64-
);
65-
}
66-
});
67-
});
36+
property: { type: "Identifier", name: "promise" },
37+
},
38+
})
39+
.forEach((callExpressionPath) => {
40+
switch (callExpressionPath.parentPath.value.type) {
41+
case "AwaitExpression":
42+
callExpressionPath.parentPath.value.argument = (
43+
callExpressionPath.value.callee as MemberExpression
44+
).object;
45+
break;
46+
case "MemberExpression":
47+
callExpressionPath.parentPath.value.object = (
48+
callExpressionPath.value.callee as MemberExpression
49+
).object;
50+
break;
51+
case "VariableDeclarator":
52+
callExpressionPath.parentPath.value.init = (
53+
callExpressionPath.value.callee as MemberExpression
54+
).object;
55+
break;
56+
default:
57+
throw new Error(
58+
`Removal of .promise() not implemented for ${callExpressionPath.parentPath.value.type}`
59+
);
60+
}
61+
});
62+
}
6863
};

0 commit comments

Comments
 (0)