Skip to content

Commit 9b3dfb1

Browse files
authored
Deep import search only for available clients (#264)
1 parent 2c8312f commit 9b3dfb1

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

.changeset/new-zoos-explode.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+
Deep import search only for available clients

src/transforms/v2-to-v3/transformer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getClientMetadata,
66
getV2ClientNamesFromGlobal,
77
getV2ClientNamesRecord,
8+
getV2ClientNamesWithServiceModule,
89
getV2GlobalName,
910
isTypeScriptFile,
1011
removePromiseCalls,
@@ -19,7 +20,8 @@ const transformer = async (file: FileInfo, api: API) => {
1920
const source = j(file.source);
2021

2122
const v2GlobalName = getV2GlobalName(j, source);
22-
const v2ClientNamesRecord = getV2ClientNamesRecord(j, source);
23+
const v2ClientNamesWithServiceModule = getV2ClientNamesWithServiceModule(file.source);
24+
const v2ClientNamesRecord = getV2ClientNamesRecord(j, source, v2ClientNamesWithServiceModule);
2325

2426
if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
2527
return source.toSource();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { hasRequire } from "../has";
44
import { getV2ClientNamesRecordFromImport } from "./getV2ClientNamesRecordFromImport";
55
import { getV2ClientNamesRecordFromRequire } from "./getV2ClientNamesRecordFromRequire";
66

7-
export const getV2ClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) =>
7+
export const getV2ClientNamesRecord = (
8+
j: JSCodeshift,
9+
source: Collection<unknown>,
10+
v2ClientNamesWithServiceModule: string[]
11+
) =>
812
hasRequire(j, source)
9-
? getV2ClientNamesRecordFromRequire(j, source)
10-
: getV2ClientNamesRecordFromImport(j, source);
13+
? getV2ClientNamesRecordFromRequire(j, source, v2ClientNamesWithServiceModule)
14+
: getV2ClientNamesRecordFromImport(j, source, v2ClientNamesWithServiceModule);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ const getImportSpecifiers = (j: JSCodeshift, source: Collection<unknown>, source
2020
.map((importDeclaration) => importDeclaration.specifiers)
2121
.flat() as (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[];
2222

23-
export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collection<unknown>) => {
23+
export const getV2ClientNamesRecordFromImport = (
24+
j: JSCodeshift,
25+
source: Collection<unknown>,
26+
v2ClientNamesWithServiceModule: string[]
27+
) => {
2428
const v2ClientNamesRecord: Record<string, string> = {};
2529

2630
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
@@ -31,18 +35,17 @@ export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collect
3135
const clientImportSpecifier = specifiersFromNamedImport.find(
3236
(specifier) => specifier?.imported.name === clientName
3337
);
34-
3538
if (clientImportSpecifier) {
3639
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name;
37-
continue;
3840
}
41+
}
3942

43+
for (const clientName of v2ClientNamesWithServiceModule) {
4044
const deepImportPath = getV2ServiceModulePath(clientName);
4145
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
4246
(specifier) =>
4347
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
4448
);
45-
4649
if (specifiersFromDeepImport.length > 0) {
4750
v2ClientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
4851
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ const getRequireIds = (j: JSCodeshift, source: Collection<unknown>, sourceValue:
1515
.nodes()
1616
.map((variableDeclarator) => variableDeclarator.id);
1717

18-
export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collection<unknown>) => {
18+
export const getV2ClientNamesRecordFromRequire = (
19+
j: JSCodeshift,
20+
source: Collection<unknown>,
21+
v2ClientNamesWithServiceModule: string[]
22+
) => {
1923
const v2ClientNamesRecord: Record<string, string> = {};
2024

2125
const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME)
@@ -27,12 +31,12 @@ export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collec
2731
const propertyWithClientName = idPropertiesFromNamedImport.find(
2832
(property) => (property?.key as Identifier).name === clientName
2933
);
30-
3134
if (propertyWithClientName) {
3235
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name;
33-
continue;
3436
}
37+
}
3538

39+
for (const clientName of v2ClientNamesWithServiceModule) {
3640
const deepRequirePath = getV2ServiceModulePath(clientName);
3741
const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter(
3842
(id) => id.type === "Identifier"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
2+
3+
const SERVICE_MODULE_PATH_REGEXP = new RegExp(`${PACKAGE_NAME}/clients/([\\w]*)`, "g");
4+
5+
export const getV2ClientNamesWithServiceModule = (fileSource: string) => {
6+
const clientsFromServiceModule = new Set(
7+
[...fileSource.matchAll(SERVICE_MODULE_PATH_REGEXP)].map((regExpMatch) => regExpMatch[1]).flat()
8+
);
9+
10+
return CLIENT_NAMES.filter((clientName) =>
11+
clientsFromServiceModule.has(clientName.toLowerCase())
12+
);
13+
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from "./getV2ClientIdentifiers";
44
export * from "./getV2ClientIdThisExpressions";
55
export * from "./getV2ClientNamesFromGlobal";
66
export * from "./getV2ClientNamesRecord";
7+
export * from "./getV2ClientNamesWithServiceModule";
78
export * from "./getV2ClientNewExpression";
89
export * from "./getV2ClientTSTypeRef";
910
export * from "./getV2ClientTypeNames";

0 commit comments

Comments
 (0)