Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformer): do not transform ** with bigint literals #6023

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
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
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"]
}