fix corner case in merge_vars (#4254)

fixes #4253
This commit is contained in:
Alex Lam S.L
2020-11-01 02:37:21 +00:00
committed by GitHub
parent d8563caba7
commit cbf7269296
2 changed files with 56 additions and 15 deletions

View File

@@ -4469,6 +4469,11 @@ merge(Compressor.prototype, {
pop();
return true;
}
if (node instanceof AST_Break) {
var target = tw.loopcontrol_target(node);
if (!(target instanceof AST_IterationStatement)) insert(target);
return true;
}
if (node instanceof AST_Conditional) {
node.condition.walk(tw);
push();
@@ -4488,20 +4493,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Continue) {
var target = tw.loopcontrol_target(node);
if (!(target instanceof AST_Do)) return true;
var stack = [];
while (!HOP(segment, "block") || segment.block !== target) {
stack.push(segment);
pop();
}
segment.loop = "c";
push();
while (stack.length) {
var seg = stack.pop();
push();
if (HOP(seg, "block")) segment.block = seg.block;
if (HOP(seg, "loop")) segment.loop = seg.loop;
}
if (target instanceof AST_Do) insert(target);
return true;
}
if (node instanceof AST_Do) {
@@ -4550,7 +4542,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_LabeledStatement) {
push();
segment.block = node;
segment.block = node.body;
node.body.walk(tw);
pop();
return true;
@@ -4587,6 +4579,7 @@ merge(Compressor.prototype, {
segment = save;
node.body.forEach(function(branch) {
push();
segment.block = node;
walk_body(branch, tw);
pop();
});
@@ -4742,6 +4735,22 @@ merge(Compressor.prototype, {
});
}
function insert(target) {
var stack = [];
while (!HOP(segment, "block") || segment.block !== target) {
stack.push(segment);
pop();
}
segment.loop = "c";
push();
while (stack.length) {
var seg = stack.pop();
push();
if (HOP(seg, "block")) segment.block = seg.block;
if (HOP(seg, "loop")) segment.loop = seg.loop;
}
}
function must_visit(base, segment) {
return base === segment || base.isPrototypeOf(segment);
}

View File

@@ -3092,3 +3092,35 @@ issue_4237_2: {
}
expect_stdout: "PASS"
}
issue_4253: {
options = {
merge_vars: true,
toplevel: true,
}
input: {
switch (0) {
default:
var a = "FAIL";
a = a && a;
try {
break;
} catch (e) {}
var b = 42;
}
console.log(b);
}
expect: {
switch (0) {
default:
var a = "FAIL";
a = a && a;
try {
break;
} catch (e) {}
var b = 42;
}
console.log(b);
}
expect_stdout: "undefined"
}