@@ -141,13 +141,11 @@ export class TypeResolver {
141
141
let additionalType : Tsoa . Type | undefined ;
142
142
143
143
if ( indexMember ) {
144
+ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
144
145
const indexSignatureDeclaration = indexMember as ts . IndexSignatureDeclaration ;
145
146
const indexType = new TypeResolver ( indexSignatureDeclaration . parameters [ 0 ] . type as ts . TypeNode , this . current , this . parentNode , this . context ) . resolve ( ) ;
146
147
147
- throwUnless (
148
- indexType . dataType === 'string' ,
149
- new GenerateMetadataError ( `Only string indexers are supported.` , this . typeNode ) ,
150
- ) ;
148
+ throwUnless ( indexType . dataType === 'string' , new GenerateMetadataError ( `Only string indexers are supported.` , this . typeNode ) ) ;
151
149
152
150
additionalType = new TypeResolver ( indexSignatureDeclaration . type , this . current , this . parentNode , this . context ) . resolve ( ) ;
153
151
}
@@ -216,7 +214,7 @@ export class TypeResolver {
216
214
const parent = getOneOrigDeclaration ( property ) ; //If there are more declarations, we need to get one of them, from where we want to recognize jsDoc
217
215
const type = new TypeResolver ( typeNode , this . current , parent , this . context , propertyType ) . resolve ( ) ;
218
216
219
- const required = ! ( this . hasFlag ( property , ts . SymbolFlags . Optional ) ) ;
217
+ const required = ! this . hasFlag ( property , ts . SymbolFlags . Optional ) ;
220
218
221
219
const comments = property . getDocumentationComment ( this . current . typeChecker ) ;
222
220
const description = comments . length ? ts . displayPartsToString ( comments ) : undefined ;
@@ -319,22 +317,12 @@ export class TypeResolver {
319
317
return new TypeResolver ( this . typeNode . type , this . current , this . typeNode , this . context , this . referencer ) . resolve ( ) ;
320
318
}
321
319
322
- throwUnless (
323
- this . typeNode . kind === ts . SyntaxKind . TypeReference ,
324
- new GenerateMetadataError ( `Unknown type: ${ ts . SyntaxKind [ this . typeNode . kind ] } ` , this . typeNode ) ,
325
- ) ;
320
+ throwUnless ( this . typeNode . kind === ts . SyntaxKind . TypeReference , new GenerateMetadataError ( `Unknown type: ${ ts . SyntaxKind [ this . typeNode . kind ] } ` , this . typeNode ) ) ;
326
321
327
322
return this . resolveTypeReferenceNode ( this . typeNode as ts . TypeReferenceNode , this . current , this . context , this . parentNode ) ;
328
323
}
329
324
330
- private resolveTypeOperatorNode (
331
- typeNode : ts . TypeOperatorNode ,
332
- typeChecker : ts . TypeChecker ,
333
- current : MetadataGenerator ,
334
- context : Context ,
335
- parentNode ?: ts . Node ,
336
- referencer ?: ts . Type ,
337
- ) : Tsoa . Type {
325
+ private resolveTypeOperatorNode ( typeNode : ts . TypeOperatorNode , typeChecker : ts . TypeChecker , current : MetadataGenerator , context : Context , parentNode ?: ts . Node , referencer ?: ts . Type ) : Tsoa . Type {
338
326
switch ( typeNode . operator ) {
339
327
case ts . SyntaxKind . KeyOfKeyword : {
340
328
// keyof
@@ -344,10 +332,7 @@ export class TypeResolver {
344
332
const symbol = type . type . getSymbol ( ) ;
345
333
if ( symbol && symbol . getFlags ( ) & ts . TypeFlags . TypeParameter ) {
346
334
const typeName = symbol . getEscapedName ( ) ;
347
- throwUnless (
348
- typeof typeName === 'string' ,
349
- new GenerateMetadataError ( `typeName is not string, but ${ typeof typeName } ` , typeNode ) ,
350
- ) ;
335
+ throwUnless ( typeof typeName === 'string' , new GenerateMetadataError ( `typeName is not string, but ${ typeof typeName } ` , typeNode ) ) ;
351
336
352
337
if ( context [ typeName ] ) {
353
338
const subResult = new TypeResolver ( context [ typeName ] . type , current , parentNode , context ) . resolve ( ) ;
@@ -358,10 +343,7 @@ export class TypeResolver {
358
343
} ;
359
344
}
360
345
const properties = ( subResult as Tsoa . RefObjectType ) . properties ?. map ( v => v . name ) ;
361
- throwUnless (
362
- properties ,
363
- new GenerateMetadataError ( `TypeOperator 'keyof' on node which have no properties` , context [ typeName ] . type ) ,
364
- ) ;
346
+ throwUnless ( properties , new GenerateMetadataError ( `TypeOperator 'keyof' on node which have no properties` , context [ typeName ] . type ) ) ;
365
347
366
348
return {
367
349
dataType : 'enum' ,
@@ -459,22 +441,13 @@ export class TypeResolver {
459
441
const isNumberIndexType = indexType . kind === ts . SyntaxKind . NumberKeyword ;
460
442
const typeOfObjectType = typeChecker . getTypeFromTypeNode ( objectType ) ;
461
443
const type = isNumberIndexType ? typeOfObjectType . getNumberIndexType ( ) : typeOfObjectType . getStringIndexType ( ) ;
462
- throwUnless (
463
- type ,
464
- new GenerateMetadataError ( `Could not determine ${ isNumberIndexType ? 'number' : 'string' } index on ${ typeChecker . typeToString ( typeOfObjectType ) } ` , typeNode ) ,
465
- ) ;
444
+ throwUnless ( type , new GenerateMetadataError ( `Could not determine ${ isNumberIndexType ? 'number' : 'string' } index on ${ typeChecker . typeToString ( typeOfObjectType ) } ` , typeNode ) ) ;
466
445
return new TypeResolver ( typeChecker . typeToTypeNode ( type , objectType , ts . NodeBuilderFlags . NoTruncation ) ! , current , typeNode , context ) . resolve ( ) ;
467
446
} else if ( ts . isLiteralTypeNode ( indexType ) && ( ts . isStringLiteral ( indexType . literal ) || ts . isNumericLiteral ( indexType . literal ) ) ) {
468
447
// Indexed by literal
469
448
const hasType = ( node : ts . Node | undefined ) : node is ts . HasType => node !== undefined && Object . prototype . hasOwnProperty . call ( node , 'type' ) ;
470
449
const symbol = typeChecker . getPropertyOfType ( typeChecker . getTypeFromTypeNode ( objectType ) , indexType . literal . text ) ;
471
- throwUnless (
472
- symbol ,
473
- new GenerateMetadataError (
474
- `Could not determine the keys on ${ typeChecker . typeToString ( typeChecker . getTypeFromTypeNode ( objectType ) ) } ` ,
475
- typeNode ,
476
- ) ,
477
- ) ;
450
+ throwUnless ( symbol , new GenerateMetadataError ( `Could not determine the keys on ${ typeChecker . typeToString ( typeChecker . getTypeFromTypeNode ( objectType ) ) } ` , typeNode ) ) ;
478
451
if ( hasType ( symbol . valueDeclaration ) && symbol . valueDeclaration . type ) {
479
452
return new TypeResolver ( symbol . valueDeclaration . type , current , typeNode , context ) . resolve ( ) ;
480
453
}
@@ -483,9 +456,7 @@ export class TypeResolver {
483
456
return new TypeResolver ( typeChecker . typeToTypeNode ( declaration , objectType , ts . NodeBuilderFlags . NoTruncation ) ! , current , typeNode , context ) . resolve ( ) ;
484
457
} catch {
485
458
throw new GenerateMetadataError (
486
- `Could not determine the keys on ${ typeChecker . typeToString (
487
- typeChecker . getTypeFromTypeNode ( typeChecker . typeToTypeNode ( declaration , undefined , ts . NodeBuilderFlags . NoTruncation ) ! ) ,
488
- ) } `,
459
+ `Could not determine the keys on ${ typeChecker . typeToString ( typeChecker . getTypeFromTypeNode ( typeChecker . typeToTypeNode ( declaration , undefined , ts . NodeBuilderFlags . NoTruncation ) ! ) ) } ` ,
489
460
typeNode ,
490
461
) ;
491
462
}
@@ -504,27 +475,22 @@ export class TypeResolver {
504
475
throw new GenerateMetadataError ( `Unknown type: ${ ts . SyntaxKind [ typeNode . kind ] } ` , typeNode ) ;
505
476
}
506
477
507
- private resolveTypeReferenceNode (
508
- typeNode : ts . TypeReferenceNode ,
509
- current : MetadataGenerator ,
510
- context : Context ,
511
- parentNode ?: ts . Node ,
512
- ) : Tsoa . Type {
478
+ private resolveTypeReferenceNode ( typeNode : ts . TypeReferenceNode , current : MetadataGenerator , context : Context , parentNode ?: ts . Node ) : Tsoa . Type {
513
479
const { typeName, typeArguments } = typeNode ;
514
480
515
481
if ( typeName . kind !== ts . SyntaxKind . Identifier ) {
516
482
return this . getReferenceType ( typeNode ) ;
517
483
}
518
484
519
- switch ( typeName . text ) {
485
+ switch ( typeName . text ) {
520
486
case 'Date' :
521
487
return new DateTransformer ( this ) . transform ( parentNode ) ;
522
488
case 'Buffer' :
523
489
case 'Readable' :
524
490
return { dataType : 'buffer' } ;
525
491
case 'Array' :
526
492
if ( typeArguments && typeArguments . length === 1 ) {
527
- return {
493
+ return {
528
494
dataType : 'array' ,
529
495
elementType : new TypeResolver ( typeArguments [ 0 ] , current , parentNode , context ) . resolve ( ) ,
530
496
} ;
@@ -559,10 +525,7 @@ export class TypeResolver {
559
525
case ts . SyntaxKind . NullKeyword :
560
526
return null ;
561
527
default :
562
- throwUnless (
563
- Object . prototype . hasOwnProperty . call ( typeNode . literal , 'text' ) ,
564
- new GenerateMetadataError ( `Couldn't resolve literal node: ${ typeNode . literal . getText ( ) } ` ) ,
565
- ) ;
528
+ throwUnless ( Object . prototype . hasOwnProperty . call ( typeNode . literal , 'text' ) , new GenerateMetadataError ( `Couldn't resolve literal node: ${ typeNode . literal . getText ( ) } ` ) ) ;
566
529
return ( typeNode . literal as ts . LiteralExpression ) . text ;
567
530
}
568
531
}
@@ -578,15 +541,13 @@ export class TypeResolver {
578
541
return nodes ;
579
542
}
580
543
581
- throwUnless (
582
- designatedNodes . length === 1 ,
583
- new GenerateMetadataError ( `Multiple models for ${ typeName } marked with '@tsoaModel'; '@tsoaModel' should only be applied to one model.` ) ,
584
- ) ;
544
+ throwUnless ( designatedNodes . length === 1 , new GenerateMetadataError ( `Multiple models for ${ typeName } marked with '@tsoaModel'; '@tsoaModel' should only be applied to one model.` ) ) ;
585
545
586
546
return designatedNodes ;
587
547
}
588
548
589
549
private hasFlag ( type : ts . Type | ts . Symbol | ts . Declaration , flag : ts . TypeFlags | ts . NodeFlags | ts . SymbolFlags ) {
550
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
590
551
return ( type . flags & flag ) === flag ;
591
552
}
592
553
@@ -650,10 +611,7 @@ export class TypeResolver {
650
611
651
612
while ( ! ts . isSourceFile ( actNode ) ) {
652
613
if ( ! ( isFirst && ts . isEnumDeclaration ( actNode ) ) && ! ts . isModuleBlock ( actNode ) ) {
653
- throwUnless (
654
- ts . isModuleDeclaration ( actNode ) ,
655
- new GenerateMetadataError ( `This node kind is unknown: ${ actNode . kind } ` , type ) ,
656
- ) ;
614
+ throwUnless ( ts . isModuleDeclaration ( actNode ) , new GenerateMetadataError ( `This node kind is unknown: ${ actNode . kind } ` , type ) ) ;
657
615
658
616
if ( ! isGlobalDeclaration ( actNode ) ) {
659
617
const moduleName = actNode . name . text ;
@@ -729,30 +687,23 @@ export class TypeResolver {
729
687
return resolvedType ;
730
688
}
731
689
if ( ts . isTypeReferenceNode ( arg ) || ts . isExpressionWithTypeArguments ( arg ) ) {
732
- const [ _ , name ] = this . calcTypeReferenceTypeName ( arg ) ;
733
- return name ;
690
+ return this . calcTypeReferenceTypeName ( arg ) [ 1 ] ;
734
691
} else if ( ts . isTypeLiteralNode ( arg ) ) {
735
692
const members = arg . members . map ( member => {
736
693
if ( ts . isPropertySignature ( member ) ) {
737
694
const name = ( member . name as ts . Identifier ) . text ;
738
695
const typeText = this . calcTypeName ( member . type as ts . TypeNode ) ;
739
696
return `"${ name } "${ member . questionToken ? '?' : '' } ${ this . calcMemberJsDocProperties ( member ) } : ${ typeText } ` ;
740
697
} else if ( ts . isIndexSignatureDeclaration ( member ) ) {
741
- throwUnless (
742
- member . parameters . length === 1 ,
743
- new GenerateMetadataError ( `Index signature parameters length != 1` , member ) ,
744
- ) ;
698
+ throwUnless ( member . parameters . length === 1 , new GenerateMetadataError ( `Index signature parameters length != 1` , member ) ) ;
745
699
746
700
const indexType = member . parameters [ 0 ] ;
747
701
throwUnless (
748
702
// now we can't reach this part of code
749
703
ts . isParameter ( indexType ) ,
750
704
new GenerateMetadataError ( `indexSignature declaration parameter kind is not SyntaxKind.Parameter` , indexType ) ,
751
705
) ;
752
- throwUnless (
753
- ! indexType . questionToken ,
754
- new GenerateMetadataError ( `Question token has found for an indexSignature declaration` , indexType ) ,
755
- ) ;
706
+ throwUnless ( ! indexType . questionToken , new GenerateMetadataError ( `Question token has found for an indexSignature declaration` , indexType ) ) ;
756
707
757
708
const typeText = this . calcTypeName ( member . type ) ;
758
709
const indexName = ( indexType . name as ts . Identifier ) . text ;
@@ -886,10 +837,7 @@ export class TypeResolver {
886
837
const deprecated = isExistJSDocTag ( modelType , tag => tag . tagName . text === 'deprecated' ) || isDecorator ( modelType , identifier => identifier . text === 'Deprecated' ) ;
887
838
888
839
// Handle toJSON methods
889
- throwUnless (
890
- modelType . name ,
891
- new GenerateMetadataError ( "Can't get Symbol from anonymous class" , modelType ) ,
892
- ) ;
840
+ throwUnless ( modelType . name , new GenerateMetadataError ( "Can't get Symbol from anonymous class" , modelType ) ) ;
893
841
894
842
const type = this . current . typeChecker . getTypeAtLocation ( modelType . name ) ;
895
843
const toJSON = this . current . typeChecker . getPropertyOfType ( type , 'toJSON' ) ;
@@ -995,23 +943,17 @@ export class TypeResolver {
995
943
}
996
944
const declarations = symbol ?. getDeclarations ( ) ;
997
945
998
- throwUnless (
999
- symbol && declarations ,
1000
- new GenerateMetadataError ( `No declarations found for referenced type ${ typeName } .` ) ,
1001
- ) ;
946
+ throwUnless ( symbol && declarations , new GenerateMetadataError ( `No declarations found for referenced type ${ typeName } .` ) ) ;
1002
947
1003
- if ( symbol . escapedName !== typeName && symbol . escapedName !== 'default' ) {
948
+ if ( ( symbol . escapedName as string ) !== typeName && ( symbol . escapedName as string ) !== 'default' ) {
1004
949
typeName = symbol . escapedName as string ;
1005
950
}
1006
951
1007
952
let modelTypes = declarations . filter ( ( node ) : node is UsableDeclarationWithoutPropertySignature => {
1008
953
return this . nodeIsUsable ( node ) && node . name ?. getText ( ) === typeName ;
1009
954
} ) ;
1010
955
1011
- throwUnless (
1012
- modelTypes . length ,
1013
- new GenerateMetadataError ( `No matching model found for referenced type ${ typeName } .` ) ,
1014
- ) ;
956
+ throwUnless ( modelTypes . length , new GenerateMetadataError ( `No matching model found for referenced type ${ typeName } .` ) ) ;
1015
957
1016
958
if ( modelTypes . length > 1 ) {
1017
959
// remove types that are from typescript e.g. 'Account'
@@ -1041,10 +983,7 @@ export class TypeResolver {
1041
983
1042
984
const indexSignatureDeclaration = indexMember as ts . IndexSignatureDeclaration ;
1043
985
const indexType = new TypeResolver ( indexSignatureDeclaration . parameters [ 0 ] . type as ts . TypeNode , this . current , this . parentNode , this . context ) . resolve ( ) ;
1044
- throwUnless (
1045
- indexType . dataType === 'string' ,
1046
- new GenerateMetadataError ( `Only string indexers are supported.` , this . typeNode ) ,
1047
- ) ;
986
+ throwUnless ( indexType . dataType === 'string' , new GenerateMetadataError ( `Only string indexers are supported.` , this . typeNode ) ) ;
1048
987
1049
988
return new TypeResolver ( indexSignatureDeclaration . type , this . current , this . parentNode , this . context ) . resolve ( ) ;
1050
989
}
0 commit comments