Skip to content

Commit 35d0638

Browse files
authored
Parse through named imports from global path instead of clients (#267)
1 parent 4473985 commit 35d0638

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

.changeset/wet-wolves-brake.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+
Parse through named imports from global path instead of clients

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ export const getV2ClientNamesRecordFromImport = (
3131
(specifier) => specifier?.type === "ImportSpecifier"
3232
) as ImportSpecifier[];
3333

34-
for (const clientName of CLIENT_NAMES) {
35-
const clientImportSpecifier = specifiersFromNamedImport.find(
36-
(specifier) => specifier?.imported.name === clientName
37-
);
38-
if (clientImportSpecifier) {
39-
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name;
34+
for (const specifier of specifiersFromNamedImport) {
35+
const importedName = specifier.imported.name;
36+
const localName = (specifier.local as Identifier).name;
37+
if (CLIENT_NAMES.includes(importedName)) {
38+
v2ClientNamesRecord[importedName] = localName ?? importedName;
4039
}
4140
}
4241

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ export const getV2ClientNamesRecordFromRequire = (
2222
) => {
2323
const v2ClientNamesRecord: Record<string, string> = {};
2424

25-
const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME)
25+
const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
2626
.filter((id) => id.type === "ObjectPattern")
2727
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
2828
.flat() as Property[];
2929

30-
for (const clientName of CLIENT_NAMES) {
31-
const propertyWithClientName = idPropertiesFromNamedImport.find(
32-
(property) => (property?.key as Identifier).name === clientName
33-
);
34-
if (propertyWithClientName) {
35-
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name;
30+
for (const idProperty of idPropertiesFromObjectPattern) {
31+
if (idProperty.type !== "Property") {
32+
continue;
33+
}
34+
const key = idProperty.key as Identifier;
35+
if (key.type !== "Identifier") {
36+
continue;
37+
}
38+
const value = idProperty.value as Identifier;
39+
if (value.type !== "Identifier") {
40+
continue;
41+
}
42+
if (CLIENT_NAMES.includes(key.name)) {
43+
v2ClientNamesRecord[key.name] = value.name;
3644
}
3745
}
3846

0 commit comments

Comments
 (0)