From fb035617996c416fd68d571fe351848d0e5aa506 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 2 May 2021 00:28:31 +0100 Subject: [PATCH] fix corner case in `hoist_vars` (#4894) fixes #4893 --- lib/compress.js | 3 +- test/compress/hoist_vars.js | 78 +++++++++++++++++++++++++++++++++++++ test/compress/pure_funcs.js | 12 +++--- test/compress/templates.js | 2 +- 4 files changed, 87 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 39392c9737..b02a116357 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1793,7 +1793,7 @@ merge(Compressor.prototype, { return make_node(AST_Assign, candidate, { operator: "=", left: make_node(AST_SymbolRef, candidate.name, candidate.name), - right: candidate.value + right: candidate.value, }); } var assign = candidate; @@ -8433,6 +8433,7 @@ merge(Compressor.prototype, { if (value) { if (value instanceof AST_Sequence) value = value.clone(); var name = make_node(AST_SymbolRef, defn.name, defn.name); + name.fixed = value; a.push(make_node(AST_Assign, defn, { operator: "=", left: name, diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index 4ca2ce5b5b..e3972b8a6b 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -295,3 +295,81 @@ issue_4859: { } expect_stdout: "Infinity" } + +issue_4893_1: { + options = { + collapse_vars: true, + evaluate: true, + hoist_vars: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f() { + function g() {} + var a = null; + var b = null; + var c = null; + b.p += a = 42; + f; + } + try { + f(); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try{ + (function f() { + var b; + b = null; + b.p += 42; + f; + })(); + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" +} + +issue_4893_2: { + options = { + collapse_vars: true, + hoist_vars: true, + pure_getters: "strict", + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f() { + function g() {} + var a = null; + var b = null; + var c = null; + b.p += a = 42; + f; + } + try { + f(); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try{ + (function() { + var b; + b = null; + b.p += 42; + })(); + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" +} diff --git a/test/compress/pure_funcs.js b/test/compress/pure_funcs.js index 98768423f2..bf38003c13 100644 --- a/test/compress/pure_funcs.js +++ b/test/compress/pure_funcs.js @@ -133,7 +133,7 @@ conditional: { relational: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { foo() in new foo(); @@ -158,7 +158,7 @@ relational: { arithmetic: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { foo() + foo(); @@ -183,7 +183,7 @@ arithmetic: { boolean_and: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { foo() && foo(); @@ -208,7 +208,7 @@ boolean_and: { boolean_or: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { foo() || foo(); @@ -233,7 +233,7 @@ boolean_or: { assign: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { var a; @@ -256,7 +256,7 @@ assign: { unary: { options = { pure_funcs: [ "foo" ], - side_effects :true, + side_effects: true, } input: { typeof foo(); diff --git a/test/compress/templates.js b/test/compress/templates.js index a27c6b8b4d..e46f3c0616 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -343,7 +343,7 @@ issue_4676: { reduce_vars: true, templates: true, toplevel: true, - unsafe:true, + unsafe: true, unused: true, } input: {