Skip to content

Commit 92938cd

Browse files
committed
check that default clause is non-empty in reachability checks
1 parent f07b4ba commit 92938cd

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,8 @@ namespace ts {
912912
preSwitchCaseFlow = currentFlow;
913913
bind(node.caseBlock);
914914
addAntecedent(postSwitchLabel, currentFlow);
915-
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause);
916-
if (!hasDefault) {
915+
const hasNonEmptyDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause && c.statements.length);
916+
if (!hasNonEmptyDefault) {
917917
addAntecedent(postSwitchLabel, preSwitchCaseFlow);
918918
}
919919
currentBreakTarget = saveBreakTarget;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [reachabilityCheckWithEmptyDefault.ts]
2+
declare function print(s: string): void;
3+
function foo(x: any) {
4+
switch(x) {
5+
case 1: return;
6+
default:
7+
}
8+
print('1');
9+
}
10+
11+
//// [reachabilityCheckWithEmptyDefault.js]
12+
function foo(x) {
13+
switch (x) {
14+
case 1: return;
15+
default:
16+
}
17+
print('1');
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts ===
2+
declare function print(s: string): void;
3+
>print : Symbol(print, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 0))
4+
>s : Symbol(s, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 23))
5+
6+
function foo(x: any) {
7+
>foo : Symbol(foo, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 40))
8+
>x : Symbol(x, Decl(reachabilityCheckWithEmptyDefault.ts, 1, 13))
9+
10+
switch(x) {
11+
>x : Symbol(x, Decl(reachabilityCheckWithEmptyDefault.ts, 1, 13))
12+
13+
case 1: return;
14+
default:
15+
}
16+
print('1');
17+
>print : Symbol(print, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 0))
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts ===
2+
declare function print(s: string): void;
3+
>print : (s: string) => void
4+
>s : string
5+
6+
function foo(x: any) {
7+
>foo : (x: any) => void
8+
>x : any
9+
10+
switch(x) {
11+
>x : any
12+
13+
case 1: return;
14+
>1 : number
15+
16+
default:
17+
}
18+
print('1');
19+
>print('1') : void
20+
>print : (s: string) => void
21+
>'1' : string
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare function print(s: string): void;
2+
function foo(x: any) {
3+
switch(x) {
4+
case 1: return;
5+
default:
6+
}
7+
print('1');
8+
}

0 commit comments

Comments
 (0)