Skip to content

Commit c63a781

Browse files
authored
Check for TsTypeReference on v2ClientLocalName (#307)
1 parent 669ad59 commit c63a781

File tree

7 files changed

+91
-68
lines changed

7 files changed

+91
-68
lines changed

src/transforms/v2-to-v3/modules/addV3ClientImports.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ export const addV3ClientImports = (
1919

2020
addV3ClientNamedImport(j, source, options);
2121

22-
const { v2ClientName, v2GlobalName } = options;
23-
const v3ClientTypeNames = getV3ClientTypeNames(j, source, { v2ClientName, v2GlobalName });
22+
const { v2ClientLocalName, v2ClientName, v2GlobalName } = options;
23+
const v3ClientTypeNames = getV3ClientTypeNames(j, source, {
24+
v2ClientLocalName,
25+
v2ClientName,
26+
v2GlobalName,
27+
});
2428

2529
// Add default import for types, if needed.
2630
if (v3ClientTypeNames.length > 0) {

src/transforms/v2-to-v3/modules/addV3ClientRequires.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ export const addV3ClientRequires = (
1111
source: Collection<unknown>,
1212
options: V3ClientModulesOptions
1313
): void => {
14-
const { v2ClientName, v2GlobalName } = options;
15-
const v3ClientTypeNames = getV3ClientTypeNames(j, source, { v2ClientName, v2GlobalName });
14+
const { v2ClientLocalName, v2ClientName, v2GlobalName } = options;
15+
const v3ClientTypeNames = getV3ClientTypeNames(j, source, {
16+
v2ClientLocalName,
17+
v2ClientName,
18+
v2GlobalName,
19+
});
1620

1721
// Add default require for types, if needed.
1822
if (v3ClientTypeNames.length > 0) {
Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import {
2-
Collection,
3-
Identifier,
4-
ImportDeclaration,
5-
ImportSpecifier,
6-
JSCodeshift,
7-
TSQualifiedName,
8-
TSTypeReference,
9-
} from "jscodeshift";
10-
11-
import { getV2ServiceModulePath } from "../utils";
12-
import { getV2ClientTSTypeRef } from "./getV2ClientTSTypeRef";
1+
import { Collection, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
2+
3+
import { getImportSpecifiers } from "../modules";
4+
import { getV2ClientTSTypeRef, getV2ServiceModulePath } from "../utils";
135

146
export interface GetV2ClientTypeNamesOptions {
157
v2ClientName: string;
168
v2GlobalName?: string;
9+
v2ClientLocalName: string;
1710
}
1811

1912
const getRightIdentifierName = (
@@ -31,31 +24,32 @@ const getRightIdentifierName = (
3124
export const getV2ClientTypeNames = (
3225
j: JSCodeshift,
3326
source: Collection<unknown>,
34-
{ v2ClientName, v2GlobalName }: GetV2ClientTypeNamesOptions
27+
{ v2ClientLocalName, v2ClientName, v2GlobalName }: GetV2ClientTypeNamesOptions
3528
): string[] => {
36-
const v2GlobalTSTypeRef = getV2ClientTSTypeRef({
37-
v2ClientName,
38-
v2GlobalName,
39-
withoutRightSection: true,
40-
});
41-
const v2ClientTSTypeRef = getV2ClientTSTypeRef({ v2ClientName, withoutRightSection: true });
42-
43-
const v2ServiceModuleImportDeclaration = {
44-
type: "ImportDeclaration",
45-
source: { value: getV2ServiceModulePath(v2ClientName) },
46-
} as ImportDeclaration;
47-
48-
return [
49-
...new Set([
50-
...getRightIdentifierName(j, source, v2GlobalTSTypeRef),
51-
...getRightIdentifierName(j, source, v2ClientTSTypeRef),
52-
...source
53-
.find(j.ImportDeclaration, v2ServiceModuleImportDeclaration)
54-
.nodes()
55-
.map((node) => node.specifiers)
56-
.flat()
57-
.filter((node) => (node as ImportSpecifier).type === "ImportSpecifier")
58-
.map((node) => ((node as ImportSpecifier).local as Identifier).name),
59-
]),
60-
];
29+
const v2ClientTypeNames = [];
30+
31+
if (v2GlobalName) {
32+
const v2GlobalTSTypeRef = getV2ClientTSTypeRef({
33+
v2ClientName,
34+
v2GlobalName,
35+
withoutRightSection: true,
36+
});
37+
v2ClientTypeNames.push(...getRightIdentifierName(j, source, v2GlobalTSTypeRef));
38+
}
39+
40+
const v2ClientTSTypeRef = getV2ClientTSTypeRef({ v2ClientLocalName, withoutRightSection: true });
41+
v2ClientTypeNames.push(...getRightIdentifierName(j, source, v2ClientTSTypeRef));
42+
43+
v2ClientTypeNames.push(
44+
...getImportSpecifiers(j, source, getV2ServiceModulePath(v2ClientName))
45+
.filter(
46+
(importSpecifier) =>
47+
importSpecifier.type === "ImportSpecifier" &&
48+
importSpecifier.local &&
49+
importSpecifier.local.type === "Identifier"
50+
)
51+
.map((importSpecifier) => (importSpecifier.local as Identifier).name)
52+
);
53+
54+
return [...new Set(v2ClientTypeNames)];
6155
};

src/transforms/v2-to-v3/ts-type/replaceTSTypeReference.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Collection, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
22

3-
import { getV3ClientDefaultLocalName } from "../utils";
4-
import { getV2ClientTSTypeRef } from "./getV2ClientTSTypeRef";
3+
import { getV2ClientTSTypeRef, getV3ClientDefaultLocalName } from "../utils";
54
import { getV2ClientTypeNames } from "./getV2ClientTypeNames";
65
import { getV3ClientTypeName } from "./getV3ClientTypeName";
76

@@ -36,30 +35,32 @@ export const replaceTSTypeReference = (
3635
{ v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName }: ReplaceTSTypeReferenceOptions
3736
): void => {
3837
// Replace type reference to client created with global name.
39-
source
40-
.find(j.TSTypeReference, getV2ClientTSTypeRef({ v2ClientName, v2GlobalName }))
41-
.replaceWith((v2ClientType) =>
42-
j.tsTypeReference(j.identifier(v3ClientName), v2ClientType.node.typeParameters)
43-
);
38+
if (v2GlobalName) {
39+
source
40+
.find(j.TSTypeReference, getV2ClientTSTypeRef({ v2ClientName, v2GlobalName }))
41+
.replaceWith((v2ClientType) =>
42+
j.tsTypeReference(j.identifier(v3ClientName), v2ClientType.node.typeParameters)
43+
);
4444

45-
// Replace reference to client types created with global name.
46-
source
47-
.find(
48-
j.TSTypeReference,
49-
getV2ClientTSTypeRef({ v2ClientName, v2GlobalName, withoutRightSection: true })
50-
)
51-
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
52-
.replaceWith((v2ClientType) =>
53-
getV3ClientTypeReference(
54-
j,
55-
v2ClientLocalName,
56-
getV3ClientTypeName(getRightIdentifierName(v2ClientType.node))
45+
// Replace reference to client types created with global name.
46+
source
47+
.find(
48+
j.TSTypeReference,
49+
getV2ClientTSTypeRef({ v2ClientName, v2GlobalName, withoutRightSection: true })
5750
)
58-
);
51+
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
52+
.replaceWith((v2ClientType) =>
53+
getV3ClientTypeReference(
54+
j,
55+
v2ClientLocalName,
56+
getV3ClientTypeName(getRightIdentifierName(v2ClientType.node))
57+
)
58+
);
59+
}
5960

6061
// Replace reference to client types created with client module.
6162
source
62-
.find(j.TSTypeReference, getV2ClientTSTypeRef({ v2ClientName, withoutRightSection: true }))
63+
.find(j.TSTypeReference, getV2ClientTSTypeRef({ v2ClientLocalName, withoutRightSection: true }))
6364
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
6465
.replaceWith((v2ClientType) =>
6566
getV3ClientTypeReference(
@@ -70,7 +71,12 @@ export const replaceTSTypeReference = (
7071
);
7172

7273
// Replace type reference to client type with modules.
73-
const v2ClientTypeNames = getV2ClientTypeNames(j, source, { v2ClientName, v2GlobalName });
74+
const v2ClientTypeNames = getV2ClientTypeNames(j, source, {
75+
v2ClientLocalName,
76+
v2ClientName,
77+
v2GlobalName,
78+
});
79+
7480
for (const v2ClientTypeName of v2ClientTypeNames) {
7581
source
7682
.find(j.TSTypeReference, { typeName: { type: "Identifier", name: v2ClientTypeName } })

src/transforms/v2-to-v3/utils/getV2ClientNewExpression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const getV2ClientNewExpression = ({
1313
}: ClientNewExpressionOptions): NewExpression => {
1414
if (!v2GlobalName && !v2ClientLocalName) {
1515
throw new Error(
16-
`At least one of the following options must be provided: v2ClientLocalName, v2GlobalName`
16+
`One of the following options must be provided: v2ClientLocalName, v2GlobalName`
1717
);
1818
}
1919

src/transforms/v2-to-v3/ts-type/getV2ClientTSTypeRef.ts renamed to src/transforms/v2-to-v3/utils/getV2ClientTSTypeRef.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
import { Identifier, TSQualifiedName, TSTypeReference } from "jscodeshift";
22

33
export interface V2ClientTsTypeRefOptions {
4+
v2ClientLocalName?: string;
5+
v2ClientName?: string;
46
v2GlobalName?: string;
5-
v2ClientName: string;
67
withoutRightSection?: boolean;
78
}
89

910
export const getV2ClientTSTypeRef = ({
10-
v2GlobalName,
11+
v2ClientLocalName,
1112
v2ClientName,
13+
v2GlobalName,
1214
withoutRightSection = false,
1315
}: V2ClientTsTypeRefOptions): TSTypeReference => {
16+
if (!v2GlobalName && !v2ClientLocalName) {
17+
throw new Error(
18+
`One of the following options must be provided: v2ClientLocalName, v2GlobalName`
19+
);
20+
}
21+
22+
if (v2GlobalName && v2ClientLocalName) {
23+
throw new Error(
24+
`Only one of the following options must be provided: v2ClientLocalName, v2GlobalName`
25+
);
26+
}
27+
1428
if (v2GlobalName) {
1529
const idWithGlobalName = {
1630
type: "TSQualifiedName",
@@ -25,7 +39,7 @@ export const getV2ClientTSTypeRef = ({
2539
} as TSTypeReference;
2640
}
2741

28-
const idWithClientName = { type: "Identifier", name: v2ClientName } as Identifier;
42+
const idWithClientName = { type: "Identifier", name: v2ClientLocalName } as Identifier;
2943
return {
3044
typeName: {
3145
...(withoutRightSection ? { left: idWithClientName } : idWithClientName),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./getV2ClientNewExpression";
2+
export * from "./getV2ClientTSTypeRef";
23
export * from "./getV2ServiceModulePath";
34
export * from "./getV3ClientDefaultLocalName";
45
export * from "./isTypeScriptFile";

0 commit comments

Comments
 (0)