Skip to content

Commit

Permalink
fix corner case in collapse_vars (#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Nov 16, 2019
1 parent a6a0319 commit 10c1a78
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
16 changes: 10 additions & 6 deletions lib/compress.js
Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -6238,7 +6238,7 @@ issue_3439_2: {
expect_stdout: "number"
}

cond_sequence_return: {
cond_sequence_return_1: {
options = {
collapse_vars: true,
}
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion test/mocha/cli.js
Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion test/mocha/spidermonkey.js
Expand Up @@ -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);
Expand Down

0 comments on commit 10c1a78

Please sign in to comment.