Skip to content

Commit

Permalink
LibJS: Allow async functions named "async" as function properties
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
trflynn89 authored and awesomekling committed Dec 26, 2024
1 parent a5455ac commit ada36e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 0 additions & 1 deletion Libraries/LibJS/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,6 @@ NonnullRefPtr<ObjectExpression const> 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;
Expand Down
9 changes: 9 additions & 0 deletions Libraries/LibJS/Tests/object-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ada36e5

Please sign in to comment.