@@ -73,7 +73,7 @@ class Annotator {
7373 this . indent ++ ;
7474 switch ( node . kind ) {
7575 case ts . SyntaxKind . VariableDeclaration :
76- this . maybeVisitType ( ( < ts . VariableDeclaration > node ) . type ) ;
76+ this . maybeEmitJSDocType ( ( < ts . VariableDeclaration > node ) . type ) ;
7777 this . writeNode ( node ) ;
7878 break ;
7979 case ts . SyntaxKind . ClassDeclaration : {
@@ -124,25 +124,33 @@ class Annotator {
124124 case ts . SyntaxKind . FunctionDeclaration :
125125 case ts . SyntaxKind . ArrowFunction :
126126 let fnDecl = < ts . FunctionLikeDeclaration > node ;
127- this . maybeVisitType ( fnDecl . type , '@return' ) ;
128127 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' ) ;
129133 // Parameters.
130134 if ( fnDecl . parameters . length ) {
131135 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+ }
137144 }
138145 }
139146 // Return type.
140147 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' ) ;
144151 }
145- // Body.
152+ this . emit ( ' */\n' ) ;
153+
146154 this . writeTextFromOffset ( writeOffset , fnDecl . body ) ;
147155 this . visit ( fnDecl . body ) ;
148156 break ;
@@ -203,7 +211,7 @@ class Annotator {
203211 if ( existingAnnotation ) {
204212 existingAnnotation += '\n' ;
205213 }
206- this . maybeVisitType ( p . type , existingAnnotation + '@type' ) ;
214+ this . maybeEmitJSDocType ( p . type , existingAnnotation + '@type' ) ;
207215 this . emit ( '\nthis.' ) ;
208216 this . emit ( p . name . getText ( ) ) ;
209217 this . emit ( ';' ) ;
@@ -233,14 +241,22 @@ class Annotator {
233241 return '' ;
234242 }
235243
236- private maybeVisitType ( type : ts . TypeNode , jsDocTag ?: string , optional ?: boolean ) {
244+ private maybeEmitJSDocType ( type : ts . TypeNode , jsDocTag ?: string ) {
237245 if ( ! type && ! this . options . untyped ) return ;
238246 this . emit ( ' /**' ) ;
239247 if ( jsDocTag ) {
240248 this . emit ( ' ' ) ;
241249 this . emit ( jsDocTag ) ;
242250 this . emit ( ' {' ) ;
243251 }
252+ this . emitType ( type ) ;
253+ if ( jsDocTag ) {
254+ this . emit ( '}' ) ;
255+ }
256+ this . emit ( ' */' ) ;
257+ }
258+
259+ private emitType ( type : ts . TypeNode , optional ?: boolean ) {
244260 if ( this . options . untyped ) {
245261 this . emit ( ' ?' ) ;
246262 } else {
@@ -249,10 +265,6 @@ class Annotator {
249265 if ( optional ) {
250266 this . emit ( '=' ) ;
251267 }
252- if ( jsDocTag ) {
253- this . emit ( '}' ) ;
254- }
255- this . emit ( ' */' ) ;
256268 }
257269
258270 private visitTypeAlias ( node : ts . TypeAliasDeclaration ) {
0 commit comments