Skip to content

Commit

Permalink
Reduce number of calls to source API in getV2ClientNamesRecord (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 30, 2022
1 parent 0cbf643 commit a1134d4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-fireants-tickle.md
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Reduce number of calls to source API in getV2ClientNamesRecord
43 changes: 0 additions & 43 deletions src/transforms/v2-to-v3/utils/get/getImportLocalNameForClient.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/transforms/v2-to-v3/utils/get/getRequireLocalNameForClient.ts

This file was deleted.

24 changes: 6 additions & 18 deletions 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<unknown>
): Record<string, string> => {
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<unknown>) =>
hasRequire(j, source)
? getV2ClientNamesRecordFromRequire(j, source)
: getV2ClientNamesRecordFromImport(j, source);
@@ -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<unknown>, 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<unknown>) => {
const v2ClientNamesRecord: Record<string, string> = {};

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;
};
@@ -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<unknown>, 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<unknown>) => {
const v2ClientNamesRecord: Record<string, string> = {};

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;
};

0 comments on commit a1134d4

Please sign in to comment.