From 92938cd8df88dd89b2cd0791084e0b7fb871a4c9 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 27 May 2016 15:38:59 -0700 Subject: [PATCH] check that default clause is non-empty in reachability checks --- src/compiler/binder.ts | 4 ++-- .../reachabilityCheckWithEmptyDefault.js | 18 +++++++++++++++ .../reachabilityCheckWithEmptyDefault.symbols | 18 +++++++++++++++ .../reachabilityCheckWithEmptyDefault.types | 22 +++++++++++++++++++ .../reachabilityCheckWithEmptyDefault.ts | 8 +++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/reachabilityCheckWithEmptyDefault.js create mode 100644 tests/baselines/reference/reachabilityCheckWithEmptyDefault.symbols create mode 100644 tests/baselines/reference/reachabilityCheckWithEmptyDefault.types create mode 100644 tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 6f0f1ac7863..fef23770475 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -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; diff --git a/tests/baselines/reference/reachabilityCheckWithEmptyDefault.js b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.js new file mode 100644 index 00000000000..0096d2d2f9a --- /dev/null +++ b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.js @@ -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'); +} diff --git a/tests/baselines/reference/reachabilityCheckWithEmptyDefault.symbols b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.symbols new file mode 100644 index 00000000000..d3123c0e8ba --- /dev/null +++ b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.symbols @@ -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)) +} diff --git a/tests/baselines/reference/reachabilityCheckWithEmptyDefault.types b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.types new file mode 100644 index 00000000000..3d01b876473 --- /dev/null +++ b/tests/baselines/reference/reachabilityCheckWithEmptyDefault.types @@ -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 +} diff --git a/tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts b/tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts new file mode 100644 index 00000000000..6429528a0da --- /dev/null +++ b/tests/cases/compiler/reachabilityCheckWithEmptyDefault.ts @@ -0,0 +1,8 @@ +declare function print(s: string): void; +function foo(x: any) { + switch(x) { + case 1: return; + default: + } + print('1'); +} \ No newline at end of file