Skip to content

Commit 8b14fb2

Browse files
committed
Initial review
1 parent 76c6ee6 commit 8b14fb2

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4624,11 +4624,11 @@
46244624
"category": "Message",
46254625
"code": 95066
46264626
},
4627-
"Add missing 'new' operator to caller": {
4627+
"Add missing 'new' operator to call": {
46284628
"category": "Message",
46294629
"code": 95067
46304630
},
4631-
"Add missing 'new' operator to all callers": {
4631+
"Add missing 'new' operator to all calls": {
46324632
"category": "Message",
46334633
"code": 95068
46344634
}

src/services/codefixes/fixAddMissingNewOperator.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@ namespace ts.codefix {
66
errorCodes,
77
getCodeActions(context) {
88
const { sourceFile, span } = context;
9-
const identifierWithoutNew = getIdentifier(sourceFile, span.start);
10-
const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, identifierWithoutNew));
11-
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_caller, fixId, Diagnostics.Add_missing_new_operator_to_all_callers)];
9+
const missingNewExpression = getMissingNewExpression(sourceFile, span.start);
10+
const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, missingNewExpression));
11+
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_call, fixId, Diagnostics.Add_missing_new_operator_to_all_calls)];
1212
},
1313
fixIds: [fixId],
1414
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
15-
addMissingNewOperator(changes, context.sourceFile, getIdentifier(diag.file, diag.start))),
15+
addMissingNewOperator(changes, context.sourceFile, getMissingNewExpression(diag.file, diag.start))),
1616
});
1717

18-
function getIdentifier(sourceFile: SourceFile, pos: number): Identifier {
18+
function getMissingNewExpression(sourceFile: SourceFile, pos: number): Expression {
1919
const token = getTokenAtPosition(sourceFile, pos);
20-
Debug.assert(token.kind === SyntaxKind.Identifier);
2120
Debug.assert(isCallExpression(token.parent));
22-
return <Identifier>token;
21+
return <Expression>token;
2322
}
2423

25-
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, identifierWithoutNew: Identifier): void {
26-
const newTypeNode = createNew(identifierWithoutNew, /*typeArguments*/ undefined, /*argumentsArray*/ undefined);
27-
changes.replaceNode(sourceFile, identifierWithoutNew, newTypeNode);
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);
2827
}
2928
}

tests/cases/fourslash/codeFixAddMissingNew.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
////var c = C();
66

77
verify.codeFix({
8-
description: "Add missing 'new' operator to caller",
8+
description: "Add missing 'new' operator to call",
99
index: 0,
1010
newFileContent: `class C {
1111
}

tests/cases/fourslash/codeFixAddMissingNew_all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
verify.codeFixAll({
1010
fixId: "addMissingNewOperator",
11-
fixAllDescription: "Add missing 'new' operator to all callers",
11+
fixAllDescription: "Add missing 'new' operator to all calls",
1212
newFileContent:
1313
`class C {
1414
constructor(num?: number) {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C<T = number> {
4+
//// x?: T;
5+
//// constructor(x: T) { this.x = x; }
6+
////}
7+
////let a = C(1, 2, 3);
8+
////let b = C<string>("hello");
9+
////let c = C<boolean>();
10+
11+
verify.codeFixAll({
12+
fixId: "addMissingNewOperator",
13+
fixAllDescription: "Add missing 'new' operator to all calls",
14+
newFileContent:
15+
`class C<T = number> {
16+
x?: T;
17+
constructor(x: T) { this.x = x; }
18+
}
19+
let a = new C(1, 2, 3);
20+
let b = new C<string>("hello");
21+
let c = new C<boolean>();`
22+
});

0 commit comments

Comments
 (0)