Merge pull request #7329 from Microsoft/allowFallFromLastCase

allow fallthrough from the last case of the switch
This commit is contained in:
Vladimir Matveev
2016-03-01 17:24:00 -08:00
8 changed files with 288 additions and 2 deletions

View File

@@ -708,10 +708,14 @@ namespace ts {
function bindCaseBlock(n: CaseBlock): void {
const startState = currentReachabilityState;
for (const clause of n.clauses) {
for (let i = 0; i < n.clauses.length; i++) {
const clause = n.clauses[i];
currentReachabilityState = startState;
bind(clause);
if (clause.statements.length && currentReachabilityState === Reachability.Reachable && options.noFallthroughCasesInSwitch) {
if (clause.statements.length &&
i !== n.clauses.length - 1 && // allow fallthrough from the last case
currentReachabilityState === Reachability.Reachable &&
options.noFallthroughCasesInSwitch) {
errorOnFirstToken(clause, Diagnostics.Fallthrough_case_in_switch);
}
}

View File

@@ -0,0 +1,44 @@
//// [fallFromLastCase1.ts]
declare function use(a: string);
function foo1(a: number) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
}
}
function foo2(a: number) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
}
}
//// [fallFromLastCase1.js]
function foo1(a) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
}
}
function foo2(a) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
}
}

View File

@@ -0,0 +1,42 @@
=== tests/cases/compiler/fallFromLastCase1.ts ===
declare function use(a: string);
>use : Symbol(use, Decl(fallFromLastCase1.ts, 0, 0))
>a : Symbol(a, Decl(fallFromLastCase1.ts, 1, 21))
function foo1(a: number) {
>foo1 : Symbol(foo1, Decl(fallFromLastCase1.ts, 1, 32))
>a : Symbol(a, Decl(fallFromLastCase1.ts, 3, 14))
switch (a) {
>a : Symbol(a, Decl(fallFromLastCase1.ts, 3, 14))
case 1:
use("1");
>use : Symbol(use, Decl(fallFromLastCase1.ts, 0, 0))
break;
case 2:
use("2");
>use : Symbol(use, Decl(fallFromLastCase1.ts, 0, 0))
}
}
function foo2(a: number) {
>foo2 : Symbol(foo2, Decl(fallFromLastCase1.ts, 11, 1))
>a : Symbol(a, Decl(fallFromLastCase1.ts, 14, 14))
switch (a) {
>a : Symbol(a, Decl(fallFromLastCase1.ts, 14, 14))
case 1:
use("1");
>use : Symbol(use, Decl(fallFromLastCase1.ts, 0, 0))
break;
default:
use("2");
>use : Symbol(use, Decl(fallFromLastCase1.ts, 0, 0))
}
}

View File

@@ -0,0 +1,56 @@
=== tests/cases/compiler/fallFromLastCase1.ts ===
declare function use(a: string);
>use : (a: string) => any
>a : string
function foo1(a: number) {
>foo1 : (a: number) => void
>a : number
switch (a) {
>a : number
case 1:
>1 : number
use("1");
>use("1") : any
>use : (a: string) => any
>"1" : string
break;
case 2:
>2 : number
use("2");
>use("2") : any
>use : (a: string) => any
>"2" : string
}
}
function foo2(a: number) {
>foo2 : (a: number) => void
>a : number
switch (a) {
>a : number
case 1:
>1 : number
use("1");
>use("1") : any
>use : (a: string) => any
>"1" : string
break;
default:
use("2");
>use("2") : any
>use : (a: string) => any
>"2" : string
}
}

View File

@@ -0,0 +1,36 @@
tests/cases/compiler/fallFromLastCase2.ts(9,9): error TS7029: Fallthrough case in switch.
tests/cases/compiler/fallFromLastCase2.ts(22,9): error TS7029: Fallthrough case in switch.
==== tests/cases/compiler/fallFromLastCase2.ts (2 errors) ====
declare function use(a: string);
function foo1(a: number) {
switch (a) {
case 1:
use("1");
break;
case 2:
~~~~
!!! error TS7029: Fallthrough case in switch.
use("2");
case 3:
use("3");
}
}
function foo2(a: number) {
switch (a) {
case 1:
use("1");
break;
default:
~~~~~~~
!!! error TS7029: Fallthrough case in switch.
use("2");
case 2:
use("3");
}
}

View File

@@ -0,0 +1,52 @@
//// [fallFromLastCase2.ts]
declare function use(a: string);
function foo1(a: number) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
case 3:
use("3");
}
}
function foo2(a: number) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
case 2:
use("3");
}
}
//// [fallFromLastCase2.js]
function foo1(a) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
case 3:
use("3");
}
}
function foo2(a) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
case 2:
use("3");
}
}

View File

@@ -0,0 +1,24 @@
// @noFallthroughCasesInSwitch: true
declare function use(a: string);
function foo1(a: number) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
}
}
function foo2(a: number) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
}
}

View File

@@ -0,0 +1,28 @@
// @noFallthroughCasesInSwitch: true
declare function use(a: string);
function foo1(a: number) {
switch (a) {
case 1:
use("1");
break;
case 2:
use("2");
case 3:
use("3");
}
}
function foo2(a: number) {
switch (a) {
case 1:
use("1");
break;
default:
use("2");
case 2:
use("3");
}
}