From ada36e5c0a17969e49e8a5d88018256bfe5ba13b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 26 Dec 2024 09:09:03 -0500 Subject: [PATCH] LibJS: Allow async functions named "async" as function properties For example, https://locals.com/site/discover has a script with an object of the form: var f = { parser: { sync() {}, async async() {}, } }; We were previously throwing a syntax error on the async function, as we specifically did not allow using "async" as a function name here. --- Libraries/LibJS/Parser.cpp | 1 - Libraries/LibJS/Tests/object-basic.js | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index b2e59213589..80c528afd7f 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -2019,7 +2019,6 @@ NonnullRefPtr Parser::parse_object_expression() if (lookahead_token.type() != TokenType::ParenOpen && lookahead_token.type() != TokenType::Colon && lookahead_token.type() != TokenType::Comma && lookahead_token.type() != TokenType::CurlyClose - && lookahead_token.type() != TokenType::Async && !lookahead_token.trivia_contains_line_terminator()) { consume(TokenType::Async); function_kind = FunctionKind::Async; diff --git a/Libraries/LibJS/Tests/object-basic.js b/Libraries/LibJS/Tests/object-basic.js index 01e7b369557..0dfae4ce1a3 100644 --- a/Libraries/LibJS/Tests/object-basic.js +++ b/Libraries/LibJS/Tests/object-basic.js @@ -207,6 +207,15 @@ describe("shorthanded properties with special names", () => { }); test("async functions as properties", () => { + expect("({ async async() {} });").toEval(); + expect('"use strict"; ({ async async() {} });').toEval(); + + expect("({ async async });").not.toEval(); + expect("({ async async, });").not.toEval(); + expect("({ async async() });").not.toEval(); + expect("({ async async: 0 });").not.toEval(); + expect("({ async async = 0 });").not.toEval(); + expect("({ async foo });").not.toEval(); expect("({ async foo, });").not.toEval(); expect("({ async foo() });").not.toEval();