From 0efc845c97e4b8f888cc6b8a09018da3a70baca4 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Fri, 10 Jan 2025 07:18:06 +0000 Subject: [PATCH] fix(minifier): `+0n` produces `TypeError` (#8410) --- .../src/constant_evaluation/is_literal_value.rs | 3 ++- .../src/ast_passes/peephole_remove_dead_code.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/oxc_ecmascript/src/constant_evaluation/is_literal_value.rs b/crates/oxc_ecmascript/src/constant_evaluation/is_literal_value.rs index bc0263306c2d4..01ffb6c23f5b4 100644 --- a/crates/oxc_ecmascript/src/constant_evaluation/is_literal_value.rs +++ b/crates/oxc_ecmascript/src/constant_evaluation/is_literal_value.rs @@ -21,13 +21,14 @@ pub fn is_immutable_value(expr: &Expression<'_>) -> bool { Expression::BooleanLiteral(_) | Expression::NullLiteral(_) | Expression::NumericLiteral(_) - | Expression::BigIntLiteral(_) | Expression::RegExpLiteral(_) | Expression::StringLiteral(_) => true, Expression::TemplateLiteral(lit) if lit.is_no_substitution_template() => true, Expression::Identifier(ident) => { matches!(ident.name.as_str(), "undefined" | "Infinity" | "NaN") } + // Operations on bigint can result type error. + // Expression::BigIntLiteral(_) => false, _ => false, } } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index e26a366837580..0c3a9ceddcf22 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -314,9 +314,7 @@ impl<'a, 'b> PeepholeRemoveDeadCode { if !template_lit.expressions.is_empty() { return None; } - let mut expressions = ctx.ast.move_vec(&mut template_lit.expressions); - if expressions.len() == 0 { return Some(ctx.ast.statement_empty(SPAN)); } else if expressions.len() == 1 { @@ -327,7 +325,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode { ), ); } - Some(ctx.ast.statement_expression( template_lit.span, ctx.ast.expression_sequence(template_lit.span, expressions), @@ -345,7 +342,13 @@ impl<'a, 'b> PeepholeRemoveDeadCode { Some(ctx.ast.statement_empty(SPAN)) } // `typeof x.y` -> `x`, `!x` -> `x`, `void x` -> `x`... - Expression::UnaryExpression(unary_expr) if !unary_expr.operator.is_delete() => { + // `+0n` -> `Uncaught TypeError: Cannot convert a BigInt value to a number` + Expression::UnaryExpression(unary_expr) + if !matches!( + unary_expr.operator, + UnaryOperator::Delete | UnaryOperator::UnaryPlus + ) => + { Some(ctx.ast.statement_expression( unary_expr.span, ctx.ast.move_expression(&mut unary_expr.argument), @@ -706,6 +709,7 @@ mod test { fold_same("delete x"); fold_same("delete x.y"); fold_same("delete x.y.z()"); + fold_same("+0n"); // Uncaught TypeError: Cannot convert a BigInt value to a number } #[test]