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