@@ -9,59 +9,63 @@ namespace ts.codefix {
99 errorCodes,
1010 getCodeActions ( context ) {
1111 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 ) ) ;
1815 return [ { description : getLocaleSpecificMessage ( Diagnostics . Convert_to_async ) , changes, fixId } ] ;
1916 } ,
2017 fixIds : [ fixId ] ,
2118 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 ) ;
2922 } ) ,
3023 } ) ;
3124
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 ;
4433 }
4534 }
4635
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 ;
4840 switch ( containingFunction . kind ) {
4941 case SyntaxKind . MethodDeclaration :
50- return containingFunction . name ;
51- case SyntaxKind . FunctionExpression :
42+ insertBefore = containingFunction . name ;
43+ break ;
5244 case SyntaxKind . FunctionDeclaration :
53- return findChildOfKind ( containingFunction , SyntaxKind . FunctionKeyword , sourceFile ) ;
45+ case SyntaxKind . FunctionExpression :
46+ insertBefore = findChildOfKind ( containingFunction , SyntaxKind . FunctionKeyword , sourceFile ) ;
47+ break ;
5448 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 ;
5651 default :
57- return undefined ;
52+ return ;
5853 }
54+
55+ return {
56+ insertBefore,
57+ returnType : getReturnType ( containingFunction )
58+ } ;
5959 }
6060
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+
6266 if ( returnType ) {
6367 const entityName = getEntityNameFromTypeNode ( returnType ) ;
64- if ( ! entityName || entityName . getText ( ) !== "Promise" ) {
68+ if ( ! entityName || entityName . kind !== SyntaxKind . Identifier || entityName . text !== "Promise" ) {
6569 changes . replaceNode ( sourceFile , returnType , createTypeReferenceNode ( "Promise" , createNodeArray ( [ returnType ] ) ) ) ;
6670 }
6771 }
0 commit comments