Skip to content

Commit

Permalink
feat(minifier): minify unary plus negation. (#6203)
Browse files Browse the repository at this point in the history
resolves #6201.
It is uncertain whether this applies to other situations, so let us eliminate it in negation first, and after I have conducted a comprehensive investigation of the Closure Compiler, we can make a definitive decision.
  • Loading branch information
7086cmd committed Oct 1, 2024
1 parent 7a381ee commit dac8f09
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ impl<'a> PeepholeFoldConstants {
Some(ctx.ast.move_expression(&mut expr.argument))
}
// `+1` -> `1`
UnaryOperator::UnaryPlus if expr.argument.is_number() => {
Some(ctx.ast.move_expression(&mut expr.argument))
}
UnaryOperator::UnaryPlus => match &expr.argument {
Expression::UnaryExpression(unary) => {
matches!(unary.operator, UnaryOperator::UnaryNegation)
.then(|| ctx.ast.move_expression(&mut expr.argument))
}
_ if expr.argument.is_number() => Some(ctx.ast.move_expression(&mut expr.argument)),
_ => None,
},
_ => None,
}
}
Expand Down Expand Up @@ -1276,7 +1281,7 @@ mod test {
test("a=+0", "a=0");
// test("a=+Infinity", "a=Infinity");
// test("a=+NaN", "a=NaN");
// test("a=+-7", "a=-7");
test("a=+-7", "a=-7");
// test("a=+.5", "a=.5");

// test("a=~0xffffffff", "a=0");
Expand Down

0 comments on commit dac8f09

Please sign in to comment.