Skip to content

Commit 7c15378

Browse files
committed
InsertNode instead of replace + handling call expressions
1 parent 8b14fb2 commit 7c15378

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

src/services/codefixes/fixAddMissingNewOperator.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ namespace ts.codefix {
66
errorCodes,
77
getCodeActions(context) {
88
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));
1110
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_call, fixId, Diagnostics.Add_missing_new_operator_to_all_calls)];
1211
},
1312
fixIds: [fixId],
1413
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
15-
addMissingNewOperator(changes, context.sourceFile, getMissingNewExpression(diag.file, diag.start))),
14+
addMissingNewOperator(changes, context.sourceFile, diag)),
1615
});
1716

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+
}
2224
}
2325

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;
2733
}
2834
}

tests/cases/fourslash/codeFixAddMissingNew.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
verify.codeFix({
88
description: "Add missing 'new' operator to call",
99
index: 0,
10-
newFileContent: `class C {
10+
newFileContent:
11+
`class C {
1112
}
1213
var c = new C();`
1314
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C {
4+
////}
5+
////let x = (() => C)()();
6+
7+
verify.codeFix({
8+
description: "Add missing 'new' operator to call",
9+
index: 0,
10+
newFileContent:
11+
`class C {
12+
}
13+
let x = new ((() => C)())();`
14+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C {
4+
////}
5+
////let x = [C];
6+
////let a = x[0]();
7+
8+
verify.codeFix({
9+
description: "Add missing 'new' operator to call",
10+
index: 0,
11+
newFileContent:
12+
`class C {
13+
}
14+
let x = [C];
15+
let a = new x[0]();`
16+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C {
4+
////}
5+
////class D {
6+
////}
7+
////let x = (true ? C : D)();
8+
9+
verify.codeFix({
10+
description: "Add missing 'new' operator to call",
11+
index: 0,
12+
newFileContent:
13+
`class C {
14+
}
15+
class D {
16+
}
17+
let x = new (true ? C : D)();`
18+
});

0 commit comments

Comments
 (0)