@@ -73,7 +73,7 @@ class Annotator {
73
73
this . indent ++ ;
74
74
switch ( node . kind ) {
75
75
case ts . SyntaxKind . VariableDeclaration :
76
- this . maybeVisitType ( ( < ts . VariableDeclaration > node ) . type ) ;
76
+ this . maybeEmitJSDocType ( ( < ts . VariableDeclaration > node ) . type ) ;
77
77
this . writeNode ( node ) ;
78
78
break ;
79
79
case ts . SyntaxKind . ClassDeclaration : {
@@ -124,25 +124,33 @@ class Annotator {
124
124
case ts . SyntaxKind . FunctionDeclaration :
125
125
case ts . SyntaxKind . ArrowFunction :
126
126
let fnDecl = < ts . FunctionLikeDeclaration > node ;
127
- this . maybeVisitType ( fnDecl . type , '@return' ) ;
128
127
let writeOffset = fnDecl . getFullStart ( ) ;
128
+ writeOffset = this . writeRange ( writeOffset , fnDecl . getStart ( ) ) ;
129
+ // The first \n makes the output sometimes uglier than necessary,
130
+ // but it's needed to work around
131
+ // https://github.com/Microsoft/TypeScript/issues/6982
132
+ this . emit ( '\n/**\n' ) ;
129
133
// Parameters.
130
134
if ( fnDecl . parameters . length ) {
131
135
for ( let param of fnDecl . parameters ) {
132
- this . writeTextFromOffset ( writeOffset , param ) ;
133
- writeOffset = param . getEnd ( ) ;
134
- let optional = param . initializer != null || param . questionToken != null ;
135
- this . maybeVisitType ( param . type , null , optional ) ;
136
- this . visit ( param ) ;
136
+ if ( param . type ) {
137
+ let optional = param . initializer != null || param . questionToken != null ;
138
+ this . emit ( ' * @param {' ) ;
139
+ this . emitType ( param . type , optional ) ;
140
+ this . emit ( '} ' ) ;
141
+ this . writeNode ( param . name ) ;
142
+ this . emit ( '\n' ) ;
143
+ }
137
144
}
138
145
}
139
146
// Return type.
140
147
if ( fnDecl . type ) {
141
- this . writeTextFromOffset ( writeOffset , fnDecl . type ) ;
142
- this . visit ( fnDecl . type ) ;
143
- writeOffset = fnDecl . type . getEnd ( ) ;
148
+ this . emit ( ' * @return {' ) ;
149
+ this . emitType ( fnDecl . type ) ;
150
+ this . emit ( '}\n' ) ;
144
151
}
145
- // Body.
152
+ this . emit ( ' */\n' ) ;
153
+
146
154
this . writeTextFromOffset ( writeOffset , fnDecl . body ) ;
147
155
this . visit ( fnDecl . body ) ;
148
156
break ;
@@ -203,7 +211,7 @@ class Annotator {
203
211
if ( existingAnnotation ) {
204
212
existingAnnotation += '\n' ;
205
213
}
206
- this . maybeVisitType ( p . type , existingAnnotation + '@type' ) ;
214
+ this . maybeEmitJSDocType ( p . type , existingAnnotation + '@type' ) ;
207
215
this . emit ( '\nthis.' ) ;
208
216
this . emit ( p . name . getText ( ) ) ;
209
217
this . emit ( ';' ) ;
@@ -233,14 +241,22 @@ class Annotator {
233
241
return '' ;
234
242
}
235
243
236
- private maybeVisitType ( type : ts . TypeNode , jsDocTag ?: string , optional ?: boolean ) {
244
+ private maybeEmitJSDocType ( type : ts . TypeNode , jsDocTag ?: string ) {
237
245
if ( ! type && ! this . options . untyped ) return ;
238
246
this . emit ( ' /**' ) ;
239
247
if ( jsDocTag ) {
240
248
this . emit ( ' ' ) ;
241
249
this . emit ( jsDocTag ) ;
242
250
this . emit ( ' {' ) ;
243
251
}
252
+ this . emitType ( type ) ;
253
+ if ( jsDocTag ) {
254
+ this . emit ( '}' ) ;
255
+ }
256
+ this . emit ( ' */' ) ;
257
+ }
258
+
259
+ private emitType ( type : ts . TypeNode , optional ?: boolean ) {
244
260
if ( this . options . untyped ) {
245
261
this . emit ( ' ?' ) ;
246
262
} else {
@@ -249,10 +265,6 @@ class Annotator {
249
265
if ( optional ) {
250
266
this . emit ( '=' ) ;
251
267
}
252
- if ( jsDocTag ) {
253
- this . emit ( '}' ) ;
254
- }
255
- this . emit ( ' */' ) ;
256
268
}
257
269
258
270
private visitTypeAlias ( node : ts . TypeAliasDeclaration ) {
0 commit comments