Skip to content

Commit e5e2496

Browse files
committed
refactor(minifier): clean up try_compress_typeof_undefined (#12958)
1 parent 577d742 commit e5e2496

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl<'a> Expression<'a> {
9191
/// Returns `true` for [string literals](StringLiteral) matching the
9292
/// expected value. Note that [non-substitution template
9393
/// literals](TemplateLiteral) are not considered.
94+
#[inline]
9495
pub fn is_specific_string_literal(&self, string: &str) -> bool {
9596
match self {
9697
Self::StringLiteral(s) => s.value == string,

crates/oxc_minifier/examples/minifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn main() -> std::io::Result<()> {
5151
let mut allocator = Allocator::default();
5252
let ret = minify(&allocator, &source_text, source_type, source_map_path, mangle, nospace);
5353
let printed = ret.code;
54-
println!("{printed}");
54+
// println!("{printed}");
5555

5656
if let Some(source_map) = ret.map {
5757
let result = source_map.to_json_string();

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,10 @@ impl<'a> PeepholeOptimizations {
242242
expr: &mut BinaryExpression<'a>,
243243
ctx: &mut Ctx<'a, '_>,
244244
) -> Option<Expression<'a>> {
245-
let Expression::UnaryExpression(unary_expr) = &expr.left else { return None };
245+
let Expression::UnaryExpression(unary_expr) = &mut expr.left else { return None };
246246
if !unary_expr.operator.is_typeof() {
247247
return None;
248248
}
249-
if !expr.right.is_specific_string_literal("undefined") {
250-
return None;
251-
}
252249
let (new_eq_op, new_comp_op) = match expr.operator {
253250
BinaryOperator::Equality | BinaryOperator::StrictEquality => {
254251
(BinaryOperator::StrictEquality, BinaryOperator::GreaterThan)
@@ -258,19 +255,22 @@ impl<'a> PeepholeOptimizations {
258255
}
259256
_ => return None,
260257
};
261-
if let Expression::Identifier(ident) = &unary_expr.argument {
262-
if ctx.is_global_reference(ident) {
263-
let left = expr.left.take_in(ctx.ast);
264-
let right = ctx.ast.expression_string_literal(expr.right.span(), "u", None);
265-
return Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right));
266-
}
258+
if !expr.right.is_specific_string_literal("undefined") {
259+
return None;
267260
}
268-
269-
let Expression::UnaryExpression(unary_expr) = expr.left.take_in(ctx.ast) else {
270-
unreachable!()
271-
};
272-
let right = ctx.ast.void_0(expr.right.span());
273-
Some(ctx.ast.expression_binary(expr.span, unary_expr.unbox().argument, new_eq_op, right))
261+
if let Expression::Identifier(ident) = &unary_expr.argument
262+
&& ctx.is_global_reference(ident)
263+
{
264+
let left = expr.left.take_in(ctx.ast);
265+
let right = ctx.ast.expression_string_literal(expr.right.span(), "u", None);
266+
return Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right));
267+
}
268+
Some(ctx.ast.expression_binary(
269+
expr.span,
270+
unary_expr.take_in(ctx.ast).argument,
271+
new_eq_op,
272+
ctx.ast.void_0(expr.right.span()),
273+
))
274274
}
275275

276276
/// Remove unary `+` if `ToNumber` conversion is done by the parent expression

0 commit comments

Comments
 (0)