Skip to content

Commit

Permalink
fix(transformer/nullish-coalescing): correct span (#7269)
Browse files Browse the repository at this point in the history
Transformed expression inherit same `Span` as the input.
  • Loading branch information
overlookmotel committed Nov 14, 2024
1 parent e219ae8 commit 5b5c8a9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_transformer/src/es2020/nullish_coalescing_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
Self::clone_expression(&logical_expr.left, ctx),
logical_expr.left,
logical_expr.right,
logical_expr.span,
ctx,
);
return;
Expand Down Expand Up @@ -98,8 +99,13 @@ impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
binding.create_read_write_target(ctx),
logical_expr.left,
);
let mut new_expr =
Self::create_conditional_expression(reference, assignment, logical_expr.right, ctx);
let mut new_expr = Self::create_conditional_expression(
reference,
assignment,
logical_expr.right,
logical_expr.span,
ctx,
);

if is_parent_formal_parameter {
// Replace `function (a, x = a.b ?? c) {}` to `function (a, x = (() => a.b ?? c)() ){}`
Expand Down Expand Up @@ -173,6 +179,7 @@ impl<'a, 'ctx> NullishCoalescingOperator<'a, 'ctx> {
reference: Expression<'a>,
assignment: Expression<'a>,
default: Expression<'a>,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let op = BinaryOperator::StrictInequality;
Expand All @@ -186,6 +193,6 @@ impl<'a, 'ctx> NullishCoalescingOperator<'a, 'ctx> {
);
let test = ctx.ast.expression_logical(SPAN, left, LogicalOperator::And, right);

ctx.ast.expression_conditional(SPAN, test, reference, default)
ctx.ast.expression_conditional(span, test, reference, default)
}
}

0 comments on commit 5b5c8a9

Please sign in to comment.