enhance `dead_code` (#5130)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 21 Sep 2021 19:11:45 +0000 (20:11 +0100)
committerGitHub <noreply@github.com>
Tue, 21 Sep 2021 19:11:45 +0000 (03:11 +0800)
lib/compress.js
test/compress/labels.js

index 99d9b9c..399bfdc 100644 (file)
@@ -3331,14 +3331,11 @@ merge(Compressor.prototype, {
                 var stat = statements[i];
                 if (stat instanceof AST_LoopControl) {
                     var lct = compressor.loopcontrol_target(stat);
-                    if (stat instanceof AST_Break
-                            && !(lct instanceof AST_IterationStatement)
-                            && loop_body(lct) === self
-                        || stat instanceof AST_Continue
-                            && loop_body(lct) === self) {
-                        if (stat.label) remove(stat.label.thedef.references, stat);
-                    } else {
+                    if (loop_body(lct) !== self
+                        || stat instanceof AST_Break && lct instanceof AST_IterationStatement) {
                         statements[n++] = stat;
+                    } else if (stat.label) {
+                        remove(stat.label.thedef.references, stat);
                     }
                 } else {
                     statements[n++] = stat;
@@ -5485,6 +5482,21 @@ merge(Compressor.prototype, {
         return compressor.option("unused") && self.label.references.length == 0 ? self.body : self;
     });
 
+    OPT(AST_LoopControl, function(self, compressor) {
+        if (!compressor.option("dead_code")) return self;
+        var label = self.label;
+        if (label) {
+            var lct = compressor.loopcontrol_target(self);
+            self.label = null;
+            if (compressor.loopcontrol_target(self) === lct) {
+                remove(label.thedef.references, self);
+            } else {
+                self.label = label;
+            }
+        }
+        return self;
+    });
+
     OPT(AST_Block, function(self, compressor) {
         self.body = tighten_body(self.body, compressor);
         return self;
index 6b7c5a3..4bf3264 100644 (file)
@@ -83,8 +83,9 @@ labels_5: {
         conditionals: true,
         dead_code: true,
         if_return: true,
+        unused: true,
     }
-    // should keep the break-s in the following
+    // should keep `break`s below
     input: {
         while (foo) {
             if (bar) break;
@@ -100,8 +101,8 @@ labels_5: {
             if (bar) break;
             console.log("foo");
         }
-        out: while (foo) {
-            if (bar) break out;
+        while (foo) {
+            if (bar) break;
             console.log("foo");
         }
     }
@@ -189,23 +190,22 @@ labels_10: {
         conditionals: true,
         dead_code: true,
         if_return: true,
+        unused: true,
     }
     input: {
-        out: while (foo) {
-            x();
-            y();
+        out: while (42) {
+            console.log("PASS");
             break out;
-            z();
-            k();
+            console.log("FAIL");
         }
     };
     expect: {
-        out: while (foo) {
-            x();
-            y();
-            break out;
+        while (42) {
+            console.log("PASS");
+            break;
         }
     }
+    expect_stdout: "PASS"
 }
 
 issue_4466_1: {