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
50 changes: 16 additions & 34 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use oxc_allocator::TakeIn;
use oxc_ast::ast::*;
use oxc_ecmascript::{
ToInt32,
constant_evaluation::{ConstantEvaluation, ConstantValue, DetermineValueType},
};
use oxc_ecmascript::constant_evaluation::{ConstantEvaluation, ConstantValue, DetermineValueType};
use oxc_span::GetSpan;
use oxc_syntax::es_target::ESTarget;

Expand Down Expand Up @@ -276,43 +273,28 @@ impl<'a> PeepholeOptimizations {
true
}

/// Compress `a = a + b` to `a += b`
/// Compress `a -= 1` to `--a` and `a -= -1` to `++a`
#[expect(clippy::float_cmp)]
fn try_compress_assignment_to_update_expression(
expr: &mut AssignmentExpression<'a>,
ctx: Ctx<'a, '_>,
) -> Option<Expression<'a>> {
let target = expr.left.as_simple_assignment_target_mut()?;
if !matches!(expr.operator, AssignmentOperator::Subtraction) {
return None;
}
match &expr.right {
Expression::NumericLiteral(num) if num.value.to_int_32() == 1 => {
// The `_` will not be placed to the target code.
let target = std::mem::replace(
target,
ctx.ast
.simple_assignment_target_assignment_target_identifier(target.span(), "_"),
);
Some(ctx.ast.expression_update(expr.span, UpdateOperator::Decrement, true, target))
}
Expression::UnaryExpression(un)
if matches!(un.operator, UnaryOperator::UnaryNegation) =>
{
let Expression::NumericLiteral(num) = &un.argument else { return None };
(num.value.to_int_32() == 1).then(|| {
// The `_` will not be placed to the target code.
let target = std::mem::replace(
target,
ctx.ast.simple_assignment_target_assignment_target_identifier(
target.span(),
"_",
),
);
ctx.ast.expression_update(expr.span, UpdateOperator::Increment, true, target)
})
}
_ => None,
}
let Expression::NumericLiteral(num) = &expr.right else {
return None;
};
let target = expr.left.as_simple_assignment_target_mut()?;
let operator = if num.value == 1.0 {
UpdateOperator::Decrement
} else if num.value == -1.0 {
UpdateOperator::Increment
} else {
return None;
};
let target = target.take_in(ctx.ast.allocator);
Some(ctx.ast.expression_update(expr.span, operator, true, target))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,7 @@ mod test {
#[test]
fn test_fold_subtraction_assignment() {
test("x -= 1", "--x");
// FIXME
// test("x -= -1", "++x");
test("x -= -1", "++x");
test_same("x -= 2");
test_same("x += 1"); // The string concatenation may be triggered, so we don't fold this.
test_same("x += -1");
Expand Down
Loading