Commit 796450a
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 file1 parent 1fc223a commit 796450a
File tree
4 files changed
+16
-8
lines changed- src/compiler
- tests/baselines/reference
4 files changed
+16
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2527 | 2527 | | |
2528 | 2528 | | |
2529 | 2529 | | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
| 2534 | + | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
2530 | 2538 | | |
2531 | 2539 | | |
2532 | 2540 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | 2 | | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
468 | | - | |
| 468 | + | |
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| |||
0 commit comments