Skip to content

Commit 76c6ee6

Browse files
committed
Codefix: add quick fix for missing 'new' operator
1 parent e9c6d96 commit 76c6ee6

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4608,7 +4608,6 @@
46084608
"category": "Message",
46094609
"code": 95062
46104610
},
4611-
46124611
"Add missing enum member '{0}'": {
46134612
"category": "Message",
46144613
"code": 95063
@@ -4619,10 +4618,18 @@
46194618
},
46204619
"Convert to async function":{
46214620
"category": "Message",
4622-
"code": 95065
4621+
"code": 95065
46234622
},
46244623
"Convert all to async functions": {
4625-
"category": "Message",
4626-
"code": 95066
4624+
"category": "Message",
4625+
"code": 95066
4626+
},
4627+
"Add missing 'new' operator to caller": {
4628+
"category": "Message",
4629+
"code": 95067
4630+
},
4631+
"Add missing 'new' operator to all callers": {
4632+
"category": "Message",
4633+
"code": 95068
46274634
}
46284635
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "addMissingNewOperator";
4+
const errorCodes = [Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new.code];
5+
registerCodeFix({
6+
errorCodes,
7+
getCodeActions(context) {
8+
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)];
12+
},
13+
fixIds: [fixId],
14+
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
15+
addMissingNewOperator(changes, context.sourceFile, getIdentifier(diag.file, diag.start))),
16+
});
17+
18+
function getIdentifier(sourceFile: SourceFile, pos: number): Identifier {
19+
const token = getTokenAtPosition(sourceFile, pos);
20+
Debug.assert(token.kind === SyntaxKind.Identifier);
21+
Debug.assert(isCallExpression(token.parent));
22+
return <Identifier>token;
23+
}
24+
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);
28+
}
29+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"codefixes/importFixes.ts",
5454
"codefixes/fixSpelling.ts",
5555
"codefixes/fixAddMissingMember.ts",
56+
"codefixes/fixAddMissingNewOperator.ts",
5657
"codefixes/fixCannotFindModule.ts",
5758
"codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
5859
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class C {
4+
////}
5+
////var c = C();
6+
7+
verify.codeFix({
8+
description: "Add missing 'new' operator to caller",
9+
index: 0,
10+
newFileContent: `class C {
11+
}
12+
var c = new C();`
13+
});
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+
//// constructor(num?: number) {}
5+
////}
6+
////var a = C();
7+
////var b = C(3);
8+
9+
verify.codeFixAll({
10+
fixId: "addMissingNewOperator",
11+
fixAllDescription: "Add missing 'new' operator to all callers",
12+
newFileContent:
13+
`class C {
14+
constructor(num?: number) {}
15+
}
16+
var a = new C();
17+
var b = new C(3);`
18+
});

0 commit comments

Comments
 (0)