Skip to content

Commit 80ccd86

Browse files
authoredMar 17, 2025··
fix(es/minifier): Do not drop numbers incorrectly (#10211)
**Description:** Previously, numbers could be dropped wrongly if they were not zero-valued. It's unrealistic in real-world world source code, but it's still a bug.
1 parent 99b590f commit 80ccd86

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed
 

‎crates/swc_ecma_minifier/src/compress/pure/misc.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ impl Pure<'_> {
749749
arg.as_deref_mut().unwrap(),
750750
DropOpts {
751751
drop_global_refs_if_unused: true,
752-
drop_zero: true,
752+
drop_number: true,
753753
drop_str_lit: true,
754754
..Default::default()
755755
},
@@ -919,7 +919,7 @@ impl Pure<'_> {
919919
DropOpts {
920920
drop_global_refs_if_unused: true,
921921
drop_str_lit: true,
922-
drop_zero: true,
922+
drop_number: true,
923923
},
924924
);
925925

@@ -1091,10 +1091,11 @@ impl Pure<'_> {
10911091
_ => (),
10921092
}
10931093

1094-
if self.options.unused {
1094+
if self.options.unused && opts.drop_number {
10951095
if let Expr::Lit(Lit::Num(n)) = e {
10961096
// Skip 0
10971097
if n.value != 0.0 && n.value.classify() == FpCategory::Normal {
1098+
report_change!("Dropping a number");
10981099
self.changed = true;
10991100
*e = Invalid { span: DUMMY_SP }.into();
11001101
return;
@@ -1133,7 +1134,7 @@ impl Pure<'_> {
11331134
DropOpts {
11341135
drop_str_lit: true,
11351136
drop_global_refs_if_unused: true,
1136-
drop_zero: true,
1137+
drop_number: true,
11371138
..opts
11381139
},
11391140
);
@@ -1206,7 +1207,8 @@ impl Pure<'_> {
12061207
if self.options.unused || self.options.side_effects {
12071208
match e {
12081209
Expr::Lit(Lit::Num(n)) => {
1209-
if n.value == 0.0 && opts.drop_zero {
1210+
if n.value == 0.0 && opts.drop_number {
1211+
report_change!("Dropping a zero number");
12101212
self.changed = true;
12111213
*e = Invalid { span: DUMMY_SP }.into();
12121214
return;
@@ -1259,7 +1261,7 @@ impl Pure<'_> {
12591261
self.ignore_return_value(
12601262
&mut bin.left,
12611263
DropOpts {
1262-
drop_zero: true,
1264+
drop_number: true,
12631265
drop_global_refs_if_unused: true,
12641266
drop_str_lit: true,
12651267
..opts
@@ -1268,7 +1270,7 @@ impl Pure<'_> {
12681270
self.ignore_return_value(
12691271
&mut bin.right,
12701272
DropOpts {
1271-
drop_zero: true,
1273+
drop_number: true,
12721274
drop_global_refs_if_unused: true,
12731275
drop_str_lit: true,
12741276
..opts
@@ -1487,7 +1489,7 @@ impl Pure<'_> {
14871489
&mut e.expr,
14881490
DropOpts {
14891491
drop_global_refs_if_unused: true,
1490-
drop_zero: true,
1492+
drop_number: true,
14911493
drop_str_lit: true,
14921494
..opts
14931495
},
@@ -1518,7 +1520,7 @@ impl Pure<'_> {
15181520
&mut expr,
15191521
DropOpts {
15201522
drop_str_lit: true,
1521-
drop_zero: true,
1523+
drop_number: true,
15221524
drop_global_refs_if_unused: true,
15231525
..opts
15241526
},
@@ -1715,7 +1717,7 @@ pub(super) struct DropOpts {
17151717
/// If true and `unused` option is enabled, references to global variables
17161718
/// will be dropped, even if `side_effects` is false.
17171719
pub drop_global_refs_if_unused: bool,
1718-
pub drop_zero: bool,
1720+
pub drop_number: bool,
17191721
pub drop_str_lit: bool,
17201722
}
17211723

‎crates/swc_ecma_minifier/src/compress/pure/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl VisitMut for Pure<'_> {
306306
arg,
307307
DropOpts {
308308
drop_global_refs_if_unused: true,
309-
drop_zero: true,
309+
drop_number: true,
310310
drop_str_lit: true,
311311
},
312312
);
@@ -537,7 +537,7 @@ impl VisitMut for Pure<'_> {
537537
self.ignore_return_value(
538538
&mut s.expr,
539539
DropOpts {
540-
drop_zero: true,
540+
drop_number: true,
541541
drop_global_refs_if_unused: true,
542542
drop_str_lit: false,
543543
},
@@ -684,7 +684,7 @@ impl VisitMut for Pure<'_> {
684684
self.ignore_return_value(
685685
e,
686686
DropOpts {
687-
drop_zero: true,
687+
drop_number: true,
688688
drop_global_refs_if_unused: true,
689689
drop_str_lit: true,
690690
..Default::default()
@@ -788,7 +788,7 @@ impl VisitMut for Pure<'_> {
788788
self.ignore_return_value(
789789
e,
790790
DropOpts {
791-
drop_zero: can_drop_zero,
791+
drop_number: can_drop_zero,
792792
drop_global_refs_if_unused: false,
793793
drop_str_lit: true,
794794
},

0 commit comments

Comments
 (0)
Please sign in to comment.