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
124 changes: 114 additions & 10 deletions crates/oxc_minifier/src/peephole/minimize_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,39 @@ impl<'a> PeepholeOptimizations {
return Some(changed);
}
}
Expression::AssignmentExpression(_assign_expr) => {
// TODO: requires implementing `MayHaveSideEffects` to `AssignmentTarget`
Expression::AssignmentExpression(assign_expr) => {
if assign_expr.left.may_have_side_effects(ctx) {
// Do not reorder past a side effect in an assignment target, as that may
// change the replacement value. For example, "fn()" may change "a" here:
// ```js
// let a = 1;
// foo[fn()] = a;
// ```
return Some(false);
}
if assign_expr.operator != AssignmentOperator::Assign && replacement_has_side_effect
{
// If this is a read-modify-write assignment and the replacement has side
// effects, don't reorder it past the assignment target. The assignment
// target is being read so it may be changed by the side effect. For
// example, "fn()" may change "foo" here:
// ```js
// let a = fn();
// foo += a;
// ```
return Some(false);
}
// If we get here then it should be safe to attempt to substitute the
// replacement past the left operand into the right operand.
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
&mut assign_expr.right,
search_for,
replacement,
replacement_has_side_effect,
ctx,
) {
return Some(changed);
}
}
Expression::LogicalExpression(logical_expr) => {
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
Expand All @@ -1130,8 +1161,16 @@ impl<'a> PeepholeOptimizations {
return Some(changed);
}
}
Expression::PrivateInExpression(_private_in_expr) => {
// TODO: implement
Expression::PrivateInExpression(private_in_expr) => {
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
&mut private_in_expr.right,
search_for,
replacement,
replacement_has_side_effect,
ctx,
) {
return Some(changed);
}
}
Expression::ConditionalExpression(cond_expr) => {
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
Expand Down Expand Up @@ -1203,8 +1242,16 @@ impl<'a> PeepholeOptimizations {
return Some(changed);
}
}
Expression::PrivateFieldExpression(_private_field_expr) => {
// TODO: implement
Expression::PrivateFieldExpression(private_field_expr) => {
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
&mut private_field_expr.object,
search_for,
replacement,
replacement_has_side_effect,
ctx,
) {
return Some(changed);
}
}
Expression::CallExpression(call_expr) => {
// Don't substitute something into a call target that could change "this"
Expand Down Expand Up @@ -1260,8 +1307,55 @@ impl<'a> PeepholeOptimizations {
}
}
}
Expression::NewExpression(_new_expr) => {
// TODO: implement
Expression::NewExpression(new_expr) => {
// Don't substitute something into a call target that could change "this"
if !((replacement.is_member_expression()
|| matches!(replacement, Expression::ChainExpression(_)))
&& new_expr.callee.is_identifier_reference())
{
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
&mut new_expr.callee,
search_for,
replacement,
replacement_has_side_effect,
ctx,
) {
return Some(changed);
}

for arg in &mut new_expr.arguments {
match arg {
Argument::SpreadElement(spread_elem) => {
if let Some(changed) =
Self::substitute_single_use_symbol_in_expression(
&mut spread_elem.argument,
search_for,
replacement,
replacement_has_side_effect,
ctx,
)
{
return Some(changed);
}
// spread element may have sideeffects
return Some(false);
}
match_expression!(Argument) => {
if let Some(changed) =
Self::substitute_single_use_symbol_in_expression(
arg.to_expression_mut(),
search_for,
replacement,
replacement_has_side_effect,
ctx,
)
{
return Some(changed);
}
}
}
}
}
}
Expression::ArrayExpression(array_expr) => {
for elem in &mut array_expr.elements {
Expand Down Expand Up @@ -1391,8 +1485,18 @@ impl<'a> PeepholeOptimizations {
chain_expr.expression = expr.into_chain_element().expect("should be chain element");
return changed;
}
Expression::SequenceExpression(_sequence_expr) => {
// TODO: implement
Expression::SequenceExpression(sequence_expr) => {
for expr in &mut sequence_expr.expressions {
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
expr,
search_for,
replacement,
replacement_has_side_effect,
ctx,
) {
return Some(changed);
}
}
}
_ => {}
}
Expand Down
96 changes: 48 additions & 48 deletions crates/oxc_minifier/tests/peephole/esbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,54 +1793,54 @@ fn test_inline_single_use_variable() {
"var foo; function wrapper(arg0, arg1) { let x = foo; x ||= 2}",
"var foo; function wrapper(arg0, arg1) { let x = foo; x ||= 2;}",
);
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; arg0 = x}",
// "var foo; function wrapper(arg0, arg1) { arg0 = foo;}",
// );
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; arg0 += x}",
// "var foo; function wrapper(arg0, arg1) { arg0 += foo;}",
// );
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; arg0 ||= x}",
// "var foo; function wrapper(arg0, arg1) { arg0 ||= foo;}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); arg0 = x}",
// "function wrapper(arg0, arg1) { arg0 = fn();}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); arg0 += x}",
// "function wrapper(arg0, arg1) { let x = fn(); arg0 += x;}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); arg0 ||= x}",
// "function wrapper(arg0, arg1) { let x = fn(); arg0 ||= x;}",
// );
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z = x}",
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z = x;}",
// );
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z += x}",
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z += x;}",
// );
// test(
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z ||= x}",
// "var foo; function wrapper(arg0, arg1) { let x = foo; y.z ||= x;}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); y.z = x}",
// "function wrapper(arg0, arg1) { let x = fn(); y.z = x;}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); y.z += x}",
// "function wrapper(arg0, arg1) { let x = fn(); y.z += x;}",
// );
// test(
// "function wrapper(arg0, arg1) { let x = fn(); y.z ||= x}",
// "function wrapper(arg0, arg1) { let x = fn(); y.z ||= x;}",
// );
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; arg0 = x}",
"var foo; function wrapper(arg0, arg1) { arg0 = foo;}",
);
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; arg0 += x}",
"var foo; function wrapper(arg0, arg1) { arg0 += foo;}",
);
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; arg0 ||= x}",
"var foo; function wrapper(arg0, arg1) { arg0 ||= foo;}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); arg0 = x}",
"function wrapper(arg0, arg1) { arg0 = fn();}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); arg0 += x}",
"function wrapper(arg0, arg1) { let x = fn(); arg0 += x;}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); arg0 ||= x}",
"function wrapper(arg0, arg1) { let x = fn(); arg0 ||= x;}",
);
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z = x}",
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z = x;}",
);
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z += x}",
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z += x;}",
);
test(
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z ||= x}",
"var foo; function wrapper(arg0, arg1) { let x = foo; y.z ||= x;}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); y.z = x}",
"function wrapper(arg0, arg1) { let x = fn(); y.z = x;}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); y.z += x}",
"function wrapper(arg0, arg1) { let x = fn(); y.z += x;}",
);
test(
"function wrapper(arg0, arg1) { let x = fn(); y.z ||= x}",
"function wrapper(arg0, arg1) { let x = fn(); y.z ||= x;}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; return x ? y : z;}",
"function wrapper(arg0, arg1) { return arg0 ? y : z;}",
Expand Down
116 changes: 116 additions & 0 deletions crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,122 @@ use super::{test, test_same};
fn test_inline_single_use_variable() {
test_same("function wrapper(arg0, arg1) {using x = foo; return x}");
test_same("async function wrapper(arg0, arg1) { await using x = foo; return x}");

test(
"
class Foo {
#foo;
static {
let v = this;
let r = #foo in v;
console.log(r);
}
}
",
"
class Foo {
#foo;
static {
let r = #foo in this;
console.log(r);
}
}
",
);
test(
"
class Foo {
#foo;
static {
let x = foo;
this.#foo = x;
}
}
",
"
class Foo {
#foo;
static {
this.#foo = foo;
}
}
",
);
test(
"
class Foo {
#foo;
static {
let x = this;
let y = x.#foo;
console.log(y);
}
}
",
"
class Foo {
#foo;
static {
let y = this.#foo;
console.log(y);
}
}
",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; return new arg1(...x);}",
"function wrapper(arg0, arg1) { return new arg1(...arg0);}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; return new arg1(x, ...arg1);}",
"function wrapper(arg0, arg1) { return new arg1(arg0, ...arg1);}",
);
test_same("function wrapper(arg0, arg1) { let x = arg0; return new arg1(...arg1, x);}");
test(
"function wrapper(arg0, arg1) { let x = arg0; new arg1(x);}",
"function wrapper(arg0, arg1) { new arg1(arg0);}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; new x()}",
"function wrapper(arg0, arg1) { new arg0();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; new (0, x)()}",
"function wrapper(arg0, arg1) { new arg0();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0.foo; new x.bar()}",
"function wrapper(arg0, arg1) { new arg0.foo.bar();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0.foo; new x[bar]()}",
"function wrapper(arg0, arg1) { new arg0.foo[bar]();}",
);
test_same("function wrapper(arg0, arg1) { let x = arg0.foo; new x()}");
test_same("function wrapper(arg0, arg1) { let x = arg0[foo]; new x()}");
test_same("function wrapper(arg0, arg1) { let x = arg0?.foo; new x()}");
test_same("function wrapper(arg0, arg1) { let x = arg0?.[foo]; new x()}");
test(
"function wrapper(arg0, arg1) { let x = arg0.foo; new (0, x)()}",
"function wrapper(arg0, arg1) { let x = arg0.foo; new x();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0[foo]; new (0, x)()}",
"function wrapper(arg0, arg1) { let x = arg0[foo]; new x();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0?.foo; new (0, x)()}",
"function wrapper(arg0, arg1) { let x = arg0?.foo; new x();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0?.[foo]; new (0, x)()}",
"function wrapper(arg0, arg1) { let x = arg0?.[foo]; new x();}",
);
test(
"function wrapper(arg0, arg1) { let x = arg0; return (x(), 1);}",
"function wrapper(arg0, arg1) { return (arg0(), 1);}",
);
test_same("function wrapper(arg0, arg1) { let x = arg0; return (foo(), x(), 1);}");
}

#[test]
Expand Down
Loading
Loading