Skip to content

Commit 306cf98

Browse files
authored
fix(es/minifier): Fix minifier (#2597)
swc_ecma_minifier: - Fix `negate_cost`.
1 parent c05f35d commit 306cf98

File tree

18 files changed

+24600
-101
lines changed

18 files changed

+24600
-101
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecmascript/minifier/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "src/lists/*.json"]
77
license = "Apache-2.0/MIT"
88
name = "swc_ecma_minifier"
99
repository = "https://github.com/swc-project/swc.git"
10-
version = "0.47.0"
10+
version = "0.47.1"
1111

1212
[features]
1313
debug = ["backtrace"]

ecmascript/minifier/src/compress/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ where
424424
noop_visit_mut_type!();
425425

426426
fn visit_mut_fn_expr(&mut self, n: &mut FnExpr) {
427-
if n.function.span.has_mark(self.marks.standalone) {
427+
if false && n.function.span.has_mark(self.marks.standalone) {
428428
self.optimize_unit_repeatedly(n);
429429
return;
430430
}
@@ -433,8 +433,9 @@ where
433433
}
434434

435435
fn visit_mut_module(&mut self, n: &mut Module) {
436-
let is_bundle_mode = n.span.has_mark(self.marks.bundle_of_standalone);
436+
let is_bundle_mode = false && n.span.has_mark(self.marks.bundle_of_standalone);
437437

438+
// Disable
438439
if is_bundle_mode {
439440
self.left_parallel_depth = MAX_PAR_DEPTH - 1;
440441
} else {

ecmascript/minifier/src/compress/optimize/bools.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ where
2222
expr: &mut Expr,
2323
is_ret_val_ignored: bool,
2424
) -> bool {
25-
if negate_cost(&expr, is_ret_val_ignored, is_ret_val_ignored).unwrap_or(isize::MAX) >= 0 {
25+
let cost = negate_cost(&expr, is_ret_val_ignored, is_ret_val_ignored).unwrap_or(isize::MAX);
26+
if cost >= 0 {
2627
return false;
2728
}
2829

@@ -56,6 +57,8 @@ where
5657
//
5758
// `_ || 'undefined' == typeof require`
5859
tracing::debug!(
60+
is_return_value_ignored = is_ret_val_ignored,
61+
negate_cost = cost,
5962
"bools: Negating: (!a && !b) => !(a || b) (because both expression are good for \
6063
negation)",
6164
);
@@ -77,7 +80,7 @@ where
7780
self.with_ctx(ctx).negate(&mut e.right);
7881

7982
if cfg!(feature = "debug") {
80-
tracing::trace!("[Change] {} => {}", start, dump(&*e));
83+
tracing::debug!("[Change] {} => {}", start, dump(&*e));
8184
}
8285

8386
true

ecmascript/minifier/src/compress/optimize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ where
10111011
_ => false,
10121012
} =>
10131013
{
1014-
tracing::debug!("ignore_return_value: Preserving negated iife");
1014+
tracing::trace!("ignore_return_value: Preserving negated iife");
10151015
return Some(e.take());
10161016
}
10171017

ecmascript/minifier/src/compress/util/mod.rs

Lines changed: 97 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -211,94 +211,122 @@ pub(crate) fn negate_cost(e: &Expr, in_bool_ctx: bool, is_ret_val_ignored: bool)
211211
bin_op: Option<BinaryOp>,
212212
is_ret_val_ignored: bool,
213213
) -> isize {
214-
match e {
215-
Expr::Unary(UnaryExpr {
216-
op: op!("!"), arg, ..
217-
}) => {
218-
// TODO: Check if this argument is actually start of line.
219-
match &**arg {
220-
Expr::Call(CallExpr {
221-
callee: ExprOrSuper::Expr(callee),
222-
..
223-
}) => match &**callee {
224-
Expr::Fn(..) => return 0,
214+
let cost = (|| {
215+
match e {
216+
Expr::Unary(UnaryExpr {
217+
op: op!("!"), arg, ..
218+
}) => {
219+
// TODO: Check if this argument is actually start of line.
220+
match &**arg {
221+
Expr::Call(CallExpr {
222+
callee: ExprOrSuper::Expr(callee),
223+
..
224+
}) => match &**callee {
225+
Expr::Fn(..) => return 0,
226+
_ => {}
227+
},
225228
_ => {}
226-
},
227-
_ => {}
228-
}
229+
}
229230

230-
if in_bool_ctx {
231-
let c = -cost(arg, true, None, is_ret_val_ignored);
232-
return c.min(-1);
233-
}
231+
match &**arg {
232+
Expr::Bin(BinExpr {
233+
op: op!("&&") | op!("||"),
234+
..
235+
}) => {}
236+
_ => {
237+
if in_bool_ctx {
238+
let c = -cost(arg, true, None, is_ret_val_ignored);
239+
return c.min(-1);
240+
}
241+
}
242+
}
234243

235-
match &**arg {
236-
Expr::Unary(UnaryExpr { op: op!("!"), .. }) => -1,
244+
match &**arg {
245+
Expr::Unary(UnaryExpr { op: op!("!"), .. }) => -1,
237246

238-
_ => 1,
247+
_ => {
248+
if in_bool_ctx {
249+
-1
250+
} else {
251+
1
252+
}
253+
}
254+
}
239255
}
240-
}
241-
Expr::Bin(BinExpr {
242-
op: op!("===") | op!("!==") | op!("==") | op!("!="),
243-
..
244-
}) => 0,
256+
Expr::Bin(BinExpr {
257+
op: op!("===") | op!("!==") | op!("==") | op!("!="),
258+
..
259+
}) => 0,
245260

246-
Expr::Bin(BinExpr {
247-
op: op @ op!("||") | op @ op!("&&"),
248-
left,
249-
right,
250-
..
251-
}) => {
252-
let l_cost = cost(&left, in_bool_ctx, Some(*op), false);
261+
Expr::Bin(BinExpr {
262+
op: op @ op!("||") | op @ op!("&&"),
263+
left,
264+
right,
265+
..
266+
}) => {
267+
let l_cost = cost(&left, in_bool_ctx, Some(*op), false);
253268

254-
if !is_ret_val_ignored && !is_ok_to_negate_rhs(&right) {
255-
return l_cost + 3;
269+
if !is_ret_val_ignored && !is_ok_to_negate_rhs(&right) {
270+
return l_cost + 3;
271+
}
272+
l_cost + cost(&right, in_bool_ctx, Some(*op), is_ret_val_ignored)
256273
}
257-
l_cost + cost(&right, in_bool_ctx, Some(*op), is_ret_val_ignored)
258-
}
259274

260-
Expr::Cond(CondExpr { cons, alt, .. })
261-
if is_ok_to_negate_for_cond(&cons) && is_ok_to_negate_for_cond(&alt) =>
262-
{
263-
cost(&cons, in_bool_ctx, bin_op, is_ret_val_ignored)
264-
+ cost(&alt, in_bool_ctx, bin_op, is_ret_val_ignored)
265-
}
275+
Expr::Cond(CondExpr { cons, alt, .. })
276+
if is_ok_to_negate_for_cond(&cons) && is_ok_to_negate_for_cond(&alt) =>
277+
{
278+
cost(&cons, in_bool_ctx, bin_op, is_ret_val_ignored)
279+
+ cost(&alt, in_bool_ctx, bin_op, is_ret_val_ignored)
280+
}
266281

267-
Expr::Cond(..)
268-
| Expr::Update(..)
269-
| Expr::Bin(BinExpr {
270-
op: op!("in") | op!("instanceof"),
271-
..
272-
}) => 3,
282+
Expr::Cond(..)
283+
| Expr::Update(..)
284+
| Expr::Bin(BinExpr {
285+
op: op!("in") | op!("instanceof"),
286+
..
287+
}) => 3,
273288

274-
Expr::Assign(..) => {
275-
if is_ret_val_ignored {
276-
0
277-
} else {
278-
3
289+
Expr::Assign(..) => {
290+
if is_ret_val_ignored {
291+
0
292+
} else {
293+
3
294+
}
279295
}
280-
}
281296

282-
Expr::Seq(e) => {
283-
if let Some(last) = e.exprs.last() {
284-
return cost(&last, in_bool_ctx, bin_op, is_ret_val_ignored);
285-
}
297+
Expr::Seq(e) => {
298+
if let Some(last) = e.exprs.last() {
299+
return cost(&last, in_bool_ctx, bin_op, is_ret_val_ignored);
300+
}
286301

287-
if is_ret_val_ignored {
288-
0
289-
} else {
290-
1
302+
if is_ret_val_ignored {
303+
0
304+
} else {
305+
1
306+
}
291307
}
292-
}
293308

294-
_ => {
295-
if is_ret_val_ignored {
296-
0
297-
} else {
298-
1
309+
_ => {
310+
if is_ret_val_ignored {
311+
0
312+
} else {
313+
1
314+
}
299315
}
300316
}
317+
})();
318+
319+
if cfg!(test) {
320+
tracing::trace!(
321+
"negation cost of `{}` = {}\nin_book_ctx={:?}\nis_ret_val_ignored={:?}",
322+
dump(&e.clone().fold_with(&mut as_folder(fixer(None)))),
323+
cost,
324+
in_bool_ctx,
325+
is_ret_val_ignored
326+
);
301327
}
328+
329+
cost
302330
}
303331

304332
let cost = cost(e, in_bool_ctx, None, is_ret_val_ignored);

0 commit comments

Comments
 (0)