Skip to content

Commit f92d930

Browse files
committed
Refactoring
1 parent 65745b9 commit f92d930

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/parsers/propertyParser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ export function parseProperty(
2929

3030
let isOptional = false;
3131

32-
// Typechecker only gives the type "any" if it's present in a union
33-
// This means the type of "a" in {a?:any} isn't "any | undefined"
34-
// So instead we check for the questionmark to detect optional types
3532
const parsedType = resolveType(
3633
type,
3734
context,
3835
isTypeParameterLike(type) ? undefined : propertySignature?.type,
3936
skipResolvingComplexTypes,
4037
);
38+
39+
// Typechecker only gives the type "any" if it's present in a union
40+
// This means the type of "a" in {a?:any} isn't "any | undefined"
41+
// So instead we check for the questionmark to detect optional types
4142
if ((type.flags & ts.TypeFlags.Any || type.flags & ts.TypeFlags.Unknown) && propertySignature) {
4243
isOptional = Boolean(propertySignature.questionToken);
4344
} else {

src/parsers/typeResolver.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ export function resolveType(
3737
typeStack.push(typeId);
3838
}
3939

40+
function areEquivalent(typeNodeName: ts.EntityName, type: ts.Type): boolean | undefined {
41+
if (ts.isIdentifier(typeNodeName)) {
42+
const typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName);
43+
if (!typeSymbolCandidate) {
44+
return undefined;
45+
}
46+
47+
return (
48+
typeNodeName.text === type.aliasSymbol?.name &&
49+
getTypeSymbolNamespaces(typeSymbolCandidate).join('.') === getTypeNamespaces(type).join('.')
50+
);
51+
} else if (ts.isQualifiedName(typeNodeName)) {
52+
const typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName.right);
53+
if (!typeSymbolCandidate) {
54+
return undefined;
55+
}
56+
57+
return (
58+
typeNodeName.right.text === type.aliasSymbol?.name &&
59+
getTypeSymbolNamespaces(typeSymbolCandidate).join('.') === getTypeNamespaces(type).join('.')
60+
);
61+
}
62+
63+
return undefined;
64+
}
65+
4066
// The following code handles cases where the type is a simple alias of another type (type Alias = SomeType).
4167
// TypeScript resolves the alias automatically, but we want to preserve the original type symbol if it exists.
4268
//
@@ -45,29 +71,19 @@ export function resolveType(
4571
let typeSymbol: ts.Symbol | undefined;
4672
if (typeNode && ts.isTypeReferenceNode(typeNode)) {
4773
const typeNodeName = (typeNode as ts.TypeReferenceNode).typeName;
74+
let typeSymbolCandidate: ts.Symbol | undefined;
4875
if (ts.isIdentifier(typeNodeName)) {
49-
const typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName);
50-
51-
if (
52-
typeSymbolCandidate &&
53-
(typeNodeName.text !== type.aliasSymbol?.name ||
54-
getTypeSymbolNamespaces(typeSymbolCandidate).join('.') !==
55-
getTypeNamespaces(type).join('.')) &&
56-
!(typeSymbolCandidate.flags & ts.SymbolFlags.TypeParameter)
57-
) {
58-
typeSymbol = typeSymbolCandidate;
59-
}
76+
typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName);
6077
} else if (ts.isQualifiedName(typeNodeName)) {
61-
const typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName.right);
62-
if (
63-
typeSymbolCandidate &&
64-
(typeNodeName.right.text !== type.aliasSymbol?.name ||
65-
getTypeSymbolNamespaces(typeSymbolCandidate).join('.') !==
66-
getTypeNamespaces(type).join('.')) &&
67-
!(typeSymbolCandidate.flags & ts.SymbolFlags.TypeParameter)
68-
) {
69-
typeSymbol = typeSymbolCandidate;
70-
}
78+
typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName.right);
79+
}
80+
81+
if (
82+
typeSymbolCandidate &&
83+
!areEquivalent(typeNodeName, type) &&
84+
!(typeSymbolCandidate.flags & ts.SymbolFlags.TypeParameter)
85+
) {
86+
typeSymbol = typeSymbolCandidate;
7187
}
7288
}
7389

0 commit comments

Comments
 (0)