[Pkg-javascript-commits] [uglifyjs] 03/50: fix `unused` on labeled for-loop (#1831)

Jonas Smedegaard dr at jones.dk
Thu Aug 17 23:06:43 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit d0faa471db8ad75ec2bddda062272c7ad0d394e6
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Apr 20 04:18:38 2017 +0800

    fix `unused` on labeled for-loop (#1831)
    
    fixes #1830
---
 lib/compress.js              | 34 ++++++++++++++++++++--------------
 test/compress/drop-unused.js | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 613c686..135dde1 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2070,26 +2070,32 @@ merge(Compressor.prototype, {
                             return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
                         }
                     }
+                    // certain combination of unused name + side effect leads to:
+                    //    https://github.com/mishoo/UglifyJS2/issues/44
+                    //    https://github.com/mishoo/UglifyJS2/issues/1830
+                    // that's an invalid AST.
+                    // We fix it at this stage by moving the `var` outside the `for`.
                     if (node instanceof AST_For) {
                         descend(node, this);
-
                         if (node.init instanceof AST_BlockStatement) {
-                            // certain combination of unused name + side effect leads to:
-                            //    https://github.com/mishoo/UglifyJS2/issues/44
-                            // that's an invalid AST.
-                            // We fix it at this stage by moving the `var` outside the `for`.
-
-                            var body = node.init.body.slice(0, -1);
-                            node.init = node.init.body.slice(-1)[0].body;
-                            body.push(node);
-
-                            return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
-                                body: body
-                            });
+                            var block = node.init;
+                            node.init = block.body.pop();
+                            block.body.push(node);
+                            return in_list ? MAP.splice(block.body) : block;
                         } else if (is_empty(node.init)) {
                             node.init = null;
-                            return node;
                         }
+                        return node;
+                    }
+                    if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
+                        descend(node, this);
+                        if (node.body instanceof AST_BlockStatement) {
+                            var block = node.body;
+                            node.body = block.body.pop();
+                            block.body.push(node);
+                            return in_list ? MAP.splice(block.body) : block;
+                        }
+                        return node;
                     }
                     if (node instanceof AST_Scope && node !== self)
                         return node;
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index 99d9cac..9d37dc7 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -1029,3 +1029,38 @@ delete_assign_2: {
     }
     expect_stdout: true
 }
+
+issue_1830_1: {
+    options = {
+        unused: true,
+    }
+    input: {
+        !function() {
+            L: for (var b = console.log(1); !1;) continue L;
+        }();
+    }
+    expect: {
+        !function() {
+            L: for (console.log(1); !1;) continue L;
+        }();
+    }
+    expect_stdout: "1"
+}
+
+issue_1830_2: {
+    options = {
+        unused: true,
+    }
+    input: {
+        !function() {
+            L: for (var a = 1, b = console.log(a); --a;) continue L;
+        }();
+    }
+    expect: {
+        !function() {
+            var a = 1;
+            L: for (console.log(a); --a;) continue L;
+        }();
+    }
+    expect_stdout: "1"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list