-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve error position/range for arrow functions with expression body #60799
Open
OliverJAsh
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
OliverJAsh:fix-57866
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
41 changes: 41 additions & 0 deletions
41
tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.errors.txt
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,41 @@ | ||
arrowFunctionReturnTypeErrorSpan.ts(2,7): error TS2451: Cannot redeclare block-scoped variable 'a'. | ||
arrowFunctionReturnTypeErrorSpan.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'. | ||
arrowFunctionReturnTypeErrorSpan.ts(6,7): error TS2451: Cannot redeclare block-scoped variable 'a'. | ||
arrowFunctionReturnTypeErrorSpan.ts(7,10): error TS2304: Cannot find name 'missing'. | ||
arrowFunctionReturnTypeErrorSpan.ts(11,15): error TS2322: Type 'string' is not assignable to type 'number'. | ||
arrowFunctionReturnTypeErrorSpan.ts(14,15): error TS2322: Type 'string' is not assignable to type 'number'. | ||
arrowFunctionReturnTypeErrorSpan.ts(16,25): error TS2304: Cannot find name 'missing'. | ||
|
||
|
||
==== arrowFunctionReturnTypeErrorSpan.ts (7 errors) ==== | ||
// block body | ||
const a = (): number => { | ||
~ | ||
!!! error TS2451: Cannot redeclare block-scoped variable 'a'. | ||
return "foo"; | ||
~~~~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'number'. | ||
}; | ||
|
||
const a = (): number => { | ||
~ | ||
!!! error TS2451: Cannot redeclare block-scoped variable 'a'. | ||
return missing; | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'missing'. | ||
}; | ||
|
||
// expression body | ||
const b = (): number => "foo"; | ||
~~~~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'number'. | ||
|
||
type F<T> = T; | ||
const c = (): F<number> => "foo"; | ||
~~~~~~~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'number'. | ||
|
||
const d = (): number => missing; | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'missing'. | ||
OliverJAsh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.js
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,33 @@ | ||
//// [tests/cases/compiler/arrowFunctionReturnTypeErrorSpan.ts] //// | ||
|
||
//// [arrowFunctionReturnTypeErrorSpan.ts] | ||
// block body | ||
const a = (): number => { | ||
return "foo"; | ||
}; | ||
|
||
const a = (): number => { | ||
return missing; | ||
}; | ||
|
||
// expression body | ||
const b = (): number => "foo"; | ||
|
||
type F<T> = T; | ||
const c = (): F<number> => "foo"; | ||
|
||
const d = (): number => missing; | ||
|
||
|
||
//// [arrowFunctionReturnTypeErrorSpan.js] | ||
// block body | ||
var a = function () { | ||
return "foo"; | ||
}; | ||
var a = function () { | ||
return missing; | ||
}; | ||
// expression body | ||
var b = function () { return "foo"; }; | ||
var c = function () { return "foo"; }; | ||
var d = function () { return missing; }; |
32 changes: 32 additions & 0 deletions
32
tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.symbols
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,32 @@ | ||
//// [tests/cases/compiler/arrowFunctionReturnTypeErrorSpan.ts] //// | ||
|
||
=== arrowFunctionReturnTypeErrorSpan.ts === | ||
// block body | ||
const a = (): number => { | ||
>a : Symbol(a, Decl(arrowFunctionReturnTypeErrorSpan.ts, 1, 5)) | ||
|
||
return "foo"; | ||
}; | ||
|
||
const a = (): number => { | ||
>a : Symbol(a, Decl(arrowFunctionReturnTypeErrorSpan.ts, 5, 5)) | ||
|
||
return missing; | ||
}; | ||
|
||
// expression body | ||
const b = (): number => "foo"; | ||
>b : Symbol(b, Decl(arrowFunctionReturnTypeErrorSpan.ts, 10, 5)) | ||
|
||
type F<T> = T; | ||
>F : Symbol(F, Decl(arrowFunctionReturnTypeErrorSpan.ts, 10, 30)) | ||
>T : Symbol(T, Decl(arrowFunctionReturnTypeErrorSpan.ts, 12, 7)) | ||
>T : Symbol(T, Decl(arrowFunctionReturnTypeErrorSpan.ts, 12, 7)) | ||
|
||
const c = (): F<number> => "foo"; | ||
>c : Symbol(c, Decl(arrowFunctionReturnTypeErrorSpan.ts, 13, 5)) | ||
>F : Symbol(F, Decl(arrowFunctionReturnTypeErrorSpan.ts, 10, 30)) | ||
|
||
const d = (): number => missing; | ||
>d : Symbol(d, Decl(arrowFunctionReturnTypeErrorSpan.ts, 15, 5)) | ||
|
57 changes: 57 additions & 0 deletions
57
tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.types
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,57 @@ | ||
//// [tests/cases/compiler/arrowFunctionReturnTypeErrorSpan.ts] //// | ||
|
||
=== arrowFunctionReturnTypeErrorSpan.ts === | ||
// block body | ||
const a = (): number => { | ||
>a : () => number | ||
> : ^^^^^^ | ||
>(): number => { return "foo";} : () => number | ||
> : ^^^^^^ | ||
|
||
return "foo"; | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
}; | ||
|
||
const a = (): number => { | ||
>a : () => number | ||
> : ^^^^^^ | ||
>(): number => { return missing;} : () => number | ||
> : ^^^^^^ | ||
|
||
return missing; | ||
>missing : any | ||
> : ^^^ | ||
|
||
}; | ||
|
||
// expression body | ||
const b = (): number => "foo"; | ||
>b : () => number | ||
> : ^^^^^^ | ||
>(): number => "foo" : () => number | ||
> : ^^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
type F<T> = T; | ||
>F : T | ||
> : ^ | ||
|
||
const c = (): F<number> => "foo"; | ||
>c : () => F<number> | ||
> : ^^^^^^ | ||
>(): F<number> => "foo" : () => F<number> | ||
> : ^^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
const d = (): number => missing; | ||
>d : () => number | ||
> : ^^^^^^ | ||
>(): number => missing : () => number | ||
> : ^^^^^^ | ||
>missing : any | ||
> : ^^^ | ||
|
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
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 |
---|---|---|
|
@@ -51,3 +51,7 @@ f( // comment 1 | |
// body is not a block | ||
f(_ => 1 + | ||
2); | ||
|
||
f((): number => { }); | ||
|
||
f((): number => ''); |
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 @@ | ||
// block body | ||
const a = (): number => { | ||
return "foo"; | ||
}; | ||
|
||
const a = (): number => { | ||
return missing; | ||
}; | ||
|
||
// expression body | ||
const b = (): number => "foo"; | ||
|
||
type F<T> = T; | ||
const c = (): F<number> => "foo"; | ||
|
||
const d = (): number => missing; |
Oops, something went wrong.
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.
Alternatively we could position the error on
=>
:If the span highlights the return type, it might suggest an issue with the return type rather than the returned value/expression.
=>
on the other hand would suggest it could be an issue with either the return type or the value. This would be synonymous to the behaviour with block body arrow functions where we highlight thereturn
keyboard.However, the error might be harder to see, because
=>
is only 2 characters. I would personally be fine with this.