[Pkg-javascript-commits] [uglifyjs] 86/228: fix deep cloning of labels (#1565)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:19 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 8a8a94a596f57981e2cc65ac37921b154a9992b7
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Tue Mar 7 18:38:27 2017 +0800

    fix deep cloning of labels (#1565)
    
    `AST_Label.references` get `.initialize()` to `[]` every time after `.clone()`
    
    So walk down the tree to pick up the cloned `AST_LoopControl` pieces and put it back together.
---
 lib/ast.js                   | 19 ++++++++++++++++++-
 test/compress/reduce_vars.js | 29 +++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/lib/ast.js b/lib/ast.js
index a2125e7..f7ab52e 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -91,7 +91,7 @@ var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos
 }, null);
 
 var AST_Node = DEFNODE("Node", "start end", {
-    clone: function(deep) {
+    _clone: function(deep) {
         if (deep) {
             var self = this.clone();
             return self.transform(new TreeTransformer(function(node) {
@@ -102,6 +102,9 @@ var AST_Node = DEFNODE("Node", "start end", {
         }
         return new this.CTOR(this);
     },
+    clone: function(deep) {
+        return this._clone(deep);
+    },
     $documentation: "Base class of all AST nodes",
     $propdoc: {
         start: "[AST_Token] The first token of this node",
@@ -207,6 +210,20 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
             this.label._walk(visitor);
             this.body._walk(visitor);
         });
+    },
+    clone: function(deep) {
+        var node = this._clone(deep);
+        if (deep) {
+            var refs = node.label.references;
+            var label = this.label;
+            node.walk(new TreeWalker(function(node) {
+                if (node instanceof AST_LoopControl
+                    && node.label && node.label.thedef === label) {
+                    refs.push(node);
+                }
+            }));
+        }
+        return node;
     }
 }, AST_StatementWithBody);
 
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index a373de2..53e2815 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -1093,3 +1093,32 @@ func_modified: {
         }
     }
 }
+
+defun_label: {
+    options = {
+        passes: 2,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        !function() {
+            function f(a) {
+                L: {
+                    if (a) break L;
+                    return 1;
+                }
+            }
+            console.log(f(2));
+        }();
+    }
+    expect: {
+        !function() {
+            console.log(function(a) {
+                L: {
+                    if (a) break L;
+                    return 1;
+                }
+            }(2));
+        }();
+    }
+}

-- 
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