Skip to content

Commit a104a57

Browse files
authored
Use filter with isV2ClientInputOutputType in replaceTSTypeReference (#229)
1 parent d3f130c commit a104a57

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

src/transforms/v2-to-v3/utils/replace/replaceTSTypeReference.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,10 @@ export interface ReplaceTypeReferenceOptions {
1616
v2DefaultModuleName: string;
1717
}
1818

19-
const getV3ClientTypeFromRightIdentifier = (
20-
j: JSCodeshift,
21-
v2ClientType: ASTPath<TSTypeReference>
22-
) => {
23-
const v3ClientType = getV3ClientTypeName(
24-
((v2ClientType.node.typeName as TSQualifiedName).right as Identifier).name
25-
);
26-
if (!v3ClientType) {
27-
return v2ClientType.node;
28-
}
29-
return j.tsTypeReference(j.identifier(v3ClientType));
30-
};
19+
const getRightIdentifierName = (node: TSTypeReference) =>
20+
((node.typeName as TSQualifiedName).right as Identifier).name;
3121

32-
const getV3ClientTypeFromIdentifier = (j: JSCodeshift, v2ClientType: ASTPath<TSTypeReference>) => {
33-
const v3ClientType = getV3ClientTypeName((v2ClientType.node.typeName as Identifier).name);
34-
if (!v3ClientType) {
35-
return v2ClientType.node;
36-
}
37-
return j.tsTypeReference(j.identifier(v3ClientType));
38-
};
22+
const getIdentifierName = (node: TSTypeReference) => (node.typeName as Identifier).name;
3923

4024
// Replace v2 client type reference with v3 client type reference.
4125
export const replaceTSTypeReference = (
@@ -67,7 +51,8 @@ export const replaceTSTypeReference = (
6751
right: { type: "Identifier" },
6852
},
6953
})
70-
.replaceWith((v2ClientType) => getV3ClientTypeFromRightIdentifier(j, v2ClientType));
54+
.filter((v2ClientType) => isV2ClientInputOutputType(getRightIdentifierName(v2ClientType.node)))
55+
.replaceWith((v2ClientType) => getV3ClientTypeName(getRightIdentifierName(v2ClientType.node)));
7156

7257
// Replace type reference to client created with client module.
7358
source
@@ -76,20 +61,19 @@ export const replaceTSTypeReference = (
7661
left: { type: "Identifier", name: v2ClientName },
7762
},
7863
})
79-
.replaceWith((v2ClientType) => getV3ClientTypeFromRightIdentifier(j, v2ClientType));
64+
.filter((v2ClientType) => isV2ClientInputOutputType(getRightIdentifierName(v2ClientType.node)))
65+
.replaceWith((v2ClientType) => getV3ClientTypeName(getRightIdentifierName(v2ClientType.node)));
8066

8167
// Replace type reference to client input/output import with named imports.
82-
const v2ClientTypeNames = getV2ClientTypeNames(j, source, {
83-
v2ClientName,
84-
v2DefaultModuleName,
85-
});
68+
const v2ClientTypeNames = getV2ClientTypeNames(j, source, { v2ClientName, v2DefaultModuleName });
8669
for (const v2ClientTypeName of v2ClientTypeNames) {
8770
if (isV2ClientInputOutputType(v2ClientTypeName)) {
8871
source
8972
.find(j.TSTypeReference, {
9073
typeName: { type: "Identifier", name: v2ClientTypeName },
9174
})
92-
.replaceWith((v2ClientType) => getV3ClientTypeFromIdentifier(j, v2ClientType));
75+
.filter((v2ClientType) => isV2ClientInputOutputType(getIdentifierName(v2ClientType.node)))
76+
.replaceWith((v2ClientType) => getV3ClientTypeName(getIdentifierName(v2ClientType.node)));
9377
}
9478
}
9579
};

0 commit comments

Comments
 (0)