Skip to content

Commit aba497a

Browse files
authored
Remove enforcement of v2GlobalName to string (#234)
1 parent 10be584 commit aba497a

11 files changed

+55
-65
lines changed

.changeset/thirty-cycles-know.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 enforcement of v2GlobalName to string

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export default function transformer(file: FileInfo, api: API) {
1818
const j = isTypeScriptFile(file.path) ? api.jscodeshift.withParser("ts") : api.jscodeshift;
1919
const source = j(file.source);
2020

21-
// ToDo: Make v2GlobalName optional downstream as it can be undefined.
22-
const v2GlobalName = getV2GlobalName(j, source) as string;
21+
const v2GlobalName = getV2GlobalName(j, source);
2322
const v2ClientNames = getV2ClientNames(j, source);
2423

2524
if (!v2GlobalName && v2ClientNames.length === 0) {
@@ -45,7 +44,9 @@ export default function transformer(file: FileInfo, api: API) {
4544
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
4645
}
4746

48-
removeV2GlobalModule(j, source, v2GlobalName);
47+
if (v2GlobalName) {
48+
removeV2GlobalModule(j, source, v2GlobalName);
49+
}
4950

5051
return source.toSource();
5152
}

src/transforms/v2-to-v3/utils/add/addV3ClientModules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { addV3ClientRequires } from "./addV3ClientRequires";
66

77
export interface AddV3ClientModulesOptions {
88
v2ClientName: string;
9+
v2GlobalName?: string;
910
v3ClientName: string;
1011
v3ClientPackageName: string;
11-
v2GlobalName: string;
1212
}
1313

1414
export const addV3ClientModules = (

src/transforms/v2-to-v3/utils/get/getV2ClientIdNamesFromNewExpr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getV2ClientNewExpression } from "./getV2ClientNewExpression";
55

66
export interface GetV2ClientIdNamesFromNewExprOptions {
77
v2ClientName: string;
8-
v2GlobalName: string;
8+
v2GlobalName?: string;
99
}
1010

1111
const getNamesFromVariableDeclarator = (

src/transforms/v2-to-v3/utils/get/getV2ClientIdNamesFromTSTypeRef.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicat
44

55
export interface GetV2ClientIdNamesFromTSTypeRefOptions {
66
v2ClientName: string;
7-
v2GlobalName: string;
7+
v2GlobalName?: string;
88
}
99

1010
export const getV2ClientIdNamesFromTSTypeRef = (
1111
j: JSCodeshift,
1212
source: Collection<unknown>,
1313
{ v2GlobalName, v2ClientName }: GetV2ClientIdNamesFromTSTypeRefOptions
1414
): string[] => {
15-
const clientIdNamesFromGlobalModule = source
16-
.find(j.Identifier, {
17-
typeAnnotation: {
18-
typeAnnotation: {
19-
typeName: {
20-
left: { name: v2GlobalName },
21-
right: { name: v2ClientName },
15+
const namesFromGlobalName = v2GlobalName
16+
? source
17+
.find(j.Identifier, {
18+
typeAnnotation: {
19+
typeAnnotation: {
20+
typeName: {
21+
left: { name: v2GlobalName },
22+
right: { name: v2ClientName },
23+
},
24+
},
2225
},
23-
},
24-
},
25-
})
26-
.nodes()
27-
.map((identifier) => identifier.name);
26+
})
27+
.nodes()
28+
.map((identifier) => identifier.name)
29+
: [];
2830

29-
const clientIdNamesFromServiceModule = source
31+
const namesFromClientName = source
3032
.find(j.Identifier, {
3133
typeAnnotation: {
3234
typeAnnotation: {
@@ -37,8 +39,5 @@ export const getV2ClientIdNamesFromTSTypeRef = (
3739
.nodes()
3840
.map((identifier) => identifier.name);
3941

40-
return getMergedArrayWithoutDuplicates(
41-
clientIdNamesFromGlobalModule,
42-
clientIdNamesFromServiceModule
43-
);
42+
return getMergedArrayWithoutDuplicates(namesFromGlobalName, namesFromClientName);
4443
};

src/transforms/v2-to-v3/utils/get/getV2ClientIdentifiers.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,18 @@ import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicat
44
import { getV2ClientIdNamesFromNewExpr } from "./getV2ClientIdNamesFromNewExpr";
55
import { getV2ClientIdNamesFromTSTypeRef } from "./getV2ClientIdNamesFromTSTypeRef";
66

7-
export interface GetV2ClientIdNamesOptions {
7+
export interface GetV2ClientIdentifiersOptions {
88
v2ClientName: string;
9-
v2GlobalName: string;
9+
v2GlobalName?: string;
1010
}
1111

1212
export const getV2ClientIdentifiers = (
1313
j: JSCodeshift,
1414
source: Collection<unknown>,
15-
{ v2GlobalName, v2ClientName }: GetV2ClientIdNamesOptions
15+
options: GetV2ClientIdentifiersOptions
1616
): Identifier[] => {
17-
const v2ClientIdNamesFromNewExpr = getV2ClientIdNamesFromNewExpr(j, source, {
18-
v2GlobalName,
19-
v2ClientName,
20-
});
21-
22-
const v2ClientIdNamesFromTSTypeRef = getV2ClientIdNamesFromTSTypeRef(j, source, {
23-
v2GlobalName,
24-
v2ClientName,
25-
});
26-
27-
const clientIdNames = getMergedArrayWithoutDuplicates(
28-
v2ClientIdNamesFromNewExpr,
29-
v2ClientIdNamesFromTSTypeRef
30-
);
31-
17+
const namesFromNewExpr = getV2ClientIdNamesFromNewExpr(j, source, options);
18+
const namesFromTSTypeRef = getV2ClientIdNamesFromTSTypeRef(j, source, options);
19+
const clientIdNames = getMergedArrayWithoutDuplicates(namesFromNewExpr, namesFromTSTypeRef);
3220
return clientIdNames.map((clientidName) => ({ type: "Identifier", name: clientidName }));
3321
};

src/transforms/v2-to-v3/utils/get/getV2ClientTypeNames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getV2ServiceModulePath } from "./getV2ServiceModulePath";
1313

1414
export interface GetV2ClientTypeNamesOptions {
1515
v2ClientName: string;
16-
v2GlobalName: string;
16+
v2GlobalName?: string;
1717
}
1818

1919
const getRightIdentifierName = (

src/transforms/v2-to-v3/utils/remove/removePromiseCalls.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ import { removePromiseForCallExpression } from "./removePromiseForCallExpression
55

66
export interface RemovePromiseCallsOptions {
77
v2ClientName: string;
8-
v2GlobalName: string;
8+
v2GlobalName?: string;
99
}
1010

1111
// Removes .promise() from client API calls.
1212
export const removePromiseCalls = (
1313
j: JSCodeshift,
1414
source: Collection<unknown>,
15-
{ v2GlobalName, v2ClientName }: RemovePromiseCallsOptions
15+
options: RemovePromiseCallsOptions
1616
): void => {
17-
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, {
18-
v2GlobalName,
19-
v2ClientName,
20-
});
17+
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);
2118
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);
2219

2320
for (const v2ClientId of [...v2ClientIdentifiers, ...v2ClientIdThisExpressions]) {

src/transforms/v2-to-v3/utils/remove/removeV2ClientModule.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { removeRequireIdentifierName } from "./removeRequireIdentifierName";
88

99
export interface RemoveV2ClientModuleOptions {
1010
v2ClientName: string;
11-
v2GlobalName: string;
11+
v2GlobalName?: string;
1212
}
1313

1414
export const removeV2ClientModule = (
1515
j: JSCodeshift,
1616
source: Collection<unknown>,
17-
{ v2ClientName, v2GlobalName }: RemoveV2ClientModuleOptions
17+
options: RemoveV2ClientModuleOptions
1818
) => {
19+
const { v2ClientName } = options;
1920
const literalValue = getV2ServiceModulePath(v2ClientName);
2021
const removeIdentifierNameOptions = {
2122
identifierName: v2ClientName,
@@ -27,10 +28,7 @@ export const removeV2ClientModule = (
2728
} else {
2829
removeImportIdentifierName(j, source, removeIdentifierNameOptions);
2930

30-
const v2ClientTypeNames = getV2ClientTypeNames(j, source, {
31-
v2ClientName,
32-
v2GlobalName,
33-
});
31+
const v2ClientTypeNames = getV2ClientTypeNames(j, source, options);
3432
for (const v2ClientTypeName of v2ClientTypeNames) {
3533
if (isV2ClientInputOutputType(v2ClientTypeName)) {
3634
removeImportIdentifierName(j, source, {

src/transforms/v2-to-v3/utils/replace/replaceClientCreation.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ import { getV2ClientNewExpression } from "../get";
44

55
export interface ReplaceClientCreationOptions {
66
v2ClientName: string;
7+
v2GlobalName?: string;
78
v3ClientName: string;
8-
v2GlobalName: string;
99
}
1010

1111
// Replace v2 client creation with v3 client creation.
1212
export const replaceClientCreation = (
1313
j: JSCodeshift,
1414
source: Collection<unknown>,
15-
{ v2GlobalName, v2ClientName, v3ClientName }: ReplaceClientCreationOptions
15+
{ v2ClientName, v2GlobalName, v3ClientName }: ReplaceClientCreationOptions
1616
): void => {
17-
// Replace clients created with default module.
18-
source
19-
.find(j.NewExpression, getV2ClientNewExpression({ v2GlobalName, v2ClientName }))
20-
.replaceWith((v2ClientNewExpression) =>
21-
j.newExpression(j.identifier(v3ClientName), v2ClientNewExpression.node.arguments)
22-
);
17+
if (v2GlobalName) {
18+
// Replace clients created with default module.
19+
source
20+
.find(j.NewExpression, getV2ClientNewExpression({ v2GlobalName, v2ClientName }))
21+
.replaceWith((v2ClientNewExpression) =>
22+
j.newExpression(j.identifier(v3ClientName), v2ClientNewExpression.node.arguments)
23+
);
24+
}
2325

2426
// Replace clients created with client module.
2527
source

0 commit comments

Comments
 (0)