fix corner case in evaluate (#3904)

fixes #3903
This commit is contained in:
Alex Lam S.L
2020-05-17 15:25:13 +01:00
committed by GitHub
parent a00f8dade7
commit 48b62393a4
2 changed files with 31 additions and 1 deletions

View File

@@ -3571,6 +3571,7 @@ merge(Compressor.prototype, {
var args = eval_args(this.args); var args = eval_args(this.args);
if (!args && !ignore_side_effects) return this; if (!args && !ignore_side_effects) return this;
if (!stat.value) return; if (!stat.value) return;
var cached_args = [];
if (args && !all(fn.argnames, function(sym, i) { if (args && !all(fn.argnames, function(sym, i) {
var value = args[i]; var value = args[i];
var def = sym.definition(); var def = sym.definition();
@@ -3579,13 +3580,16 @@ merge(Compressor.prototype, {
node._eval = function() { node._eval = function() {
return value; return value;
}; };
cached.push(node); cached_args.push(node);
}); });
return true; return true;
}) && !ignore_side_effects) return this; }) && !ignore_side_effects) return this;
fn.evaluating = true; fn.evaluating = true;
var val = stat.value._eval(compressor, ignore_side_effects, cached, depth); var val = stat.value._eval(compressor, ignore_side_effects, cached, depth);
delete fn.evaluating; delete fn.evaluating;
cached_args.forEach(function(node) {
delete node._eval;
});
if (val === stat.value) return this; if (val === stat.value) return this;
return val; return val;
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) { } else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {

View File

@@ -2418,3 +2418,29 @@ issue_3887: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_3903: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = "PASS";
function f(b, c) {
return console, c;
}
var d = f(f(), a = a);
console.log(d);
}
expect: {
var a = "PASS";
function f(b, c) {
return console, c;
}
var d = f(f(), a = a);
console.log(d);
}
expect_stdout: "PASS"
}