Skip to content

Commit dae748d

Browse files
Merge pull request #3652 from patrick-sko:fix_for_issue_with_switch_validator
PiperOrigin-RevId: 324264590
2 parents fdb739a + a27151e commit dae748d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/com/google/javascript/jscomp/AstValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ private void validateSwitch(Node n) {
14831483
validateExpression(n.getFirstChild());
14841484
int defaults = 0;
14851485
for (Node c = n.getSecondChild(); c != null; c = c.getNext()) {
1486-
validateSwitchMember(n.getLastChild());
1486+
validateSwitchMember(c);
14871487
if (c.isDefaultCase()) {
14881488
defaults++;
14891489
}

test/com/google/javascript/jscomp/AstValidatorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,35 @@ public void testUnresolvedTypesAreBanned() {
12651265
expectValid(expr, Check.STATEMENT);
12661266
}
12671267

1268+
@Test
1269+
public void testSwitchStatement() {
1270+
// Since we're building the AST by hand, there won't be any types on it.
1271+
enableTypeInfoValidation = false;
1272+
1273+
String switchStatement =
1274+
lines(
1275+
"function foo(x) {",
1276+
" switch(x) {",
1277+
" case 1: ",
1278+
" var y = 5;",
1279+
" case 2: ",
1280+
" var y = 5;",
1281+
" }",
1282+
"}");
1283+
1284+
valid(switchStatement);
1285+
1286+
Node rootScriptNode = parseValidScript(switchStatement);
1287+
Node blockNode = IR.block();
1288+
1289+
Node firstCaseNode =
1290+
stream(NodeUtil.preOrderIterable(rootScriptNode)).filter(Node::isCase).findFirst().get();
1291+
1292+
firstCaseNode.addChildToFront(blockNode);
1293+
// A CASE node is only allowed to have 2 children, 1 expression and 1 block.
1294+
expectInvalid(rootScriptNode, Check.SCRIPT);
1295+
}
1296+
12681297
private void valid(String code) {
12691298
testSame(code);
12701299
assertThat(lastCheckWasValid).isTrue();

0 commit comments

Comments
 (0)