Skip to content

Commit c77b0d9

Browse files
authored
Use local names to minimize transformation (#237)
1 parent dde3c75 commit c77b0d9

37 files changed

+581
-369
lines changed

.changeset/shy-dancers-camp.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": minor
3+
---
4+
5+
Use local names to minimize transformation
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { CLIENT_NAMES, CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/utils/config";
1+
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config";
22

33
export const getV3ClientsNewExpressionCode = () => {
44
let v3ClientsNewExpressionCode = ``;
55
for (const v2ClientName of CLIENT_NAMES) {
6-
v3ClientsNewExpressionCode += `new ${CLIENT_NAMES_MAP[v2ClientName]}();\n`;
6+
v3ClientsNewExpressionCode += `new ${v2ClientName}();\n`;
77
}
88
return v3ClientsNewExpressionCode;
99
};

scripts/generateNewClientTests/getV3PackageImportsCode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export const getV3PackageImportsCode = (sortedV2ClientNames: typeof CLIENT_NAMES
99
for (const v2ClientName of sortedV2ClientNames) {
1010
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName];
1111
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`;
12-
v3PackageImportsCode += `import { ${v3ClientName} } from "${v3ClientPackageName}";\n`;
12+
const v3ImportSpecifier =
13+
v3ClientName === v2ClientName ? v3ClientName : `${v3ClientName} as ${v2ClientName}`;
14+
v3PackageImportsCode += `import { ${v3ImportSpecifier} } from "${v3ClientPackageName}";\n`;
1315
}
1416
return v3PackageImportsCode;
1517
};

scripts/generateNewClientTests/getV3PackageRequireCode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const getV3PackageRequireCode = (
1212
for (const v2ClientName of sortedV2ClientNames) {
1313
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName];
1414
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`;
15-
v3PackageRequireCode += `const {\n ${v3ClientName}\n} = require("${v3ClientPackageName}");\n`;
15+
const v3RequireKeyValuePair =
16+
v3ClientName === v2ClientName ? v3ClientName : `${v3ClientName}: ${v2ClientName}`;
17+
v3PackageRequireCode += `const {\n ${v3RequireKeyValuePair}\n} = require("${v3ClientPackageName}");\n`;
1618
if (extraNewLine) v3PackageRequireCode += `\n`;
1719
}
1820
return v3PackageRequireCode;

src/transforms/v2-to-v3/__fixtures__/api-promise/service-import.input.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import DynamoDB from "aws-sdk/clients/dynamodb";
1+
import AWS_DynamoDB from "aws-sdk/clients/dynamodb";
22

3-
const client = new DynamoDB();
3+
const client = new AWS_DynamoDB();
44

55
// Promise without params
66
{
@@ -21,7 +21,7 @@ const client = new DynamoDB();
2121

2222
// Client as class member
2323
class ClientClassMember {
24-
constructor(clientInCtr = new DynamoDB()) {
24+
constructor(clientInCtr = new AWS_DynamoDB()) {
2525
this.clientInClass = clientInCtr;
2626
}
2727

src/transforms/v2-to-v3/__fixtures__/api-promise/service-import.output.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DynamoDB } from "@aws-sdk/client-dynamodb";
1+
import { DynamoDB as AWS_DynamoDB } from "@aws-sdk/client-dynamodb";
22

3-
const client = new DynamoDB();
3+
const client = new AWS_DynamoDB();
44

55
// Promise without params
66
{
@@ -20,7 +20,7 @@ const client = new DynamoDB();
2020

2121
// Client as class member
2222
class ClientClassMember {
23-
constructor(clientInCtr = new DynamoDB()) {
23+
constructor(clientInCtr = new AWS_DynamoDB()) {
2424
this.clientInClass = clientInCtr;
2525
}
2626

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const AWS_DynamoDB = require("aws-sdk/clients/dynamodb");
2+
3+
const client = new AWS_DynamoDB();
4+
5+
// Promise without params
6+
{
7+
// async/await
8+
try {
9+
await client.listTables().promise();
10+
console.log(data);
11+
} catch (err) {
12+
console.log(err, err.stack);
13+
}
14+
15+
// .then() and .catch()
16+
client
17+
.listTables()
18+
.promise()
19+
.then((data) => console.log(data))
20+
.catch((err) => console.log(err, err.stack));
21+
22+
// Client as class member
23+
class ClientClassMember {
24+
constructor(clientInCtr = new AWS_DynamoDB()) {
25+
this.clientInClass = clientInCtr;
26+
}
27+
28+
async listTables() {
29+
return await this.clientInClass.listTables().promise();
30+
}
31+
}
32+
33+
// Variable declarator
34+
const listTablesPromise = client.listTables().promise();
35+
36+
// Promise call on request in variable declarator
37+
const listTablesRequest = client.listTables();
38+
listTablesRequest
39+
.promise()
40+
.then((data) => console.log(data))
41+
.catch((err) => console.log(err, err.stack));
42+
}
43+
44+
// Promise with params
45+
{
46+
// async/await
47+
try {
48+
await client.listTagsOfResource({ ResourceArn: "STRING_VALUE" }).promise();
49+
console.log(data);
50+
} catch (err) {
51+
console.log(err, err.stack);
52+
}
53+
54+
// .then() and .catch()
55+
client
56+
.listTagsOfResource({ ResourceArn: "STRING_VALUE" })
57+
.promise()
58+
.then((data) => console.log(data))
59+
.catch((err) => console.log(err, err.stack));
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const {
2+
DynamoDB: AWS_DynamoDB
3+
} = require("@aws-sdk/client-dynamodb");
4+
5+
const client = new AWS_DynamoDB();
6+
7+
// Promise without params
8+
{
9+
// async/await
10+
try {
11+
await client.listTables();
12+
console.log(data);
13+
} catch (err) {
14+
console.log(err, err.stack);
15+
}
16+
17+
// .then() and .catch()
18+
client
19+
.listTables()
20+
.then((data) => console.log(data))
21+
.catch((err) => console.log(err, err.stack));
22+
23+
// Client as class member
24+
class ClientClassMember {
25+
constructor(clientInCtr = new AWS_DynamoDB()) {
26+
this.clientInClass = clientInCtr;
27+
}
28+
29+
async listTables() {
30+
return await this.clientInClass.listTables();
31+
}
32+
}
33+
34+
// Variable declarator
35+
const listTablesPromise = client.listTables();
36+
37+
// Promise call on request in variable declarator
38+
const listTablesRequest = client.listTables();
39+
listTablesRequest
40+
.then((data) => console.log(data))
41+
.catch((err) => console.log(err, err.stack));
42+
}
43+
44+
// Promise with params
45+
{
46+
// async/await
47+
try {
48+
await client.listTagsOfResource({ ResourceArn: "STRING_VALUE" });
49+
console.log(data);
50+
} catch (err) {
51+
console.log(err, err.stack);
52+
}
53+
54+
// .then() and .catch()
55+
client
56+
.listTagsOfResource({ ResourceArn: "STRING_VALUE" })
57+
.then((data) => console.log(data))
58+
.catch((err) => console.log(err, err.stack));
59+
}

0 commit comments

Comments
 (0)