Skip to content

Commit 40faaca

Browse files
committed
Correctly handle nested switches
1 parent 40ac03c commit 40faaca

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25258,9 +25258,14 @@ namespace ts {
2525825258
// if (true) x; // <- bad (note: need to walk to parent)
2525925259
// }
2526025260
if (container.kind === SyntaxKind.CaseBlock) {
25261-
const usageCaseBlock = getAncestor(node, SyntaxKind.CaseClause);
25262-
if (usageCaseBlock) {
25263-
if (usageCaseBlock.pos > symbol.valueDeclaration.pos) {
25261+
let usageCaseClause = getAncestor(node, SyntaxKind.CaseClause);
25262+
if (usageCaseClause) {
25263+
// Walk up until we're at the same level as the declaring block
25264+
while (usageCaseClause.parent !== container) {
25265+
usageCaseClause = usageCaseClause!.parent;
25266+
}
25267+
25268+
if (usageCaseClause.pos > symbol.valueDeclaration.pos) {
2526425269
error(node, Diagnostics.Variable_0_is_declared_in_a_prior_case_block, symbolToString(symbol));
2526525270
}
2526625271
}

tests/baselines/reference/switchCaseTdz.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ tests/cases/compiler/switchCaseTdz.ts(46,13): error TS2837: Variable 'z' is decl
1919
tests/cases/compiler/switchCaseTdz.ts(50,13): error TS2837: Variable 'x' is declared in a prior case block.
2020
tests/cases/compiler/switchCaseTdz.ts(51,13): error TS2837: Variable 'y' is declared in a prior case block.
2121
tests/cases/compiler/switchCaseTdz.ts(52,13): error TS2837: Variable 'z' is declared in a prior case block.
22-
tests/cases/compiler/switchCaseTdz.ts(64,17): error TS2837: Variable 'x' is declared in a prior case block.
2322

2423

25-
==== tests/cases/compiler/switchCaseTdz.ts (22 errors) ====
24+
==== tests/cases/compiler/switchCaseTdz.ts (21 errors) ====
2625
switch (1 + 1) {
2726
case -1:
2827
x;
@@ -132,7 +131,5 @@ tests/cases/compiler/switchCaseTdz.ts(64,17): error TS2837: Variable 'x' is decl
132131
case 2:
133132
// Legal
134133
x;
135-
~
136-
!!! error TS2837: Variable 'x' is declared in a prior case block.
137134
}
138135
}

0 commit comments

Comments
 (0)