Skip to content

Commit 9f8d6dc

Browse files
authored
Make importedName optional in ImportSpecifierType (#788)
1 parent d8249e8 commit 9f8d6dc

File tree

13 files changed

+48
-51
lines changed

13 files changed

+48
-51
lines changed

.changeset/proud-kings-exercise.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+
Make importedName optional in ImportSpecifierType

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const replaceAwsIdentity = (
6969
if (credsNewExpressions.size() > 0) {
7070
addNamedModule(j, source, {
7171
importType,
72-
importedName: v3ProviderName,
72+
localName: v3ProviderName,
7373
packageName: identityPackageName,
7474
});
7575
credsNewExpressions.replaceWith(({ node }) =>

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

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

33
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4-
import {
5-
ImportSpecifierDefault,
6-
ImportSpecifierPattern,
7-
getImportEqualsDeclarationType,
8-
} from "../modules";
4+
import { getImportEqualsDeclarationType } from "../modules";
95
import { getImportSpecifiers } from "../modules/importModule";
106
import { getClientDeepImportPath } from "../utils";
117

@@ -17,23 +13,24 @@ export const getClientNamesRecordFromImport = (
1713
const clientNamesRecord: Record<string, string> = {};
1814

1915
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
20-
(importSpecifier) => typeof importSpecifier === "object"
21-
) as ImportSpecifierPattern[];
16+
(importSpecifier) => importSpecifier.importedName
17+
);
2218

2319
for (const { importedName, localName } of specifiersFromNamedImport) {
24-
if (CLIENT_NAMES.includes(importedName)) {
25-
clientNamesRecord[importedName] = localName ?? importedName;
20+
const clientName = importedName ?? localName;
21+
if (CLIENT_NAMES.includes(clientName)) {
22+
clientNamesRecord[clientName] = localName;
2623
}
2724
}
2825

2926
for (const clientName of clientNamesFromDeepImport) {
3027
const deepImportPath = getClientDeepImportPath(clientName);
3128

3229
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
33-
(importSpecifier) => typeof importSpecifier === "string"
34-
) as ImportSpecifierDefault[];
30+
(importSpecifier) => !importSpecifier.importedName
31+
);
3532
if (specifiersFromDeepImport.length > 0) {
36-
clientNamesRecord[clientName] = specifiersFromDeepImport[0];
33+
clientNamesRecord[clientName] = specifiersFromDeepImport[0].localName;
3734
}
3835

3936
const identifiersFromImportEquals = source.find(

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

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

33
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4-
import { ImportSpecifierDefault, ImportSpecifierPattern } from "../modules";
54
import { getImportSpecifiers } from "../modules/requireModule";
65
import { getClientDeepImportPath } from "../utils";
76

@@ -13,22 +12,23 @@ export const getClientNamesRecordFromRequire = (
1312
const clientNamesRecord: Record<string, string> = {};
1413

1514
const idPropertiesFromObjectPattern = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
16-
(importSpecifier) => typeof importSpecifier === "object"
17-
) as ImportSpecifierPattern[];
15+
(importSpecifier) => importSpecifier.importedName
16+
);
1817

1918
for (const { importedName, localName } of idPropertiesFromObjectPattern) {
20-
if (CLIENT_NAMES.includes(importedName)) {
21-
clientNamesRecord[importedName] = localName || importedName;
19+
const clientName = importedName ?? localName;
20+
if (CLIENT_NAMES.includes(clientName)) {
21+
clientNamesRecord[clientName] = localName;
2222
}
2323
}
2424

2525
for (const clientName of clientNamesFromDeepImport) {
2626
const deepImportPath = getClientDeepImportPath(clientName);
2727
const idsFromDefaultImport = getImportSpecifiers(j, source, deepImportPath).filter(
28-
(importSpecifier) => typeof importSpecifier === "string"
29-
) as ImportSpecifierDefault[];
28+
(importSpecifier) => !importSpecifier.importedName
29+
);
3030
if (idsFromDefaultImport.length) {
31-
clientNamesRecord[clientName] = idsFromDefaultImport[0];
31+
clientNamesRecord[clientName] = idsFromDefaultImport[0].localName;
3232
}
3333
}
3434

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const addClientModules = (
4545
for (const v3ClientType of v3ClientTypes) {
4646
addNamedModule(j, source, {
4747
importType,
48-
importedName: v3ClientType,
48+
localName: v3ClientType,
4949
packageName: v3ClientPackageName,
5050
});
5151
}
@@ -63,7 +63,7 @@ export const addClientModules = (
6363
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
6464
addNamedModule(j, source, {
6565
importType,
66-
importedName: v3WaiterApiName,
66+
localName: v3WaiterApiName,
6767
packageName: v3ClientPackageName,
6868
});
6969
}
@@ -72,21 +72,21 @@ export const addClientModules = (
7272
if (isS3UploadApiUsed(j, source, clientIdentifiers)) {
7373
addNamedModule(j, source, {
7474
importType,
75-
importedName: "Upload",
75+
localName: "Upload",
7676
packageName: "@aws-sdk/lib-storage",
7777
});
7878
}
7979

8080
if (isS3GetSignedUrlApiUsed(j, source, clientIdentifiers)) {
8181
addNamedModule(j, source, {
8282
importType,
83-
importedName: "getSignedUrl",
83+
localName: "getSignedUrl",
8484
packageName: "@aws-sdk/s3-request-presigner",
8585
});
8686
for (const apiName of getS3SignedUrlApiNames(j, source, clientIdentifiers)) {
8787
addNamedModule(j, source, {
8888
importType,
89-
importedName: getCommandName(apiName),
89+
localName: getCommandName(apiName),
9090
packageName: v3ClientPackageName,
9191
});
9292
}
@@ -111,15 +111,15 @@ export const addClientModules = (
111111
for (const docClientType of docClientTypes) {
112112
addNamedModule(j, source, {
113113
importType,
114-
importedName: docClientType,
114+
localName: docClientType,
115115
packageName: "@aws-sdk/lib-dynamodb",
116116
});
117117
}
118118

119119
if (docClientNewExpressionCount > 0) {
120120
addNamedModule(j, source, {
121121
importType,
122-
importedName: DYNAMODB_DOCUMENT,
122+
localName: DYNAMODB_DOCUMENT,
123123
packageName: "@aws-sdk/lib-dynamodb",
124124
});
125125
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { PACKAGE_NAME } from "../config";
44
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
55
import { getImportSpecifiers } from "./importModule";
66
import { getRequireDeclarators } from "./requireModule";
7-
import { ImportSpecifierDefault } from ".";
87

98
export const getGlobalNameFromModule = (
109
j: JSCodeshift,
@@ -19,11 +18,11 @@ export const getGlobalNameFromModule = (
1918
}
2019

2120
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
22-
(importSpecifier) => typeof importSpecifier === "string"
23-
) as ImportSpecifierDefault[];
21+
(importSpecifier) => !importSpecifier.importedName
22+
);
2423

2524
if (importDefaultSpecifiers.length > 0) {
26-
return importDefaultSpecifiers[0];
25+
return importDefaultSpecifiers[0].localName;
2726
}
2827

2928
const importEqualsDeclarations = source.find(

src/transforms/v2-to-v3/modules/importEqualsModule/addNamedModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const addNamedModule = (
1010
source: Collection<unknown>,
1111
options: ModulesOptions
1212
) => {
13-
const { importedName, localName = importedName, packageName } = options;
13+
const { localName, importedName = localName, packageName } = options;
1414

1515
const defaultLocalName = getDefaultName(packageName);
1616

src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const addNamedModule = (
99
source: Collection<unknown>,
1010
options: ModulesOptions
1111
) => {
12-
const { importedName, localName = importedName, packageName } = options;
12+
const { localName, importedName = localName, packageName } = options;
1313

1414
const importSpecifiers = getImportSpecifiers(j, source, packageName);
1515

src/transforms/v2-to-v3/modules/importModule/getImportSpecifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const getImportSpecifiers = (
2424
case "ImportNamespaceSpecifier":
2525
case "ImportDefaultSpecifier": {
2626
if (specifier.local) {
27-
importSpecifiers.add(specifier.local.name);
27+
importSpecifiers.add({ localName: specifier.local.name });
2828
}
2929
break;
3030
}

src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export const addNamedModule = (
1818
source: Collection<unknown>,
1919
options: ModulesOptions
2020
) => {
21-
const { importedName, localName = importedName, packageName } = options;
21+
const { localName, importedName = localName, packageName } = options;
2222

2323
const clientObjectProperty = j.objectProperty.from({
2424
key: j.identifier(importedName),
25-
value: j.identifier(localName ?? importedName),
25+
value: j.identifier(localName),
2626
shorthand: true,
2727
});
2828
const existingRequires = getRequireDeclarators(j, source, packageName);

0 commit comments

Comments
 (0)