From b09220a573177c0dc68dacb3265152a37a8a00a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:21:07 +0900 Subject: [PATCH 01/31] Enable test --- crates/swc_ecma_minifier/tests/TODO.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index 36c51d818c38..3a916067aec1 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -297,7 +297,6 @@ reduce_vars/iife/input.js reduce_vars/iife_new/input.js reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js -reduce_vars/issue_1595_3/input.js reduce_vars/issue_1670_2/input.js reduce_vars/issue_1670_4/input.js reduce_vars/issue_1670_5/input.js From 30e1a21774d11e02f33568ddd2c30bac0eebf10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:26:58 +0900 Subject: [PATCH 02/31] Respect option --- crates/swc_ecma_minifier/src/compress/optimize/iife.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs index e57461436703..4a928713a4ae 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs @@ -430,7 +430,7 @@ impl Optimizer<'_> { pub(super) fn invoke_iife(&mut self, e: &mut Expr) { trace_op!("iife: invoke_iife"); - if self.options.inline == 0 { + if self.options.inline == 0 && !self.options.reduce_vars { let skip = match e { Expr::Call(v) => !v.callee.span().is_dummy(), _ => true, From 605873d7919083fb85faed08cf915f3c73e6082d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:32:02 +0900 Subject: [PATCH 03/31] Respect option --- crates/swc_ecma_minifier/src/compress/optimize/iife.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs index 4a928713a4ae..3f9e4fb6e1a2 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/iife.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/iife.rs @@ -430,7 +430,9 @@ impl Optimizer<'_> { pub(super) fn invoke_iife(&mut self, e: &mut Expr) { trace_op!("iife: invoke_iife"); - if self.options.inline == 0 && !self.options.reduce_vars { + if self.options.inline == 0 + && !(self.options.reduce_vars && self.options.reduce_fns && self.options.evaluate) + { let skip = match e { Expr::Call(v) => !v.callee.span().is_dummy(), _ => true, From def61f5e5d4c7164d9bcacc2bcee1cd9bc659684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:33:48 +0900 Subject: [PATCH 04/31] Update test refs (verified) --- .../issue_1609/chained_evaluation_1/output.js | 6 +----- .../tests/terser/compress/issue_1656/f7/output.js | 4 +--- .../compress/reduce_vars/defun_reference/output.js | 5 +---- .../terser/compress/reduce_vars/iife_assign/output.js | 11 +---------- .../terser/compress/reduce_vars/immutable/output.js | 4 +--- .../compress/reduce_vars/issue_1595_2/output.js | 1 + .../compress/reduce_vars/issue_1595_3/output.js | 4 +--- .../compress/reduce_vars/var_assign_1/output.js | 4 +--- .../compress/reduce_vars/var_assign_2/output.js | 4 +--- 9 files changed, 9 insertions(+), 34 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js index 5190f9d8d7e4..9a4742864df4 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_1/output.js @@ -1,5 +1 @@ -(function () { - (function () { - f(1).bar = 1; - })(); -})(); +f(1).bar = 1; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js index 1dc131a91fdb..223ae0cd40b3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1656/f7/output.js @@ -1,4 +1,2 @@ var b = 10; -!function() { - b = 100; -}(), console.log(100, b); +console.log(100, b = 100); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js index ced459561939..234b5b6082f9 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/defun_reference/output.js @@ -3,10 +3,7 @@ function f() { x(); return a; } - var a = function() { - y(); - return 2; - }(); + var a = (y(), 2); var b = 2; return a + 2; } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js index 96b2b6a0dd1b..296d5492b003 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/iife_assign/output.js @@ -1,10 +1 @@ -!(function () { - var a = 1, - b = 0; - !(function () { - b++; - return; - a = 2; - })(); - console.log(a); -})(); +console.log(1); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js index 6cf85793521d..86cac6b94aa3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/immutable/output.js @@ -1,3 +1 @@ -!(function () { - console.log("test".indexOf("e")); -})(); +console.log("test".indexOf("e")); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js index 74462d6b35c8..580a086a82ad 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js @@ -1,3 +1,4 @@ (function(a) { return g(3); })(0); +g(3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js index c44c43c1d31a..4c14ab0a4ab0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_3/output.js @@ -1,3 +1 @@ -(function (a) { - return g(3); -})(); +g(3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js index 80cde5a86f24..e00b68aa31ad 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_1/output.js @@ -1,3 +1 @@ -!(function () { - console.log(2); -})(); +console.log(2); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js index 6aa4f3429c94..e00b68aa31ad 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_2/output.js @@ -1,3 +1 @@ -!function() { - console.log(2); -}(); +console.log(2); From 4c906c81c9f7b713ae19741694a850c6966eded6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:34:37 +0900 Subject: [PATCH 05/31] Update test refs (verified) --- .../compress/issue_1609/chained_evaluation_2/output.js | 8 +++----- .../terser/compress/reduce_vars/issue_1814_1/output.js | 5 ++--- .../terser/compress/reduce_vars/issue_1814_2/output.js | 5 ++--- .../compress/reduce_vars/toplevel_on_loops_1/output.js | 4 +--- .../compress/reduce_vars/toplevel_on_loops_2/output.js | 4 +--- .../terser/compress/reduce_vars/var_assign_5/output.js | 5 ++--- 6 files changed, 11 insertions(+), 20 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js index 90abd344fc0f..127fc602c90e 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_1609/chained_evaluation_2/output.js @@ -1,6 +1,4 @@ -(function () { - (function () { - var b = "long piece of string"; - f(b).bar = b; - })(); +(function() { + var b; + b = "long piece of string", f(b).bar = b; })(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js index 6bf7793cafb0..3f684501d414 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_1/output.js @@ -1,6 +1,5 @@ const a = 42; !function() { - !function(a) { - console.log(a++, 42); - }(0); + var a; + a = 0, console.log(a++, 42); }(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js index 0743f91fda70..a2756e48d5f1 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1814_2/output.js @@ -1,6 +1,5 @@ const a = "32"; !function() { - !function(a) { - console.log("321", a++); - }(0); + var a; + a = 0, console.log("321", a++); }(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js index 23806c824580..3efbb16ddd20 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_1/output.js @@ -1,5 +1,3 @@ var x = 3; -do (function() { - console.log("bar:", --x); -})(); +do console.log("bar:", --x); while (x) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js index 82d1caa8657a..1e52ba580cd0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/toplevel_on_loops_2/output.js @@ -1,3 +1 @@ -for(;;)(function() { - console.log("bar:"); -})(); +for(;;)console.log("bar:"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js index b6a1c4996a6b..3150c743eeaa 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_5/output.js @@ -1,6 +1,5 @@ !function() { var a; - !function(b) { - console.log(a = 2, b); - }(a); + var b; + b = a, console.log(a = 2, b); }(); From 6c474ebbc18db080bc38ef47f110839a30a0536e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:35:23 +0900 Subject: [PATCH 06/31] Update test refs (verified) --- .../compress/reduce_vars/inner_var_for_in_1/output.js | 10 +++------- .../terser/compress/reduce_vars/var_assign_6/output.js | 6 +++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js index 531609c6dea5..3954b13c27a0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/inner_var_for_in_1/output.js @@ -1,11 +1,7 @@ function f() { - var a = 1, - b = 2; - for (b in (function () { - return x(1, b, c); - })()) { - var c = 3, - d = 4; + var a = 1, b = 2; + for(b in x(1, b, c)){ + var c = 3, d = 4; x(1, b, c, d); } x(1, b, c, d); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js index 46c2da7be8b1..653cbc482c32 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/var_assign_6/output.js @@ -1,4 +1,4 @@ -!(function () { - var a = (function () {})((a = 1)); +!function() { + var a = void (a = 1); console.log(a); -})(); +}(); From 2438e4266c2aee21581fcc24dfa72026a5587ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:35:52 +0900 Subject: [PATCH 07/31] Update the list --- crates/swc_ecma_minifier/tests/passing.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_ecma_minifier/tests/passing.txt b/crates/swc_ecma_minifier/tests/passing.txt index ab0647af1078..0f0333817be3 100644 --- a/crates/swc_ecma_minifier/tests/passing.txt +++ b/crates/swc_ecma_minifier/tests/passing.txt @@ -1308,6 +1308,7 @@ reduce_vars/issue_1533_1/input.js reduce_vars/issue_1533_2/input.js reduce_vars/issue_1595_1/input.js reduce_vars/issue_1595_2/input.js +reduce_vars/issue_1595_3/input.js reduce_vars/issue_1595_4/input.js reduce_vars/issue_1606/input.js reduce_vars/issue_1670_1/input.js From a4ce8773aa0800ea318052cd1229e78566824fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:36:53 +0900 Subject: [PATCH 08/31] Enable --- crates/swc_ecma_minifier/tests/TODO.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index 3a916067aec1..cc812f429779 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -297,7 +297,6 @@ reduce_vars/iife/input.js reduce_vars/iife_new/input.js reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js -reduce_vars/issue_1670_2/input.js reduce_vars/issue_1670_4/input.js reduce_vars/issue_1670_5/input.js reduce_vars/issue_1850_2/input.js From 5041fd84b2eeeedf74a520e51d856f970aabf26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:44:07 +0900 Subject: [PATCH 09/31] reduce_vars must enable seq inliner --- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 946714199bdb..cc5dee492a26 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -612,7 +612,7 @@ impl Optimizer<'_> { where T: ModuleItemExt, { - if !self.options.sequences() && !self.options.collapse_vars { + if !self.options.sequences() && !self.options.collapse_vars && !self.options.reduce_vars { log_abort!("sequences: [x] Disabled"); return; } From e863ccfb4ec1d3dbc0f8b9a8c1de13cddd584d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:46:44 +0900 Subject: [PATCH 10/31] top_retain is now respected by seq inliner --- .../swc_ecma_minifier/src/compress/optimize/sequences.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index cc5dee492a26..76537effa448 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1513,6 +1513,13 @@ impl Optimizer<'_> { ) }; + // Respect top_retain + if let Some(a_id) = a.id() { + if self.options.top_retain.contains(&a_id.0) { + return Ok(false); + } + } + if match &*b { Expr::Arrow(..) | Expr::Fn(..) From b5750e7e05964e3f34fe4bbb6015b5c5b5358833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:47:54 +0900 Subject: [PATCH 11/31] arguments is now ignored by seq inliner --- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 76537effa448..6fe2e4598e1f 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1515,7 +1515,7 @@ impl Optimizer<'_> { // Respect top_retain if let Some(a_id) = a.id() { - if self.options.top_retain.contains(&a_id.0) { + if a_id.0 == "arguments" || self.options.top_retain.contains(&a_id.0) { return Ok(false); } } From 0f2b7de65304dfd2e0ce9d287db1448dc4899f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:49:26 +0900 Subject: [PATCH 12/31] Update test refs (verified) --- .../compress/drop_unused/issue_t161_top_retain_14/output.js | 2 +- .../compress/drop_unused/issue_t161_top_retain_3/output.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js index ffc287766515..3fc81a2d17f3 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_14/output.js @@ -4,7 +4,7 @@ class Alpha { } } let x = 2, z = 4; -console.log(2, 3, 4, 6, 8, 12, 2, 3, 4, new Alpha().num(), new class { +console.log(2, 3, z, 6, 2 * z, 3 * z, 2, 3, z, new Alpha().num(), new class { num() { return 3; } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js index 2f276ce45e88..446e64265475 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/issue_t161_top_retain_3/output.js @@ -1 +1,4 @@ -console.log(2, 3); +function f() { + return 2; +} +console.log(f(), 3); From 3e9920837aab164f71b8344b05c4cd78c27d8997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:49:31 +0900 Subject: [PATCH 13/31] top-level check --- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 6fe2e4598e1f..bc57489336ec 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1515,7 +1515,9 @@ impl Optimizer<'_> { // Respect top_retain if let Some(a_id) = a.id() { - if a_id.0 == "arguments" || self.options.top_retain.contains(&a_id.0) { + if a_id.0 == "arguments" + || (self.ctx.top_level && self.options.top_retain.contains(&a_id.0)) + { return Ok(false); } } From 053ab95c01b0fa1cca4628d2ce270a4ae0dc2967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:50:40 +0900 Subject: [PATCH 14/31] Update test refs (verified) --- .../compress/drop_unused/vardef_value/output.js | 5 ++--- .../compress/functions/issue_2663_3/output.js | 3 +-- .../compress/functions/issue_2842/output.js | 17 +++++++---------- .../reduce_vars/boolean_binary_assign/output.js | 5 +---- .../compress/reduce_vars/cond_assign/output.js | 5 +---- .../compress/reduce_vars/issue_1670_1/output.js | 5 +---- .../compress/reduce_vars/issue_1670_2/output.js | 4 +--- .../compress/reduce_vars/issue_1670_3/output.js | 5 +---- 8 files changed, 15 insertions(+), 34 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js index c1ac28d23483..528a05163fdf 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js @@ -1,6 +1,5 @@ function f() { - var a = (function () { + return (function() { return x(); - })(); - return a(42); + })()(42); } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js index 510f9716e3a9..d135a3374f16 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js @@ -1,12 +1,11 @@ (function(outputs) { var handlers = []; for(var i = 0; i < outputs.length; i++){ - var output = outputs[i]; var handleEventClosure = function(eventName) { return function() { return console.log(eventName); }; - }(output.eventName); + }(outputs[i].eventName); handlers.push(handleEventClosure); } return handlers; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 1c76575b74c2..6d3b61fb0b31 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,14 +1,11 @@ (function() { (function() { - { - const data = function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ]); - console.log(data); - } + console.log(function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ])); })(); })(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js index 915e71b8c6b4..1feb7103be74 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js @@ -1,4 +1 @@ -!(function () { - var a; - console.log(a); -})(); +console.log(void 0); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js index 915e71b8c6b4..1feb7103be74 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js @@ -1,4 +1 @@ -!(function () { - var a; - console.log(a); -})(); +console.log(void 0); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js index 8385afee60ed..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js @@ -1,4 +1 @@ -(function () { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js index 1d10f61fe231..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js @@ -1,3 +1 @@ -(function () { - console.log("PASS"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js index 8385afee60ed..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js @@ -1,4 +1 @@ -(function () { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); From 387d8e4b2445877ad4aebc4aaf10abdee87ea129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 11:58:04 +0900 Subject: [PATCH 15/31] fixup for top-level check --- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index bc57489336ec..bd3c1cd1aad8 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1516,7 +1516,7 @@ impl Optimizer<'_> { // Respect top_retain if let Some(a_id) = a.id() { if a_id.0 == "arguments" - || (self.ctx.top_level && self.options.top_retain.contains(&a_id.0)) + || (self.ctx.in_top_level() && self.options.top_retain.contains(&a_id.0)) { return Ok(false); } From d89aece9a1efb024e9d4356abbcf7e0389a1e23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:19:42 +0900 Subject: [PATCH 16/31] Revert test refs --- .../compress/drop_unused/vardef_value/output.js | 5 +++-- .../compress/functions/issue_2663_3/output.js | 3 ++- .../compress/functions/issue_2842/output.js | 17 ++++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js index 528a05163fdf..41352df2ab6f 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js @@ -1,5 +1,6 @@ function f() { - return (function() { + var a = function() { return x(); - })()(42); + }(); + return a(42); } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js index d135a3374f16..510f9716e3a9 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js @@ -1,11 +1,12 @@ (function(outputs) { var handlers = []; for(var i = 0; i < outputs.length; i++){ + var output = outputs[i]; var handleEventClosure = function(eventName) { return function() { return console.log(eventName); }; - }(outputs[i].eventName); + }(output.eventName); handlers.push(handleEventClosure); } return handlers; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 6d3b61fb0b31..1c76575b74c2 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,11 +1,14 @@ (function() { (function() { - console.log(function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ])); + { + const data = function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ]); + console.log(data); + } })(); })(); From 628e1c7fe75c34998656cb2ee62cd5cf78c8178f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:21:07 +0900 Subject: [PATCH 17/31] Revert test refs --- .../compress/drop_unused/vardef_value/output.js | 5 ++--- .../compress/functions/issue_2842/output.js | 17 +++++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js index 41352df2ab6f..528a05163fdf 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/vardef_value/output.js @@ -1,6 +1,5 @@ function f() { - var a = function() { + return (function() { return x(); - }(); - return a(42); + })()(42); } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 1c76575b74c2..6d3b61fb0b31 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,14 +1,11 @@ (function() { (function() { - { - const data = function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ]); - console.log(data); - } + console.log(function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ])); })(); })(); From 6daf5455a0f734cccd6d4e1a30d9b636f432eff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:31:14 +0900 Subject: [PATCH 18/31] Respect options in seq inliner via seq_exprs_of --- .../swc_ecma_minifier/src/compress/optimize/sequences.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index bd3c1cd1aad8..98df82738e91 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -557,7 +557,13 @@ impl Optimizer<'_> { options: &CompressOptions, ) -> Option<Vec<Mergable<'a>>> { Some(match s { - Stmt::Expr(e) => vec![Mergable::Expr(&mut e.expr)], + Stmt::Expr(e) => { + if !self.options.sequences() && !self.options.collapse_vars { + return None; + } else { + vec![Mergable::Expr(&mut e.expr)] + } + } Stmt::Decl(Decl::Var(v)) => { if options.reduce_vars || options.collapse_vars { v.decls.iter_mut().map(Mergable::Var).collect() From 25f2cce3fa4fdd8f190f0d5754b84df05a2d57db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:32:05 +0900 Subject: [PATCH 19/31] Revert test refs --- .../compress/functions/issue_2663_3/output.js | 3 +-- .../compress/functions/issue_2842/output.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js index 510f9716e3a9..d135a3374f16 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2663_3/output.js @@ -1,12 +1,11 @@ (function(outputs) { var handlers = []; for(var i = 0; i < outputs.length; i++){ - var output = outputs[i]; var handleEventClosure = function(eventName) { return function() { return console.log(eventName); }; - }(output.eventName); + }(outputs[i].eventName); handlers.push(handleEventClosure); } return handlers; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 6d3b61fb0b31..1c76575b74c2 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,11 +1,14 @@ (function() { (function() { - console.log(function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ])); + { + const data = function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ]); + console.log(data); + } })(); })(); From 7bcc6dbe5e70a8a33ceda0bc0a1d6be05645a8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:32:55 +0900 Subject: [PATCH 20/31] Update test refs (likely revert) --- .../compress/reduce_vars/boolean_binary_assign/output.js | 5 ++++- .../tests/terser/compress/reduce_vars/cond_assign/output.js | 5 ++++- .../tests/terser/compress/reduce_vars/issue_1670_1/output.js | 5 ++++- .../tests/terser/compress/reduce_vars/issue_1670_2/output.js | 5 ++++- .../tests/terser/compress/reduce_vars/issue_1670_3/output.js | 5 ++++- .../terser/compress/reduce_vars/lvalues_def_2/output.js | 4 ++-- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js index 1feb7103be74..4df86f06d751 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/boolean_binary_assign/output.js @@ -1 +1,4 @@ -console.log(void 0); +!function() { + var a; + console.log(a); +}(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js index 1feb7103be74..4df86f06d751 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/cond_assign/output.js @@ -1 +1,4 @@ -console.log(void 0); +!function() { + var a; + console.log(a); +}(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js index 5669eec47340..79634e51866b 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js @@ -1 +1,4 @@ -console.log("PASS"); +(function() { + var a; + void 0 === a ? console.log("PASS") : console.log("FAIL"); +})(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js index 5669eec47340..79634e51866b 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js @@ -1 +1,4 @@ -console.log("PASS"); +(function() { + var a; + void 0 === a ? console.log("PASS") : console.log("FAIL"); +})(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js index 5669eec47340..79634e51866b 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js @@ -1 +1,4 @@ -console.log("PASS"); +(function() { + var a; + void 0 === a ? console.log("PASS") : console.log("FAIL"); +})(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js index c9757f3de3fc..42a40d68d719 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/lvalues_def_2/output.js @@ -1,3 +1,3 @@ -var b = 1; -var a = b += 1, b = NaN; +var b; +var a = b = 2, b = NaN; console.log(a, b); From c1b4ea0dcf1d54cc00a6bed9fb22cb010d1baa8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:52:39 +0900 Subject: [PATCH 21/31] Fix context of function --- crates/swc_ecma_minifier/src/compress/optimize/mod.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 820888ed1a3b..fcac26e5d978 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -2041,6 +2041,7 @@ impl VisitMut for Optimizer<'_> { let ctx = Ctx { top_level: false, + in_fn_like: true, is_lhs_of_assign: false, is_exact_lhs_of_assign: false, ..self.ctx @@ -2062,7 +2063,14 @@ impl VisitMut for Optimizer<'_> { } } - e.visit_mut_children_with(self); + let ctx = Ctx { + top_level: false, + in_fn_like: true, + is_lhs_of_assign: false, + is_exact_lhs_of_assign: false, + ..self.ctx + }; + e.visit_mut_children_with(&mut *self.with_ctx(ctx)); } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] From 60bf1f4a8c984d21dc4a55afc9391b03f1c69506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 12:55:13 +0900 Subject: [PATCH 22/31] Revert test refs --- .../tests/terser/compress/reduce_vars/issue_1670_2/output.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js index 79634e51866b..1d10f61fe231 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js @@ -1,4 +1,3 @@ -(function() { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); +(function () { + console.log("PASS"); })(); From 4ab9ac3a35c9d22f782e44ab9bb48f76a9fb1367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 13:55:24 +0900 Subject: [PATCH 23/31] seq option --- .../swc_ecma_minifier/src/compress/optimize/sequences.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 98df82738e91..649b58ac5fa0 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -558,10 +558,13 @@ impl Optimizer<'_> { ) -> Option<Vec<Mergable<'a>>> { Some(match s { Stmt::Expr(e) => { - if !self.options.sequences() && !self.options.collapse_vars { - return None; - } else { + if self.options.sequences() + || self.options.collapse_vars + || self.options.side_effects + { vec![Mergable::Expr(&mut e.expr)] + } else { + return None; } } Stmt::Decl(Decl::Var(v)) => { From 8ea4f7a042a3d286a16dd27d121d7d57a23dc185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 13:55:32 +0900 Subject: [PATCH 24/31] Update test refs (verified) --- .../compress/functions/issue_2842/output.js | 17 +++++++---------- .../compress/reduce_vars/issue_1670_1/output.js | 5 +---- .../compress/reduce_vars/issue_1670_2/output.js | 4 +--- .../compress/reduce_vars/issue_1670_3/output.js | 5 +---- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js index 1c76575b74c2..6d3b61fb0b31 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/issue_2842/output.js @@ -1,14 +1,11 @@ (function() { (function() { - { - const data = function(data) { - return data[data[0]]; - }([ - 1, - 2, - 3 - ]); - console.log(data); - } + console.log(function(data) { + return data[data[0]]; + }([ + 1, + 2, + 3 + ])); })(); })(); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js index 79634e51866b..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_1/output.js @@ -1,4 +1 @@ -(function() { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js index 1d10f61fe231..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_2/output.js @@ -1,3 +1 @@ -(function () { - console.log("PASS"); -})(); +console.log("PASS"); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js index 79634e51866b..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_3/output.js @@ -1,4 +1 @@ -(function() { - var a; - void 0 === a ? console.log("PASS") : console.log("FAIL"); -})(); +console.log("PASS"); From 451c2c4920fc3a4d194d8282fcb81dc999aec112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 14:02:29 +0900 Subject: [PATCH 25/31] Enable more tests --- crates/swc_ecma_minifier/tests/TODO.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index cc812f429779..492ef129320b 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -297,8 +297,6 @@ reduce_vars/iife/input.js reduce_vars/iife_new/input.js reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js -reduce_vars/issue_1670_4/input.js -reduce_vars/issue_1670_5/input.js reduce_vars/issue_1850_2/input.js reduce_vars/issue_2485/input.js reduce_vars/issue_2757_1/input.js From 4b10b35bb1e0ff1b4e035a95c0f412bf02b7a2c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 14:02:32 +0900 Subject: [PATCH 26/31] Update test refs (better than terser) --- .../tests/terser/compress/reduce_vars/issue_1670_4/output.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js index 1d10f61fe231..5669eec47340 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_4/output.js @@ -1,3 +1 @@ -(function () { - console.log("PASS"); -})(); +console.log("PASS"); From b80a7bb22a35fdda9349b0619407c1f041c87d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Thu, 1 Feb 2024 15:26:20 +0900 Subject: [PATCH 27/31] Update test refs (verified) --- .../tests/fixture/next/56408/output.js | 163 +++++++++--------- .../reduce_vars/issue_1670_5/output.js | 7 +- 2 files changed, 82 insertions(+), 88 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js b/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js index 2c2202d78dbc..603cb4ee2579 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/56408/output.js @@ -4,9 +4,7 @@ exports.ids = [ ]; exports.modules = { 4622: function(e, t, n) { - !function(e, i) { - i(t, n(9885), n(5601)); - }(0, function(e, t, n) { + void function(e, t, n) { "use strict"; function r(e) { return e && "object" == typeof e && "default" in e ? e : { @@ -677,8 +675,7 @@ exports.modules = { function P(e) { var t = e.getAsFile(); if (!t) return Promise.reject("".concat(e, " is not a File")); - var n = h(t); - return Promise.resolve(n); + return Promise.resolve(h(t)); } function S(e) { return v(this, void 0, void 0, function() { @@ -754,8 +751,7 @@ exports.modules = { 2, new Promise(function(t, n) { e.file(function(n) { - var i = h(n, e.fullPath); - t(i); + t(h(n, e.fullPath)); }, function(e) { n(e); }); @@ -765,8 +761,7 @@ exports.modules = { }); } var T = "file-invalid-type", I = "file-too-large", M = "file-too-small", L = "too-many-files", _ = function(e) { - e = Array.isArray(e) && 1 === e.length ? e[0] : e; - var t = Array.isArray(e) ? "one of ".concat(e.join(", ")) : e; + var t = Array.isArray(e = Array.isArray(e) && 1 === e.length ? e[0] : e) ? "one of ".concat(e.join(", ")) : e; return { code: T, message: "File type must be ".concat(t) @@ -947,9 +942,7 @@ exports.modules = { return D(e) && D(e.dataTransfer) ? [ 2, j(e.dataTransfer, e.type) - ] : function(e) { - return D(e) && D(e.target); - }(e) ? [ + ] : D(e) && D(e.target) ? [ 2, x(e) ] : Array.isArray(e) && e.every(function(e) { @@ -1030,9 +1023,9 @@ exports.modules = { return "function" == typeof es ? es : me; }, [ es - ]), eF = t.useRef(null), ej = t.useRef(null), ek = t.useReducer(ve, ec), eE = s(ek, 2), eP = eE[0], eC = eE[1], eS = eP.isFocused, ez = eP.isFileDialogActive, eR = t.useRef("undefined" != typeof window && window.isSecureContext && ep && Z()), ie = function() { - !eR.current && ez && setTimeout(function() { - ej.current && (ej.current.files.length || (eC({ + ]), eF = t.useRef(null), ej = t.useRef(null), ek = s(t.useReducer(ve, ec), 2), eE = ek[0], eP = ek[1], eC = eE.isFocused, eS = eE.isFileDialogActive, ez = t.useRef("undefined" != typeof window && window.isSecureContext && ep && Z()), ie = function() { + !ez.current && eS && setTimeout(function() { + ej.current && (ej.current.files.length || (eP({ type: "closeDialog" }), eO())); }, 300); @@ -1043,12 +1036,12 @@ exports.modules = { }; }, [ ej, - ez, + eS, eO, - eR + ez ]); - var eT = t.useRef([]), ce = function(e) { - eF.current && eF.current.contains(e.target) || (e.preventDefault(), eT.current = []); + var eR = t.useRef([]), ce = function(e) { + eF.current && eF.current.contains(e.target) || (e.preventDefault(), eR.current = []); }; t.useEffect(function() { return ev && (document.addEventListener("dragover", Y, !1), document.addEventListener("drop", ce, !1)), function() { @@ -1064,12 +1057,12 @@ exports.modules = { ed, b ]); - var eI = t.useCallback(function(e) { + var eT = t.useCallback(function(e) { eh ? eh(e) : console.error(e); }, [ eh - ]), eM = t.useCallback(function(e) { - e.preventDefault(), e.persist(), ke(e), eT.current = [].concat(f(eT.current), [ + ]), eI = t.useCallback(function(e) { + e.preventDefault(), e.persist(), ke(e), eR.current = [].concat(f(eR.current), [ e.target ]), N(e) && Promise.resolve(w(e)).then(function(t) { if (!H(e) || eb) { @@ -1082,7 +1075,7 @@ exports.modules = { maxFiles: M, validator: eD }); - eC({ + eP({ isDragAccept: i, isDragReject: n > 0 && !i, isDragActive: !0, @@ -1090,12 +1083,12 @@ exports.modules = { }), L && L(e); } }).catch(function(e) { - return eI(e); + return eT(e); }); }, [ w, L, - eI, + eT, eb, ew, T, @@ -1103,7 +1096,7 @@ exports.modules = { I, M, eD - ]), eL = t.useCallback(function(e) { + ]), eM = t.useCallback(function(e) { e.preventDefault(), e.persist(), ke(e); var t = N(e); if (t && e.dataTransfer) try { @@ -1113,12 +1106,12 @@ exports.modules = { }, [ en, eb - ]), e_ = t.useCallback(function(e) { + ]), eL = t.useCallback(function(e) { e.preventDefault(), e.persist(), ke(e); - var t = eT.current.filter(function(e) { + var t = eR.current.filter(function(e) { return eF.current && eF.current.contains(e); }), n = t.indexOf(e.target); - -1 !== n && t.splice(n, 1), eT.current = t, t.length > 0 || (eC({ + -1 !== n && t.splice(n, 1), eR.current = t, t.length > 0 || (eP({ type: "setDraggedFiles", isDragActive: !1, isDragAccept: !1, @@ -1128,7 +1121,7 @@ exports.modules = { eF, et, eb - ]), eB = t.useCallback(function(e, t) { + ]), e_ = t.useCallback(function(e, t) { var n = [], i = []; e.forEach(function(e) { var t = s(U(e, ew), 2), b = t[0], w = t[1], I = s(W(e, T, R), 2), M = I[0], L = I[1], q = eD ? eD(e) : null; @@ -1152,13 +1145,13 @@ exports.modules = { q ] }); - }), n.splice(0)), eC({ + }), n.splice(0)), eP({ acceptedFiles: n, fileRejections: i, type: "setFiles" }), ei && ei(n, i, t), i.length > 0 && el && el(i, t), n.length > 0 && eu && eu(n, t); }, [ - eC, + eP, I, ew, T, @@ -1168,22 +1161,22 @@ exports.modules = { eu, el, eD - ]), eK = t.useCallback(function(e) { - e.preventDefault(), e.persist(), ke(e), eT.current = [], N(e) && Promise.resolve(w(e)).then(function(t) { - H(e) && !eb || eB(t, e); + ]), eB = t.useCallback(function(e) { + e.preventDefault(), e.persist(), ke(e), eR.current = [], N(e) && Promise.resolve(w(e)).then(function(t) { + H(e) && !eb || e_(t, e); }).catch(function(e) { - return eI(e); - }), eC({ + return eT(e); + }), eP({ type: "reset" }); }, [ w, - eB, - eI, + e_, + eT, eb - ]), e$ = t.useCallback(function() { - if (eR.current) { - eC({ + ]), eK = t.useCallback(function() { + if (ez.current) { + eP({ type: "openDialog" }), eA(); window.showOpenFilePicker({ @@ -1192,44 +1185,44 @@ exports.modules = { }).then(function(e) { return w(e); }).then(function(e) { - eB(e, null), eC({ + e_(e, null), eP({ type: "closeDialog" }); }).catch(function(e) { - ne(e) ? (eO(e), eC({ + ne(e) ? (eO(e), eP({ type: "closeDialog" - })) : re(e) ? (eR.current = !1, ej.current ? (ej.current.value = null, ej.current.click()) : eI(Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no <input> was provided."))) : eI(e); + })) : re(e) ? (ez.current = !1, ej.current ? (ej.current.value = null, ej.current.click()) : eT(Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no <input> was provided."))) : eT(e); }); - } else ej.current && (eC({ + } else ej.current && (eP({ type: "openDialog" }), eA(), ej.current.value = null, ej.current.click()); }, [ - eC, + eP, eA, eO, ep, - eB, - eI, + e_, + eT, ex, I - ]), eX = t.useCallback(function(e) { - eF.current && eF.current.isEqualNode(e.target) && (" " !== e.key && "Enter" !== e.key && 32 !== e.keyCode && 13 !== e.keyCode || (e.preventDefault(), e$())); + ]), e$ = t.useCallback(function(e) { + eF.current && eF.current.isEqualNode(e.target) && (" " !== e.key && "Enter" !== e.key && 32 !== e.keyCode && 13 !== e.keyCode || (e.preventDefault(), eK())); }, [ eF, - e$ - ]), eH = t.useCallback(function() { - eC({ + eK + ]), eX = t.useCallback(function() { + eP({ type: "focus" }); - }, []), eN = t.useCallback(function() { - eC({ + }, []), eH = t.useCallback(function() { + eP({ type: "blur" }); - }, []), eU = t.useCallback(function() { - em || (V() ? setTimeout(e$, 0) : e$()); + }, []), eN = t.useCallback(function() { + em || (V() ? setTimeout(eK, 0) : eK()); }, [ em, - e$ + eK ]), Ae = function(e) { return b ? null : e; }, Oe = function(e) { @@ -1238,18 +1231,18 @@ exports.modules = { return ey ? null : Ae(e); }, ke = function(e) { eb && e.stopPropagation(); - }, eW = t.useMemo(function() { + }, eU = t.useMemo(function() { return function() { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.role, i = e.onKeyDown, w = e.onFocus, R = e.onBlur, T = e.onClick, I = e.onDragEnter, M = e.onDragOver, L = e.onDragLeave, q = e.onDrop, et = l(e, er); return c(c(u({ - onKeyDown: Oe(X(i, eX)), - onFocus: Oe(X(w, eH)), - onBlur: Oe(X(R, eN)), - onClick: Ae(X(T, eU)), - onDragEnter: Ee(X(I, eM)), - onDragOver: Ee(X(M, eL)), - onDragLeave: Ee(X(L, e_)), - onDrop: Ee(X(q, eK)), + onKeyDown: Oe(X(i, e$)), + onFocus: Oe(X(w, eX)), + onBlur: Oe(X(R, eH)), + onClick: Ae(X(T, eN)), + onDragEnter: Ee(X(I, eI)), + onDragOver: Ee(X(M, eM)), + onDragLeave: Ee(X(L, eL)), + onDrop: Ee(X(q, eB)), role: "string" == typeof n && "" !== n ? n : "presentation" }, void 0 === t ? "ref" : t, eF), b || eg ? {} : { tabIndex: 0 @@ -1257,48 +1250,48 @@ exports.modules = { }; }, [ eF, + e$, eX, eH, eN, - eU, + eI, eM, eL, - e_, - eK, + eB, eg, ey, b - ]), eG = t.useCallback(function(e) { + ]), eW = t.useCallback(function(e) { e.stopPropagation(); - }, []), eY = t.useMemo(function() { + }, []), eG = t.useMemo(function() { return function() { - var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.onChange, i = e.onClick, b = l(e, eo), w = u({ + var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.refKey, n = e.onChange, i = e.onClick, b = l(e, eo); + return c(c({}, u({ accept: ew, multiple: I, type: "file", style: { display: "none" }, - onChange: Ae(X(n, eK)), - onClick: Ae(X(i, eG)), + onChange: Ae(X(n, eB)), + onClick: Ae(X(i, eW)), tabIndex: -1 - }, void 0 === t ? "ref" : t, ej); - return c(c({}, w), b); + }, void 0 === t ? "ref" : t, ej)), b); }; }, [ ej, i, I, - eK, + eB, b ]); - return c(c({}, eP), {}, { - isFocused: eS && !b, - getRootProps: eW, - getInputProps: eY, + return c(c({}, eE), {}, { + isFocused: eC && !b, + getRootProps: eU, + getInputProps: eG, rootRef: eF, inputRef: ej, - open: Ae(e$) + open: Ae(eK) }); } function ve(e, t) { @@ -1345,6 +1338,6 @@ exports.modules = { }, e.default = ei, e.useDropzone = de, Object.defineProperty(e, "__esModule", { value: !0 }); - }); + }(t, n(9885), n(5601)); } }; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js index 17a626e090fb..bf6e9092ca72 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js @@ -1,3 +1,4 @@ -(function () { - console.log(1); -})(); +(function(a) { + if (1 === a) console.log(a); + else console.log(2); +})(1); From f4b743dbf9e9a4ee324369524ad42f0c9227b4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Fri, 2 Feb 2024 11:05:14 +0900 Subject: [PATCH 28/31] fixup for rebase --- .../tests/terser/compress/reduce_vars/issue_1595_2/output.js | 3 --- .../tests/terser/compress/reduce_vars/issue_1670_5/output.js | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js index 580a086a82ad..4c14ab0a4ab0 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1595_2/output.js @@ -1,4 +1 @@ -(function(a) { - return g(3); -})(0); g(3); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js index bf6e9092ca72..296d5492b003 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/reduce_vars/issue_1670_5/output.js @@ -1,4 +1 @@ -(function(a) { - if (1 === a) console.log(a); - else console.log(2); -})(1); +console.log(1); From c46ec43d8881827ae9ce1e9a52de9d09cec45e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Fri, 2 Feb 2024 11:06:40 +0900 Subject: [PATCH 29/31] Update the list --- crates/swc_ecma_minifier/tests/TODO.txt | 6 ------ crates/swc_ecma_minifier/tests/passing.txt | 9 +++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index 492ef129320b..52ba12010a4d 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -43,7 +43,6 @@ collapse_vars/issue_1858/input.js collapse_vars/issue_2187_2/input.js collapse_vars/issue_2203_2/input.js collapse_vars/issue_2203_4/input.js -collapse_vars/issue_2298/input.js collapse_vars/issue_2319_1/input.js collapse_vars/issue_2319_3/input.js collapse_vars/issue_2436_1/input.js @@ -119,11 +118,8 @@ drop_unused/issue_2660_2/input.js drop_unused/issue_2665/input.js drop_unused/issue_2846/input.js drop_unused/issue_t161_top_retain_10/input.js -drop_unused/issue_t161_top_retain_11/input.js drop_unused/issue_t161_top_retain_12/input.js drop_unused/issue_t161_top_retain_15/input.js -drop_unused/issue_t161_top_retain_8/input.js -drop_unused/issue_t161_top_retain_9/input.js drop_unused/issue_t183/input.js drop_unused/keep_assign/input.js drop_unused/reassign_const/input.js @@ -159,7 +155,6 @@ functions/issue_2107/input.js functions/issue_2114_1/input.js functions/issue_2114_2/input.js functions/issue_2601_2/input.js -functions/issue_2604_2/input.js functions/issue_2620_2/input.js functions/issue_2620_3/input.js functions/issue_2630_2/input.js @@ -299,7 +294,6 @@ reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js reduce_vars/issue_1850_2/input.js reduce_vars/issue_2485/input.js -reduce_vars/issue_2757_1/input.js reduce_vars/issue_2799_2/input.js reduce_vars/issue_2836/input.js reduce_vars/issue_2860_2/input.js diff --git a/crates/swc_ecma_minifier/tests/passing.txt b/crates/swc_ecma_minifier/tests/passing.txt index 0f0333817be3..252de272a74b 100644 --- a/crates/swc_ecma_minifier/tests/passing.txt +++ b/crates/swc_ecma_minifier/tests/passing.txt @@ -188,6 +188,7 @@ collapse_vars/issue_2203_1/input.js collapse_vars/issue_2203_3/input.js collapse_vars/issue_2250_1/input.js collapse_vars/issue_2250_2/input.js +collapse_vars/issue_2298/input.js collapse_vars/issue_2313_1/input.js collapse_vars/issue_2313_2/input.js collapse_vars/issue_2319_2/input.js @@ -459,6 +460,7 @@ drop_unused/issue_3192/input.js drop_unused/issue_805_1/input.js drop_unused/issue_805_2/input.js drop_unused/issue_t161_top_retain_1/input.js +drop_unused/issue_t161_top_retain_11/input.js drop_unused/issue_t161_top_retain_13/input.js drop_unused/issue_t161_top_retain_14/input.js drop_unused/issue_t161_top_retain_2/input.js @@ -467,6 +469,8 @@ drop_unused/issue_t161_top_retain_4/input.js drop_unused/issue_t161_top_retain_5/input.js drop_unused/issue_t161_top_retain_6/input.js drop_unused/issue_t161_top_retain_7/input.js +drop_unused/issue_t161_top_retain_8/input.js +drop_unused/issue_t161_top_retain_9/input.js drop_unused/keep_fnames/input.js drop_unused/unused_block_decls/input.js drop_unused/unused_block_decls_in_catch/input.js @@ -624,6 +628,7 @@ functions/issue_2531_2/input.js functions/issue_2531_3/input.js functions/issue_2601_1/input.js functions/issue_2604_1/input.js +functions/issue_2604_2/input.js functions/issue_2616/input.js functions/issue_2620_1/input.js functions/issue_2620_4/input.js @@ -1312,7 +1317,10 @@ reduce_vars/issue_1595_3/input.js reduce_vars/issue_1595_4/input.js reduce_vars/issue_1606/input.js reduce_vars/issue_1670_1/input.js +reduce_vars/issue_1670_2/input.js reduce_vars/issue_1670_3/input.js +reduce_vars/issue_1670_4/input.js +reduce_vars/issue_1670_5/input.js reduce_vars/issue_1670_6/input.js reduce_vars/issue_1814_1/input.js reduce_vars/issue_1814_2/input.js @@ -1353,6 +1361,7 @@ reduce_vars/issue_2496/input.js reduce_vars/issue_2598/input.js reduce_vars/issue_2669/input.js reduce_vars/issue_2670/input.js +reduce_vars/issue_2757_1/input.js reduce_vars/issue_2757_2/input.js reduce_vars/issue_2774/input.js reduce_vars/issue_2799_1/input.js From e094f8b4e1cb86f763a13278ae27d01ab887de37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= <kdy1997.dev@gmail.com> Date: Fri, 2 Feb 2024 16:23:34 +0900 Subject: [PATCH 30/31] Make top_retain correct --- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 649b58ac5fa0..ea189114b7ab 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1525,7 +1525,9 @@ impl Optimizer<'_> { // Respect top_retain if let Some(a_id) = a.id() { if a_id.0 == "arguments" - || (self.ctx.in_top_level() && self.options.top_retain.contains(&a_id.0)) + || (self.ctx.in_top_level() + && a_id.1 == self.marks.top_level_ctxt + && self.options.top_retain.contains(&a_id.0)) { return Ok(false); } From f3550f1d6e79da0214370d7fed13c7a1f3621a5c Mon Sep 17 00:00:00 2001 From: austaras <austaras@outlook.com> Date: Fri, 2 Feb 2024 16:22:35 +0800 Subject: [PATCH 31/31] fix --- crates/swc_ecma_minifier/src/compress/optimize/mod.rs | 4 ++-- crates/swc_ecma_minifier/src/compress/optimize/sequences.rs | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index fcac26e5d978..5a7704819749 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -31,7 +31,7 @@ use crate::{ maybe_par, mode::Mode, option::{CompressOptions, MangleOptions}, - program_data::ProgramData, + program_data::{ProgramData, VarUsageInfo}, util::{ contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt, }, @@ -323,7 +323,7 @@ impl From<&Function> for FnMetadata { impl Optimizer<'_> { fn may_remove_ident(&self, id: &Ident) -> bool { - if self.ctx.is_exported { + if let Some(VarUsageInfo { exported: true, .. }) = self.data.vars.get(&id.clone().to_id()) { return false; } diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index ea189114b7ab..eedfdd1050f1 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1525,9 +1525,8 @@ impl Optimizer<'_> { // Respect top_retain if let Some(a_id) = a.id() { if a_id.0 == "arguments" - || (self.ctx.in_top_level() - && a_id.1 == self.marks.top_level_ctxt - && self.options.top_retain.contains(&a_id.0)) + || (matches!(a, Mergable::Var(_) | Mergable::FnDecl(_)) + && !self.may_remove_ident(&Ident::from(a_id))) { return Ok(false); }