1
- import type {
2
- Token ,
3
- InterfaceDeclaration ,
4
- ClassDeclaration ,
5
- PropertyDeclaration ,
6
- ParameterDeclaration ,
7
- ConstructorDeclaration ,
8
- TypeElement ,
9
- ClassElement ,
10
- PropertySignature ,
11
- } from 'typescript' ;
12
- import {
13
- NodeFlags ,
14
- NodeBuilderFlags ,
15
- SyntaxKind ,
16
- isInterfaceDeclaration ,
17
- isPropertyDeclaration ,
18
- isConstructorDeclaration ,
19
- isPropertySignature ,
20
- } from 'typescript' ;
1
+ import type { Token , InterfaceDeclaration , ClassDeclaration , PropertyDeclaration , ParameterDeclaration , ConstructorDeclaration , TypeElement , ClassElement , PropertySignature } from 'typescript' ;
2
+ import { NodeFlags , NodeBuilderFlags , SyntaxKind , isInterfaceDeclaration , isPropertyDeclaration , isConstructorDeclaration , isPropertySignature } from 'typescript' ;
21
3
import { Tsoa } from '@tsoa/runtime' ;
22
4
23
5
import { Transformer } from './transformer' ;
@@ -32,7 +14,7 @@ import { throwUnless } from '../../utils/flowUtils';
32
14
type OverrideToken = Token < SyntaxKind . QuestionToken > | Token < SyntaxKind . PlusToken > | Token < SyntaxKind . MinusToken > | undefined ;
33
15
34
16
export class PropertyTransformer extends Transformer {
35
- public transform ( node : InterfaceDeclaration | ClassDeclaration , overrideToken ?: OverrideToken ) : Tsoa . Property [ ] {
17
+ public transform ( resolver : TypeResolver , node : InterfaceDeclaration | ClassDeclaration , overrideToken ?: OverrideToken ) : Tsoa . Property [ ] {
36
18
const isIgnored = ( e : TypeElement | ClassElement ) => {
37
19
let ignore = isExistJSDocTag ( e , tag => tag . tagName . text === 'ignore' ) ;
38
20
ignore = ignore || ( e . flags & NodeFlags . ThisNodeHasError ) > 0 ;
@@ -43,7 +25,7 @@ export class PropertyTransformer extends Transformer {
43
25
if ( isInterfaceDeclaration ( node ) ) {
44
26
return node . members
45
27
. filter ( ( member ) : member is PropertySignature => ! isIgnored ( member ) && isPropertySignature ( member ) )
46
- . map ( ( member : PropertySignature ) => this . propertyFromSignature ( member , overrideToken ) ) ;
28
+ . map ( ( member : PropertySignature ) => this . propertyFromSignature ( resolver , member , overrideToken ) ) ;
47
29
}
48
30
49
31
const properties : Array < PropertyDeclaration | ParameterDeclaration > = [ ] ;
@@ -61,10 +43,10 @@ export class PropertyTransformer extends Transformer {
61
43
properties . push ( ...constructorProperties ) ;
62
44
}
63
45
64
- return properties . map ( property => this . propertyFromDeclaration ( property , overrideToken ) ) ;
46
+ return properties . map ( property => this . propertyFromDeclaration ( resolver , property , overrideToken ) ) ;
65
47
}
66
48
67
- private propertyFromSignature ( propertySignature : PropertySignature , overrideToken ?: OverrideToken ) : Tsoa . Property {
49
+ private propertyFromSignature ( resolver : TypeResolver , propertySignature : PropertySignature , overrideToken ?: OverrideToken ) : Tsoa . Property {
68
50
throwUnless ( propertySignature . type , new GenerateMetadataError ( `No valid type found for property declaration.` ) ) ;
69
51
70
52
let required = ! propertySignature . questionToken ;
@@ -78,54 +60,54 @@ export class PropertyTransformer extends Transformer {
78
60
79
61
const property : Tsoa . Property = {
80
62
default : def ,
81
- description : this . resolver . getNodeDescription ( propertySignature ) ,
82
- example : this . resolver . getNodeExample ( propertySignature ) ,
83
- format : this . resolver . getNodeFormat ( propertySignature ) ,
84
- name : this . resolver . getPropertyName ( propertySignature ) ,
63
+ description : resolver . getNodeDescription ( propertySignature ) ,
64
+ example : resolver . getNodeExample ( propertySignature ) ,
65
+ format : resolver . getNodeFormat ( propertySignature ) ,
66
+ name : resolver . getPropertyName ( propertySignature ) ,
85
67
required,
86
- type : new TypeResolver ( propertySignature . type , this . resolver . current , propertySignature . type . parent , this . resolver . context ) . resolve ( ) ,
68
+ type : new TypeResolver ( propertySignature . type , resolver . current , propertySignature . type . parent , resolver . context ) . resolve ( ) ,
87
69
validators : getPropertyValidators ( propertySignature ) || { } ,
88
70
deprecated : isExistJSDocTag ( propertySignature , tag => tag . tagName . text === 'deprecated' ) ,
89
- extensions : this . resolver . getNodeExtension ( propertySignature ) ,
71
+ extensions : resolver . getNodeExtension ( propertySignature ) ,
90
72
} ;
91
73
return property ;
92
74
}
93
75
94
- private propertyFromDeclaration ( propertyDeclaration : PropertyDeclaration | ParameterDeclaration , overrideToken ?: OverrideToken ) : Tsoa . Property {
76
+ private propertyFromDeclaration ( resolver : TypeResolver , propertyDeclaration : PropertyDeclaration | ParameterDeclaration , overrideToken ?: OverrideToken ) : Tsoa . Property {
95
77
let typeNode = propertyDeclaration . type ;
96
78
97
- const tsType = this . resolver . current . typeChecker . getTypeAtLocation ( propertyDeclaration ) ;
79
+ const tsType = resolver . current . typeChecker . getTypeAtLocation ( propertyDeclaration ) ;
98
80
99
81
if ( ! typeNode ) {
100
82
// Type is from initializer
101
- typeNode = this . resolver . current . typeChecker . typeToTypeNode ( tsType , undefined , NodeBuilderFlags . NoTruncation ) ! ;
83
+ typeNode = resolver . current . typeChecker . typeToTypeNode ( tsType , undefined , NodeBuilderFlags . NoTruncation ) ! ;
102
84
}
103
85
104
- const type = new TypeResolver ( typeNode , this . resolver . current , propertyDeclaration , this . resolver . context , tsType ) . resolve ( ) ;
86
+ const type = new TypeResolver ( typeNode , resolver . current , propertyDeclaration , resolver . context , tsType ) . resolve ( ) ;
105
87
106
88
let required = ! propertyDeclaration . questionToken && ! propertyDeclaration . initializer ;
107
89
if ( overrideToken && overrideToken . kind === SyntaxKind . MinusToken ) {
108
90
required = true ;
109
91
} else if ( overrideToken && overrideToken . kind === SyntaxKind . QuestionToken ) {
110
92
required = false ;
111
93
}
112
- let def = getInitializerValue ( propertyDeclaration . initializer , this . resolver . current . typeChecker ) ;
94
+ let def = getInitializerValue ( propertyDeclaration . initializer , resolver . current . typeChecker ) ;
113
95
if ( def === undefined ) {
114
96
def = TypeResolver . getDefault ( propertyDeclaration ) ;
115
97
}
116
98
117
99
const property : Tsoa . Property = {
118
100
default : def ,
119
- description : this . resolver . getNodeDescription ( propertyDeclaration ) ,
120
- example : this . resolver . getNodeExample ( propertyDeclaration ) ,
121
- format : this . resolver . getNodeFormat ( propertyDeclaration ) ,
122
- name : this . resolver . getPropertyName ( propertyDeclaration ) ,
101
+ description : resolver . getNodeDescription ( propertyDeclaration ) ,
102
+ example : resolver . getNodeExample ( propertyDeclaration ) ,
103
+ format : resolver . getNodeFormat ( propertyDeclaration ) ,
104
+ name : resolver . getPropertyName ( propertyDeclaration ) ,
123
105
required,
124
106
type,
125
107
validators : getPropertyValidators ( propertyDeclaration ) || { } ,
126
108
// class properties and constructor parameters may be deprecated either via jsdoc annotation or decorator
127
109
deprecated : isExistJSDocTag ( propertyDeclaration , tag => tag . tagName . text === 'deprecated' ) || isDecorator ( propertyDeclaration , identifier => identifier . text === 'Deprecated' ) ,
128
- extensions : this . resolver . getNodeExtension ( propertyDeclaration ) ,
110
+ extensions : resolver . getNodeExtension ( propertyDeclaration ) ,
129
111
} ;
130
112
return property ;
131
113
}
0 commit comments