diff options
author | Linus Groh <mail@linusgroh.de> | 2020-10-18 23:12:27 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-19 11:30:14 +0200 |
commit | 2dbea60fe2cdb7023cdcf6a9dcfba4b8ea4aadd7 (patch) | |
tree | e4192761221cfcd2fe0d73d6622679b06f2c73da | |
parent | 57e7b2f8e40f9663bd4a7fca2a09b719c476722c (diff) | |
download | serenity-2dbea60fe2cdb7023cdcf6a9dcfba4b8ea4aadd7.zip |
LibJS: Multiple 'default' clauses in switch statement are a syntax error
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/switch-basic.js | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index a90c635647..08b5a10868 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -1519,8 +1519,15 @@ NonnullRefPtr<SwitchStatement> Parser::parse_switch_statement() 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()); + } consume(TokenType::CurlyClose); diff --git a/Libraries/LibJS/Tests/switch-basic.js b/Libraries/LibJS/Tests/switch-basic.js index 5c2a851ac0..36210dda17 100644 --- a/Libraries/LibJS/Tests/switch-basic.js +++ b/Libraries/LibJS/Tests/switch-basic.js @@ -68,3 +68,11 @@ describe("basic switch tests", () => { 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(); + }); +}); |