From b3ef5e514dd0cefeba926401d2538907a8712b99 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 15 Mar 2019 02:48:23 +0800 Subject: [PATCH] enhance `evaluate` (#3339) fixes #3299 --- lib/compress.js | 24 +++++++++++++++--------- test/compress/collapse_vars.js | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 2a4f0df8..b5d155ea 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2727,19 +2727,25 @@ merge(Compressor.prototype, { return typeof function(){}; } if (!non_converting_unary[this.operator]) depth++; - e = e._eval(compressor, cached, depth); - if (e === this.expression) return this; + var v = e._eval(compressor, cached, depth); + if (v === this.expression) return this; switch (this.operator) { - case "!": return !e; + case "!": return !v; case "typeof": // typeof returns "object" or "function" on different platforms // so cannot evaluate reliably - if (e instanceof RegExp) return this; - return typeof e; - case "void": return void e; - case "~": return ~e; - case "-": return -e; - case "+": return +e; + if (v instanceof RegExp) return this; + return typeof v; + case "void": return void v; + case "~": return ~v; + case "-": return -v; + case "+": return +v; + case "++": + case "--": + if (e instanceof AST_SymbolRef) { + var refs = e.definition().references; + if (refs[refs.length - 1] === e) return v; + } } return this; }); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 9873e482..6c867123 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -947,7 +947,7 @@ collapse_vars_misc1: { function f8() { var b = window.a * window.z; return b + (b + 5) } function f9() { var b = window.a * window.z; return bar() || b } function f10(x) { var a = 5; return a += 3; } - function f11(x) { var a = 5, b = 3; return a += --b; } + function f11(x) { var a = 5; return a += 2; } } } -- 2.34.1