Skip to content

Commit a301bf9

Browse files
authored
Split getClientNewExpression into global/local name (#899)
1 parent 01efdc8 commit a301bf9

10 files changed

+99
-99
lines changed

.changeset/hungry-grapes-burn.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+
Split getClientNewExpression into global/local name

src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import type {
77
} from "jscodeshift";
88

99
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
10-
import { getClientNewExpression } from "../utils";
10+
import {
11+
getClientNewExpressionFromGlobalName,
12+
getClientNewExpressionFromLocalName,
13+
} from "../utils";
1114

1215
export interface GetClientIdNamesFromNewExprOptions {
1316
v2ClientName: string;
@@ -70,27 +73,27 @@ export const getClientIdNamesFromNewExpr = (
7073
]) {
7174
if (v2GlobalName) {
7275
namesFromGlobalModule.push(
73-
...getNames(j, source, getClientNewExpression({ v2GlobalName, v2ClientName }))
76+
...getNames(j, source, getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName))
7477
);
7578
if (v2ClientName === DYNAMODB) {
7679
namesFromGlobalModule.push(
7780
...getNames(
7881
j,
7982
source,
80-
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
83+
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
8184
)
8285
);
8386
}
8487
}
8588
namesFromServiceModule.push(
86-
...getNames(j, source, getClientNewExpression({ v2ClientLocalName }))
89+
...getNames(j, source, getClientNewExpressionFromLocalName(v2ClientLocalName))
8790
);
8891
if (v2ClientName === DYNAMODB) {
8992
namesFromServiceModule.push(
9093
...getNames(
9194
j,
9295
source,
93-
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
96+
getClientNewExpressionFromLocalName(`${v2ClientLocalName}.${DOCUMENT_CLIENT}`)
9497
)
9598
);
9699
}

src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";
2-
import { getClientNewExpression } from "../utils";
2+
import {
3+
getClientNewExpressionFromGlobalName,
4+
getClientNewExpressionFromLocalName,
5+
} from "../utils";
36
import { getNewClientExpression } from "./getNewClientExpression";
47

58
export interface ReplaceClientCreationOptions {
@@ -25,14 +28,14 @@ export const replaceClientCreation = (
2528
const clientName = v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName;
2629

2730
source
28-
.find(j.NewExpression, getClientNewExpression({ v2ClientName, v2ClientLocalName }))
31+
.find(j.NewExpression, getClientNewExpressionFromLocalName(v2ClientLocalName))
2932
.replaceWith((v2ClientNewExpression) =>
3033
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
3134
);
3235

3336
if (v2GlobalName) {
3437
source
35-
.find(j.NewExpression, getClientNewExpression({ v2GlobalName, v2ClientName }))
38+
.find(j.NewExpression, getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName))
3639
.replaceWith((v2ClientNewExpression) =>
3740
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
3841
);

src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { Collection, JSCodeshift } from "jscodeshift";
22

33
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
4-
import { getClientNewExpression } from "../utils";
4+
import {
5+
getClientNewExpressionFromGlobalName,
6+
getClientNewExpressionFromLocalName,
7+
} from "../utils";
58
import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs";
69

710
export interface ReplaceDocClientCreationOptions {
@@ -21,7 +24,7 @@ export const replaceDocClientCreation = (
2124
source
2225
.find(
2326
j.NewExpression,
24-
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
27+
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
2528
)
2629
.replaceWith((v2DocClientNewExpression) =>
2730
j.callExpression(
@@ -34,7 +37,7 @@ export const replaceDocClientCreation = (
3437
source
3538
.find(
3639
j.NewExpression,
37-
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
40+
getClientNewExpressionFromLocalName(`${v2ClientLocalName}.${DOCUMENT_CLIENT}`)
3841
)
3942
.replaceWith((v2DocClientNewExpression) =>
4043
j.callExpression(

src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import type { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";
22

33
import { DYNAMODB_DOCUMENT_CLIENT } from "../config";
4-
import { getClientNewExpression } from "../utils";
4+
import { getClientNewExpressionFromGlobalName } from "../utils";
55

66
export const getNamesFromNewExpr = (
77
j: JSCodeshift,
88
source: Collection<unknown>,
99
v2GlobalName: string
1010
): string[] => [
1111
...source
12-
.find(j.NewExpression, getClientNewExpression({ v2GlobalName }))
12+
.find(j.NewExpression, getClientNewExpressionFromGlobalName(v2GlobalName))
1313
.nodes()
1414
.map(
1515
(newExpression) => ((newExpression.callee as MemberExpression).property as Identifier).name
1616
),
1717
...source
1818
.find(
1919
j.NewExpression,
20-
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
20+
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
2121
)
2222
.nodes()
2323
.map(

src/transforms/v2-to-v3/modules/getNewExpressionCount.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { Collection, JSCodeshift } from "jscodeshift";
22

33
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
4-
import { getClientNewExpression } from "../utils";
4+
import {
5+
getClientNewExpressionFromGlobalName,
6+
getClientNewExpressionFromLocalName,
7+
} from "../utils";
58
import type { ClientModulesOptions } from "./types";
69

710
export const getNewExpressionCount = (
@@ -15,14 +18,14 @@ export const getNewExpressionCount = (
1518
if (v2GlobalName) {
1619
const newExpressionsFromGlobalName = source.find(
1720
j.NewExpression,
18-
getClientNewExpression({ v2ClientName, v2GlobalName })
21+
getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName)
1922
);
2023
newExpressionCount += newExpressionsFromGlobalName.length;
2124
}
2225

2326
const newExpressionsFromClientLocalName = source.find(
2427
j.NewExpression,
25-
getClientNewExpression({ v2ClientLocalName })
28+
getClientNewExpressionFromLocalName(v2ClientLocalName)
2629
);
2730
newExpressionCount += newExpressionsFromClientLocalName.length;
2831

src/transforms/v2-to-v3/utils/getClientNewExpression.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { NewExpression } from "jscodeshift";
2+
3+
export const getClientNewExpressionFromGlobalName = (
4+
v2GlobalName: string,
5+
v2ClientName?: string
6+
): NewExpression => {
7+
if (v2ClientName) {
8+
// Support for DynamoDB.DocumentClient
9+
const [clientName, subClientName] = v2ClientName.split(".");
10+
11+
if (subClientName) {
12+
return {
13+
type: "NewExpression",
14+
callee: {
15+
type: "MemberExpression",
16+
object: {
17+
type: "MemberExpression",
18+
object: { type: "Identifier", name: v2GlobalName },
19+
property: { type: "Identifier", name: clientName },
20+
},
21+
property: { type: "Identifier", name: subClientName },
22+
},
23+
} as NewExpression;
24+
}
25+
26+
return {
27+
type: "NewExpression",
28+
callee: {
29+
object: { type: "Identifier", name: v2GlobalName },
30+
property: { type: "Identifier", name: clientName },
31+
},
32+
} as NewExpression;
33+
}
34+
35+
return {
36+
type: "NewExpression",
37+
callee: {
38+
object: { type: "Identifier", name: v2GlobalName },
39+
property: { type: "Identifier" },
40+
},
41+
} as NewExpression;
42+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { NewExpression } from "jscodeshift";
2+
3+
export const getClientNewExpressionFromLocalName = (v2ClientLocalName: string): NewExpression => {
4+
// Support for DynamoDB.DocumentClient
5+
const [clientName, subClientName] = v2ClientLocalName.split(".");
6+
7+
if (subClientName) {
8+
return {
9+
type: "NewExpression",
10+
callee: {
11+
object: { type: "Identifier", name: clientName },
12+
property: { type: "Identifier", name: subClientName },
13+
},
14+
} as NewExpression;
15+
}
16+
17+
return {
18+
type: "NewExpression",
19+
callee: { type: "Identifier", name: clientName },
20+
} as NewExpression;
21+
};

src/transforms/v2-to-v3/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./getClientDeepImportPath";
2-
export * from "./getClientNewExpression";
2+
export * from "./getClientNewExpressionFromGlobalName";
3+
export * from "./getClientNewExpressionFromLocalName";
34
export * from "./getFormattedSourceString";
45
export * from "./getMostUsedIndentationType";
56
export * from "./getMostUsedStringLiteralQuote";

0 commit comments

Comments
 (0)