feat(ast): allow passing span to void_0 method (#6065)

unblock #6021

Keep the original expression's `span` to insert comments correctly. Have tested in #6021 and it worked
This commit is contained in:
Dunqing 2024-09-26 05:46:40 +00:00
parent 77647931e4
commit 60c52ba2b9
5 changed files with 8 additions and 7 deletions

View file

@ -127,10 +127,10 @@ impl<'a> AstBuilder<'a> {
/// `void 0` /// `void 0`
#[inline] #[inline]
pub fn void_0(self) -> Expression<'a> { pub fn void_0(self, span: Span) -> Expression<'a> {
let num = self.number_0(); let num = self.number_0();
Expression::UnaryExpression(self.alloc(self.unary_expression( Expression::UnaryExpression(self.alloc(self.unary_expression(
Span::default(), span,
UnaryOperator::Void, UnaryOperator::Void,
num, num,
))) )))

View file

@ -1,5 +1,5 @@
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_span::SPAN; use oxc_span::{GetSpan, SPAN};
use oxc_syntax::{ use oxc_syntax::{
number::NumberBase, number::NumberBase,
operator::{BinaryOperator, UnaryOperator}, operator::{BinaryOperator, UnaryOperator},
@ -101,7 +101,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
/// Transforms `undefined` => `void 0` /// Transforms `undefined` => `void 0`
fn compress_undefined(&self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) -> bool { fn compress_undefined(&self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) -> bool {
if ctx.is_expression_undefined(expr) { if ctx.is_expression_undefined(expr) {
*expr = ctx.ast.void_0(); *expr = ctx.ast.void_0(expr.span());
return true; return true;
}; };
false false

View file

@ -1,5 +1,6 @@
use oxc_allocator::Vec; use oxc_allocator::Vec;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_span::GetSpan;
use oxc_traverse::{Traverse, TraverseCtx}; use oxc_traverse::{Traverse, TraverseCtx};
use crate::{CompressOptions, CompressorPass}; use crate::{CompressOptions, CompressorPass};
@ -77,7 +78,7 @@ impl<'a> RemoveSyntax {
fn compress_console(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { fn compress_console(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.drop_console && Self::is_console(expr) { if self.options.drop_console && Self::is_console(expr) {
*expr = ctx.ast.void_0(); *expr = ctx.ast.void_0(expr.span());
} }
} }

View file

@ -224,7 +224,7 @@ impl<'a> NullishCoalescingOperator<'a> {
SPAN, SPAN,
Self::clone_expression(&reference, ctx), Self::clone_expression(&reference, ctx),
op, op,
ctx.ast.void_0(), ctx.ast.void_0(SPAN),
); );
let test = ctx.ast.expression_logical(SPAN, left, LogicalOperator::And, right); let test = ctx.ast.expression_logical(SPAN, left, LogicalOperator::And, right);

View file

@ -676,7 +676,7 @@ impl<'a> ReactJsx<'a> {
if key_prop.is_some() { if key_prop.is_some() {
arguments.push(Argument::from(self.transform_jsx_attribute_value(key_prop, ctx))); arguments.push(Argument::from(self.transform_jsx_attribute_value(key_prop, ctx)));
} else if is_development { } else if is_development {
arguments.push(Argument::from(self.ctx.ast.void_0())); arguments.push(Argument::from(self.ctx.ast.void_0(SPAN)));
} }
// isStaticChildren // isStaticChildren