@@ -9,19 +9,42 @@ namespace ts.codefix {
9
9
errorCodes,
10
10
getCodeActions ( context ) {
11
11
const { sourceFile, span } = context ;
12
- const node = getNodeToInsertBefore ( sourceFile , span . start ) ;
13
- if ( ! node ) return undefined ;
14
- const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , node ) ) ;
12
+ const token = getTokenAtPosition ( sourceFile , span . start , /*includeJsDocComment*/ false ) ;
13
+ const containingFunction = getContainingFunction ( token ) ;
14
+ const insertBefore = getNodeToInsertBefore ( sourceFile , containingFunction ) ;
15
+ const returnType = getReturnTypeNode ( containingFunction ) ;
16
+ if ( ! insertBefore ) return undefined ;
17
+ const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , insertBefore , returnType ) ) ;
15
18
return [ { description : getLocaleSpecificMessage ( Diagnostics . Convert_to_async ) , changes, fixId } ] ;
16
19
} ,
17
20
fixIds : [ fixId ] ,
18
- getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) =>
19
- doChange ( changes , context . sourceFile , getNodeToInsertBefore ( diag . file , diag . start ! ) ) ) ,
21
+ getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) => {
22
+ const token = getTokenAtPosition ( diag . file , diag . start ! , /*includeJsDocComment*/ false ) ;
23
+ const containingFunction = getContainingFunction ( token ) ;
24
+ const insertBefore = getNodeToInsertBefore ( diag . file , containingFunction ) ;
25
+ const returnType = getReturnTypeNode ( containingFunction ) ;
26
+ if ( insertBefore ) {
27
+ doChange ( changes , context . sourceFile , insertBefore , returnType ) ;
28
+ }
29
+ } ) ,
20
30
} ) ;
21
31
22
- function getNodeToInsertBefore ( sourceFile : SourceFile , pos : number ) : Node | undefined {
23
- const token = getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) ;
24
- const containingFunction = getContainingFunction ( token ) ;
32
+ function getReturnTypeNode ( containingFunction : FunctionLike ) : TypeNode | undefined {
33
+ switch ( containingFunction . kind ) {
34
+ case SyntaxKind . MethodDeclaration :
35
+ case SyntaxKind . FunctionDeclaration :
36
+ return containingFunction . type ;
37
+ case SyntaxKind . FunctionExpression :
38
+ case SyntaxKind . ArrowFunction :
39
+ if ( isVariableDeclaration ( containingFunction . parent ) &&
40
+ containingFunction . parent . type &&
41
+ isFunctionTypeNode ( containingFunction . parent . type ) ) {
42
+ return containingFunction . parent . type . type ;
43
+ }
44
+ }
45
+ }
46
+
47
+ function getNodeToInsertBefore ( sourceFile : SourceFile , containingFunction : FunctionLike ) : Node | undefined {
25
48
switch ( containingFunction . kind ) {
26
49
case SyntaxKind . MethodDeclaration :
27
50
return containingFunction . name ;
@@ -35,7 +58,13 @@ namespace ts.codefix {
35
58
}
36
59
}
37
60
38
- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , insertBefore : Node ) : void {
61
+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , insertBefore : Node , returnType : TypeNode | undefined ) : void {
62
+ if ( returnType ) {
63
+ const entityName = getEntityNameFromTypeNode ( returnType ) ;
64
+ if ( ! entityName || entityName . getText ( ) !== "Promise" ) {
65
+ changes . replaceNode ( sourceFile , returnType , createTypeReferenceNode ( "Promise" , createNodeArray ( [ returnType ] ) ) ) ;
66
+ }
67
+ }
39
68
changes . insertModifierBefore ( sourceFile , SyntaxKind . AsyncKeyword , insertBefore ) ;
40
69
}
41
70
}
0 commit comments