Skip to content

Commit

Permalink
LibJS: Ensure a function follows an async identifier in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
trflynn89 authored and awesomekling committed Dec 26, 2024
1 parent 4eda7b5 commit a5455ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Libraries/LibJS/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,7 @@ NonnullRefPtr<ObjectExpression const> Parser::parse_object_expression()
function_kind = FunctionKind::Async;
}
}

if (match(TokenType::Asterisk)) {
consume();
property_type = ObjectProperty::Type::KeyValue;
Expand Down Expand Up @@ -2058,6 +2059,7 @@ NonnullRefPtr<ObjectExpression const> Parser::parse_object_expression()
continue;
}
}

if (match(TokenType::Equals)) {
// Not a valid object literal, but a valid assignment target
consume();
Expand All @@ -2078,6 +2080,11 @@ NonnullRefPtr<ObjectExpression const> Parser::parse_object_expression()
parse_options |= FunctionNodeParseOptions::IsAsyncFunction;
auto function = parse_function_node<FunctionExpression>(parse_options, function_start);
properties.append(create_ast_node<ObjectProperty>({ m_source_code, rule_start.position(), position() }, *property_key, function, property_type, true));
} else if (function_kind == FunctionKind::Async) {
// If we previously parsed an `async` keyword, then a function must follow.
syntax_error("Expected function after async keyword");
skip_to_next_property();
continue;
} else if (match(TokenType::Colon)) {
if (!property_key) {
expected("a property name");
Expand Down
8 changes: 8 additions & 0 deletions Libraries/LibJS/Tests/object-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ describe("shorthanded properties with special names", () => {
expect('"use strict"; var await = 8; ({ await, })').toEval();
expect('"use strict"; var async = 7; ({ async, })').toEval();
});

test("async functions as properties", () => {
expect("({ async foo });").not.toEval();
expect("({ async foo, });").not.toEval();
expect("({ async foo() });").not.toEval();
expect("({ async foo: 0 });").not.toEval();
expect("({ async foo = 0 });").not.toEval();
});
});

describe("errors", () => {
Expand Down

0 comments on commit a5455ac

Please sign in to comment.