From 6afc590db8be48d65ead987446de6f9fdaf24fd6 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Mon, 6 Jan 2025 23:26:18 +0000 Subject: [PATCH] feat(minifier): compress typeof addition string (#8301) --- .../src/ast_passes/peephole_fold_constants.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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 29dc61ca5a25c..081d4826981f3 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -334,6 +334,18 @@ impl<'a, 'b> PeepholeFoldConstants { } } } + + // typeof foo + "" + if let Expression::UnaryExpression(left) = &e.left { + if left.operator.is_typeof() { + if let Expression::StringLiteral(right) = &e.right { + if right.value.is_empty() { + return Some(ctx.ast.move_expression(&mut e.left)); + } + } + } + } + None } @@ -1777,4 +1789,12 @@ mod test { test("true.toString()", "'true'"); test("false.toString()", "'false'"); } + + #[test] + fn test_fold_typeof_addition_string() { + test_same("typeof foo"); + test_same("typeof foo + '123'"); + test("typeof foo + ''", "typeof foo"); + test_same("typeof foo - ''"); + } }