@@ -31,11 +31,11 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
31
31
}
32
32
33
33
const node = getTokenAtPosition ( context . file , context . startPosition , /*includeJsDocComment*/ false ) ;
34
- const decl = findAncestor ( node , isTypedNode ) ;
34
+ const decl = findAncestor ( node , isDeclarationWithType ) ;
35
35
if ( decl && ! decl . type ) {
36
36
const type = getJSDocType ( decl ) ;
37
- const returnType = getJSDocReturnType ( decl ) ;
38
- const annotate = ( returnType || type && decl . kind === SyntaxKind . Parameter ) ? annotateFunctionFromJSDoc :
37
+ const isFunctionWithJSDoc = isFunctionLikeDeclaration ( decl ) && ( getJSDocReturnType ( decl ) || decl . parameters . some ( p => ! ! getJSDocType ( p ) ) ) ;
38
+ const annotate = ( isFunctionWithJSDoc || type && decl . kind === SyntaxKind . Parameter ) ? annotateFunctionFromJSDoc :
39
39
type ? annotateTypeFromJSDoc :
40
40
undefined ;
41
41
if ( annotate ) {
@@ -61,7 +61,7 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
61
61
62
62
const sourceFile = context . file ;
63
63
const token = getTokenAtPosition ( sourceFile , context . startPosition , /*includeJsDocComment*/ false ) ;
64
- const decl = findAncestor ( token , isTypedNode ) ;
64
+ const decl = findAncestor ( token , isDeclarationWithType ) ;
65
65
const jsdocType = getJSDocReturnType ( decl ) || getJSDocType ( decl ) ;
66
66
if ( ! decl || ! jsdocType || decl . type ) {
67
67
Debug . fail ( `!decl || !jsdocType || decl.type: !${ decl } || !${ jsdocType } || ${ decl . type } ` ) ;
@@ -95,7 +95,7 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
95
95
} ;
96
96
}
97
97
98
- function isTypedNode ( node : Node ) : node is DeclarationWithType {
98
+ function isDeclarationWithType ( node : Node ) : node is DeclarationWithType {
99
99
return isFunctionLikeDeclaration ( node ) ||
100
100
node . kind === SyntaxKind . VariableDeclaration ||
101
101
node . kind === SyntaxKind . Parameter ||
@@ -104,22 +104,23 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
104
104
}
105
105
106
106
function addTypesToFunctionLike ( decl : FunctionLikeDeclaration ) {
107
- const returnType = decl . type || transformJSDocType ( getJSDocReturnType ( decl ) ) as TypeNode ;
107
+ const typeParameters = getEffectiveTypeParameterDeclarations ( decl , /*checkJSDoc*/ true ) ;
108
108
const parameters = decl . parameters . map (
109
- p => createParameter ( p . decorators , p . modifiers , p . dotDotDotToken , p . name , p . questionToken , p . type || transformJSDocType ( getJSDocType ( p ) ) as TypeNode , p . initializer ) ) ;
109
+ p => createParameter ( p . decorators , p . modifiers , p . dotDotDotToken , p . name , p . questionToken , transformJSDocType ( getEffectiveTypeAnnotationNode ( p , /*checkJSDoc*/ true ) ) as TypeNode , p . initializer ) ) ;
110
+ const returnType = transformJSDocType ( getEffectiveReturnTypeNode ( decl , /*checkJSDoc*/ true ) ) as TypeNode ;
110
111
switch ( decl . kind ) {
111
112
case SyntaxKind . FunctionDeclaration :
112
- return createFunctionDeclaration ( decl . decorators , decl . modifiers , decl . asteriskToken , decl . name , decl . typeParameters , parameters , returnType , decl . body ) ;
113
+ return createFunctionDeclaration ( decl . decorators , decl . modifiers , decl . asteriskToken , decl . name , typeParameters , parameters , returnType , decl . body ) ;
113
114
case SyntaxKind . Constructor :
114
115
return createConstructor ( decl . decorators , decl . modifiers , parameters , decl . body ) ;
115
116
case SyntaxKind . FunctionExpression :
116
- return createFunctionExpression ( decl . modifiers , decl . asteriskToken , ( decl as FunctionExpression ) . name , decl . typeParameters , parameters , returnType , decl . body ) ;
117
+ return createFunctionExpression ( decl . modifiers , decl . asteriskToken , ( decl as FunctionExpression ) . name , typeParameters , parameters , returnType , decl . body ) ;
117
118
case SyntaxKind . ArrowFunction :
118
- return createArrowFunction ( decl . modifiers , decl . typeParameters , parameters , returnType , decl . equalsGreaterThanToken , decl . body ) ;
119
+ return createArrowFunction ( decl . modifiers , typeParameters , parameters , returnType , decl . equalsGreaterThanToken , decl . body ) ;
119
120
case SyntaxKind . MethodDeclaration :
120
- return createMethod ( decl . decorators , decl . modifiers , decl . asteriskToken , decl . name , decl . questionToken , decl . typeParameters , parameters , returnType , decl . body ) ;
121
+ return createMethod ( decl . decorators , decl . modifiers , decl . asteriskToken , decl . name , decl . questionToken , typeParameters , parameters , returnType , decl . body ) ;
121
122
case SyntaxKind . GetAccessor :
122
- return createGetAccessor ( decl . decorators , decl . modifiers , decl . name , parameters , returnType , decl . body ) ;
123
+ return createGetAccessor ( decl . decorators , decl . modifiers , decl . name , decl . parameters , returnType , decl . body ) ;
123
124
case SyntaxKind . SetAccessor :
124
125
return createSetAccessor ( decl . decorators , decl . modifiers , decl . name , parameters , decl . body ) ;
125
126
default :
0 commit comments