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 - ''"); + } }