Skip to content

Commit b47678f

Browse files
committed
Handle simple aliases in optional parameters
1 parent af737be commit b47678f

File tree

4 files changed

+219
-11
lines changed

4 files changed

+219
-11
lines changed

src/parsers/typeResolver.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function resolveType(
5252
}
5353
} else if (
5454
ts.isQualifiedName(typeNodeName) &&
55-
typeNodeName.right.text !== type.aliasSymbol?.name
55+
typeNodeName.right.text !== type.aliasSymbol?.name /*&&
56+
typeNodeName.left.getFullText() !== getTypeNamespaces(type).join('.')*/
5657
) {
5758
const typeSymbolCandidate = checker.getSymbolAtLocation(typeNodeName.right);
5859
if (typeSymbolCandidate && !(typeSymbolCandidate.flags & ts.SymbolFlags.TypeParameter)) {
@@ -115,24 +116,30 @@ export function resolveType(
115116
}
116117

117118
if (type.isUnion()) {
118-
const memberTypes: TypeNode[] = [];
119+
let memberTypes: ts.Type[] = type.types;
120+
const parsedMemberTypes: TypeNode[] = [];
119121
const typeName = getTypeName(type, typeSymbol, checker, false);
120122

121123
// @ts-expect-error - Internal API
122124
if (type.origin?.isUnion()) {
123125
// @ts-expect-error - Internal API
124-
for (const memberType of type.origin.types) {
125-
memberTypes.push(resolveType(memberType, context));
126+
memberTypes = type.origin.types;
127+
}
128+
129+
if (memberTypes.length === 2 && memberTypes.some((x) => x.flags & ts.TypeFlags.Undefined)) {
130+
// If the union is `T | undefined`, we propagate the typeNode of T to the union member so that any aliases are resolved correctly.
131+
for (const memberType of memberTypes) {
132+
parsedMemberTypes.push(resolveType(memberType, context, typeNode));
126133
}
127134
} else {
128-
for (const memberType of type.types) {
129-
memberTypes.push(resolveType(memberType, context));
135+
for (const memberType of memberTypes) {
136+
parsedMemberTypes.push(resolveType(memberType, context));
130137
}
131138
}
132139

133-
return memberTypes.length === 1
134-
? memberTypes[0]
135-
: new UnionNode(typeName, namespaces, memberTypes);
140+
return parsedMemberTypes.length === 1
141+
? parsedMemberTypes[0]
142+
: new UnionNode(typeName, namespaces, parsedMemberTypes);
136143
}
137144

138145
if (type.isIntersection()) {

test/aliases/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
}
9191
],
9292
"parentNamespaces": [],
93-
"name": "SomeType"
93+
"name": "Alias"
9494
},
9595
{
9696
"kind": "intrinsic",
@@ -260,7 +260,7 @@
260260
}
261261
],
262262
"parentNamespaces": [],
263-
"name": "SomeType"
263+
"name": "Alias"
264264
},
265265
{
266266
"kind": "intrinsic",

test/namespaces/input.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export namespace Root {
55
export interface Params {
66
s: Grade;
77
a: NamespacedType;
8+
os?: Grade;
9+
oa?: NamespacedType;
810
}
911

1012
export enum Grade {
@@ -26,6 +28,10 @@ export namespace Root {
2628
export type NamespacedType = OutsideType;
2729
}
2830

31+
export namespace Other {
32+
export type Orientation = 'horizontal' | 'vertical';
33+
}
34+
2935
export function fn3(params: Root.Sub.Params) {}
3036

3137
export function fn4(a: Root.NamespacedType) {}

test/namespaces/output.json

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,71 @@
6363
"name": "NamespacedType"
6464
},
6565
"optional": false
66+
},
67+
{
68+
"name": "os",
69+
"type": {
70+
"kind": "union",
71+
"types": [
72+
{
73+
"kind": "enum",
74+
"name": "Grade",
75+
"parentNamespaces": [
76+
"Root",
77+
"Sub"
78+
],
79+
"members": [
80+
{
81+
"name": "good",
82+
"value": 0
83+
},
84+
{
85+
"name": "bad",
86+
"value": 1
87+
}
88+
]
89+
},
90+
{
91+
"kind": "intrinsic",
92+
"parentNamespaces": [],
93+
"intrinsic": "undefined"
94+
}
95+
]
96+
},
97+
"optional": true
98+
},
99+
{
100+
"name": "oa",
101+
"type": {
102+
"kind": "union",
103+
"types": [
104+
{
105+
"kind": "union",
106+
"types": [
107+
{
108+
"kind": "literal",
109+
"parentNamespaces": [],
110+
"value": "\"one\""
111+
},
112+
{
113+
"kind": "literal",
114+
"parentNamespaces": [],
115+
"value": "\"two\""
116+
}
117+
],
118+
"parentNamespaces": [
119+
"Root"
120+
],
121+
"name": "NamespacedType"
122+
},
123+
{
124+
"kind": "intrinsic",
125+
"parentNamespaces": [],
126+
"intrinsic": "undefined"
127+
}
128+
]
129+
},
130+
"optional": true
66131
}
67132
]
68133
},
@@ -206,6 +271,71 @@
206271
"name": "NamespacedType"
207272
},
208273
"optional": false
274+
},
275+
{
276+
"name": "os",
277+
"type": {
278+
"kind": "union",
279+
"types": [
280+
{
281+
"kind": "enum",
282+
"name": "Grade",
283+
"parentNamespaces": [
284+
"Root",
285+
"Sub"
286+
],
287+
"members": [
288+
{
289+
"name": "good",
290+
"value": 0
291+
},
292+
{
293+
"name": "bad",
294+
"value": 1
295+
}
296+
]
297+
},
298+
{
299+
"kind": "intrinsic",
300+
"parentNamespaces": [],
301+
"intrinsic": "undefined"
302+
}
303+
]
304+
},
305+
"optional": true
306+
},
307+
{
308+
"name": "oa",
309+
"type": {
310+
"kind": "union",
311+
"types": [
312+
{
313+
"kind": "union",
314+
"types": [
315+
{
316+
"kind": "literal",
317+
"parentNamespaces": [],
318+
"value": "\"one\""
319+
},
320+
{
321+
"kind": "literal",
322+
"parentNamespaces": [],
323+
"value": "\"two\""
324+
}
325+
],
326+
"parentNamespaces": [
327+
"Root"
328+
],
329+
"name": "NamespacedType"
330+
},
331+
{
332+
"kind": "intrinsic",
333+
"parentNamespaces": [],
334+
"intrinsic": "undefined"
335+
}
336+
]
337+
},
338+
"optional": true
209339
}
210340
]
211341
},
@@ -313,6 +443,71 @@
313443
"name": "NamespacedType"
314444
},
315445
"optional": false
446+
},
447+
{
448+
"name": "os",
449+
"type": {
450+
"kind": "union",
451+
"types": [
452+
{
453+
"kind": "enum",
454+
"name": "Grade",
455+
"parentNamespaces": [
456+
"Root",
457+
"Sub"
458+
],
459+
"members": [
460+
{
461+
"name": "good",
462+
"value": 0
463+
},
464+
{
465+
"name": "bad",
466+
"value": 1
467+
}
468+
]
469+
},
470+
{
471+
"kind": "intrinsic",
472+
"parentNamespaces": [],
473+
"intrinsic": "undefined"
474+
}
475+
]
476+
},
477+
"optional": true
478+
},
479+
{
480+
"name": "oa",
481+
"type": {
482+
"kind": "union",
483+
"types": [
484+
{
485+
"kind": "union",
486+
"types": [
487+
{
488+
"kind": "literal",
489+
"parentNamespaces": [],
490+
"value": "\"one\""
491+
},
492+
{
493+
"kind": "literal",
494+
"parentNamespaces": [],
495+
"value": "\"two\""
496+
}
497+
],
498+
"parentNamespaces": [
499+
"Root"
500+
],
501+
"name": "NamespacedType"
502+
},
503+
{
504+
"kind": "intrinsic",
505+
"parentNamespaces": [],
506+
"intrinsic": "undefined"
507+
}
508+
]
509+
},
510+
"optional": true
316511
}
317512
]
318513
}

0 commit comments

Comments
 (0)