Skip to content

Commit

Permalink
fix(minifier): +0n produces TypeError (#8410)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 10, 2025
1 parent 5c63414 commit 0efc845
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
12 changes: 8 additions & 4 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -327,7 +325,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
),
);
}

Some(ctx.ast.statement_expression(
template_lit.span,
ctx.ast.expression_sequence(template_lit.span, expressions),
Expand All @@ -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),
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 0efc845

Please sign in to comment.