check that default clause is non-empty in reachability checks

This commit is contained in:
Vladimir Matveev 2016-05-27 15:38:59 -07:00
parent f07b4ba9d7
commit 92938cd8df
5 changed files with 68 additions and 2 deletions

View file

@ -912,8 +912,8 @@ namespace ts {
preSwitchCaseFlow = currentFlow;
bind(node.caseBlock);
addAntecedent(postSwitchLabel, currentFlow);
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause);
if (!hasDefault) {
const hasNonEmptyDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause && c.statements.length);
if (!hasNonEmptyDefault) {
addAntecedent(postSwitchLabel, preSwitchCaseFlow);
}
currentBreakTarget = saveBreakTarget;

View file

@ -0,0 +1,18 @@
//// [reachabilityCheckWithEmptyDefault.ts]
declare function print(s: string): void;
function foo(x: any) {
switch(x) {
case 1: return;
default:
}
print('1');
}
//// [reachabilityCheckWithEmptyDefault.js]
function foo(x) {
switch (x) {
case 1: return;
default:
}
print('1');
}

View file

@ -0,0 +1,18 @@
=== tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts ===
declare function print(s: string): void;
>print : Symbol(print, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 0))
>s : Symbol(s, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 23))
function foo(x: any) {
>foo : Symbol(foo, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 40))
>x : Symbol(x, Decl(reachabilityCheckWithEmptyDefault.ts, 1, 13))
switch(x) {
>x : Symbol(x, Decl(reachabilityCheckWithEmptyDefault.ts, 1, 13))
case 1: return;
default:
}
print('1');
>print : Symbol(print, Decl(reachabilityCheckWithEmptyDefault.ts, 0, 0))
}

View file

@ -0,0 +1,22 @@
=== tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts ===
declare function print(s: string): void;
>print : (s: string) => void
>s : string
function foo(x: any) {
>foo : (x: any) => void
>x : any
switch(x) {
>x : any
case 1: return;
>1 : number
default:
}
print('1');
>print('1') : void
>print : (s: string) => void
>'1' : string
}

View file

@ -0,0 +1,8 @@
declare function print(s: string): void;
function foo(x: any) {
switch(x) {
case 1: return;
default:
}
print('1');
}