fix corner case in `merge_vars` (#4254)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 1 Nov 2020 02:37:21 +0000 (02:37 +0000)
committerGitHub <noreply@github.com>
Sun, 1 Nov 2020 02:37:21 +0000 (10:37 +0800)
fixes #4253

lib/compress.js
test/compress/merge_vars.js

index e8c8801..8eb3619 100644 (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);
         }
index 6ada929..b61b486 100644 (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"
+}