Skip to content

Commit f386efc

Browse files
committed
1 parent 6cff132 commit f386efc

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use oxc_ecmascript::{
55
constant_evaluation::{ConstantEvaluation, ConstantValue, DetermineValueType, ValueType},
66
side_effects::MayHaveSideEffects,
77
};
8-
use oxc_span::GetSpan;
8+
use oxc_span::{GetSpan, SPAN};
99
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};
1010

1111
use crate::ctx::Ctx;
@@ -361,7 +361,7 @@ impl<'a> PeepholeOptimizations {
361361
}
362362
debug_assert_eq!(e.operator, BinaryOperator::Addition);
363363

364-
if let Some(expr) = Self::try_fold_add_op(&mut e.left, &mut e.right, ctx) {
364+
if let Some(expr) = Self::try_fold_add_op(&mut e.left, &mut e.right, e.span, ctx) {
365365
return Some(expr);
366366
}
367367

@@ -373,15 +373,19 @@ impl<'a> PeepholeOptimizations {
373373
left_binary_expr.right.get_side_free_string_value(ctx),
374374
e.right.get_side_free_string_value(ctx),
375375
) {
376-
let span = Span::new(left_binary_expr.right.span().start, e.right.span().end);
376+
let span = left_binary_expr
377+
.right
378+
.span()
379+
.merge_within(e.right.span(), e.span)
380+
.unwrap_or(SPAN);
377381
let value = ctx.ast.atom_from_strs_array([&left_str, &right_str]);
378382
let right = ctx.ast.expression_string_literal(span, value, None);
379383
let left = left_binary_expr.left.take_in(ctx.ast);
380384
return Some(ctx.ast.expression_binary(e.span, left, e.operator, right));
381385
}
382386

383387
if let Some(new_right) =
384-
Self::try_fold_add_op(&mut left_binary_expr.right, &mut e.right, ctx)
388+
Self::try_fold_add_op(&mut left_binary_expr.right, &mut e.right, e.span, ctx)
385389
{
386390
let left = left_binary_expr.left.take_in(ctx.ast);
387391
return Some(ctx.ast.expression_binary(e.span, left, e.operator, new_right));
@@ -394,12 +398,13 @@ impl<'a> PeepholeOptimizations {
394398
fn try_fold_add_op(
395399
left_expr: &mut Expression<'a>,
396400
right_expr: &mut Expression<'a>,
401+
parent_span: Span,
397402
ctx: &Ctx<'a, '_>,
398403
) -> Option<Expression<'a>> {
399404
if let Expression::TemplateLiteral(left) = left_expr {
400405
// "`${a}b` + `x${y}`" => "`${a}bx${y}`"
401406
if let Expression::TemplateLiteral(right) = right_expr {
402-
left.span = Span::new(left.span.start, right.span.end);
407+
left.span = left.span.merge_within(right.span, parent_span).unwrap_or(SPAN);
403408
let left_last_quasi =
404409
left.quasis.last_mut().expect("template literal must have at least one quasi");
405410
let right_first_quasi = right
@@ -426,7 +431,7 @@ impl<'a> PeepholeOptimizations {
426431

427432
// "`${x}y` + 'z'" => "`${x}yz`"
428433
if let Some(right_str) = right_expr.get_side_free_string_value(ctx) {
429-
left.span = Span::new(left.span.start, right_expr.span().end);
434+
left.span = left.span.merge_within(right_expr.span(), parent_span).unwrap_or(SPAN);
430435
let last_quasi =
431436
left.quasis.last_mut().expect("template literal must have at least one quasi");
432437
let new_raw = last_quasi.value.raw.to_string()
@@ -442,7 +447,7 @@ impl<'a> PeepholeOptimizations {
442447
} else if let Expression::TemplateLiteral(right) = right_expr {
443448
// "'x' + `y${z}`" => "`xy${z}`"
444449
if let Some(left_str) = left_expr.get_side_free_string_value(ctx) {
445-
right.span = Span::new(left_expr.span().start, right.span.end);
450+
right.span = right.span.merge_within(left_expr.span(), parent_span).unwrap_or(SPAN);
446451
let first_quasi = right
447452
.quasis
448453
.first_mut()
@@ -509,7 +514,10 @@ impl<'a> PeepholeOptimizations {
509514
e.span,
510515
expr_to_move.take_in(ctx.ast),
511516
op,
512-
ctx.value_to_expr(Span::new(left.right.span().start, e.right.span().end), v),
517+
ctx.value_to_expr(
518+
left.right.span().merge_within(e.right.span(), e.span).unwrap_or(SPAN),
519+
v,
520+
),
513521
))
514522
}
515523

crates/oxc_minifier/src/peephole/minimize_logical_expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use oxc_allocator::TakeIn;
22
use oxc_ast::ast::*;
33
use oxc_compat::ESFeature;
44
use oxc_semantic::ReferenceFlags;
5-
use oxc_span::{ContentEq, GetSpan};
5+
use oxc_span::{ContentEq, GetSpan, SPAN};
66

77
use crate::ctx::Ctx;
88

@@ -55,7 +55,7 @@ impl<'a> PeepholeOptimizations {
5555
if left.operator != op {
5656
return None;
5757
}
58-
let new_span = Span::new(left.right.span().start, expr.span.end);
58+
let new_span = left.right.span().merge_within(expr.right.span(), expr.span).unwrap_or(SPAN);
5959
Self::try_compress_is_null_or_undefined_for_left_and_right(
6060
&mut left.right,
6161
&mut expr.right,

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<'a> PeepholeOptimizations {
409409
let Some(new_expr) = Self::try_compress_is_object_and_not_null_for_left_and_right(
410410
&left.right,
411411
&e.right,
412-
Span::new(left.right.span().start, e.span.end),
412+
left.right.span().merge_within(e.right.span(), e.span).unwrap_or(SPAN),
413413
ctx,
414414
inversed,
415415
) else {

crates/oxc_minifier/tests/peephole/oxc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ fn integration() {
6767
",
6868
"v = KEY === 'delete' || KEY === 'has' ? function () { return 1 } : function () { return 2 }",
6969
);
70+
71+
test_unused(
72+
"
73+
const a = 'a';
74+
window.foo
75+
const b = `b`;
76+
console.log(a + b);
77+
",
78+
"window.foo, console.log('ab');",
79+
);
7080
}
7181

7282
#[test]

0 commit comments

Comments
 (0)