Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Fix infinite loop #6300

Merged
merged 8 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/scripts/test.sh
Expand Up @@ -6,6 +6,8 @@ export UPDATE=1
export SWC_CHECK=0
export SWC_RUN=0

touch tests/compress.rs

cargo test -q -p swc_ecma_minifier -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@

# find ../swc/tests/ -type f -empty -delete
39 changes: 22 additions & 17 deletions crates/swc_ecma_minifier/src/lib.rs
Expand Up @@ -38,12 +38,7 @@
#![allow(clippy::match_like_matches_macro)]

use once_cell::sync::Lazy;
use swc_common::{
comments::Comments,
pass::{Repeat, Repeated},
sync::Lrc,
SourceMap, SyntaxContext,
};
use swc_common::{comments::Comments, pass::Repeated, sync::Lrc, SourceMap, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_visit::VisitMutWith;
Expand Down Expand Up @@ -230,17 +225,27 @@ pub fn optimize(
let _timer = timer!("postcompress");

m.visit_mut_with(&mut postcompress_optimizer(options));
m.visit_mut_with(&mut Repeat::new(pure_optimizer(
options,
None,
marks,
PureOptimizerConfig {
force_str_for_tpl: Minification::force_str_for_tpl(),
enable_join_vars: true,
#[cfg(feature = "debug")]
debug_infinite_loop: false,
},
)));

let mut pass = 0;
loop {
pass += 1;

let mut v = pure_optimizer(
options,
None,
marks,
PureOptimizerConfig {
force_str_for_tpl: Minification::force_str_for_tpl(),
enable_join_vars: true,
#[cfg(feature = "debug")]
debug_infinite_loop: false,
},
);
m.visit_mut_with(&mut v);
if !v.changed() || options.passes <= pass {
break;
}
}
}

if let Some(ref mut _t) = timings {
Expand Down