diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index e1a2b8820a..4982f0baea 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -482,7 +482,9 @@ NonnullRefPtr Parser::parse_object_expression() continue; } - if (need_colon || match(TokenType::Colon)) { + if (!is_spread && match(TokenType::ParenOpen)) { + property_value = parse_function_node(false); + } else if (need_colon || match(TokenType::Colon)) { consume(TokenType::Colon); property_value = parse_expression(0); } @@ -756,11 +758,13 @@ NonnullRefPtr Parser::parse_block_statement() } template -NonnullRefPtr Parser::parse_function_node() +NonnullRefPtr Parser::parse_function_node(bool needs_function_keyword) { ScopePusher scope(*this, ScopePusher::Var); - consume(TokenType::Function); + if (needs_function_keyword) + consume(TokenType::Function); + String name; if (FunctionNodeType::must_have_name()) { name = consume(TokenType::Identifier).value(); diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h index e6df36e390..8d171c46a4 100644 --- a/Libraries/LibJS/Parser.h +++ b/Libraries/LibJS/Parser.h @@ -44,7 +44,7 @@ public: NonnullRefPtr parse_program(); template - NonnullRefPtr parse_function_node(); + NonnullRefPtr parse_function_node(bool need_function_keyword = true); NonnullRefPtr parse_statement(); NonnullRefPtr parse_block_statement(); diff --git a/Libraries/LibJS/Tests/object-method-shorthand.js b/Libraries/LibJS/Tests/object-method-shorthand.js new file mode 100644 index 0000000000..94c3454d84 --- /dev/null +++ b/Libraries/LibJS/Tests/object-method-shorthand.js @@ -0,0 +1,29 @@ +load("test-common.js"); + +try { + const o = { + foo: "bar", + getFoo() { + return this.foo; + }, + 12() { + return this.getFoo(); + }, + "hello friends"() { + return this.getFoo(); + }, + [4 + 10]() { + return this.getFoo(); + }, + }; + + assert(o.foo === "bar"); + assert(o.getFoo() === "bar"); + assert(o[12]() === "bar"); + assert(o["hello friends"]() === "bar"); + assert(o[14]() === "bar"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}