From: Alex Lam S.L Date: Sat, 16 Nov 2019 21:24:02 +0000 (+0800) Subject: fix corner case in `collapse_vars` (#3591) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=10c1a78772c1d6ca1bc7f78f0fa1f2ab50b07ae2;p=UglifyJS.git fix corner case in `collapse_vars` (#3591) --- diff --git a/lib/compress.js b/lib/compress.js index e85cf5d3..801cbaea 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1691,13 +1691,17 @@ merge(Compressor.prototype, { if (expr instanceof AST_Unary) return false; if (side_effects) return false; if (value_def) return true; - if (lhs instanceof AST_SymbolRef) { - var def = lhs.definition(); - if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) { - return true; - } + if (!(lhs instanceof AST_SymbolRef)) return false; + var referenced; + if (expr instanceof AST_VarDef) { + referenced = 1; + } else if (expr.operator == "=") { + referenced = 2; + } else { + return false; } - return false; + var def = lhs.definition(); + return def.references.length - def.replaced == referenced; } function symbol_in_lvalues(sym, parent) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 2e82cc9d..6ad02b21 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -6238,7 +6238,7 @@ issue_3439_2: { expect_stdout: "number" } -cond_sequence_return: { +cond_sequence_return_1: { options = { collapse_vars: true, } @@ -6259,6 +6259,27 @@ cond_sequence_return: { expect_stdout: "2" } +cond_sequence_return_2: { + options = { + collapse_vars: true, + } + input: { + console.log(function(n) { + var c = 0; + for (var k in [0, 1]) + if (c += 1, k == n) return c; + }(1)); + } + expect: { + console.log(function(n) { + var c = 0; + for (var k in [0, 1]) + if (c += 1, k == n) return c; + }(1)); + } + expect_stdout: "2" +} + issue_3520: { options = { collapse_vars: true, diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 7a484487..dd181769 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -12,7 +12,9 @@ describe("bin/uglifyjs", function() { it("Should produce a functional build when using --self", function(done) { this.timeout(30000); var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS'; - exec(command, function(err, stdout) { + exec(command, { + maxBuffer: 1048576 + }, function(err, stdout) { if (err) throw err; eval(stdout); assert.strictEqual(typeof WrappedUglifyJS, "object"); diff --git a/test/mocha/spidermonkey.js b/test/mocha/spidermonkey.js index 196f1447..32351d03 100644 --- a/test/mocha/spidermonkey.js +++ b/test/mocha/spidermonkey.js @@ -10,7 +10,9 @@ describe("spidermonkey export/import sanity test", function() { var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " + uglifyjs + " -p spidermonkey -cm"; - exec(command, function(err, stdout) { + exec(command, { + maxBuffer: 1048576 + }, function(err, stdout) { if (err) throw err; eval(stdout);