@@ -9,44 +9,33 @@ namespace ts.codefix {
9
9
errorCodes,
10
10
getCodeActions ( context ) {
11
11
const { sourceFile, span } = context ;
12
- const node = getNode ( sourceFile , span . start ) ;
12
+ const node = getNodeToInsertBefore ( sourceFile , span . start ) ;
13
13
if ( ! node ) return undefined ;
14
14
const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , node ) ) ;
15
15
return [ { description : getLocaleSpecificMessage ( Diagnostics . Convert_to_async ) , changes, fixId } ] ;
16
16
} ,
17
17
fixIds : [ fixId ] ,
18
18
getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) =>
19
- doChange ( changes , context . sourceFile , getNode ( diag . file , diag . start ! ) ) ) ,
19
+ doChange ( changes , context . sourceFile , getNodeToInsertBefore ( diag . file , diag . start ! ) ) ) ,
20
20
} ) ;
21
21
22
- function getNode ( sourceFile : SourceFile , pos : number ) : FunctionLikeDeclaration {
22
+ function getNodeToInsertBefore ( sourceFile : SourceFile , pos : number ) : Node | undefined { //name
23
23
const token = getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) ;
24
24
const containingFunction = getContainingFunction ( token ) ;
25
- if ( ! isFunctionLikeDeclaration ( containingFunction ) ||
26
- isConstructorDeclaration ( containingFunction ) ||
27
- isGetAccessorDeclaration ( containingFunction ) ||
28
- isSetAccessorDeclaration ( containingFunction ) ) return ;
29
- return containingFunction ;
30
- }
31
-
32
- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , decl : FunctionLikeDeclaration ) {
33
- const asyncToken = createToken ( SyntaxKind . AsyncKeyword ) ;
34
- const modifiers = decl . modifiers ? decl . modifiers . concat ( asyncToken ) : createNodeArray ( [ asyncToken ] ) ;
35
- let changed ;
36
- switch ( decl . kind ) {
25
+ switch ( containingFunction . kind ) {
37
26
case SyntaxKind . MethodDeclaration :
38
- changed = createMethod ( decl . decorators , modifiers , decl . asteriskToken , decl . name , decl . questionToken , decl . typeParameters , decl . parameters , decl . type , decl . body ) ;
39
- break ;
27
+ return containingFunction . name ;
40
28
case SyntaxKind . FunctionExpression :
41
- changed = createFunctionExpression ( modifiers , decl . asteriskToken , decl . name , decl . typeParameters , decl . parameters , decl . type , decl . body ) ;
42
- break ;
43
29
case SyntaxKind . FunctionDeclaration :
44
- changed = createFunctionDeclaration ( decl . decorators , modifiers , decl . asteriskToken , decl . name , decl . typeParameters , decl . parameters , decl . type , decl . body ) ;
45
- break ;
30
+ return findChildOfKind ( containingFunction , SyntaxKind . FunctionKeyword , sourceFile ) ;
46
31
case SyntaxKind . ArrowFunction :
47
- changed = createArrowFunction ( modifiers , decl . typeParameters , decl . parameters , decl . type , decl . equalsGreaterThanToken , decl . body ) ;
48
- break ;
32
+ return findChildOfKind ( containingFunction , SyntaxKind . OpenParenToken , sourceFile ) || first ( containingFunction . parameters ) ;
33
+ default :
34
+ return undefined ;
49
35
}
50
- changes . replaceNode ( sourceFile , decl , changed ) ;
36
+ }
37
+
38
+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , insertBefore : Node ) : void {
39
+ changes . insertModifierBefore ( sourceFile , SyntaxKind . AsyncKeyword , insertBefore ) ;
51
40
}
52
41
}
0 commit comments