Initial review

This commit is contained in:
iliashkolyar 2018-09-11 13:56:51 +03:00
parent 76c6ee6e53
commit 8b14fb22ec
5 changed files with 35 additions and 14 deletions

View File

@ -4624,11 +4624,11 @@
"category": "Message",
"code": 95066
},
"Add missing 'new' operator to caller": {
"Add missing 'new' operator to call": {
"category": "Message",
"code": 95067
},
"Add missing 'new' operator to all callers": {
"Add missing 'new' operator to all calls": {
"category": "Message",
"code": 95068
}

View File

@ -6,24 +6,23 @@ namespace ts.codefix {
errorCodes,
getCodeActions(context) {
const { sourceFile, span } = context;
const identifierWithoutNew = getIdentifier(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, identifierWithoutNew));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_caller, fixId, Diagnostics.Add_missing_new_operator_to_all_callers)];
const missingNewExpression = getMissingNewExpression(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, missingNewExpression));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_call, fixId, Diagnostics.Add_missing_new_operator_to_all_calls)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
addMissingNewOperator(changes, context.sourceFile, getIdentifier(diag.file, diag.start))),
addMissingNewOperator(changes, context.sourceFile, getMissingNewExpression(diag.file, diag.start))),
});
function getIdentifier(sourceFile: SourceFile, pos: number): Identifier {
function getMissingNewExpression(sourceFile: SourceFile, pos: number): Expression {
const token = getTokenAtPosition(sourceFile, pos);
Debug.assert(token.kind === SyntaxKind.Identifier);
Debug.assert(isCallExpression(token.parent));
return <Identifier>token;
return <Expression>token;
}
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, identifierWithoutNew: Identifier): void {
const newTypeNode = createNew(identifierWithoutNew, /*typeArguments*/ undefined, /*argumentsArray*/ undefined);
changes.replaceNode(sourceFile, identifierWithoutNew, newTypeNode);
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, missingNewExpression: Expression): void {
const newTypeNode = createNew(missingNewExpression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined);
changes.replaceNode(sourceFile, missingNewExpression, newTypeNode);
}
}

View File

@ -5,7 +5,7 @@
////var c = C();
verify.codeFix({
description: "Add missing 'new' operator to caller",
description: "Add missing 'new' operator to call",
index: 0,
newFileContent: `class C {
}

View File

@ -8,7 +8,7 @@
verify.codeFixAll({
fixId: "addMissingNewOperator",
fixAllDescription: "Add missing 'new' operator to all callers",
fixAllDescription: "Add missing 'new' operator to all calls",
newFileContent:
`class C {
constructor(num?: number) {}

View File

@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />
////class C<T = number> {
//// x?: T;
//// constructor(x: T) { this.x = x; }
////}
////let a = C(1, 2, 3);
////let b = C<string>("hello");
////let c = C<boolean>();
verify.codeFixAll({
fixId: "addMissingNewOperator",
fixAllDescription: "Add missing 'new' operator to all calls",
newFileContent:
`class C<T = number> {
x?: T;
constructor(x: T) { this.x = x; }
}
let a = new C(1, 2, 3);
let b = new C<string>("hello");
let c = new C<boolean>();`
});