mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 07:13:45 -05:00
Merge pull request #7329 from Microsoft/allowFallFromLastCase
allow fallthrough from the last case of the switch
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
44
tests/baselines/reference/fallFromLastCase1.js
Normal file
44
tests/baselines/reference/fallFromLastCase1.js
Normal 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");
|
||||
}
|
||||
}
|
||||
42
tests/baselines/reference/fallFromLastCase1.symbols
Normal file
42
tests/baselines/reference/fallFromLastCase1.symbols
Normal 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))
|
||||
}
|
||||
}
|
||||
56
tests/baselines/reference/fallFromLastCase1.types
Normal file
56
tests/baselines/reference/fallFromLastCase1.types
Normal 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
|
||||
}
|
||||
}
|
||||
36
tests/baselines/reference/fallFromLastCase2.errors.txt
Normal file
36
tests/baselines/reference/fallFromLastCase2.errors.txt
Normal 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");
|
||||
}
|
||||
}
|
||||
52
tests/baselines/reference/fallFromLastCase2.js
Normal file
52
tests/baselines/reference/fallFromLastCase2.js
Normal 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");
|
||||
}
|
||||
}
|
||||
24
tests/cases/compiler/fallFromLastCase1.ts
Normal file
24
tests/cases/compiler/fallFromLastCase1.ts
Normal 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");
|
||||
}
|
||||
}
|
||||
28
tests/cases/compiler/fallFromLastCase2.ts
Normal file
28
tests/cases/compiler/fallFromLastCase2.ts
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user