-
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
Merged
DanielRosenwasser
merged 9 commits into
microsoft:master
from
iliashkolyar:codefix_add_missing_new_operator
Nov 15, 2018
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76c6ee6
Codefix: add quick fix for missing 'new' operator
iliashkolyar 8b14fb2
Initial review
iliashkolyar 7c15378
InsertNode instead of replace + handling call expressions
iliashkolyar 304c6e9
Merge branch 'master' into codefix_add_missing_new_operator
iliashkolyar 35665d1
Merge branch 'master' into codefix_add_missing_new_operator
iliashkolyar 58b262e
Merge branch 'master' into codefix_add_missing_new_operator
iliashkolyar 6bd9b76
Code review - remove 'isCallExpression' check
iliashkolyar 21feb20
Merge branch 'master' into codefix_add_missing_new_operator
iliashkolyar 047b76f
Merge branch 'master' into codefix_add_missing_new_operator
iliashkolyar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* @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 changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, span)); | ||
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, diag)), | ||
}); | ||
|
||
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, span: TextSpan): void { | ||
const call = cast(findAncestorMatchingSpan(sourceFile, span), isCallExpression); | ||
changes.insertNodeAt(sourceFile, span.start, createToken(SyntaxKind.NewKeyword), { suffix: " " }); | ||
if (isCallExpression(call.expression)) { | ||
changes.insertNodeAt(sourceFile, call.expression.getStart(sourceFile), createToken(SyntaxKind.OpenParenToken)); | ||
changes.insertNodeAt(sourceFile, call.expression.end, createToken(SyntaxKind.CloseParenToken)); | ||
} | ||
} | ||
|
||
function findAncestorMatchingSpan(sourceFile: SourceFile, span: TextSpan): Node { | ||
let token = getTokenAtPosition(sourceFile, span.start); | ||
const end = textSpanEnd(span); | ||
while (token.end < end) { | ||
token = token.parent; | ||
} | ||
return token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <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();` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class C { | ||
////} | ||
////let x = (() => C)()(); | ||
|
||
verify.codeFix({ | ||
description: "Add missing 'new' operator to call", | ||
index: 0, | ||
newFileContent: | ||
`class C { | ||
} | ||
let x = new ((() => C)())();` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class C { | ||
////} | ||
////let x = [C]; | ||
////let a = x[0](); | ||
|
||
verify.codeFix({ | ||
description: "Add missing 'new' operator to call", | ||
index: 0, | ||
newFileContent: | ||
`class C { | ||
} | ||
let x = [C]; | ||
let a = new x[0]();` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class C { | ||
////} | ||
////class D { | ||
////} | ||
////let x = (true ? C : D)(); | ||
|
||
verify.codeFix({ | ||
description: "Add missing 'new' operator to call", | ||
index: 0, | ||
newFileContent: | ||
`class C { | ||
} | ||
class D { | ||
} | ||
let x = new (true ? C : D)();` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);` | ||
}); |
22 changes: 22 additions & 0 deletions
22
tests/cases/fourslash/codeFixAddMissingNew_all_arguments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>();` | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I really feel this is masking a problem with the change tracker API. We shouldn't have to have special checks, the change tracker should do it automatically when replacing nodes, much like how our factories/printers automatically do this.
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.
The problem is that if we replace a node with another it triggers re-formatting of the whole body (and can mess with comments), which could be annoying to users who just expected the codefix to make one local change.
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.
Great example of where this won't work:
Here, you'll end up with
instead of
We already have a
parenthesizeForNew
function infactory.ts
: https://github.com/Microsoft/TypeScript/blob/7c875465b5565e9d92a046ec24939c115c8e34cb/src/compiler/factory.ts#L4171-L4184So the change tracker should be doing something similar here.
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.
Ah, I see, that's a good point. My worry is that this won't be the last time we do something like this though. Maybe for now the right fix is to expose
parenthesizeForNew
incompiler/utilities.ts
and use it here.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.
Thanks for the feedback!
I'm trying to implement your suggestion but I don't think I fully understand the internals needed for it to work.
According to @Andy-MS, we should not use
replaceNode
and stick with theinsertNodeAt
so we won't generate the source file again.Using the
insertNodeAt
doesn't really create anew
node, but rather adds anew
token to the start of the text span (without any change to the existing nodes or the text span).On the other-hand, you are suggesting to use
parenthesizeForNew
, which receives an expression and returns it after wrapping it in parentheses if needed.As I understand it, to work with the
parenthesizeForNew
API I'll need to create aNewExpression
(similarly to what occurs increateNew
), which brings us back to thereplaceNode
logic that we decided to avoid.The only solution that aligns with both requirements is having a similar logic to the
switch
case inparenthesizeForNew
in my code, that adds the parentheses (usingchanges.insertNodeAt
) in these cases only (replacing the currentisCallExpression
check).But again, i'm not sure that this is what you guys are targeting for.
What do you think?