Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 16 additions & 16 deletions crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,10 @@ impl<'a> PeepholeOptimizations {
expr: &mut BinaryExpression<'a>,
ctx: &mut Ctx<'a, '_>,
) -> Option<Expression<'a>> {
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)
Expand All @@ -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
Expand Down
Loading