Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/oxc_ecmascript/src/side_effects/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ impl<'a> MayHaveSideEffects<'a> for ObjectPropertyKind<'a> {
} else {
match &e.argument {
Expression::ArrayExpression(arr) => arr.may_have_side_effects(ctx),
Expression::ObjectExpression(obj) => obj
.properties
.iter()
.any(|property| property.may_have_side_effects(ctx)),
Comment on lines +653 to +656

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Treat getter-backed object spreads as side-effectful

When property_read_side_effects is All (the default), spreading an object literal with accessor properties must be considered effectful because object spread performs a property read (Get) for each enumerable key. This new branch only checks property.may_have_side_effects(ctx), which does not account for PropertyKind::Get, so expressions like ({...{ get a() { foo() } }}) can be misclassified as side-effect-free and then removed by remove_unused_object_expr, dropping the getter invocation.

Useful? React with 👍 / 👎.

Expression::StringLiteral(_) => false,
Expression::TemplateLiteral(t) => t.may_have_side_effects(ctx),
_ => true,
Expand Down
7 changes: 6 additions & 1 deletion crates/oxc_minifier/src/peephole/remove_unused_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ impl<'a> PeepholeOptimizations {
return true;
}
if object_expr.properties.iter().all(ObjectPropertyKind::is_spread) {
return false;
// All-spread objects like `({...x})` can only be removed if
// the spread arguments themselves have no side effects.
return !object_expr
.properties
.iter()
.any(|property| property.may_have_side_effects(ctx));
}

let mut transformed_elements = ctx.ast.vec();
Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,11 @@ fn closure_compiler_tests() {
test("new Object(...true)", true);

// OBJECT_SPREAD
// These could all invoke getters.
// Spreading unknown identifiers could invoke getters.
test("({...x})", true);
test("({...{}})", true);
test("({...{a:1}})", true);
// Spreading object literals is side-effect-free if the properties are.
test("({...{}})", false);
test("({...{a:1}})", false);
test("({...{a:i++}})", true);
test("({...{a:f()}})", true);
test("({...f()})", true);
Expand Down Expand Up @@ -711,6 +712,11 @@ fn test_object_expression() {
test("({...`foo${foo}`})", true);
test("({...`foo${foo()}`})", true);
test("({...foo()})", true);
// Spreading object literals
test("({...{}})", false);
test("({...{a: 1}})", false);
test("({...{a: foo()}})", true);
test("({...{[foo()]: 1}})", true);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ fn test_object_literal() {
// Object-spread may trigger getters.
test_same("({...a})");
test_same("({...foo()})");
// Spreading object literals is safe if contents are safe.
test("({...{}})", "");
test("({...{a: 1}})", "");
test("({...{a: foo()}})", "foo()");
test("({ [{ foo: foo() }]: 0 })", "foo()");
test("({ foo: { foo: foo() } })", "foo()");

Expand Down
Loading