From dac8f09232389ecdee42774a1e841b9010796ce7 Mon Sep 17 00:00:00 2001 From: 7086cmd <54303040+7086cmd@users.noreply.github.com> Date: Tue, 1 Oct 2024 07:13:39 +0000 Subject: [PATCH] feat(minifier): minify unary plus negation. (#6203) 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. --- .../src/ast_passes/peephole_fold_constants.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index b48b91fbc4afc..728c3420a77d9 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -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, } } @@ -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");