-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codefix: add quick fix for missing 'new' operator #27019
Changes from 2 commits
76c6ee6
8b14fb2
7c15378
304c6e9
35665d1
58b262e
6bd9b76
21feb20
047b76f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "addMissingNewOperator"; | ||
const errorCodes = [Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new.code]; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const { sourceFile, span } = context; | ||
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, getMissingNewExpression(diag.file, diag.start))), | ||
}); | ||
|
||
function getMissingNewExpression(sourceFile: SourceFile, pos: number): Expression { | ||
const token = getTokenAtPosition(sourceFile, pos); | ||
Debug.assert(isCallExpression(token.parent)); | ||
return <Expression>token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The token isn't necessarily an expression (for example, opening parentheses). I think you should be returning the call expression itself. |
||
} | ||
|
||
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, missingNewExpression: Expression): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you return a call expression above, you now have a CallExpression to work with. From there, you can pass along the (optionaly present) |
||
const newTypeNode = createNew(missingNewExpression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined); | ||
changes.replaceNode(sourceFile, missingNewExpression, newTypeNode); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class C { | ||
////} | ||
////var c = C(); | ||
|
||
verify.codeFix({ | ||
description: "Add missing 'new' operator to call", | ||
index: 0, | ||
newFileContent: `class C { | ||
} | ||
var c = new C();` | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class C { | ||
//// constructor(num?: number) {} | ||
////} | ||
////var a = C(); | ||
////var b = C(3); | ||
|
||
verify.codeFixAll({ | ||
fixId: "addMissingNewOperator", | ||
fixAllDescription: "Add missing 'new' operator to all calls", | ||
newFileContent: | ||
`class C { | ||
constructor(num?: number) {} | ||
} | ||
var a = new C(); | ||
var b = new C(3);` | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>();` | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.assertNode