Skip to content

Commit a1134d4

Browse files
authored
Reduce number of calls to source API in getV2ClientNamesRecord (#260)
1 parent 0cbf643 commit a1134d4

File tree

6 files changed

+109
-103
lines changed

6 files changed

+109
-103
lines changed

.changeset/wicked-fireants-tickle.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+
Reduce number of calls to source API in getV2ClientNamesRecord

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { CLIENT_NAMES } from "../config";
43
import { hasRequire } from "../has";
5-
import { getImportLocalNameForClient } from "./getImportLocalNameForClient";
6-
import { getRequireLocalNameForClient } from "./getRequireLocalNameForClient";
4+
import { getV2ClientNamesRecordFromImport } from "./getV2ClientNamesRecordFromImport";
5+
import { getV2ClientNamesRecordFromRequire } from "./getV2ClientNamesRecordFromRequire";
76

8-
export const getV2ClientNamesRecord = (
9-
j: JSCodeshift,
10-
source: Collection<unknown>
11-
): Record<string, string> => {
12-
const getIdentifierNameFn = hasRequire(j, source)
13-
? getRequireLocalNameForClient
14-
: getImportLocalNameForClient;
15-
16-
return Object.fromEntries(
17-
CLIENT_NAMES.map((clientName) => [
18-
clientName,
19-
getIdentifierNameFn(j, source, clientName),
20-
]).filter(([, v2ClientLocalName]) => v2ClientLocalName !== undefined)
21-
);
22-
};
7+
export const getV2ClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) =>
8+
hasRequire(j, source)
9+
? getV2ClientNamesRecordFromRequire(j, source)
10+
: getV2ClientNamesRecordFromImport(j, source);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
Collection,
3+
Identifier,
4+
ImportDefaultSpecifier,
5+
ImportNamespaceSpecifier,
6+
ImportSpecifier,
7+
JSCodeshift,
8+
} from "jscodeshift";
9+
10+
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
11+
import { getV2ServiceModulePath } from "./getV2ServiceModulePath";
12+
13+
const getImportSpecifiers = (j: JSCodeshift, source: Collection<unknown>, sourceValue: string) =>
14+
source
15+
.find(j.ImportDeclaration, {
16+
type: "ImportDeclaration",
17+
source: { value: sourceValue },
18+
})
19+
.nodes()
20+
.map((importDeclaration) => importDeclaration.specifiers)
21+
.flat() as (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[];
22+
23+
export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collection<unknown>) => {
24+
const v2ClientNamesRecord: Record<string, string> = {};
25+
26+
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
27+
(specifier) => specifier?.type === "ImportSpecifier"
28+
) as ImportSpecifier[];
29+
30+
for (const clientName of CLIENT_NAMES) {
31+
const clientImportSpecifier = specifiersFromNamedImport.find(
32+
(specifier) => specifier?.imported.name === clientName
33+
);
34+
35+
if (clientImportSpecifier) {
36+
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name;
37+
continue;
38+
}
39+
40+
const deepImportPath = getV2ServiceModulePath(clientName);
41+
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
42+
(specifier) =>
43+
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
44+
);
45+
46+
if (specifiersFromDeepImport.length > 0) {
47+
v2ClientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
48+
}
49+
}
50+
51+
return v2ClientNamesRecord;
52+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift";
2+
3+
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4+
import { getV2ServiceModulePath } from "./getV2ServiceModulePath";
5+
6+
const getRequireIds = (j: JSCodeshift, source: Collection<unknown>, sourceValue: string) =>
7+
source
8+
.find(j.VariableDeclarator, {
9+
init: {
10+
type: "CallExpression",
11+
callee: { type: "Identifier", name: "require" },
12+
arguments: [{ value: sourceValue }],
13+
},
14+
})
15+
.nodes()
16+
.map((variableDeclarator) => variableDeclarator.id);
17+
18+
export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collection<unknown>) => {
19+
const v2ClientNamesRecord: Record<string, string> = {};
20+
21+
const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME)
22+
.filter((id) => id.type === "ObjectPattern")
23+
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
24+
.flat() as Property[];
25+
26+
for (const clientName of CLIENT_NAMES) {
27+
const propertyWithClientName = idPropertiesFromNamedImport.find(
28+
(property) => (property?.key as Identifier).name === clientName
29+
);
30+
31+
if (propertyWithClientName) {
32+
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name;
33+
continue;
34+
}
35+
36+
const deepRequirePath = getV2ServiceModulePath(clientName);
37+
const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter(
38+
(id) => id.type === "Identifier"
39+
);
40+
if (idsFromDefaultImport.length) {
41+
v2ClientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name;
42+
}
43+
}
44+
45+
return v2ClientNamesRecord;
46+
};

0 commit comments

Comments
 (0)