From 88d40e8ab899976c123b5b5f74f0ad02ec9d7868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 4 Jan 2023 13:31:12 +0900 Subject: [PATCH] fix(es/minifier): Fix usage counter to fix infinite loop (#6744) **Description:** We skip non-computed property names while checking if we can inline an expression. **Related issue:** - Closes https://github.com/swc-project/swc/issues/6729. --- .../fixture/issues-6xxx/6729/input/.swcrc | 19 +++++++++++++++ .../fixture/issues-6xxx/6729/input/index.js | 13 ++++++++++ .../fixture/issues-6xxx/6729/output/index.js | 5 ++++ .../src/compress/optimize/sequences.rs | 24 ++++++++++++------- crates/swc_ecma_minifier/tests/exec.rs | 22 +++++++++++++++++ 5 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-6xxx/6729/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-6xxx/6729/input/index.js create mode 100644 crates/swc/tests/fixture/issues-6xxx/6729/output/index.js diff --git a/crates/swc/tests/fixture/issues-6xxx/6729/input/.swcrc b/crates/swc/tests/fixture/issues-6xxx/6729/input/.swcrc new file mode 100644 index 00000000000..578baada553 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6729/input/.swcrc @@ -0,0 +1,19 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true, + "tsx": false + }, + "target": "es2020", + "loose": false, + "minify": { + "compress": true, + "mangle": false + } + }, + "module": { + "type": "es6" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-6xxx/6729/input/index.js b/crates/swc/tests/fixture/issues-6xxx/6729/input/index.js new file mode 100644 index 00000000000..157ca8510bf --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6729/input/index.js @@ -0,0 +1,13 @@ +export async function foo() { + if (undefined_var_1) { + let replace; + + if (undefined_var_2) { + replace = 1; + } else { + replace = 2; + } + + await a({ replace }) + } +} diff --git a/crates/swc/tests/fixture/issues-6xxx/6729/output/index.js b/crates/swc/tests/fixture/issues-6xxx/6729/output/index.js new file mode 100644 index 00000000000..baa943f7394 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6729/output/index.js @@ -0,0 +1,5 @@ +export async function foo() { + undefined_var_1 && await a({ + replace: undefined_var_2 ? 1 : 2 + }); +} diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index d24c59c6d53..ca9b6db5e42 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -2444,15 +2444,6 @@ impl Visit for UsageCounter<'_> { } } - fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) { - if let SuperProp::Computed(c) = &e.prop { - let old = self.in_lhs; - self.in_lhs = false; - c.expr.visit_with(self); - self.in_lhs = old; - } - } - fn visit_pat(&mut self, p: &Pat) { let old = self.in_lhs; self.in_lhs = true; @@ -2466,6 +2457,21 @@ impl Visit for UsageCounter<'_> { p.visit_children_with(self); self.in_lhs = old; } + + fn visit_prop_name(&mut self, p: &PropName) { + if let PropName::Computed(p) = p { + p.visit_with(self) + } + } + + fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) { + if let SuperProp::Computed(c) = &e.prop { + let old = self.in_lhs; + self.in_lhs = false; + c.expr.visit_with(self); + self.in_lhs = old; + } + } } #[derive(Debug)] diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index c609814dd32..75c6e10a194 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -10407,3 +10407,25 @@ fn issue_6641() { "###, ) } + +#[test] +fn issue_6728() { + run_default_exec_test( + r###" + async function foo() { + if (undefined_var_1) { + let replace; + + if (undefined_var_2) { + replace = 1; + } else { + replace = 2; + } + + await a({ replace }) + } + } + console.log('PASS') + "###, + ) +}