fix corner case in collapse_vars (#3591)

This commit is contained in:
Alex Lam S.L
2019-11-17 05:24:02 +08:00
committed by GitHub
parent a6a0319f1c
commit 10c1a78772
4 changed files with 38 additions and 9 deletions

View File

@@ -1691,13 +1691,17 @@ merge(Compressor.prototype, {
if (expr instanceof AST_Unary) return false; if (expr instanceof AST_Unary) return false;
if (side_effects) return false; if (side_effects) return false;
if (value_def) return true; if (value_def) return true;
if (lhs instanceof AST_SymbolRef) { if (!(lhs instanceof AST_SymbolRef)) return false;
var def = lhs.definition(); var referenced;
if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) { if (expr instanceof AST_VarDef) {
return true; 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) { function symbol_in_lvalues(sym, parent) {

View File

@@ -6238,7 +6238,7 @@ issue_3439_2: {
expect_stdout: "number" expect_stdout: "number"
} }
cond_sequence_return: { cond_sequence_return_1: {
options = { options = {
collapse_vars: true, collapse_vars: true,
} }
@@ -6259,6 +6259,27 @@ cond_sequence_return: {
expect_stdout: "2" 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: { issue_3520: {
options = { options = {
collapse_vars: true, collapse_vars: true,

View File

@@ -12,7 +12,9 @@ describe("bin/uglifyjs", function() {
it("Should produce a functional build when using --self", function(done) { it("Should produce a functional build when using --self", function(done) {
this.timeout(30000); this.timeout(30000);
var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS'; var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS';
exec(command, function(err, stdout) { exec(command, {
maxBuffer: 1048576
}, function(err, stdout) {
if (err) throw err; if (err) throw err;
eval(stdout); eval(stdout);
assert.strictEqual(typeof WrappedUglifyJS, "object"); assert.strictEqual(typeof WrappedUglifyJS, "object");

View File

@@ -10,7 +10,9 @@ describe("spidermonkey export/import sanity test", function() {
var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " + var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " +
uglifyjs + " -p spidermonkey -cm"; uglifyjs + " -p spidermonkey -cm";
exec(command, function(err, stdout) { exec(command, {
maxBuffer: 1048576
}, function(err, stdout) {
if (err) throw err; if (err) throw err;
eval(stdout); eval(stdout);