Skip to content

Commit e83a169

Browse files
authored
Remove v2 from utility names in client-names folder (#463)
1 parent a61eb1b commit e83a169

9 files changed

+51
-51
lines changed
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 DEEP_IMPORT_PATH_REGEXP = new RegExp(`${PACKAGE_NAME}/clients/([\\w]*)`, "g");
4+
5+
export const getClientNamesFromDeepImport = (fileSource: string) => {
6+
const clientsFromDeepImportPath = new Set(
7+
[...fileSource.matchAll(DEEP_IMPORT_PATH_REGEXP)].map((regExpMatch) => regExpMatch[1]).flat()
8+
);
9+
10+
return CLIENT_NAMES.filter((clientName) =>
11+
clientsFromDeepImportPath.has(clientName.toLowerCase())
12+
);
13+
};

src/transforms/v2-to-v3/client-names/getV2ClientNamesFromGlobal.ts renamed to src/transforms/v2-to-v3/client-names/getClientNamesFromGlobal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CLIENT_NAMES } from "../config";
44
import { getNamesFromNewExpr } from "./getNamesFromNewExpr";
55
import { getNamesFromTSQualifiedName } from "./getNamesFromTSQualifiedName";
66

7-
export const getV2ClientNamesFromGlobal = (
7+
export const getClientNamesFromGlobal = (
88
j: JSCodeshift,
99
source: Collection<unknown>,
1010
v2GlobalName: string
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { hasRequire } from "../modules";
4+
import { getClientNamesFromDeepImport } from "./getClientNamesFromDeepImport";
5+
import { getClientNamesRecordFromImport } from "./getClientNamesRecordFromImport";
6+
import { getClientNamesRecordFromRequire } from "./getClientNamesRecordFromRequire";
7+
8+
export const getClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) => {
9+
const clientNamesFromDeepImport = getClientNamesFromDeepImport(source.toSource());
10+
return hasRequire(j, source)
11+
? getClientNamesRecordFromRequire(j, source, clientNamesFromDeepImport)
12+
: getClientNamesRecordFromImport(j, source, clientNamesFromDeepImport);
13+
};

src/transforms/v2-to-v3/client-names/getV2ClientNamesRecordFromImport.ts renamed to src/transforms/v2-to-v3/client-names/getClientNamesRecordFromImport.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { getImportEqualsDeclarationType } from "../modules";
55
import { getImportSpecifiers } from "../modules";
66
import { getClientDeepImportPath } from "../utils";
77

8-
export const getV2ClientNamesRecordFromImport = (
8+
export const getClientNamesRecordFromImport = (
99
j: JSCodeshift,
1010
source: Collection<unknown>,
11-
v2ClientNamesWithServiceModule: string[]
11+
clientNamesFromDeepImport: string[]
1212
) => {
13-
const v2ClientNamesRecord: Record<string, string> = {};
13+
const clientNamesRecord: Record<string, string> = {};
1414

1515
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
1616
(specifier) => specifier?.type === "ImportSpecifier"
@@ -20,29 +20,29 @@ export const getV2ClientNamesRecordFromImport = (
2020
const importedName = specifier.imported.name;
2121
const localName = (specifier.local as Identifier).name;
2222
if (CLIENT_NAMES.includes(importedName)) {
23-
v2ClientNamesRecord[importedName] = localName ?? importedName;
23+
clientNamesRecord[importedName] = localName ?? importedName;
2424
}
2525
}
2626

27-
for (const clientName of v2ClientNamesWithServiceModule) {
27+
for (const clientName of clientNamesFromDeepImport) {
2828
const deepImportPath = getClientDeepImportPath(clientName);
2929

3030
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
3131
(specifier) =>
3232
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
3333
);
3434
if (specifiersFromDeepImport.length > 0) {
35-
v2ClientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
35+
clientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
3636
}
3737

3838
const identifiersFromImportEquals = source.find(
3939
j.TSImportEqualsDeclaration,
4040
getImportEqualsDeclarationType(deepImportPath)
4141
);
4242
if (identifiersFromImportEquals.length > 0) {
43-
v2ClientNamesRecord[clientName] = identifiersFromImportEquals.nodes()[0]?.id.name;
43+
clientNamesRecord[clientName] = identifiersFromImportEquals.nodes()[0]?.id.name;
4444
}
4545
}
4646

47-
return v2ClientNamesRecord;
47+
return clientNamesRecord;
4848
};

src/transforms/v2-to-v3/client-names/getV2ClientNamesRecordFromRequire.ts renamed to src/transforms/v2-to-v3/client-names/getClientNamesRecordFromRequire.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { getRequireDeclaratorsWithProperty } from "../modules";
1212
import { getClientDeepImportPath } from "../utils";
1313
import { getRequireIds } from "./getRequireIds";
1414

15-
export const getV2ClientNamesRecordFromRequire = (
15+
export const getClientNamesRecordFromRequire = (
1616
j: JSCodeshift,
1717
source: Collection<unknown>,
18-
v2ClientNamesWithServiceModule: string[]
18+
clientNamesFromDeepImport: string[]
1919
) => {
20-
const v2ClientNamesRecord: Record<string, string> = {};
20+
const clientNamesRecord: Record<string, string> = {};
2121

2222
const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
2323
.filter((id) => id.type === "ObjectPattern")
@@ -34,7 +34,7 @@ export const getV2ClientNamesRecordFromRequire = (
3434
continue;
3535
}
3636
if (CLIENT_NAMES.includes(key.name)) {
37-
v2ClientNamesRecord[key.name] = value.name;
37+
clientNamesRecord[key.name] = value.name;
3838
}
3939
}
4040

@@ -50,22 +50,22 @@ export const getV2ClientNamesRecordFromRequire = (
5050
init.type === "MemberExpression" &&
5151
init.property.type === "Identifier"
5252
) {
53-
const v2ClientName = (init.property as Identifier).name;
54-
if (CLIENT_NAMES.includes(v2ClientName)) {
55-
v2ClientNamesRecord[v2ClientName] = (id as Identifier).name;
53+
const clientName = (init.property as Identifier).name;
54+
if (CLIENT_NAMES.includes(clientName)) {
55+
clientNamesRecord[clientName] = (id as Identifier).name;
5656
}
5757
}
5858
}
5959

60-
for (const clientName of v2ClientNamesWithServiceModule) {
60+
for (const clientName of clientNamesFromDeepImport) {
6161
const deepImportPath = getClientDeepImportPath(clientName);
6262
const idsFromDefaultImport = getRequireIds(j, source, deepImportPath).filter(
6363
(id) => id.type === "Identifier"
6464
);
6565
if (idsFromDefaultImport.length) {
66-
v2ClientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name;
66+
clientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name;
6767
}
6868
}
6969

70-
return v2ClientNamesRecord;
70+
return clientNamesRecord;
7171
};

src/transforms/v2-to-v3/client-names/getV2ClientNamesRecord.ts

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

src/transforms/v2-to-v3/client-names/getV2ClientNamesWithServiceModule.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./getClientMetadataRecord";
2-
export * from "./getV2ClientNamesFromGlobal";
3-
export * from "./getV2ClientNamesRecord";
2+
export * from "./getClientNamesFromGlobal";
3+
export * from "./getClientNamesRecord";

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { replaceS3UploadApi } from "./apis/replaceS3UploadApi";
55
import { replaceClientCreation } from "./client-instances";
66
import {
77
getClientMetadataRecord,
8-
getV2ClientNamesFromGlobal,
9-
getV2ClientNamesRecord,
8+
getClientNamesFromGlobal,
9+
getClientNamesRecord,
1010
} from "./client-names";
1111
import {
1212
addClientModules,
@@ -22,14 +22,14 @@ const transformer = async (file: FileInfo, api: API) => {
2222
const source = j(file.source);
2323

2424
const v2GlobalName = getGlobalNameFromModule(j, source);
25-
const v2ClientNamesRecord = getV2ClientNamesRecord(j, source);
25+
const v2ClientNamesRecord = getClientNamesRecord(j, source);
2626

2727
if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
2828
return source.toSource();
2929
}
3030

3131
if (v2GlobalName) {
32-
for (const v2ClientNameFromGlobal of getV2ClientNamesFromGlobal(j, source, v2GlobalName)) {
32+
for (const v2ClientNameFromGlobal of getClientNamesFromGlobal(j, source, v2GlobalName)) {
3333
if (!(v2ClientNameFromGlobal in v2ClientNamesRecord)) {
3434
v2ClientNamesRecord[v2ClientNameFromGlobal] = v2ClientNameFromGlobal;
3535
}

0 commit comments

Comments
 (0)