Skip to content

Commit

Permalink
Parse through named imports from global path instead of clients (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 30, 2022
1 parent 4473985 commit 35d0638
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-wolves-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Parse through named imports from global path instead of clients
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ export const getV2ClientNamesRecordFromImport = (
(specifier) => specifier?.type === "ImportSpecifier"
) as ImportSpecifier[];

for (const clientName of CLIENT_NAMES) {
const clientImportSpecifier = specifiersFromNamedImport.find(
(specifier) => specifier?.imported.name === clientName
);
if (clientImportSpecifier) {
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name;
for (const specifier of specifiersFromNamedImport) {
const importedName = specifier.imported.name;
const localName = (specifier.local as Identifier).name;
if (CLIENT_NAMES.includes(importedName)) {
v2ClientNamesRecord[importedName] = localName ?? importedName;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ export const getV2ClientNamesRecordFromRequire = (
) => {
const v2ClientNamesRecord: Record<string, string> = {};

const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME)
const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
.filter((id) => id.type === "ObjectPattern")
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
.flat() as Property[];

for (const clientName of CLIENT_NAMES) {
const propertyWithClientName = idPropertiesFromNamedImport.find(
(property) => (property?.key as Identifier).name === clientName
);
if (propertyWithClientName) {
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name;
for (const idProperty of idPropertiesFromObjectPattern) {
if (idProperty.type !== "Property") {
continue;
}
const key = idProperty.key as Identifier;
if (key.type !== "Identifier") {
continue;
}
const value = idProperty.value as Identifier;
if (value.type !== "Identifier") {
continue;
}
if (CLIENT_NAMES.includes(key.name)) {
v2ClientNamesRecord[key.name] = value.name;
}
}

Expand Down

0 comments on commit 35d0638

Please sign in to comment.