Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/fallFromLastCase1.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
42 changes: 42 additions & 0 deletions tests/baselines/reference/fallFromLastCase1.symbols
Original file line number Diff line number Diff line change
@@ -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))
}
}
56 changes: 56 additions & 0 deletions tests/baselines/reference/fallFromLastCase1.types
Original file line number Diff line number Diff line change
@@ -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
}
}
36 changes: 36 additions & 0 deletions tests/baselines/reference/fallFromLastCase2.errors.txt
Original file line number Diff line number Diff line change
@@ -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");
}
}
52 changes: 52 additions & 0 deletions tests/baselines/reference/fallFromLastCase2.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
24 changes: 24 additions & 0 deletions tests/cases/compiler/fallFromLastCase1.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
28 changes: 28 additions & 0 deletions tests/cases/compiler/fallFromLastCase2.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}