From 3ceedbd16ae34fcb2c8fea6134c2383e7c873283 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Tue, 29 Aug 2023 21:38:42 +0100 Subject: [PATCH] LibJS: Allow assignment expression in spreading property definition See: https://tc39.es/ecma262/#sec-runtime-semantics-propertydefinitionevaluation PropertyDefinition : ... AssignmentExpression Also add a test for this in array spreading, which already had this in place. --- Userland/Libraries/LibJS/Parser.cpp | 2 +- .../Tests/builtins/Array/array-spread.js | 20 +++++++++++++++++++ .../Libraries/LibJS/Tests/object-spread.js | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index f4692573de..ac8403ceac 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -1926,7 +1926,7 @@ NonnullRefPtr Parser::parse_object_expression() if (match(TokenType::TripleDot)) { consume(); - property_key = parse_expression(4); + property_key = parse_expression(2); properties.append(create_ast_node({ m_source_code, rule_start.position(), position() }, *property_key, nullptr, ObjectProperty::Type::Spread, false)); if (!match(TokenType::Comma)) break; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/array-spread.js b/Userland/Libraries/LibJS/Tests/builtins/Array/array-spread.js index cb570edffe..ec9546cd28 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Array/array-spread.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Array/array-spread.js @@ -23,3 +23,23 @@ test("basic functionality", () => { expect([...[], ...[...[1, 2, 3]], 4]).toEqual([1, 2, 3, 4]); }); + +test("allows assignment expressions", () => { + expect("([ ...a = { hello: 'world' } ])").toEval(); + expect("([ ...a += 'hello' ])").toEval(); + expect("([ ...a -= 'hello' ])").toEval(); + expect("([ ...a **= 'hello' ])").toEval(); + expect("([ ...a *= 'hello' ])").toEval(); + expect("([ ...a /= 'hello' ])").toEval(); + expect("([ ...a %= 'hello' ])").toEval(); + expect("([ ...a <<= 'hello' ])").toEval(); + expect("([ ...a >>= 'hello' ])").toEval(); + expect("([ ...a >>>= 'hello' ])").toEval(); + expect("([ ...a &= 'hello' ])").toEval(); + expect("([ ...a ^= 'hello' ])").toEval(); + expect("([ ...a |= 'hello' ])").toEval(); + expect("([ ...a &&= 'hello' ])").toEval(); + expect("([ ...a ||= 'hello' ])").toEval(); + expect("([ ...a ??= 'hello' ])").toEval(); + expect("function* test() { return ([ ...yield a ]); }").toEval(); +}); diff --git a/Userland/Libraries/LibJS/Tests/object-spread.js b/Userland/Libraries/LibJS/Tests/object-spread.js index 340e04746a..c34f932226 100644 --- a/Userland/Libraries/LibJS/Tests/object-spread.js +++ b/Userland/Libraries/LibJS/Tests/object-spread.js @@ -172,3 +172,23 @@ describe("modification of spreadable objects during spread", () => { expect(arrayResult).toHaveProperty("1000", 1000); }); }); + +test("allows assignment expressions", () => { + expect("({ ...a = { hello: 'world' } })").toEval(); + expect("({ ...a += 'hello' })").toEval(); + expect("({ ...a -= 'hello' })").toEval(); + expect("({ ...a **= 'hello' })").toEval(); + expect("({ ...a *= 'hello' })").toEval(); + expect("({ ...a /= 'hello' })").toEval(); + expect("({ ...a %= 'hello' })").toEval(); + expect("({ ...a <<= 'hello' })").toEval(); + expect("({ ...a >>= 'hello' })").toEval(); + expect("({ ...a >>>= 'hello' })").toEval(); + expect("({ ...a &= 'hello' })").toEval(); + expect("({ ...a ^= 'hello' })").toEval(); + expect("({ ...a |= 'hello' })").toEval(); + expect("({ ...a &&= 'hello' })").toEval(); + expect("({ ...a ||= 'hello' })").toEval(); + expect("({ ...a ??= 'hello' })").toEval(); + expect("function* test() { return ({ ...yield a }); }").toEval(); +});