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
7 changes: 7 additions & 0 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use oxc_ecmascript::{
constant_evaluation::{ConstantEvaluation, ConstantValue, DetermineValueType},
side_effects::MayHaveSideEffects,
};
use oxc_semantic::ReferenceFlags;
use oxc_span::GetSpan;
use oxc_syntax::es_target::ESTarget;

Expand Down Expand Up @@ -200,6 +201,9 @@ impl<'a> PeepholeOptimizations {
return;
}

let reference = ctx.scoping_mut().get_reference_mut(write_id_ref.reference_id());
reference.flags_mut().insert(ReferenceFlags::Read);

let new_op = logical_expr.operator.to_assignment_operator();
expr.operator = new_op;
expr.right = logical_expr.right.take_in(ctx.ast);
Expand All @@ -220,6 +224,9 @@ impl<'a> PeepholeOptimizations {
{
return;
}

Self::mark_assignment_target_as_read(&expr.left, ctx);

expr.operator = new_op;
expr.right = binary_expr.right.take_in(ctx.ast);
ctx.state.changed = true;
Expand Down
17 changes: 17 additions & 0 deletions crates/oxc_minifier/src/peephole/minimize_logical_expression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use oxc_allocator::TakeIn;
use oxc_ast::ast::*;
use oxc_semantic::ReferenceFlags;
use oxc_span::{ContentEq, GetSpan};
use oxc_syntax::es_target::ESTarget;

Expand Down Expand Up @@ -236,6 +237,8 @@ impl<'a> PeepholeOptimizations {
unreachable!()
};

Self::mark_assignment_target_as_read(&assignment_expr.left, ctx);

let assign_value = assignment_expr.right.take_in(ctx.ast);
sequence_expr.expressions.push(assign_value);
*expr = ctx.ast.expression_assignment(
Expand All @@ -259,6 +262,9 @@ impl<'a> PeepholeOptimizations {
{
return;
}

Self::mark_assignment_target_as_read(&assignment_expr.left, ctx);

let span = e.span;
let Expression::AssignmentExpression(assignment_expr) = &mut e.right else {
return;
Expand All @@ -268,4 +274,15 @@ impl<'a> PeepholeOptimizations {
*expr = e.right.take_in(ctx.ast);
ctx.state.changed = true;
}

/// Marks the AssignmentTargetIdentifier of assignment expressions as ReferenceFlags::Read
///
/// When creating AssignmentTargetIdentifier from normal expressions, the identifier only has ReferenceFlags::Write.
/// But assignment expressions changes the value, so we should add ReferenceFlags::Read.
pub fn mark_assignment_target_as_read(assign_target: &AssignmentTarget, ctx: &mut Ctx<'a, '_>) {
if let AssignmentTarget::AssignmentTargetIdentifier(id) = assign_target {
let reference = ctx.scoping_mut().get_reference_mut(id.reference_id());
reference.flags_mut().insert(ReferenceFlags::Read);
}
}
}
75 changes: 75 additions & 0 deletions crates/oxc_minifier/tests/peephole/oxc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use oxc_minifier::CompressOptions;

use super::super::test as test_options;
/// Oxc Integration Tests
use super::{test, test_same};

fn test_unused(source_text: &str, expected: &str) {
test_options(source_text, expected, CompressOptions::default());
}

#[test]
fn integration() {
test(
Expand Down Expand Up @@ -104,3 +111,71 @@ fn eval() {
test_same("eval?.(x, y)");
test_same("eval?.(x,y)");
}

#[test]
fn unused() {
test_unused(
"(function() {
let v;
window.foo = function() {
return v ?? (v = bar());
}
})()
",
"(function() {
let v;
window.foo = function() {
return v ??= bar();
}
})()
",
);
test_unused(
"(function() {
let v;
window.foo = function() {
return v ?? (console.log(), v = bar());
}
})()
",
"(function() {
let v;
window.foo = function() {
return v ??= (console.log(), bar());
}
})()
",
);
test_unused(
"(function() {
let v;
window.foo = function() {
return v = v || bar();
}
})()
",
"(function() {
let v;
window.foo = function() {
return v ||= bar();
}
})()
",
);
test_unused(
"(function() {
let v;
window.foo = function() {
return v = v + bar();
}
})()
",
"(function() {
let v;
window.foo = function() {
return v += bar();
}
})()
",
);
}
Loading