diff --git a/.changeset/wicked-fireants-tickle.md b/.changeset/wicked-fireants-tickle.md new file mode 100644 index 00000000..09669cc5 --- /dev/null +++ b/.changeset/wicked-fireants-tickle.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Reduce number of calls to source API in getV2ClientNamesRecord diff --git a/src/transforms/v2-to-v3/utils/get/getImportLocalNameForClient.ts b/src/transforms/v2-to-v3/utils/get/getImportLocalNameForClient.ts deleted file mode 100644 index 06781cce..00000000 --- a/src/transforms/v2-to-v3/utils/get/getImportLocalNameForClient.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Collection, Identifier, ImportSpecifier, JSCodeshift } from "jscodeshift"; - -import { PACKAGE_NAME } from "../config"; -import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; - -const getImportSpecifiers = (j: JSCodeshift, source: Collection, sourceValue: string) => - source - .find(j.ImportDeclaration, { - type: "ImportDeclaration", - source: { value: sourceValue }, - }) - .nodes() - .map((importDeclaration) => importDeclaration.specifiers) - .flat(); - -export const getImportLocalNameForClient = ( - j: JSCodeshift, - source: Collection, - clientName: string -): string | undefined => { - const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter( - (specifier) => specifier?.type === "ImportSpecifier" - ) as ImportSpecifier[]; - - const clientImportSpecifier = specifiersFromNamedImport.find( - (specifier) => specifier?.imported.name === clientName - ); - if (clientImportSpecifier) { - return (clientImportSpecifier.local as Identifier).name; - } - - const deepImportPath = getV2ServiceModulePath(clientName); - const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter( - (specifier) => - ["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string) - ); - - if (specifiersFromDeepImport.length > 0) { - return (specifiersFromDeepImport[0]?.local as Identifier).name; - } - - return undefined; -}; diff --git a/src/transforms/v2-to-v3/utils/get/getRequireLocalNameForClient.ts b/src/transforms/v2-to-v3/utils/get/getRequireLocalNameForClient.ts deleted file mode 100644 index 34a771c1..00000000 --- a/src/transforms/v2-to-v3/utils/get/getRequireLocalNameForClient.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift"; - -import { PACKAGE_NAME } from "../config"; -import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; - -const getRequireIds = (j: JSCodeshift, source: Collection, sourceValue: string) => - source - .find(j.VariableDeclarator, { - init: { - type: "CallExpression", - callee: { type: "Identifier", name: "require" }, - arguments: [{ value: sourceValue }], - }, - }) - .nodes() - .map((variableDeclarator) => variableDeclarator.id); - -export const getRequireLocalNameForClient = ( - j: JSCodeshift, - source: Collection, - clientName: string -): string | undefined => { - const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME) - .filter((id) => id.type === "ObjectPattern") - .map((objectPattern) => (objectPattern as ObjectPattern).properties) - .flat() as Property[]; - - const propertyWithClientName = idPropertiesFromNamedImport.find( - (property) => (property?.key as Identifier).name === clientName - ); - if (propertyWithClientName) { - return (propertyWithClientName.value as Identifier).name; - } - - const deepRequirePath = getV2ServiceModulePath(clientName); - const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter( - (id) => id.type === "Identifier" - ); - if (idsFromDefaultImport.length) { - return (idsFromDefaultImport[0] as Identifier).name; - } -}; diff --git a/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts index 8854bee2..df67f1e7 100644 --- a/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts +++ b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts @@ -1,22 +1,10 @@ import { Collection, JSCodeshift } from "jscodeshift"; -import { CLIENT_NAMES } from "../config"; import { hasRequire } from "../has"; -import { getImportLocalNameForClient } from "./getImportLocalNameForClient"; -import { getRequireLocalNameForClient } from "./getRequireLocalNameForClient"; +import { getV2ClientNamesRecordFromImport } from "./getV2ClientNamesRecordFromImport"; +import { getV2ClientNamesRecordFromRequire } from "./getV2ClientNamesRecordFromRequire"; -export const getV2ClientNamesRecord = ( - j: JSCodeshift, - source: Collection -): Record => { - const getIdentifierNameFn = hasRequire(j, source) - ? getRequireLocalNameForClient - : getImportLocalNameForClient; - - return Object.fromEntries( - CLIENT_NAMES.map((clientName) => [ - clientName, - getIdentifierNameFn(j, source, clientName), - ]).filter(([, v2ClientLocalName]) => v2ClientLocalName !== undefined) - ); -}; +export const getV2ClientNamesRecord = (j: JSCodeshift, source: Collection) => + hasRequire(j, source) + ? getV2ClientNamesRecordFromRequire(j, source) + : getV2ClientNamesRecordFromImport(j, source); diff --git a/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromImport.ts b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromImport.ts new file mode 100644 index 00000000..d9ef3925 --- /dev/null +++ b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromImport.ts @@ -0,0 +1,52 @@ +import { + Collection, + Identifier, + ImportDefaultSpecifier, + ImportNamespaceSpecifier, + ImportSpecifier, + JSCodeshift, +} from "jscodeshift"; + +import { CLIENT_NAMES, PACKAGE_NAME } from "../config"; +import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; + +const getImportSpecifiers = (j: JSCodeshift, source: Collection, sourceValue: string) => + source + .find(j.ImportDeclaration, { + type: "ImportDeclaration", + source: { value: sourceValue }, + }) + .nodes() + .map((importDeclaration) => importDeclaration.specifiers) + .flat() as (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[]; + +export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collection) => { + const v2ClientNamesRecord: Record = {}; + + const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter( + (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; + continue; + } + + 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; + } + } + + return v2ClientNamesRecord; +}; diff --git a/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromRequire.ts b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromRequire.ts new file mode 100644 index 00000000..1b99e67d --- /dev/null +++ b/src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromRequire.ts @@ -0,0 +1,46 @@ +import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift"; + +import { CLIENT_NAMES, PACKAGE_NAME } from "../config"; +import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; + +const getRequireIds = (j: JSCodeshift, source: Collection, sourceValue: string) => + source + .find(j.VariableDeclarator, { + init: { + type: "CallExpression", + callee: { type: "Identifier", name: "require" }, + arguments: [{ value: sourceValue }], + }, + }) + .nodes() + .map((variableDeclarator) => variableDeclarator.id); + +export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collection) => { + const v2ClientNamesRecord: Record = {}; + + const idPropertiesFromNamedImport = 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; + continue; + } + + const deepRequirePath = getV2ServiceModulePath(clientName); + const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter( + (id) => id.type === "Identifier" + ); + if (idsFromDefaultImport.length) { + v2ClientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name; + } + } + + return v2ClientNamesRecord; +};