Skip to content

Commit 045c055

Browse files
committed
Fix handling of non-generic aliases that point to generic types
1 parent 2a7b241 commit 045c055

File tree

11 files changed

+71
-52
lines changed

11 files changed

+71
-52
lines changed

src/parsers/objectParser.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ import ts from 'typescript';
22
import { parseProperty } from './propertyParser';
33
import { ParserContext } from '../parser';
44
import { ObjectNode } from '../models';
5-
import { getTypeNamespaces } from './typeResolver';
5+
import { getTypeName, getTypeNamespaces } from './typeResolver';
66

77
export function parseObjectType(
88
type: ts.Type,
99
context: ParserContext,
1010
skipResolvingComplexTypes: boolean,
1111
): ObjectNode | undefined {
12-
const { shouldInclude, shouldResolveObject, typeStack, includeExternalTypes } = context;
12+
const { shouldInclude, shouldResolveObject, typeStack, includeExternalTypes, checker } = context;
1313

1414
const properties = type
1515
.getProperties()
1616
.filter((property) => includeExternalTypes || !isPropertyExternal(property));
1717

18-
const typeSymbol = type.aliasSymbol ?? type.getSymbol();
19-
let typeName = typeSymbol?.getName();
20-
if (typeName === '__type') {
21-
typeName = undefined;
22-
}
18+
const typeName = getTypeName(type, undefined, checker, false);
2319

2420
if (properties.length) {
2521
if (

src/parsers/typeResolver.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export function resolveType(
8989
return new ReferenceNode('RefCallback', []);
9090
}
9191

92-
return new ReferenceNode(getTypeName(type, typeSymbol, checker), namespaces);
92+
return new ReferenceNode(
93+
getTypeName(type, typeSymbol, checker) ?? checker.typeToString(type),
94+
namespaces,
95+
);
9396
}
9497

9598
if (hasFlag(type.flags, ts.TypeFlags.Boolean)) {
@@ -116,11 +119,7 @@ export function resolveType(
116119

117120
if (type.isUnion()) {
118121
const memberTypes: TypeNode[] = [];
119-
const symbol = typeSymbol ?? type.aliasSymbol ?? type.getSymbol();
120-
let typeName = symbol?.getName();
121-
if (typeName === '__type') {
122-
typeName = undefined;
123-
}
122+
const typeName = getTypeName(type, typeSymbol, checker, false);
124123

125124
// @ts-expect-error - Internal API
126125
if (type.origin?.isUnion()) {
@@ -141,11 +140,7 @@ export function resolveType(
141140

142141
if (type.isIntersection()) {
143142
const memberTypes: TypeNode[] = [];
144-
const symbol = typeSymbol ?? type.aliasSymbol ?? type.getSymbol();
145-
let typeName = symbol?.getName();
146-
if (typeName === '__type') {
147-
typeName = undefined;
148-
}
143+
const typeName = getTypeName(type, typeSymbol, checker, false);
149144

150145
for (const memberType of type.types) {
151146
memberTypes.push(resolveType(memberType, context));
@@ -246,12 +241,7 @@ export function resolveType(
246241
type.flags & ts.TypeFlags.Object ||
247242
(type.flags & ts.TypeFlags.NonPrimitive && checker.typeToString(type) === 'object')
248243
) {
249-
const typeSymbol = type.aliasSymbol ?? type.getSymbol();
250-
let typeName = typeSymbol?.getName();
251-
if (typeName === '__type') {
252-
typeName = undefined;
253-
}
254-
244+
const typeName = getTypeName(type, typeSymbol, checker, false);
255245
return new ObjectNode(typeName, namespaces, [], undefined);
256246
}
257247

@@ -358,30 +348,39 @@ function isTypeExternal(type: ts.Type, checker: ts.TypeChecker): boolean {
358348
);
359349
}
360350

361-
function getTypeName(
351+
export function getTypeName(
362352
type: ts.Type,
363353
typeSymbol: ts.Symbol | undefined,
364354
checker: ts.TypeChecker,
365-
): string {
355+
useFallback: boolean = true,
356+
): string | undefined {
366357
const symbol = typeSymbol ?? type.aliasSymbol ?? type.getSymbol();
367358
if (!symbol) {
368-
return checker.typeToString(type);
359+
return useFallback ? checker.typeToString(type) : undefined;
369360
}
370361

371362
const typeName = symbol.getName();
372363
if (typeName === '__type') {
373-
return checker.typeToString(type);
364+
return useFallback ? checker.typeToString(type) : undefined;
374365
}
375366

376367
let typeArguments: string[] | undefined;
377-
if ('target' in type) {
378-
typeArguments = checker
379-
.getTypeArguments(type as ts.TypeReference)
380-
?.map((x) => getTypeName(x, undefined, checker));
381-
}
382368

383-
if (!typeArguments?.length) {
384-
typeArguments = type.aliasTypeArguments?.map((x) => getTypeName(x, undefined, checker)) ?? [];
369+
if (type.aliasSymbol && !type.aliasTypeArguments) {
370+
typeArguments = [];
371+
} else {
372+
if ('target' in type) {
373+
typeArguments = checker
374+
.getTypeArguments(type as ts.TypeReference)
375+
?.map((x) => getTypeName(x, undefined, checker, true) ?? 'unknown');
376+
}
377+
378+
if (!typeArguments?.length) {
379+
typeArguments =
380+
type.aliasTypeArguments?.map(
381+
(x) => getTypeName(x, undefined, checker, true) ?? 'unknown',
382+
) ?? [];
383+
}
385384
}
386385

387386
if (typeArguments && typeArguments.length > 0) {

test/aliases/input.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export interface A {
22
a: Alias;
33
r: MyRecord<string, string>;
4+
s: AliasToGeneric;
45
}
56

6-
export function fn1(a: Alias, r: MyRecord<string, string>) {}
7+
export function fn1(a: Alias, r: MyRecord<string, string>, s: AliasToGeneric) {}
78

89
type SomeType = 1 | 2;
910
type Alias = SomeType;
1011
type MyRecord<Key extends string | number, Value> = Record<Key, Value>;
12+
type AliasToGeneric = Set<number>;

test/aliases/output.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,22 @@
3434
{
3535
"type": {
3636
"kind": "object",
37-
"name": "MyRecord",
37+
"name": "MyRecord<string, string>",
3838
"parentNamespaces": [],
3939
"properties": []
4040
},
4141
"name": "r",
4242
"optional": false
43+
},
44+
{
45+
"type": {
46+
"kind": "object",
47+
"name": "AliasToGeneric",
48+
"parentNamespaces": [],
49+
"properties": []
50+
},
51+
"name": "s",
52+
"optional": false
4353
}
4454
],
4555
"returnValueType": {
@@ -83,7 +93,17 @@
8393
"name": "r",
8494
"type": {
8595
"kind": "object",
86-
"name": "MyRecord",
96+
"name": "MyRecord<string, string>",
97+
"parentNamespaces": [],
98+
"properties": []
99+
},
100+
"optional": false
101+
},
102+
{
103+
"name": "s",
104+
"type": {
105+
"kind": "object",
106+
"name": "AliasToGeneric",
87107
"parentNamespaces": [],
88108
"properties": []
89109
},

test/base-ui-component/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
]
209209
}
210210
],
211-
"name": "HTMLProps",
211+
"name": "HTMLProps<any>",
212212
"parentNamespaces": [],
213213
"properties": [
214214
{
@@ -397,7 +397,7 @@
397397
]
398398
}
399399
],
400-
"name": "HTMLProps",
400+
"name": "HTMLProps<any>",
401401
"parentNamespaces": [],
402402
"properties": [
403403
{

test/conditional-types/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
{
5454
"type": {
5555
"kind": "object",
56-
"name": "Props",
56+
"name": "Props<Multiple>",
5757
"parentNamespaces": [],
5858
"properties": [
5959
{

test/generics-closed-parameters/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"type": {
1515
"kind": "object",
16-
"name": "GenericObject",
16+
"name": "GenericObject<MyUnion>",
1717
"parentNamespaces": [],
1818
"properties": [
1919
{

test/generics/output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"type": {
1515
"kind": "object",
16-
"name": "GenericFunctionParameters",
16+
"name": "GenericFunctionParameters<T>",
1717
"parentNamespaces": [],
1818
"properties": [
1919
{
@@ -30,14 +30,14 @@
3030
"name": "nestedGenericType",
3131
"type": {
3232
"kind": "object",
33-
"name": "GenericInterface",
33+
"name": "GenericInterface<GenericInterface<string>>",
3434
"parentNamespaces": [],
3535
"properties": [
3636
{
3737
"name": "data",
3838
"type": {
3939
"kind": "object",
40-
"name": "GenericInterface",
40+
"name": "GenericInterface<string>",
4141
"parentNamespaces": [],
4242
"properties": [
4343
{

test/mapped-types/output.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"type": {
1515
"kind": "object",
16-
"name": "Partial",
16+
"name": "Partial<Base>",
1717
"parentNamespaces": [],
1818
"properties": [
1919
{
@@ -83,7 +83,7 @@
8383
{
8484
"type": {
8585
"kind": "object",
86-
"name": "Required",
86+
"name": "Required<Base>",
8787
"parentNamespaces": [],
8888
"properties": [
8989
{
@@ -131,7 +131,7 @@
131131
{
132132
"type": {
133133
"kind": "object",
134-
"name": "Partial",
134+
"name": "Partial<Base>",
135135
"parentNamespaces": [],
136136
"properties": [
137137
{
@@ -201,7 +201,7 @@
201201
{
202202
"type": {
203203
"kind": "object",
204-
"name": "Required",
204+
"name": "Required<Base>",
205205
"parentNamespaces": [],
206206
"properties": [
207207
{

test/object-types/output.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
{
8080
"type": {
8181
"kind": "object",
82+
"name": "ObjectKeyword",
8283
"parentNamespaces": [],
8384
"properties": []
8485
},
@@ -153,6 +154,7 @@
153154
"name": "objectKeyword",
154155
"type": {
155156
"kind": "object",
157+
"name": "ObjectKeyword",
156158
"parentNamespaces": [],
157159
"properties": []
158160
},

0 commit comments

Comments
 (0)