Skip to content

Commit

Permalink
feat(transformer): do not transform ** with bigint literals (#6023)
Browse files Browse the repository at this point in the history
part of #5822

They will produce runtime errors.
  • Loading branch information
Boshen committed Sep 24, 2024
1 parent 2b380c8 commit 28da771
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ impl<'a> Expression<'a> {
matches!(self, Self::NumericLiteral(_) | Self::BigIntLiteral(_))
}

pub fn is_big_int_literal(&self) -> bool {
matches!(self, Self::BigIntLiteral(_))
}

pub fn is_specific_string_literal(&self, string: &str) -> bool {
match self {
Self::StringLiteral(s) => s.value == string,
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
}
}

// NOTE: Bail bigint arguments to `Math.pow`, which are runtime errors.
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
// left ** right
if let Expression::BinaryExpression(binary_expr) = expr {
if binary_expr.operator == BinaryOperator::Exponential {
if binary_expr.left.is_big_int_literal() || binary_expr.right.is_big_int_literal() {
return;
}
let left = ctx.ast.move_expression(&mut binary_expr.left);
let right = ctx.ast.move_expression(&mut binary_expr.right);
*expr = Self::math_pow(left, right, ctx);
Expand All @@ -105,6 +109,9 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
// left **= right
if let Expression::AssignmentExpression(assign_expr) = expr {
if assign_expr.operator == AssignmentOperator::Exponential {
if assign_expr.right.is_big_int_literal() {
return;
}
let mut nodes = ctx.ast.vec();
let Some(Exploded { reference, uid }) =
self.explode(&mut assign_expr.left, &mut nodes, ctx)
Expand Down
3 changes: 2 additions & 1 deletion tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
commit: 3bcfee23

Passed: 55/64
Passed: 56/65

# All Passed:
* babel-plugin-transform-nullish-coalescing-operator
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-exponentiation-operator
* babel-plugin-transform-arrow-functions
* babel-preset-typescript
* babel-plugin-transform-react-jsx-source
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
a ** b; // transform
a ** 1; // transform

a ** 2n; // bail
2n ** b; // bail
2n ** 2n; // bail

a **= 2n; // bail
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Math.pow(a, b); // transform
Math.pow(a, 1); // transform

a ** 2n; // bail
2n ** b; // bail
2n ** 2n; // bail

a **= 2n; // bail
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-exponentiation-operator"]
}

0 comments on commit 28da771

Please sign in to comment.