Skip to content

Commit 01cf8b0

Browse files
author
Andy Hanson
committed
Increase span of unreachable code error
1 parent 65655f2 commit 01cf8b0

14 files changed

+72
-38
lines changed

src/compiler/binder.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ namespace ts {
12111211
bind(node.statement);
12121212
popActiveLabel();
12131213
if (!activeLabel.referenced && !options.allowUnusedLabels) {
1214-
errorOrSuggestionOnFirstToken(unusedLabelIsError(options), node, Diagnostics.Unused_label);
1214+
errorOrSuggestion(unusedLabelIsError(options), node.label, Diagnostics.Unused_label);
12151215
}
12161216
if (!node.statement || node.statement.kind !== SyntaxKind.DoStatement) {
12171217
// do statement sets current flow inside bindDoStatement
@@ -1918,9 +1918,16 @@ namespace ts {
19181918
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2));
19191919
}
19201920

1921-
function errorOrSuggestionOnFirstToken(isError: boolean, node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any) {
1922-
const span = getSpanOfTokenAtPosition(file, node.pos);
1923-
const diag = createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2);
1921+
function errorOrSuggestion(isError: boolean, node: Node, message: DiagnosticMessage): void {
1922+
addErrorOrSuggestionDiagnostic(isError, { pos: getTokenPosOfNode(node, file), end: node.end }, message);
1923+
}
1924+
1925+
function errorOrSuggestionRange(isError: boolean, startNode: Node, endNode: Node, message: DiagnosticMessage): void {
1926+
addErrorOrSuggestionDiagnostic(isError, { pos: getTokenPosOfNode(startNode, file), end: endNode.end }, message);
1927+
}
1928+
1929+
function addErrorOrSuggestionDiagnostic(isError: boolean, range: TextRange, message: DiagnosticMessage): void {
1930+
const diag = createFileDiagnostic(file, range.pos, range.end - range.pos, message);
19241931
if (isError) {
19251932
file.bindDiagnostics.push(diag);
19261933
}
@@ -2792,7 +2799,7 @@ namespace ts {
27922799
node.declarationList.declarations.some(d => !!d.initializer)
27932800
);
27942801

2795-
errorOrSuggestionOnFirstToken(isError, node, Diagnostics.Unreachable_code_detected);
2802+
errorOrSuggestionRange(isError, node, isBlock(node.parent) ? last(node.parent.statements) : node, Diagnostics.Unreachable_code_detected);
27962803
}
27972804
}
27982805
}

tests/baselines/reference/cf.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tests/cases/compiler/cf.ts(36,13): error TS7027: Unreachable code detected.
1414
if (y==7) {
1515
continue L1;
1616
x=11;
17-
~
17+
~~~~~
1818
!!! error TS7027: Unreachable code detected.
1919
}
2020
if (y==3) {
@@ -28,7 +28,7 @@ tests/cases/compiler/cf.ts(36,13): error TS7027: Unreachable code detected.
2828
if (y==20) {
2929
break;
3030
x=12;
31-
~
31+
~~~~~
3232
!!! error TS7027: Unreachable code detected.
3333
}
3434
} while (y<41);
@@ -41,13 +41,13 @@ tests/cases/compiler/cf.ts(36,13): error TS7027: Unreachable code detected.
4141
L3: if (x<y) {
4242
break L2;
4343
x=13;
44-
~
44+
~~~~~
4545
!!! error TS7027: Unreachable code detected.
4646
}
4747
else {
4848
break L3;
4949
x=14;
50-
~
50+
~~~~~
5151
!!! error TS7027: Unreachable code detected.
5252
}
5353
}

tests/baselines/reference/jsFileCompilationBindErrors.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tests/cases/compiler/a.js(11,9): error TS1100: Invalid use of 'arguments' in str
1515
function f() {
1616
return;
1717
return; // Error: Unreachable code detected.
18-
~~~~~~
18+
~~~~~~~
1919
!!! error TS7027: Unreachable code detected.
2020
}
2121

tests/baselines/reference/jsFileCompilationBindReachabilityErrors.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tests/cases/compiler/a.js(19,1): error TS7028: Unused label.
2222
function bar2() {
2323
}
2424
var x = 10; // error
25-
~~~
25+
~~~~~~~~~~~
2626
!!! error TS7027: Unreachable code detected.
2727
}
2828

tests/baselines/reference/reachabilityChecks1.errors.txt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ tests/cases/compiler/reachabilityChecks1.ts(69,5): error TS7027: Unreachable cod
1010
==== tests/cases/compiler/reachabilityChecks1.ts (7 errors) ====
1111
while (true);
1212
var x = 1;
13-
~~~
13+
~~~~~~~~~~
1414
!!! error TS7027: Unreachable code detected.
1515

1616
module A {
1717
while (true);
1818
let x;
19-
~~~
19+
~~~~~~
2020
!!! error TS7027: Unreachable code detected.
2121
}
2222

@@ -30,10 +30,12 @@ tests/cases/compiler/reachabilityChecks1.ts(69,5): error TS7027: Unreachable cod
3030
module A2 {
3131
while (true);
3232
module A {
33-
~~~~~~
34-
!!! error TS7027: Unreachable code detected.
33+
~~~~~~~~~~
3534
var x = 1;
35+
~~~~~~~~~~~~~~~~~~
3636
}
37+
~~~~~
38+
!!! error TS7027: Unreachable code detected.
3739
}
3840

3941
module A3 {
@@ -44,10 +46,12 @@ tests/cases/compiler/reachabilityChecks1.ts(69,5): error TS7027: Unreachable cod
4446
module A4 {
4547
while (true);
4648
module A {
47-
~~~~~~
48-
!!! error TS7027: Unreachable code detected.
49+
~~~~~~~~~~
4950
const enum E { X }
51+
~~~~~~~~~~~~~~~~~~~~~~~~~~
5052
}
53+
~~~~~
54+
!!! error TS7027: Unreachable code detected.
5155
}
5256

5357
function f1(x) {
@@ -63,9 +67,10 @@ tests/cases/compiler/reachabilityChecks1.ts(69,5): error TS7027: Unreachable cod
6367
function f2() {
6468
return;
6569
class A {
66-
~~~~~
67-
!!! error TS7027: Unreachable code detected.
70+
~~~~~~~~~
6871
}
72+
~~~~~
73+
!!! error TS7027: Unreachable code detected.
6974
}
7075

7176
module B {
@@ -78,21 +83,25 @@ tests/cases/compiler/reachabilityChecks1.ts(69,5): error TS7027: Unreachable cod
7883
do {
7984
} while (true);
8085
enum E {
81-
~~~~
82-
!!! error TS7027: Unreachable code detected.
86+
~~~~~~~~
8387
X = 1
88+
~~~~~~~~~~~~~
8489
}
90+
~~~~~
91+
!!! error TS7027: Unreachable code detected.
8592
}
8693

8794
function f4() {
8895
if (true) {
8996
throw new Error();
9097
}
9198
const enum E {
92-
~~~~~
93-
!!! error TS7027: Unreachable code detected.
99+
~~~~~~~~~~~~~~
94100
X = 1
101+
~~~~~~~~~~~~~
95102
}
103+
~~~~~
104+
!!! error TS7027: Unreachable code detected.
96105
}
97106

98107

tests/baselines/reference/reachabilityChecks2.errors.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ tests/cases/compiler/reachabilityChecks2.ts(4,1): error TS7027: Unreachable code
66
const enum E { X }
77

88
module A4 {
9-
~~~~~~
10-
!!! error TS7027: Unreachable code detected.
9+
~~~~~~~~~~~
1110
while (true);
11+
~~~~~~~~~~~~~~~~~
1212
module A {
13+
~~~~~~~~~~~~~~
1314
const enum E { X }
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~
1416
}
17+
~~~~~
1518
}
19+
~
20+
!!! error TS7027: Unreachable code detected.
1621

1722

tests/baselines/reference/reachabilityChecks5.errors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ tests/cases/compiler/reachabilityChecks5.ts(122,13): error TS7027: Unreachable c
109109
}
110110
else {
111111
return 1;
112-
~~~~~~
112+
~~~~~~~~~
113113
!!! error TS7027: Unreachable code detected.
114114
}
115115
}
@@ -124,7 +124,7 @@ tests/cases/compiler/reachabilityChecks5.ts(122,13): error TS7027: Unreachable c
124124
try {
125125
while (false) {
126126
return 1;
127-
~~~~~~
127+
~~~~~~~~~
128128
!!! error TS7027: Unreachable code detected.
129129
}
130130
}
@@ -154,7 +154,7 @@ tests/cases/compiler/reachabilityChecks5.ts(122,13): error TS7027: Unreachable c
154154
break test;
155155
} while (true);
156156
x++;
157-
~
157+
~~~~
158158
!!! error TS7027: Unreachable code detected.
159159
} while (true);
160160
}

tests/baselines/reference/reachabilityChecks6.errors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ tests/cases/compiler/reachabilityChecks6.ts(122,13): error TS7027: Unreachable c
106106
}
107107
else {
108108
return 1;
109-
~~~~~~
109+
~~~~~~~~~
110110
!!! error TS7027: Unreachable code detected.
111111
}
112112
}
@@ -121,7 +121,7 @@ tests/cases/compiler/reachabilityChecks6.ts(122,13): error TS7027: Unreachable c
121121
try {
122122
while (false) {
123123
return 1;
124-
~~~~~~
124+
~~~~~~~~~
125125
!!! error TS7027: Unreachable code detected.
126126
}
127127
}
@@ -151,7 +151,7 @@ tests/cases/compiler/reachabilityChecks6.ts(122,13): error TS7027: Unreachable c
151151
break test;
152152
} while (true);
153153
x++;
154-
~
154+
~~~~
155155
!!! error TS7027: Unreachable code detected.
156156
} while (true);
157157
}

tests/baselines/reference/unreachableJavascriptChecked.errors.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ tests/cases/compiler/unreachable.js(3,5): error TS7027: Unreachable code detecte
55
function unreachable() {
66
return 1;
77
return 2;
8-
~~~~~~
8+
~~~~~~~~~
9+
return 3;
10+
~~~~~~~~~~~~~
911
!!! error TS7027: Unreachable code detected.
10-
}
12+
}
13+

tests/baselines/reference/unreachableJavascriptChecked.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
function unreachable() {
33
return 1;
44
return 2;
5-
}
5+
return 3;
6+
}
7+
68

79
//// [unreachable.js]
810
function unreachable() {
911
return 1;
1012
return 2;
13+
return 3;
1114
}

tests/baselines/reference/unreachableJavascriptChecked.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ function unreachable() {
44

55
return 1;
66
return 2;
7+
return 3;
78
}
9+
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
=== tests/cases/compiler/unreachable.js ===
22
function unreachable() {
3-
>unreachable : () => 1 | 2
3+
>unreachable : () => 1 | 2 | 3
44

55
return 1;
66
>1 : 1
77

88
return 2;
99
>2 : 2
10+
11+
return 3;
12+
>3 : 3
1013
}
14+

tests/cases/compiler/unreachableJavascriptChecked.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
function unreachable() {
77
return 1;
88
return 2;
9-
}
9+
return 3;
10+
}

tests/cases/fourslash/codeFixUnreachableCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
////function f() {
44
//// return f();
5-
//// [|return|] 1;
5+
//// [|return 1;
66
//// function f() {}
77
//// return 2;
88
//// type T = number;
@@ -13,7 +13,7 @@
1313
//// namespace N { export const x: T = 0; }
1414
//// var x: I;
1515
//// var y: T = 0;
16-
//// E; N; x; y;
16+
//// E; N; x; y;|]
1717
////}
1818

1919
verify.getSuggestionDiagnostics([{

0 commit comments

Comments
 (0)