From 7aa7f21872a443cad6fe496b1a2f66e969b19f09 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 18 Mar 2019 21:24:42 +0800 Subject: [PATCH] fix corner case in `evaluate` (#3344) --- lib/compress.js | 8 +++---- test/compress/evaluate.js | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 6f1ffe74..707982d8 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -537,13 +537,11 @@ merge(Compressor.prototype, { var d = sym.definition(); var safe = safe_to_assign(tw, d, sym.scope, node.right); d.assignments++; - if (!safe) return; var fixed = d.fixed; if (!fixed && node.operator != "=") return; var eq = node.operator == "="; var value = eq ? node.right : node; if (is_modified(compressor, tw, node, value, 0)) return; - d.references.push(sym); if (!eq) d.chained = true; d.fixed = eq ? function() { return node.right; @@ -554,6 +552,8 @@ merge(Compressor.prototype, { right: node.right }); }; + if (!safe) return; + d.references.push(sym); mark(tw, d, false); node.right.walk(tw); mark(tw, d, true); @@ -783,10 +783,8 @@ merge(Compressor.prototype, { var d = exp.definition(); var safe = safe_to_assign(tw, d, exp.scope, true); d.assignments++; - if (!safe) return; var fixed = d.fixed; if (!fixed) return; - d.references.push(exp); d.chained = true; d.fixed = function() { return make_node(AST_Binary, node, { @@ -800,6 +798,8 @@ merge(Compressor.prototype, { }) }); }; + if (!safe) return; + d.references.push(exp); mark(tw, d, true); return true; }); diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index c1cb86cd..23785284 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -1610,3 +1610,47 @@ truthy_loops: { } } } + +if_increment: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + console.log(function(a) { + if (console) + return ++a; + }(0)); + } + expect: { + console.log(function(a) { + if (console) + return 1; + }()); + } + expect_stdout: "1" +} + +try_increment: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + console.log(function(a) { + try { + return ++a; + } catch (e) {} + }(0)); + } + expect: { + console.log(function(a) { + try { + return 1; + } catch (e) {} + }()); + } + expect_stdout: "1" +} -- 2.34.1