-
Notifications
You must be signed in to change notification settings - Fork 11
/
getTypeDescription.ts
347 lines (307 loc) · 9.29 KB
/
getTypeDescription.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { TypeKind } from "tst-reflect";
import * as ts from "typescript";
import {
ConditionalType
} from "typescript";
import { Context } from "./contexts/Context";
import { TypePropertiesSource } from "./declarations";
import { getConstructors } from "./getConstructors";
import { getDecorators } from "./getDecorators";
import getLiteralName from "./getLiteralName";
import { getMethods } from "./getMethods";
import { getNativeTypeDescription } from "./getNativeTypeDescription";
import { getNodeLocationText } from "./getNodeLocationText";
import { getProperties } from "./getProperties";
import { getTypeCall } from "./getTypeCall";
import {
createCtorGetter,
getType,
getTypeFullName,
getTypeKind,
UNKNOWN_TYPE_PROPERTIES
} from "./helpers";
import { log } from "./log";
/**
* Return TypePropertiesSource object describing given type
* @param symbol
* @param type
* @param context
* @param typeCtor
*/
export function getTypeDescription(
type: ts.Type,
symbol: ts.Symbol | undefined,
context: Context,
typeCtor?: ts.EntityName | ts.DeclarationName
)
: TypePropertiesSource
{
if (context.config.debugMode && symbol?.declarations)
{
log.trace(getNodeLocationText(symbol.declarations[0]));
}
const nativeTypeDescriptionResult = getNativeTypeDescription(type, context);
if (nativeTypeDescriptionResult.ok)
{
return nativeTypeDescriptionResult.typeDescription;
}
if ((type.flags & ts.TypeFlags.Literal) != 0)
{
return {
k: TypeKind.LiteralType,
n: getLiteralName(type),
v: (type as any).value,
};
}
let typeSymbol = type.getSymbol();
if (type.aliasSymbol)
{
symbol = type.aliasSymbol;
}
if (!symbol && typeSymbol)
{
symbol = typeSymbol;
}
const checker = context.typeChecker;
if (type.isUnionOrIntersection())
{
const types = type.types
.map(childType => getTypeCall(
childType,
undefined, //checker.getSymbolAtLocation(typeNode),
context
)
);
return {
k: TypeKind.Container,
types: types,
union: type.isUnion(),
inter: type.isIntersection()
};
}
if (symbol)
{
if (symbol.flags == ts.SymbolFlags.TypeLiteral && type.flags == ts.TypeFlags.Object)
{
if (context.config.debugMode)
{
log.info("Symbol is TypeLiteral of Object type.");
}
const declaration = symbol.declarations?.[0];
if (declaration && ts.isTypeLiteralNode(declaration) && type.aliasSymbol)
{
return {
k: TypeKind.Object,
n: type.aliasSymbol.name,
fn: getTypeFullName(type.aliasSymbol),
props: getProperties(symbol, type, context)
};
}
return {
k: TypeKind.Object,
props: getProperties(symbol, type, context)
};
}
if (symbol.valueDeclaration && (
ts.isPropertyDeclaration(symbol.valueDeclaration)
|| ts.isPropertySignature(symbol.valueDeclaration)
|| ts.isVariableDeclaration(symbol.valueDeclaration)
|| ts.isParameter(symbol.valueDeclaration)
))
{
if (symbol.valueDeclaration.type)
{
if (ts.isTypeReferenceNode(symbol.valueDeclaration.type)
&& typeSymbol && (typeSymbol.flags & ts.SymbolFlags.Transient) == ts.SymbolFlags.Transient)
{
return {
k: TypeKind.TransientTypeReference,
n: (symbol.valueDeclaration.type.typeName as any).escapedText,
args: symbol.valueDeclaration.type.typeArguments?.map(typeNode => getTypeCall(
checker.getTypeAtLocation(typeNode),
checker.getSymbolAtLocation(typeNode),
context
)
) || [],
};
}
let isUnion, isIntersection = false;
if (
(isUnion = ts.isUnionTypeNode(symbol.valueDeclaration.type))
|| (isIntersection = ts.isIntersectionTypeNode(symbol.valueDeclaration.type))
)
{
const types = symbol.valueDeclaration.type.types
.map(typeNode => getTypeCall(
checker.getTypeAtLocation(typeNode),
checker.getSymbolAtLocation(typeNode),
context
)
);
return {
k: TypeKind.Container,
types: types,
union: isUnion,
inter: isIntersection
};
}
}
}
}
if (!typeSymbol)
{
if (context.config.debugMode)
{
log.info("'typeSymbol' is undefined.");
}
if ((type.flags & ts.TypeFlags.Object) == ts.TypeFlags.Object)
{
if (context.config.debugMode)
{
log.info("Symbol is TypeLiteral of Object type.");
}
return {
k: TypeKind.Object,
props: getProperties(symbol, type, context)
};
}
else if ((type.flags & ts.TypeFlags.Conditional) == ts.TypeFlags.Conditional)
{
const ct = (type as ConditionalType).root.node;
const extendsType = checker.getTypeAtLocation(ct.extendsType);
const trueType = checker.getTypeAtLocation(ct.trueType);
return {
k: TypeKind.ConditionalType,
ct: {
e: getTypeCall(extendsType, extendsType.symbol, context),
tt: getTypeCall(trueType, trueType.symbol, context),
ft: getTypeCall(checker.getTypeAtLocation(ct.falseType), checker.getSymbolAtLocation(ct.falseType), context)
}
};
}
else if ((type.flags & ts.TypeFlags.IndexedAccess) == ts.TypeFlags.IndexedAccess)
{
const indexedAccess = type as ts.IndexedAccessType;
return {
k: TypeKind.IndexedAccess,
iat: {
ot: getTypeCall(indexedAccess.objectType, indexedAccess.objectType.symbol, context),
it: getTypeCall(indexedAccess.indexType, indexedAccess.indexType.symbol, context)
}
};
}
// throw new Error("Unable to resolve type's symbol.");
log.error("Unable to resolve type's symbol. Returning type Unknown.");
return UNKNOWN_TYPE_PROPERTIES;
}
else if ((type.flags & ts.TypeFlags.Object) == ts.TypeFlags.Object && (typeSymbol.flags & ts.SymbolFlags.TypeLiteral) == ts.SymbolFlags.TypeLiteral)
{
if (context.config.debugMode)
{
log.info("'typeSymbol' is TypeLiteral.");
}
return {
k: TypeKind.Object,
props: getProperties(typeSymbol, type, context)
};
}
// Some object literal type, eg. `private foo: { a: string, b: number };`
else if ((type.flags & ts.TypeFlags.Object) == ts.TypeFlags.Object && (typeSymbol.flags & ts.SymbolFlags.ObjectLiteral) == ts.SymbolFlags.ObjectLiteral)
{
if (context.config.debugMode)
{
log.info("'typeSymbol' is ObjectLiteral.");
}
return {
k: TypeKind.Object,
props: getProperties(typeSymbol, type, context)
};
}
else if ((type.flags & ts.TypeFlags.TypeParameter) == ts.TypeFlags.TypeParameter && (typeSymbol.flags & ts.SymbolFlags.TypeParameter) == ts.SymbolFlags.TypeParameter)
{
if (context.config.debugMode)
{
log.info("'typeSymbol' is TypeParameter.");
}
if (typeSymbol.declarations)
{
const typeParameter = typeSymbol.declarations[0];
if (ts.isTypeParameterDeclaration(typeParameter))
{
return {
k: TypeKind.TypeParameter,
n: typeParameter.name.escapedText as string,
con: typeParameter.constraint && getTypeCall(
checker.getTypeAtLocation(typeParameter.constraint),
checker.getSymbolAtLocation(typeParameter.constraint),
context
) || undefined,
def: typeParameter.default && getTypeCall(
checker.getTypeAtLocation(typeParameter.default),
checker.getSymbolAtLocation(typeParameter.default),
context
) || undefined
};
}
}
throw new Error("Unable to resolve TypeParameter's declaration.");
}
const decorators = getDecorators(typeSymbol, checker);
const kind = getTypeKind(typeSymbol);
const symbolType = getType(typeSymbol, checker);
const properties: TypePropertiesSource = {
k: kind,
n: typeSymbol.getName(),
fn: getTypeFullName(typeSymbol || type.getSymbol()),
props: getProperties(symbol, type, context),
meths: getMethods(symbol, type, context),
decs: decorators,
};
if (kind === TypeKind.Class)
{
properties.ctors = getConstructors(symbolType, context);
if (typeCtor)
{
properties.ctor = createCtorGetter(typeCtor);
context.addTypeCtor(typeCtor); // TODO: Check if it has modifier "export". If yes, pass path to file and name of the exported type to the addTypeCtor; handle this in index.ts and so on. If it is not exported (it is just local module type), don't generate this into the metadata lib file, generate it into the local file like in base mode
}
}
const declaration = typeSymbol.declarations?.[0];
if (declaration && (
ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)
))
{
// extends & implements
if (declaration.heritageClauses)
{
const ext = declaration.heritageClauses.filter(h => h.token == ts.SyntaxKind.ExtendsKeyword)[0];
if (ext)
{
properties.bt = getTypeCall(
checker.getTypeAtLocation(ext.types[0]),
checker.getSymbolAtLocation(ext.types[0]),
context
);
}
const impl = declaration.heritageClauses.filter(h => h.token == ts.SyntaxKind.ImplementsKeyword)[0];
if (impl)
{
properties.iface = getTypeCall(
checker.getTypeAtLocation(impl.types[0]),
checker.getSymbolAtLocation(impl.types[0]),
context
);
}
}
// Type parameters
if (declaration.typeParameters)
{
properties.tp = declaration.typeParameters.map(typeParameterDeclaration => getTypeCall(
checker.getTypeAtLocation(typeParameterDeclaration),
checker.getSymbolAtLocation(typeParameterDeclaration),
context
));
}
}
return properties;
}