mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibJS: Add object literal method shorthand
This commit is contained in:
parent
88f7f9712d
commit
28ef654d13
Notes:
sideshowbarker
2024-07-19 07:08:47 +09:00
Author: https://github.com/mattco98 Commit: https://github.com/SerenityOS/serenity/commit/28ef654d13e Pull-request: https://github.com/SerenityOS/serenity/pull/2043
3 changed files with 37 additions and 4 deletions
|
@ -482,7 +482,9 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
|
|||
continue;
|
||||
}
|
||||
|
||||
if (need_colon || match(TokenType::Colon)) {
|
||||
if (!is_spread && match(TokenType::ParenOpen)) {
|
||||
property_value = parse_function_node<FunctionExpression>(false);
|
||||
} else if (need_colon || match(TokenType::Colon)) {
|
||||
consume(TokenType::Colon);
|
||||
property_value = parse_expression(0);
|
||||
}
|
||||
|
@ -756,11 +758,13 @@ NonnullRefPtr<BlockStatement> Parser::parse_block_statement()
|
|||
}
|
||||
|
||||
template<typename FunctionNodeType>
|
||||
NonnullRefPtr<FunctionNodeType> Parser::parse_function_node()
|
||||
NonnullRefPtr<FunctionNodeType> 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();
|
||||
|
|
|
@ -44,7 +44,7 @@ public:
|
|||
NonnullRefPtr<Program> parse_program();
|
||||
|
||||
template<typename FunctionNodeType>
|
||||
NonnullRefPtr<FunctionNodeType> parse_function_node();
|
||||
NonnullRefPtr<FunctionNodeType> parse_function_node(bool need_function_keyword = true);
|
||||
|
||||
NonnullRefPtr<Statement> parse_statement();
|
||||
NonnullRefPtr<BlockStatement> parse_block_statement();
|
||||
|
|
29
Libraries/LibJS/Tests/object-method-shorthand.js
Normal file
29
Libraries/LibJS/Tests/object-method-shorthand.js
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Reference in a new issue