fix corner case in reduce_vars (#3845)

fixes #3844
This commit is contained in:
Alex Lam S.L
2020-05-03 20:30:10 +01:00
committed by GitHub
parent ac429dc8e1
commit ffa1943177
2 changed files with 36 additions and 12 deletions

View File

@@ -716,6 +716,14 @@ merge(Compressor.prototype, {
fn.inlined = false; fn.inlined = false;
var iife; var iife;
if (!fn.name && (iife = tw.parent()) instanceof AST_Call && iife.expression === fn) { if (!fn.name && (iife = tw.parent()) instanceof AST_Call && iife.expression === fn) {
var hit = false;
var aborts = false;
fn.walk(new TreeWalker(function(node) {
if (hit) return aborts = true;
if (node instanceof AST_Return) return hit = true;
if (node instanceof AST_Scope && node !== fn) return true;
}));
if (aborts) push(tw);
reset_variables(tw, compressor, fn); reset_variables(tw, compressor, fn);
// Virtually turn IIFE parameters into variable definitions: // Virtually turn IIFE parameters into variable definitions:
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})() // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
@@ -735,22 +743,11 @@ merge(Compressor.prototype, {
d.fixed = false; d.fixed = false;
} }
}); });
var has_return = false;
var visit = tw.visit;
tw.visit = function(node, descend) {
var ret = visit.call(tw, node, descend);
if (!has_return && node instanceof AST_Return && tw.find_parent(AST_Scope) === fn) {
has_return = true;
push(tw);
}
return ret;
};
descend(); descend();
tw.visit = visit;
var safe_ids = tw.safe_ids; var safe_ids = tw.safe_ids;
pop(tw); pop(tw);
walk_defuns(tw, fn); walk_defuns(tw, fn);
if (!has_return) tw.safe_ids = safe_ids; if (!aborts) tw.safe_ids = safe_ids;
} else { } else {
push(tw); push(tw);
reset_variables(tw, compressor, fn); reset_variables(tw, compressor, fn);

View File

@@ -7006,3 +7006,30 @@ flatten_iife: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_3844: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
console.log(function() {
if (!console) switch (A = 0) {
case console.log("FAIL"):
return;
}
return typeof A;
}());
}
expect: {
console.log(function() {
if (!console) switch (A = 0) {
case console.log("FAIL"):
return;
}
return typeof A;
}());
}
expect_stdout: "undefined"
}