-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathresolveType.ts
More file actions
729 lines (617 loc) · 28.4 KB
/
Copy pathresolveType.ts
File metadata and controls
729 lines (617 loc) · 28.4 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import * as _ from 'lodash';
import * as ts from 'typescript';
import { getDecoratorName } from '../utils/decoratorUtils';
import { getFirstMatchingJSDocTagName } from '../utils/jsDocUtils';
import { keywords } from './keywordKinds';
import { ArrayType, EnumerateType, MetadataGenerator, ObjectType, Property, ReferenceType, Type } from './metadataGenerator';
const syntaxKindMap: { [kind: number]: string } = {};
syntaxKindMap[ts.SyntaxKind.NumberKeyword] = 'number';
syntaxKindMap[ts.SyntaxKind.StringKeyword] = 'string';
syntaxKindMap[ts.SyntaxKind.BooleanKeyword] = 'boolean';
syntaxKindMap[ts.SyntaxKind.VoidKeyword] = 'void';
const localReferenceTypeCache: { [typeName: string]: ReferenceType } = {};
const inProgressTypes: { [typeName: string]: boolean } = {};
type UsableDeclaration = ts.InterfaceDeclaration | ts.ClassDeclaration | ts.TypeAliasDeclaration;
export function resolveType(typeNode?: ts.TypeNode, genericTypeMap?: Map<String, ts.TypeNode>): Type {
if (!typeNode) {
return { typeName: 'void' };
}
const primitiveType = getPrimitiveType(typeNode);
if (primitiveType) {
return primitiveType;
}
if (typeNode.kind === ts.SyntaxKind.ArrayType) {
const arrayType = typeNode as ts.ArrayTypeNode;
return {
elementType: resolveType(arrayType.elementType, genericTypeMap),
typeName: 'array'
} as ArrayType;
}
if ((typeNode.kind === ts.SyntaxKind.AnyKeyword) || (typeNode.kind === ts.SyntaxKind.ObjectKeyword)) {
return { typeName: 'object' };
}
if (typeNode.kind === ts.SyntaxKind.TypeLiteral) {
return getInlineObjectType(typeNode);
}
if (typeNode.kind === ts.SyntaxKind.UnionType) {
return getUnionType(typeNode);
}
if (typeNode.kind !== ts.SyntaxKind.TypeReference) {
throw new Error(`Unknown type: ${ts.SyntaxKind[typeNode.kind]}`);
}
let typeReference: any = typeNode;
let typeName = resolveSimpleTypeName(typeReference.typeName as ts.EntityName);
if (typeName === 'Date') { return getDateType(typeNode); }
if (typeName === 'Buffer') { return { typeName: 'buffer' }; }
if (typeName === 'DownloadBinaryData') { return { typeName: 'buffer' }; }
if (typeName === 'DownloadResource') { return { typeName: 'buffer' }; }
if (typeName === 'Promise' || typeName === 'Observable') {
typeReference = typeReference.typeArguments[0];
return resolveType(typeReference, genericTypeMap);
}
if (typeName === 'Array') {
typeReference = typeReference.typeArguments[0];
return {
elementType: resolveType(typeReference, genericTypeMap),
typeName: 'array'
} as ArrayType;
}
const enumType = getEnumerateType(typeNode);
if (enumType) {
return enumType;
}
const literalType = getLiteralType(typeNode);
if (literalType) {
return literalType;
}
let referenceType: ReferenceType;
if (typeReference.typeArguments && typeReference.typeArguments.length === 1) {
const typeT: Array<ts.TypeNode> = typeReference.typeArguments as Array<ts.TypeNode>;
referenceType = getReferenceType(typeReference.typeName as ts.EntityName, genericTypeMap, typeT);
typeName = resolveSimpleTypeName(typeReference.typeName as ts.EntityName);
if (['NewResource', 'RequestAccepted', 'MovedPermanently', 'MovedTemporarily'].indexOf(typeName) >= 0) {
referenceType.typeName = typeName;
referenceType.typeArgument = resolveType(typeT[0], genericTypeMap);
} else {
MetadataGenerator.current.addReferenceType(referenceType);
}
} else {
referenceType = getReferenceType(typeReference.typeName as ts.EntityName, genericTypeMap);
MetadataGenerator.current.addReferenceType(referenceType);
}
return referenceType;
}
function getPrimitiveType(typeNode: ts.TypeNode): Type | undefined {
const primitiveType = syntaxKindMap[typeNode.kind];
if (!primitiveType) { return undefined; }
if (primitiveType === 'number') {
const parentNode = typeNode.parent as ts.Node;
if (!parentNode) {
return { typeName: 'double' };
}
const validDecorators = ['IsInt', 'IsLong', 'IsFloat', 'IsDouble'];
// Can't use decorators on interface/type properties, so support getting the type from jsdoc too.
const jsdocTagName = getFirstMatchingJSDocTagName(parentNode, tag => {
return validDecorators.some(t => t === tag.tagName.text);
});
const decoratorName = getDecoratorName(parentNode, identifier => {
return validDecorators.some(m => m === identifier.text);
});
switch (decoratorName || jsdocTagName) {
case 'IsInt':
return { typeName: 'integer' };
case 'IsLong':
return { typeName: 'long' };
case 'IsFloat':
return { typeName: 'float' };
case 'IsDouble':
return { typeName: 'double' };
default:
return { typeName: 'double' };
}
}
return { typeName: primitiveType };
}
function getDateType(typeNode: ts.TypeNode): Type {
const parentNode = typeNode.parent as ts.Node;
if (!parentNode) {
return { typeName: 'datetime' };
}
const decoratorName = getDecoratorName(parentNode, identifier => {
return ['IsDate', 'IsDateTime'].some(m => m === identifier.text);
});
switch (decoratorName) {
case 'IsDate':
return { typeName: 'date' };
case 'IsDateTime':
return { typeName: 'datetime' };
default:
return { typeName: 'datetime' };
}
}
function getEnumerateType(typeNode: ts.TypeNode): EnumerateType | undefined {
const enumName = (typeNode as any).typeName.text;
const enumTypes = MetadataGenerator.current.nodes
.filter(node => node.kind === ts.SyntaxKind.EnumDeclaration)
.filter(node => (node as any).name.text === enumName);
if (!enumTypes.length) { return undefined; }
if (enumTypes.length > 1) { throw new Error(`Multiple matching enum found for enum ${enumName}; please make enum names unique.`); }
const enumDeclaration = enumTypes[0] as ts.EnumDeclaration;
function getEnumValue(member: any) {
const initializer = member.initializer;
if (initializer) {
if (initializer.expression) {
return parseEnumValueByKind(initializer.expression.text, initializer.kind);
}
return parseEnumValueByKind(initializer.text, initializer.kind);
}
return;
}
return {
enumMembers: enumDeclaration.members.map((member: any, index) => {
return getEnumValue(member) || index;
}),
typeName: 'enum',
} as EnumerateType;
}
function parseEnumValueByKind(value: string, kind: ts.SyntaxKind): any {
return kind === ts.SyntaxKind.NumericLiteral ? parseFloat(value) : value;
}
function getUnionType(typeNode: ts.TypeNode) {
const union = typeNode as ts.UnionTypeNode;
let baseType: any = null;
let isObject = false;
union.types.forEach(type => {
if (baseType === null) {
baseType = type;
}
if (baseType.kind !== type.kind) {
isObject = true;
}
});
if (isObject) {
return { typeName: 'object' };
}
return {
enumMembers: union.types.map((type, index) => {
return type.getText() ? removeQuotes(type.getText()) : index;
}),
typeName: 'enum',
} as EnumerateType;
}
function removeQuotes(str: string) {
return str.replace(/^["']|["']$/g, '');
}
function getLiteralType(typeNode: ts.TypeNode): EnumerateType | undefined {
const literalName = (typeNode as any).typeName.text;
const literalTypes = MetadataGenerator.current.nodes
.filter(node => node.kind === ts.SyntaxKind.TypeAliasDeclaration)
.filter(node => {
const innerType = (node as any).type;
return innerType.kind === ts.SyntaxKind.UnionType && (innerType as any).types;
})
.filter(node => (node as any).name.text === literalName);
if (!literalTypes.length) { return undefined; }
if (literalTypes.length > 1) { throw new Error(`Multiple matching enum found for enum ${literalName}; please make enum names unique.`); }
const unionTypes = (literalTypes[0] as any).type.types;
return {
enumMembers: unionTypes.map((unionNode: any) => unionNode.literal.text as string),
typeName: 'enum',
} as EnumerateType;
}
function getInlineObjectType(typeNode: ts.TypeNode): ObjectType {
const type: ObjectType = {
properties: getModelTypeProperties(typeNode),
typeName: ''
};
return type;
}
function getReferenceType(type: ts.EntityName, genericTypeMap?: Map<String, ts.TypeNode>, genericTypes?: Array<ts.TypeNode>): ReferenceType {
let typeName = resolveFqTypeName(type);
if (genericTypeMap && genericTypeMap.has(typeName)) {
const refType: any = genericTypeMap.get(typeName);
type = refType.typeName as ts.EntityName;
typeName = resolveFqTypeName(type);
}
const typeNameWithGenerics = getTypeName(typeName, genericTypes);
try {
const existingType = localReferenceTypeCache[typeNameWithGenerics];
if (existingType) { return existingType; }
if (inProgressTypes[typeNameWithGenerics]) {
return createCircularDependencyResolver(typeNameWithGenerics);
}
inProgressTypes[typeNameWithGenerics] = true;
const modelTypeDeclaration = getModelTypeDeclaration(type);
const properties = getModelTypeProperties(modelTypeDeclaration, genericTypes);
const additionalProperties = getModelTypeAdditionalProperties(modelTypeDeclaration);
const referenceType: ReferenceType = {
description: getModelDescription(modelTypeDeclaration),
properties: properties,
typeName: typeNameWithGenerics,
};
if (additionalProperties && additionalProperties.length) {
referenceType.additionalProperties = additionalProperties;
}
const extendedProperties = getInheritedProperties(modelTypeDeclaration, genericTypes);
mergeReferenceTypeProperties(referenceType.properties, extendedProperties);
localReferenceTypeCache[typeNameWithGenerics] = referenceType;
return referenceType;
} catch (err) {
console.error(`There was a problem resolving type of '${getTypeName(typeName, genericTypes)}'.`);
throw err;
}
}
function mergeReferenceTypeProperties(properties: Array<Property>, extendedProperties: Array<Property>) {
extendedProperties.forEach(prop => {
const existingProp = properties.find(p => p.name === prop.name);
if (existingProp) {
existingProp.description = existingProp.description || prop.description;
} else {
properties.push(prop);
}
});
}
function resolveFqTypeName(type: ts.EntityName): string {
if (type.kind === ts.SyntaxKind.Identifier) {
return (type as ts.Identifier).text;
}
const qualifiedType = type as ts.QualifiedName;
return resolveFqTypeName(qualifiedType.left) + '.' + (qualifiedType.right as ts.Identifier).text;
}
function resolveSimpleTypeName(type: ts.EntityName): string {
if (type.kind === ts.SyntaxKind.Identifier) {
return (type as ts.Identifier).text;
}
const qualifiedType = type as ts.QualifiedName;
return (qualifiedType.right as ts.Identifier).text;
}
function getTypeName(typeName: string, genericTypes?: Array<ts.TypeNode>): string {
if (!genericTypes || !genericTypes.length) { return typeName; }
return typeName + genericTypes.map(t => getAnyTypeName(t)).join('');
}
function getAnyTypeName(typeNode: ts.TypeNode): string {
const primitiveType = syntaxKindMap[typeNode.kind];
if (primitiveType) {
return primitiveType;
}
if (typeNode.kind === ts.SyntaxKind.ArrayType) {
const arrayType = typeNode as ts.ArrayTypeNode;
return getAnyTypeName(arrayType.elementType) + 'Array';
}
if (typeNode.kind === ts.SyntaxKind.UnionType ||
typeNode.kind === ts.SyntaxKind.AnyKeyword) {
return 'object';
}
if (typeNode.kind !== ts.SyntaxKind.TypeReference) {
throw new Error(`Unknown type: ${ts.SyntaxKind[typeNode.kind]}`);
}
const typeReference = typeNode as ts.TypeReferenceNode;
try {
const typeName = (typeReference.typeName as ts.Identifier).text;
if (typeName === 'Array') {
return getAnyTypeName(typeReference.typeArguments[0]) + 'Array';
}
return typeName;
} catch (e) {
// idk what would hit this? probably needs more testing
console.error(e);
return typeNode.toString();
}
}
function createCircularDependencyResolver(typeName: string) {
const referenceType = {
description: '',
properties: new Array<Property>(),
typeName: typeName,
};
MetadataGenerator.current.onFinish(referenceTypes => {
const realReferenceType = referenceTypes[typeName];
if (!realReferenceType) { return; }
referenceType.description = realReferenceType.description;
referenceType.properties = realReferenceType.properties;
referenceType.typeName = realReferenceType.typeName;
});
return referenceType;
}
function nodeIsUsable(node: ts.Node) {
switch (node.kind) {
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.TypeAliasDeclaration:
return true;
default: return false;
}
}
function resolveLeftmostIdentifier(type: ts.EntityName): ts.Identifier {
while (type.kind !== ts.SyntaxKind.Identifier) {
type = (type as ts.QualifiedName).left;
}
return type as ts.Identifier;
}
function resolveModelTypeScope(leftmost: ts.EntityName, statements: Array<any>): Array<any> {
// while (leftmost.parent && leftmost.parent.kind === ts.SyntaxKind.QualifiedName) {
// const leftmostName = leftmost.kind === ts.SyntaxKind.Identifier
// ? (leftmost as ts.Identifier).text
// : (leftmost as ts.QualifiedName).right.text;
// const moduleDeclarations = statements
// .filter(node => {
// if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
// const moduleDeclaration = node as ts.ModuleDeclaration;
// return (moduleDeclaration.name as ts.Identifier).text.toLowerCase() === leftmostName.toLowerCase();
// }
// return false;
// }) as Array<ts.ModuleDeclaration>;
// if (!moduleDeclarations.length) { throw new Error(`No matching module declarations found for ${leftmostName}`); }
// if (moduleDeclarations.length > 1) { throw new Error(`Multiple matching module declarations found for ${leftmostName}; please make module declarations unique`); }
// const moduleBlock = moduleDeclarations[0].body as ts.ModuleBlock;
// if (moduleBlock === null || moduleBlock.kind !== ts.SyntaxKind.ModuleBlock) { throw new Error(`Module declaration found for ${leftmostName} has no body`); }
// statements = moduleBlock.statements;
// leftmost = leftmost.parent as ts.EntityName;
// }
return statements;
}
function getModelTypeDeclaration(type: ts.EntityName) {
const leftmostIdentifier = resolveLeftmostIdentifier(type);
const statements: Array<any> = resolveModelTypeScope(leftmostIdentifier, MetadataGenerator.current.nodes);
const typeName = type.kind === ts.SyntaxKind.Identifier
? (type as ts.Identifier).text
: (type as ts.QualifiedName).right.text;
const modelTypes = statements
.filter(node => {
if (!nodeIsUsable(node)) {
return false;
}
const modelTypeDeclaration = node as UsableDeclaration;
return (modelTypeDeclaration.name as ts.Identifier).text === typeName;
}) as Array<UsableDeclaration>;
if (!modelTypes.length) { throw new Error(`No matching model found for referenced type ${typeName}`); }
// if (modelTypes.length > 1) {
// const conflicts = modelTypes.map(modelType => modelType.getSourceFile().fileName).join('"; "');
// throw new Error(`Multiple matching models found for referenced type ${typeName}; please make model names unique. Conflicts found: "${conflicts}"`);
// }
return modelTypes[0];
}
function getModelTypeProperties(node: any, genericTypes?: Array<ts.TypeNode>): Array<Property> {
if (node.kind === ts.SyntaxKind.TypeLiteral || node.kind === ts.SyntaxKind.InterfaceDeclaration) {
const interfaceDeclaration = node as ts.InterfaceDeclaration;
return interfaceDeclaration.members
.filter(member => {
if ((member as any).type && (member as any).type.kind === ts.SyntaxKind.FunctionType) {
return false;
}
return member.kind === ts.SyntaxKind.PropertySignature;
})
.map((member: any) => {
const propertyDeclaration = member as ts.PropertyDeclaration;
const identifier = propertyDeclaration.name as ts.Identifier;
if (!propertyDeclaration.type) { throw new Error('No valid type found for property declaration.'); }
// Declare a variable that can be overridden if needed
let aType = propertyDeclaration.type;
// aType.kind will always be a TypeReference when the property of Interface<T> is of type T
if (aType.kind === ts.SyntaxKind.TypeReference && genericTypes && genericTypes.length && node.typeParameters) {
// The type definitions are conviently located on the object which allow us to map -> to the genericTypes
const typeParams = _.map(node.typeParameters, (typeParam: ts.TypeParameterDeclaration) => {
return typeParam.name.text;
});
// I am not sure in what cases
const typeIdentifier = (aType as ts.TypeReferenceNode).typeName;
let typeIdentifierName: string;
// typeIdentifier can either be a Identifier or a QualifiedName
if ((typeIdentifier as ts.Identifier).text) {
typeIdentifierName = (typeIdentifier as ts.Identifier).text;
} else {
typeIdentifierName = (typeIdentifier as ts.QualifiedName).right.text;
}
// I could not produce a situation where this did not find it so its possible this check is irrelevant
const indexOfType = _.indexOf<string>(typeParams, typeIdentifierName);
if (indexOfType >= 0) {
aType = genericTypes[indexOfType] as ts.TypeNode;
}
}
return {
description: getNodeDescription(propertyDeclaration),
name: identifier.text,
required: !propertyDeclaration.questionToken,
type: resolveType(aType)
};
});
}
if (node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
const typeAlias = node as ts.TypeAliasDeclaration;
return !keywords.includes(typeAlias.type.kind)
? getModelTypeProperties(typeAlias.type, genericTypes)
: [];
}
const classDeclaration = node as ts.ClassDeclaration;
let properties = classDeclaration.members.filter((member: any) => {
if (member.kind !== ts.SyntaxKind.PropertyDeclaration) { return false; }
const propertySignature = member as ts.PropertySignature;
return propertySignature && hasPublicMemberModifier(propertySignature);
}) as Array<ts.PropertyDeclaration | ts.ParameterDeclaration>;
const classConstructor = classDeclaration.members.find((member: any) => member.kind === ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration;
if (classConstructor && classConstructor.parameters) {
properties = properties.concat(classConstructor.parameters.filter(parameter => hasPublicConstructorModifier(parameter)) as any);
}
return properties
.map(declaration => {
const identifier = declaration.name as ts.Identifier;
if (!declaration.type) { throw new Error('No valid type found for property declaration.'); }
return {
description: getNodeDescription(declaration),
name: identifier.text,
required: !declaration.questionToken,
type: resolveType(resolveTypeParameter(declaration.type, classDeclaration, genericTypes))
};
});
}
function resolveTypeParameter(type: any, classDeclaration: ts.ClassDeclaration, genericTypes?: Array<ts.TypeNode>) {
if (genericTypes && classDeclaration.typeParameters && classDeclaration.typeParameters.length) {
for (let i = 0; i < classDeclaration.typeParameters.length; i++) {
if (type.typeName && classDeclaration.typeParameters[i].name.text === type.typeName.text) {
return genericTypes[i];
}
}
}
return type;
}
function getModelTypeAdditionalProperties(node: UsableDeclaration) {
if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
const interfaceDeclaration = node as ts.InterfaceDeclaration;
return interfaceDeclaration.members
.filter(member => member.kind === ts.SyntaxKind.IndexSignature)
.map((member: any) => {
const indexSignatureDeclaration = member as ts.IndexSignatureDeclaration;
const indexType = resolveType(indexSignatureDeclaration.parameters[0].type as ts.TypeNode);
if (indexType.typeName !== 'string') {
throw new Error(`Only string indexers are supported. Found ${indexType.typeName}.`);
}
return {
description: '',
name: '',
required: true,
type: resolveType(indexSignatureDeclaration.type as ts.TypeNode)
};
});
}
return undefined;
}
function hasPublicMemberModifier(node: ts.Node) {
return !node.modifiers || node.modifiers.every(modifier => {
return modifier.kind !== ts.SyntaxKind.ProtectedKeyword && modifier.kind !== ts.SyntaxKind.PrivateKeyword;
});
}
function hasPublicConstructorModifier(node: ts.Node) {
return node.modifiers && node.modifiers.some(modifier => {
return modifier.kind === ts.SyntaxKind.PublicKeyword;
});
}
function getInheritedProperties(modelTypeDeclaration: UsableDeclaration, genericTypes?: Array<ts.TypeNode>): Array<Property> {
const properties = new Array<Property>();
if (modelTypeDeclaration.kind === ts.SyntaxKind.TypeAliasDeclaration) {
return [];
}
const heritageClauses = modelTypeDeclaration.heritageClauses;
if (!heritageClauses) { return properties; }
heritageClauses.forEach(clause => {
if (!clause.types) { return; }
clause.types.forEach((t: any) => {
let type: any = MetadataGenerator.current.getClassDeclaration(t.expression.getText());
if (!type) {
type = MetadataGenerator.current.getInterfaceDeclaration(t.expression.getText());
}
const baseEntityName = t.expression as ts.EntityName;
const parentGenerictypes = resolveTypeArguments(modelTypeDeclaration as ts.ClassDeclaration, genericTypes);
const genericTypeMap = resolveTypeArguments(type, t.typeArguments, parentGenerictypes);
const subClassGenericTypes: any = getSubClassGenericTypes(genericTypeMap, t.typeArguments);
getReferenceType(baseEntityName, genericTypeMap, subClassGenericTypes).properties
.forEach(property => properties.push(property));
});
});
return properties;
}
function getModelDescription(modelTypeDeclaration: UsableDeclaration) {
return getNodeDescription(modelTypeDeclaration);
}
function getNodeDescription(node: UsableDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration) {
const symbol = MetadataGenerator.current.typeChecker.getSymbolAtLocation(node.name as ts.Node);
if (symbol) {
/**
* TODO: Workaround for what seems like a bug in the compiler
* Warrants more investigation and possibly a PR against typescript
*/
if (node.kind === ts.SyntaxKind.Parameter) {
// TypeScript won't parse jsdoc if the flag is 4, i.e. 'Property'
symbol.flags = 0;
}
const comments = symbol.getDocumentationComment(MetadataGenerator.current.typeChecker);
if (comments.length) { return ts.displayPartsToString(comments); }
}
return '';
}
function getSubClassGenericTypes(genericTypeMap?: Map<String, ts.TypeNode>, typeArguments?: Array<ts.TypeNode>) {
if (genericTypeMap && typeArguments) {
const result: Array<ts.TypeNode> = [];
typeArguments.forEach((t: any) => {
const typeName = getAnyTypeName(t);
if (genericTypeMap.has(typeName)) {
result.push(genericTypeMap.get(typeName) as ts.TypeNode);
} else {
result.push(t);
}
});
return result;
}
return null;
}
export function getSuperClass(node: ts.ClassDeclaration, typeArguments?: Map<String, ts.TypeNode>) {
const clauses = node.heritageClauses;
if (clauses) {
const filteredClauses = clauses.filter(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
if (filteredClauses.length > 0) {
const clause: ts.HeritageClause = filteredClauses[0];
if (clause.types && clause.types.length) {
const type: any = MetadataGenerator.current.getClassDeclaration(clause.types[0].expression.getText());
return {
type: type,
typeArguments: resolveTypeArguments(type, clause.types[0].typeArguments, typeArguments)
};
}
}
}
return undefined;
}
function buildGenericTypeMap(node: ts.ClassDeclaration, typeArguments?: ReadonlyArray<ts.TypeNode>) {
const result: Map<String, ts.TypeNode> = new Map<String, ts.TypeNode>();
if (node.typeParameters && typeArguments) {
node.typeParameters.forEach((typeParam, index) => {
const paramName = typeParam.name.text;
result.set(paramName, typeArguments[index]);
});
}
return result;
}
function resolveTypeArguments(node: ts.ClassDeclaration, typeArguments?: ReadonlyArray<ts.TypeNode>, parentTypeArguments?: Map<String, ts.TypeNode>) {
const result = buildGenericTypeMap(node, typeArguments);
if (parentTypeArguments) {
result.forEach((value: any, key) => {
const typeName = getAnyTypeName(value);
if (parentTypeArguments.has(typeName)) {
result.set(key, parentTypeArguments.get(typeName) as ts.TypeNode);
}
});
}
return result;
}
/**
* Used to identify union types of a primitive and array of the same primitive, e.g. `string | string[]`
*/
export function getCommonPrimitiveAndArrayUnionType(typeNode?: ts.TypeNode): Type | null {
if (typeNode && typeNode.kind === ts.SyntaxKind.UnionType) {
const union = typeNode as ts.UnionTypeNode;
const types = union.types.map(t => resolveType(t));
const arrType = types.find(t => t.typeName === 'array') as ArrayType | undefined;
const primitiveType = types.find(t => t.typeName !== 'array');
if (types.length === 2 && arrType && arrType.elementType && primitiveType && arrType.elementType.typeName === primitiveType.typeName) {
return arrType;
}
}
return null;
}
export function getLiteralValue(expression: ts.Expression): any {
if (expression.kind === ts.SyntaxKind.StringLiteral) {
return (expression as ts.StringLiteral).text;
}
if (expression.kind === ts.SyntaxKind.NumericLiteral) {
return parseFloat((expression as ts.NumericLiteral).text);
}
if (expression.kind === ts.SyntaxKind.TrueKeyword) {
return true;
}
if (expression.kind === ts.SyntaxKind.FalseKeyword) {
return false;
}
if (expression.kind === ts.SyntaxKind.ArrayLiteralExpression) {
return (expression as ts.ArrayLiteralExpression).elements.map(e => getLiteralValue(e));
}
return;
}