diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index c07f2f8012344..333975247880a 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -91,6 +91,7 @@ impl<'a> Expression<'a> { /// Returns `true` for [string literals](StringLiteral) matching the /// expected value. Note that [non-substitution template /// literals](TemplateLiteral) are not considered. + #[inline] pub fn is_specific_string_literal(&self, string: &str) -> bool { match self { Self::StringLiteral(s) => s.value == string, diff --git a/crates/oxc_minifier/examples/minifier.rs b/crates/oxc_minifier/examples/minifier.rs index 84c2779246aff..12e9c3b874131 100644 --- a/crates/oxc_minifier/examples/minifier.rs +++ b/crates/oxc_minifier/examples/minifier.rs @@ -51,7 +51,7 @@ fn main() -> std::io::Result<()> { let mut allocator = Allocator::default(); let ret = minify(&allocator, &source_text, source_type, source_map_path, mangle, nospace); let printed = ret.code; - println!("{printed}"); + // println!("{printed}"); if let Some(source_map) = ret.map { let result = source_map.to_json_string(); diff --git a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs index 040dcbd692501..06c870f820e21 100644 --- a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs @@ -242,13 +242,10 @@ impl<'a> PeepholeOptimizations { expr: &mut BinaryExpression<'a>, ctx: &mut Ctx<'a, '_>, ) -> Option> { - let Expression::UnaryExpression(unary_expr) = &expr.left else { return None }; + let Expression::UnaryExpression(unary_expr) = &mut expr.left else { return None }; if !unary_expr.operator.is_typeof() { return None; } - if !expr.right.is_specific_string_literal("undefined") { - return None; - } let (new_eq_op, new_comp_op) = match expr.operator { BinaryOperator::Equality | BinaryOperator::StrictEquality => { (BinaryOperator::StrictEquality, BinaryOperator::GreaterThan) @@ -258,19 +255,22 @@ impl<'a> PeepholeOptimizations { } _ => return None, }; - if let Expression::Identifier(ident) = &unary_expr.argument { - if ctx.is_global_reference(ident) { - let left = expr.left.take_in(ctx.ast); - let right = ctx.ast.expression_string_literal(expr.right.span(), "u", None); - return Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right)); - } + if !expr.right.is_specific_string_literal("undefined") { + return None; } - - let Expression::UnaryExpression(unary_expr) = expr.left.take_in(ctx.ast) else { - unreachable!() - }; - let right = ctx.ast.void_0(expr.right.span()); - Some(ctx.ast.expression_binary(expr.span, unary_expr.unbox().argument, new_eq_op, right)) + if let Expression::Identifier(ident) = &unary_expr.argument + && ctx.is_global_reference(ident) + { + let left = expr.left.take_in(ctx.ast); + let right = ctx.ast.expression_string_literal(expr.right.span(), "u", None); + return Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right)); + } + Some(ctx.ast.expression_binary( + expr.span, + unary_expr.take_in(ctx.ast).argument, + new_eq_op, + ctx.ast.void_0(expr.right.span()), + )) } /// Remove unary `+` if `ToNumber` conversion is done by the parent expression