Skip to content

Commit

Permalink
Deep import search only for available clients (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 30, 2022
1 parent 2c8312f commit 9b3dfb1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-zoos-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Deep import search only for available clients
4 changes: 3 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getClientMetadata,
getV2ClientNamesFromGlobal,
getV2ClientNamesRecord,
getV2ClientNamesWithServiceModule,
getV2GlobalName,
isTypeScriptFile,
removePromiseCalls,
Expand All @@ -19,7 +20,8 @@ const transformer = async (file: FileInfo, api: API) => {
const source = j(file.source);

const v2GlobalName = getV2GlobalName(j, source);
const v2ClientNamesRecord = getV2ClientNamesRecord(j, source);
const v2ClientNamesWithServiceModule = getV2ClientNamesWithServiceModule(file.source);
const v2ClientNamesRecord = getV2ClientNamesRecord(j, source, v2ClientNamesWithServiceModule);

if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
return source.toSource();
Expand Down
10 changes: 7 additions & 3 deletions src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { hasRequire } from "../has";
import { getV2ClientNamesRecordFromImport } from "./getV2ClientNamesRecordFromImport";
import { getV2ClientNamesRecordFromRequire } from "./getV2ClientNamesRecordFromRequire";

export const getV2ClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) =>
export const getV2ClientNamesRecord = (
j: JSCodeshift,
source: Collection<unknown>,
v2ClientNamesWithServiceModule: string[]
) =>
hasRequire(j, source)
? getV2ClientNamesRecordFromRequire(j, source)
: getV2ClientNamesRecordFromImport(j, source);
? getV2ClientNamesRecordFromRequire(j, source, v2ClientNamesWithServiceModule)
: getV2ClientNamesRecordFromImport(j, source, v2ClientNamesWithServiceModule);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const getImportSpecifiers = (j: JSCodeshift, source: Collection<unknown>, source
.map((importDeclaration) => importDeclaration.specifiers)
.flat() as (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[];

export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collection<unknown>) => {
export const getV2ClientNamesRecordFromImport = (
j: JSCodeshift,
source: Collection<unknown>,
v2ClientNamesWithServiceModule: string[]
) => {
const v2ClientNamesRecord: Record<string, string> = {};

const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
Expand All @@ -31,18 +35,17 @@ export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collect
const clientImportSpecifier = specifiersFromNamedImport.find(
(specifier) => specifier?.imported.name === clientName
);

if (clientImportSpecifier) {
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name;
continue;
}
}

for (const clientName of v2ClientNamesWithServiceModule) {
const deepImportPath = getV2ServiceModulePath(clientName);
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
(specifier) =>
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
);

if (specifiersFromDeepImport.length > 0) {
v2ClientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const getRequireIds = (j: JSCodeshift, source: Collection<unknown>, sourceValue:
.nodes()
.map((variableDeclarator) => variableDeclarator.id);

export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collection<unknown>) => {
export const getV2ClientNamesRecordFromRequire = (
j: JSCodeshift,
source: Collection<unknown>,
v2ClientNamesWithServiceModule: string[]
) => {
const v2ClientNamesRecord: Record<string, string> = {};

const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME)
Expand All @@ -27,12 +31,12 @@ export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collec
const propertyWithClientName = idPropertiesFromNamedImport.find(
(property) => (property?.key as Identifier).name === clientName
);

if (propertyWithClientName) {
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name;
continue;
}
}

for (const clientName of v2ClientNamesWithServiceModule) {
const deepRequirePath = getV2ServiceModulePath(clientName);
const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter(
(id) => id.type === "Identifier"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";

const SERVICE_MODULE_PATH_REGEXP = new RegExp(`${PACKAGE_NAME}/clients/([\\w]*)`, "g");

export const getV2ClientNamesWithServiceModule = (fileSource: string) => {
const clientsFromServiceModule = new Set(
[...fileSource.matchAll(SERVICE_MODULE_PATH_REGEXP)].map((regExpMatch) => regExpMatch[1]).flat()
);

return CLIENT_NAMES.filter((clientName) =>
clientsFromServiceModule.has(clientName.toLowerCase())
);
};
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/get/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./getV2ClientIdentifiers";
export * from "./getV2ClientIdThisExpressions";
export * from "./getV2ClientNamesFromGlobal";
export * from "./getV2ClientNamesRecord";
export * from "./getV2ClientNamesWithServiceModule";
export * from "./getV2ClientNewExpression";
export * from "./getV2ClientTSTypeRef";
export * from "./getV2ClientTypeNames";
Expand Down

0 comments on commit 9b3dfb1

Please sign in to comment.