Skip to content

Commit 334a834

Browse files
authored
Process clients from TS Type Reference (#77)
1 parent 39b96b4 commit 334a834

16 files changed

+197
-29
lines changed

.changeset/tricky-flowers-talk.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+
Process clients from TS Type Reference
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS from "aws-sdk";
2+
3+
export const listTables = (client: AWS.DynamoDB) => client.listTables().promise();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
export const listTables = (client: DynamoDB) => client.listTables();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import DynamoDB from "aws-sdk/clients/dynamodb";
2+
3+
export const listTables = (client: DynamoDB) => client.listTables().promise();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
export const listTables = (client: DynamoDB) => client.listTables();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ describe("v2-to-v3", () => {
1717
const outputCode = readFileSync(outputPath, "utf8");
1818
const input = { path: inputPath, source: inputCode };
1919

20-
it.each([{}, { parser: "ts" }])("with testOptions: %o", (testOptions) => {
21-
runInlineTest(transformer, null, input, outputCode, testOptions);
22-
});
20+
// Some tests are tsonly as they fail with babel parser
21+
// Refs: https://github.com/facebook/jscodeshift/issues/488
22+
it.each([{ parser: "ts" }, ...(testFilePrefix.startsWith("tsonly-") ? [] : [{}])])(
23+
"with testOptions: %o",
24+
(testOptions) => {
25+
runInlineTest(transformer, null, input, outputCode, testOptions);
26+
}
27+
);
2328
});
2429
});

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
removePromiseCalls,
1111
removeV2ClientModule,
1212
replaceClientCreation,
13+
replaceTSTypeReference,
1314
} from "./utils";
1415

1516
export default function transformer(file: FileInfo, api: API) {
@@ -31,6 +32,7 @@ export default function transformer(file: FileInfo, api: API) {
3132
removeV2ClientModule(j, source, v2ClientName);
3233
removePromiseCalls(j, source, { v2DefaultModuleName, v2ClientName });
3334
replaceClientCreation(j, source, { v2DefaultModuleName, v2ClientName, v3ClientName });
35+
replaceTSTypeReference(j, source, { v2DefaultModuleName, v2ClientName, v3ClientName });
3436
}
3537

3638
removeDefaultModuleIfNotUsed(j, source, v2DefaultModuleName);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicates";
4+
import { getV2ClientIdNamesFromNewExpr } from "./getV2ClientIdNamesFromNewExpr";
5+
import { getV2ClientIdNamesFromTSTypeRef } from "./getV2ClientIdNamesFromTSTypeRef";
6+
7+
export interface GetV2ClientIdNamesOptions {
8+
v2ClientName: string;
9+
v2DefaultModuleName: string;
10+
}
11+
12+
export const getV2ClientIdNames = (
13+
j: JSCodeshift,
14+
source: Collection<unknown>,
15+
{ v2DefaultModuleName, v2ClientName }: GetV2ClientIdNamesOptions
16+
): string[] => {
17+
const v2ClientIdNamesFromNewExpr = getV2ClientIdNamesFromNewExpr(j, source, {
18+
v2DefaultModuleName,
19+
v2ClientName,
20+
});
21+
22+
const v2ClientIdNamesFromTSTypeRef = getV2ClientIdNamesFromTSTypeRef(j, source, {
23+
v2DefaultModuleName,
24+
v2ClientName,
25+
});
26+
27+
return getMergedArrayWithoutDuplicates(v2ClientIdNamesFromNewExpr, v2ClientIdNamesFromTSTypeRef);
28+
};

src/transforms/v2-to-v3/utils/getClientIdentifierNames.ts renamed to src/transforms/v2-to-v3/utils/getV2ClientIdNamesFromNewExpr.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
22

33
import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicates";
44

5-
export interface GetClientIdentifierNamesOptions {
5+
export interface GetV2ClientIdNamesFromNewExprOptions {
66
v2ClientName: string;
77
v2DefaultModuleName: string;
88
}
99

10-
export const getClientIdentifierNames = (
10+
export const getV2ClientIdNamesFromNewExpr = (
1111
j: JSCodeshift,
1212
source: Collection<unknown>,
13-
{ v2DefaultModuleName, v2ClientName }: GetClientIdentifierNamesOptions
13+
{ v2DefaultModuleName, v2ClientName }: GetV2ClientIdNamesFromNewExprOptions
1414
): string[] => {
15-
const clientIdentifierNamesFromDefaultImport = source
15+
const clientIdNamesFromDefaultModule = source
1616
.find(j.VariableDeclarator, {
1717
id: { type: "Identifier" },
1818
init: {
@@ -26,7 +26,7 @@ export const getClientIdentifierNames = (
2626
.nodes()
2727
.map((variableDeclarator) => (variableDeclarator.id as Identifier).name);
2828

29-
const clientIdentifierNamesFromClientImport = source
29+
const clientIdNamesFromServiceModule = source
3030
.find(j.VariableDeclarator, {
3131
id: { type: "Identifier" },
3232
init: {
@@ -38,7 +38,7 @@ export const getClientIdentifierNames = (
3838
.map((variableDeclarator) => (variableDeclarator.id as Identifier).name);
3939

4040
return getMergedArrayWithoutDuplicates(
41-
clientIdentifierNamesFromDefaultImport,
42-
clientIdentifierNamesFromClientImport
41+
clientIdNamesFromDefaultModule,
42+
clientIdNamesFromServiceModule
4343
);
4444
};
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 GetV2ClientIdNamesFromTSTypeRefOptions {
6+
v2ClientName: string;
7+
v2DefaultModuleName: string;
8+
}
9+
10+
export const getV2ClientIdNamesFromTSTypeRef = (
11+
j: JSCodeshift,
12+
source: Collection<unknown>,
13+
{ v2DefaultModuleName, v2ClientName }: GetV2ClientIdNamesFromTSTypeRefOptions
14+
): string[] => {
15+
const clientIdNamesFromDefaultModule = source
16+
.find(j.Identifier, {
17+
typeAnnotation: {
18+
typeAnnotation: {
19+
typeName: {
20+
left: { name: v2DefaultModuleName },
21+
right: { name: v2ClientName },
22+
},
23+
},
24+
},
25+
})
26+
.nodes()
27+
.map((identifier) => identifier.name);
28+
29+
const clientIdNamesFromServiceModule = source
30+
.find(j.Identifier, {
31+
typeAnnotation: {
32+
typeAnnotation: {
33+
typeName: { type: "Identifier", name: v2ClientName },
34+
},
35+
},
36+
})
37+
.nodes()
38+
.map((identifier) => identifier.name);
39+
40+
return getMergedArrayWithoutDuplicates(
41+
clientIdNamesFromDefaultModule,
42+
clientIdNamesFromServiceModule
43+
);
44+
};

0 commit comments

Comments
 (0)