mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(transformer): do not transform ** with bigint literals (#6023)
part of #5822 They will produce runtime errors.
This commit is contained in:
parent
2b380c8f15
commit
28da77195b
6 changed files with 32 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
a ** b; // transform
|
||||
a ** 1; // transform
|
||||
|
||||
a ** 2n; // bail
|
||||
2n ** b; // bail
|
||||
2n ** 2n; // bail
|
||||
|
||||
a **= 2n; // bail
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"plugins": ["transform-exponentiation-operator"]
|
||||
}
|
||||
Loading…
Reference in a new issue