@@ -6,23 +6,29 @@ namespace ts.codefix {
6
6
errorCodes,
7
7
getCodeActions ( context ) {
8
8
const { sourceFile, span } = context ;
9
- const missingNewExpression = getMissingNewExpression ( sourceFile , span . start ) ;
10
- const changes = textChanges . ChangeTracker . with ( context , t => addMissingNewOperator ( t , sourceFile , missingNewExpression ) ) ;
9
+ const changes = textChanges . ChangeTracker . with ( context , t => addMissingNewOperator ( t , sourceFile , span ) ) ;
11
10
return [ createCodeFixAction ( fixId , changes , Diagnostics . Add_missing_new_operator_to_call , fixId , Diagnostics . Add_missing_new_operator_to_all_calls ) ] ;
12
11
} ,
13
12
fixIds : [ fixId ] ,
14
13
getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) =>
15
- addMissingNewOperator ( changes , context . sourceFile , getMissingNewExpression ( diag . file , diag . start ) ) ) ,
14
+ addMissingNewOperator ( changes , context . sourceFile , diag ) ) ,
16
15
} ) ;
17
16
18
- function getMissingNewExpression ( sourceFile : SourceFile , pos : number ) : Expression {
19
- const token = getTokenAtPosition ( sourceFile , pos ) ;
20
- Debug . assert ( isCallExpression ( token . parent ) ) ;
21
- return < Expression > token ;
17
+ function addMissingNewOperator ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , span : TextSpan ) : void {
18
+ const call = cast ( findAncestorMatchingSpan ( sourceFile , span ) , isCallExpression ) ;
19
+ changes . insertNodeAt ( sourceFile , span . start , createToken ( SyntaxKind . NewKeyword ) , { suffix : " " } ) ;
20
+ if ( isCallExpression ( call . expression ) ) {
21
+ changes . insertNodeAt ( sourceFile , call . expression . getStart ( sourceFile ) , createToken ( SyntaxKind . OpenParenToken ) ) ;
22
+ changes . insertNodeAt ( sourceFile , call . expression . end , createToken ( SyntaxKind . CloseParenToken ) ) ;
23
+ }
22
24
}
23
25
24
- function addMissingNewOperator ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , missingNewExpression : Expression ) : void {
25
- const newTypeNode = createNew ( missingNewExpression , /*typeArguments*/ undefined , /*argumentsArray*/ undefined ) ;
26
- changes . replaceNode ( sourceFile , missingNewExpression , newTypeNode ) ;
26
+ function findAncestorMatchingSpan ( sourceFile : SourceFile , span : TextSpan ) : Node {
27
+ let token = getTokenAtPosition ( sourceFile , span . start ) ;
28
+ const end = textSpanEnd ( span ) ;
29
+ while ( token . end < end ) {
30
+ token = token . parent ;
31
+ }
32
+ return token ;
27
33
}
28
34
}
0 commit comments