Skip to content

Commit

Permalink
feat(minifier): fold object spread ({ ...null }) -> ({}) (#8339)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 8, 2025
1 parent 6220e05 commit 214c8fc
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl<'a> Traverse<'a> for PeepholeFoldConstants {
Expression::LogicalExpression(e) => Self::try_fold_logical_expr(e, ctx),
Expression::ChainExpression(e) => Self::try_fold_optional_chain(e, ctx),
Expression::CallExpression(e) => Self::try_fold_number_constructor(e, ctx),
Expression::ObjectExpression(e) => self.fold_object_spread(e, ctx),
_ => None,
} {
*expr = folded_expr;
Expand Down Expand Up @@ -654,6 +655,38 @@ impl<'a, 'b> PeepholeFoldConstants {

None
}

fn fold_object_spread(
&mut self,
e: &mut ObjectExpression<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Expression<'a>> {
let len = e.properties.len();
e.properties.retain(|p| {
if let ObjectPropertyKind::SpreadProperty(spread_element) = p {
let e = &spread_element.argument;
if e.is_literal() || ctx.is_expression_undefined(e) {
return false;
}
if let Expression::ObjectExpression(o) = e {
if o.properties.is_empty() {
return false;
}
}
if let Expression::ArrayExpression(o) = e {
if o.elements.is_empty() {
return false;
}
}
return true;
}
true
});
if e.properties.len() != len {
self.changed = true;
}
None
}
}

/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeFoldConstantsTest.java>
Expand Down Expand Up @@ -1830,5 +1863,20 @@ mod test {
test_same("{ valueOf: function() { return 0n; } } != 0n");
test_same("{ toString: function() { return '0'; } } != 0n");
}

#[test]
fn test_fold_object_spread() {
test_same("({ z, ...a })");
let result = "({ z })";
test("({ z, ...[] })", result);
test("({ z, ...{} })", result);
test("({ z, ...undefined })", result);
test("({ z, ...void 0 })", result);
test("({ z, ...null })", result);
test("({ z, ...true })", result);
test("({ z, ...1 })", result);
test("({ z, ...1n })", result);
test("({ z, .../asdf/ })", result);
}
}
}

0 comments on commit 214c8fc

Please sign in to comment.