fix(minifier): +0n produces TypeError (#8410)

This commit is contained in:
Boshen 2025-01-10 07:18:06 +00:00
parent 5c63414c23
commit 0efc845c97
2 changed files with 10 additions and 5 deletions

View file

@ -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,
}
}

View file

@ -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]