From b4e51249e95ca891dad26c3763788c85d54903e5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 27 Oct 2020 19:16:23 +0000 Subject: [PATCH] LibJS: Always insert semicolon after do-while statement if missing https://tc39.es/ecma262/#sec-additions-and-changes-that-introduce-incompatibilities-with-prior-editions 11.9.1: In ECMAScript 2015, Automatic Semicolon Insertion adds a semicolon at the end of a do-while statement if the semicolon is missing. This change aligns the specification with the actual behaviour of most existing implementations. --- Libraries/LibJS/Parser.cpp | 5 ++++- Libraries/LibJS/Tests/loops/do-while-basic.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index cc48b20517..75a74811c2 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -1524,7 +1524,10 @@ NonnullRefPtr Parser::parse_do_while_statement() auto test = parse_expression(0); consume(TokenType::ParenClose); - consume_or_insert_semicolon(); + + // Since ES 2015 a missing semicolon is inserted here, despite the regular ASI rules not applying + if (match(TokenType::Semicolon)) + consume(); return create_ast_node(move(test), move(body)); } diff --git a/Libraries/LibJS/Tests/loops/do-while-basic.js b/Libraries/LibJS/Tests/loops/do-while-basic.js index e76c20e211..c935c5f0a6 100644 --- a/Libraries/LibJS/Tests/loops/do-while-basic.js +++ b/Libraries/LibJS/Tests/loops/do-while-basic.js @@ -18,3 +18,7 @@ test("exception in test expression", () => { do {} while (foo); }).toThrow(ReferenceError); }); + +test("automatic semicolon insertion", () => { + expect("do {} while (false) foo").toEval(); +});