Skip to content

Commit fb8722b

Browse files
committed
Improve error position/range for arrow functions with expression body
Fixes #57866 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 1ff3a2a..2ea2c5e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2527,6 +2527,14 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa } } + if (node.parent !== undefined && node.parent.kind === SyntaxKind.ArrowFunction) { + const arrowFn = node.parent as ArrowFunction; + if (arrowFn.body === node && arrowFn.type !== undefined) { + const pos = skipTrivia(sourceFile.text, arrowFn.type.pos); + return getSpanOfTokenAtPosition(sourceFile, pos); + } + } + if (errorNode === undefined) { // If we don't have a better node, then just set the error on the first token of // construct. diff --git a/tests/baselines/reference/conditionalTypes1.errors.txt b/tests/baselines/reference/conditionalTypes1.errors.txt index aa2ae24..476345e 100644 --- a/tests/baselines/reference/conditionalTypes1.errors.txt +++ b/tests/baselines/reference/conditionalTypes1.errors.txt @@ -73,7 +73,7 @@ conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'Z Type 'string | number' is not assignable to type 'ZeroOf<T>'. Type 'string' is not assignable to type 'ZeroOf<T>'. conditionalTypes1.ts(263,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'. -conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'. +conditionalTypes1.ts(288,33): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'. Type 'number | boolean' is not assignable to type 'T94<U>'. Type 'number' is not assignable to type 'T94<U>'. Type 'boolean' is not assignable to type 'true'. @@ -465,7 +465,7 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t type T95<T> = T extends string ? boolean : number; const f44 = <U>(value: T94<U>): T95<U> => value; const f45 = <U>(value: T95<U>): T94<U> => value; // Error - ~~~~~ + ~~~ !!! error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'. !!! error TS2322: Type 'number | boolean' is not assignable to type 'T94<U>'. !!! error TS2322: Type 'number' is not assignable to type 'T94<U>'. diff --git a/tests/baselines/reference/mappedTypeConstraints2.errors.txt b/tests/baselines/reference/mappedTypeConstraints2.errors.txt index 0d3b324..098bbd3 100644 --- a/tests/baselines/reference/mappedTypeConstraints2.errors.txt +++ b/tests/baselines/reference/mappedTypeConstraints2.errors.txt @@ -6,7 +6,7 @@ mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]' mappedTypeConstraints2.ts(42,7): error TS2322: Type 'Mapped6<K>[keyof Mapped6<K>]' is not assignable to type '`_${string}`'. Type 'Mapped6<K>[string] | Mapped6<K>[number] | Mapped6<K>[symbol]' is not assignable to type '`_${string}`'. Type 'Mapped6<K>[string]' is not assignable to type '`_${string}`'. -mappedTypeConstraints2.ts(51,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'. +mappedTypeConstraints2.ts(51,52): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'. mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K>[`_${K}`]' is not assignable to type 'true'. Type 'ObjectWithUnderscoredKeys<K>[`_${string}`]' is not assignable to type 'true'. @@ -75,7 +75,7 @@ mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K }; const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T' - ~~~~~~~~~~~~~~ + ~ !!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'. !!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'. diff --git a/tests/baselines/reference/parserArrowFunctionExpression17.errors.txt b/tests/baselines/reference/parserArrowFunctionExpression17.errors.txt index 28091b1..73c2b637f0 100644 --- a/tests/baselines/reference/parserArrowFunctionExpression17.errors.txt +++ b/tests/baselines/reference/parserArrowFunctionExpression17.errors.txt @@ -1,12 +1,12 @@ fileJs.js(1,1): error TS2304: Cannot find name 'a'. fileJs.js(1,5): error TS2304: Cannot find name 'b'. fileJs.js(1,15): error TS2304: Cannot find name 'd'. +fileJs.js(1,15): error TS2304: Cannot find name 'e'. fileJs.js(1,15): error TS8010: Type annotations can only be used in TypeScript files. -fileJs.js(1,20): error TS2304: Cannot find name 'e'. fileTs.ts(1,1): error TS2304: Cannot find name 'a'. fileTs.ts(1,5): error TS2304: Cannot find name 'b'. fileTs.ts(1,15): error TS2304: Cannot find name 'd'. -fileTs.ts(1,20): error TS2304: Cannot find name 'e'. +fileTs.ts(1,15): error TS2304: Cannot find name 'e'. ==== fileJs.js (5 errors) ==== @@ -18,9 +18,9 @@ fileTs.ts(1,20): error TS2304: Cannot find name 'e'. ~ !!! error TS2304: Cannot find name 'd'. ~ -!!! error TS8010: Type annotations can only be used in TypeScript files. - ~ !!! error TS2304: Cannot find name 'e'. + ~ +!!! error TS8010: Type annotations can only be used in TypeScript files. ==== fileTs.ts (4 errors) ==== a ? b : (c) : d => e @@ -30,6 +30,6 @@ fileTs.ts(1,20): error TS2304: Cannot find name 'e'. !!! error TS2304: Cannot find name 'b'. ~ !!! error TS2304: Cannot find name 'd'. - ~ + ~ !!! error TS2304: Cannot find name 'e'. \ No newline at end of file
1 parent 1fc223a commit fb8722b

File tree

6 files changed

+105
-8
lines changed

6 files changed

+105
-8
lines changed

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:NixOS/nixpkgs/master";
4+
flake-utils.url = "github:numtide/flake-utils";
5+
};
6+
7+
outputs =
8+
{ self
9+
, nixpkgs
10+
, flake-utils
11+
,
12+
}:
13+
flake-utils.lib.eachDefaultSystem
14+
(system:
15+
let
16+
pkgs = import nixpkgs {
17+
inherit system;
18+
};
19+
in
20+
{
21+
devShells.default = pkgs.mkShell {
22+
buildInputs = [
23+
pkgs.nodejs
24+
pkgs.nodePackages.npm
25+
];
26+
};
27+
});
28+
}

src/compiler/utilities.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,14 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
25272527
}
25282528
}
25292529

2530+
if (node.parent !== undefined && node.parent.kind === SyntaxKind.ArrowFunction) {
2531+
const arrowFn = node.parent as ArrowFunction;
2532+
if (arrowFn.body === node && arrowFn.type !== undefined) {
2533+
const pos = skipTrivia(sourceFile.text, arrowFn.type.pos);
2534+
return getSpanOfTokenAtPosition(sourceFile, pos);
2535+
}
2536+
}
2537+
25302538
if (errorNode === undefined) {
25312539
// If we don't have a better node, then just set the error on the first token of
25322540
// construct.

tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
arrowFunctionReturnTypeErrorSpan.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'.
2-
arrowFunctionReturnTypeErrorSpan.ts(7,25): error TS2322: Type 'string' is not assignable to type 'number'.
3-
arrowFunctionReturnTypeErrorSpan.ts(10,28): error TS2322: Type 'string' is not assignable to type 'number'.
42
arrowFunctionReturnTypeErrorSpan.ts(12,25): error TS2304: Cannot find name 'missing'.
3+
arrowFunctionReturnTypeErrorSpan.ts(7,15): error TS2322: Type 'string' is not assignable to type 'number'.
4+
arrowFunctionReturnTypeErrorSpan.ts(10,15): error TS2322: Type 'string' is not assignable to type 'number'.
55

66

77
==== arrowFunctionReturnTypeErrorSpan.ts (4 errors) ====
@@ -14,12 +14,12 @@ arrowFunctionReturnTypeErrorSpan.ts(12,25): error TS2304: Cannot find name 'miss
1414

1515
// expression body
1616
const b = (): number => "foo";
17-
~~~~~
17+
~~~~~~
1818
!!! error TS2322: Type 'string' is not assignable to type 'number'.
1919

2020
type F<T> = T;
2121
const c = (): F<number> => "foo";
22-
~~~~~
22+
~
2323
!!! error TS2322: Type 'string' is not assignable to type 'number'.
2424

2525
const d = (): number => missing;

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'Z
7373
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
7474
Type 'string' is not assignable to type 'ZeroOf<T>'.
7575
conditionalTypes1.ts(263,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
76-
conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
76+
conditionalTypes1.ts(288,33): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
7777
Type 'number | boolean' is not assignable to type 'T94<U>'.
7878
Type 'number' is not assignable to type 'T94<U>'.
7979
Type 'boolean' is not assignable to type 'true'.
@@ -465,7 +465,7 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
465465
type T95<T> = T extends string ? boolean : number;
466466
const f44 = <U>(value: T94<U>): T95<U> => value;
467467
const f45 = <U>(value: T95<U>): T94<U> => value; // Error
468-
~~~~~
468+
~~~
469469
!!! error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
470470
!!! error TS2322: Type 'number | boolean' is not assignable to type 'T94<U>'.
471471
!!! error TS2322: Type 'number' is not assignable to type 'T94<U>'.

tests/baselines/reference/mappedTypeConstraints2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]'
66
mappedTypeConstraints2.ts(42,7): error TS2322: Type 'Mapped6<K>[keyof Mapped6<K>]' is not assignable to type '`_${string}`'.
77
Type 'Mapped6<K>[string] | Mapped6<K>[number] | Mapped6<K>[symbol]' is not assignable to type '`_${string}`'.
88
Type 'Mapped6<K>[string]' is not assignable to type '`_${string}`'.
9-
mappedTypeConstraints2.ts(51,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
9+
mappedTypeConstraints2.ts(51,52): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
1010
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
1111
mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K>[`_${K}`]' is not assignable to type 'true'.
1212
Type 'ObjectWithUnderscoredKeys<K>[`_${string}`]' is not assignable to type 'true'.
@@ -75,7 +75,7 @@ mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K
7575
};
7676

7777
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
78-
~~~~~~~~~~~~~~
78+
~
7979
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
8080
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
8181

0 commit comments

Comments
 (0)