diff --git a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs index 8e14005626b..220fe9225d4 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs @@ -200,6 +200,7 @@ where if let Some(arg) = arg { match &**arg { + Expr::Lit(Lit::Regex(..)) => continue, Expr::Lit(Lit::Str(s)) if s.value.len() > 3 => continue, Expr::Lit(..) => {} _ => continue, diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 33003e87504..b5b22be4404 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -10327,3 +10327,34 @@ fn issue_6217_1() { false, ); } + +#[test] +fn issue_6279_1() { + run_default_exec_test( + r###" + function run(str, r) { + let m + while(m = r.exec(str)) { + console.log(m) + } + } + run('abcda', /a/g) + "###, + ); +} + +#[test] +fn issue_6279_2() { + run_default_exec_test( + r###" + const r = new RegExp('a', 'g'); + function run(str, r) { + let m + while (m = r.exec(str)) { + console.log(m) + } + } + run('abcda', r) + "###, + ); +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/input.js new file mode 100644 index 00000000000..5d258be97bf --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/input.js @@ -0,0 +1,7 @@ +function run(str, r) { + let m + while (m = r.exec(str)) { + console.log(m) + } +} +run('abcda', /a/g) \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/output.js new file mode 100644 index 00000000000..40a73827977 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6279/1/output.js @@ -0,0 +1,4 @@ +!function(str, r) { + let m; + for(; m = r.exec(str);)console.log(m); +}('abcda', /a/g); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/input.js new file mode 100644 index 00000000000..edea710fec9 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/input.js @@ -0,0 +1,8 @@ +const r = new RegExp('a', 'g'); +function run(str, r) { + let m + while (m = r.exec(str)) { + console.log(m) + } +} +run('abcda', r) \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/output.js new file mode 100644 index 00000000000..40a73827977 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6279/2/output.js @@ -0,0 +1,4 @@ +!function(str, r) { + let m; + for(; m = r.exec(str);)console.log(m); +}('abcda', /a/g);