From: Alex Lam S.L Date: Wed, 13 Mar 2019 15:48:52 +0000 (+0800) Subject: fix corner case in `collapse_vars` (#3333) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=d074aa6e27de77bc60e69b933f90725931c8d5df;p=UglifyJS.git fix corner case in `collapse_vars` (#3333) fixes #3247 fixes #3305 fixes #3314 fixes #3327 --- diff --git a/lib/compress.js b/lib/compress.js index e6446fec..2ccf330d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1631,7 +1631,7 @@ merge(Compressor.prototype, { function symbol_in_lvalues(sym, parent) { var lvalue = lvalues[sym.name]; if (!lvalue) return; - if (lvalue !== lhs) return !(parent instanceof AST_Call); + if (lvalue !== lhs) return !(parent instanceof AST_Call && parent.expression === sym); scan_rhs = false; } diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index d1c26c68..9873e482 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -6007,3 +6007,117 @@ issue_3238_6: { } expect_stdout: "true false" } + +issue_3247: { + options = { + collapse_vars: true, + } + input: { + function f(o) { + console.log(o.p); + } + var a; + a = Object({ p: "PASS" }); + a.q = true; + f(a, true); + } + expect: { + function f(o) { + console.log(o.p); + } + var a; + (a = Object({ p: "PASS" })).q = true; + f(a, true); + } + expect_stdout: "PASS" +} + +issue_3305: { + options = { + collapse_vars: true, + conditionals: true, + sequences: true, + } + input: { + function calc(a) { + var x, w; + if (a) { + x = a; + w = 1; + } else { + x = 1; + w = 0; + } + return add(x, w); + } + function add(x, w) { + return x + w; + } + console.log(calc(41)); + } + expect: { + function calc(a) { + var x, w; + return w = a ? (x = a, 1) : (x = 1, 0), add(x, w); + } + function add(x, w) { + return x + w; + } + console.log(calc(41)); + } + expect_stdout: "42" +} + +issue_3314: { + options = { + collapse_vars: true, + } + input: { + function test(a, b) { + console.log(a, b); + } + var a = "FAIL", b; + b = a = "PASS"; + test(a, b); + } + expect: { + function test(a, b) { + console.log(a, b); + } + var a = "FAIL", b; + b = a = "PASS"; + test(a, b); + } + expect_stdout: "PASS PASS" +} + +issue_3327: { + options = { + collapse_vars: true, + conditionals: true, + sequences: true, + } + input: { + var a, b, l = ["PASS", 42]; + if (l.length === 1) { + a = l[0].a; + b = l[0].b; + } else { + a = l[0]; + b = l[1]; + } + function echo(a, b) { + console.log(a, b); + } + echo(a, b); + } + expect: { + var a, b, l = ["PASS", 42]; + function echo(a, b) { + console.log(a, b); + } + b = 1 === l.length ? (a = l[0].a, l[0].b) : (a = l[0], l[1]), + echo(a,b); + } + expect_stdout: "PASS 42" +}