mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
LibJS: Multiple 'default' clauses in switch statement are a syntax error
This commit is contained in:
parent
57e7b2f8e4
commit
2dbea60fe2
2 changed files with 16 additions and 1 deletions
|
@ -1519,8 +1519,15 @@ NonnullRefPtr<SwitchStatement> Parser::parse_switch_statement()
|
||||||
|
|
||||||
NonnullRefPtrVector<SwitchCase> cases;
|
NonnullRefPtrVector<SwitchCase> cases;
|
||||||
|
|
||||||
while (match(TokenType::Case) || match(TokenType::Default))
|
auto has_default = false;
|
||||||
|
while (match(TokenType::Case) || match(TokenType::Default)) {
|
||||||
|
if (match(TokenType::Default)) {
|
||||||
|
if (has_default)
|
||||||
|
syntax_error("Multiple 'default' clauses in switch statement");
|
||||||
|
has_default = true;
|
||||||
|
}
|
||||||
cases.append(parse_switch_case());
|
cases.append(parse_switch_case());
|
||||||
|
}
|
||||||
|
|
||||||
consume(TokenType::CurlyClose);
|
consume(TokenType::CurlyClose);
|
||||||
|
|
||||||
|
|
|
@ -68,3 +68,11 @@ describe("basic switch tests", () => {
|
||||||
expect(i).toBe(5);
|
expect(i).toBe(5);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("syntax errors", () => {
|
||||||
|
expect("switch () {}").not.toEval();
|
||||||
|
expect("switch (foo) { bar }").not.toEval();
|
||||||
|
expect("switch (foo) { default: default: }").not.toEval();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue