[Pkg-javascript-commits] [uglifyjs] 290/491: extend escape analysis on constant expression properties (#2509)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:45 UTC 2018


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

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 3b28b915ebad2d9452c0e8649033ab4813eabca4
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Nov 24 14:07:39 2017 +0800

    extend escape analysis on constant expression properties (#2509)
    
    fixes #2508
---
 lib/compress.js              |  16 +++---
 test/compress/hoist_props.js | 133 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+), 7 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index d8a0bd7..744a1ea 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -333,7 +333,7 @@ merge(Compressor.prototype, {
                             }
                         }
                     }
-                    mark_escaped(d, node, value, 0);
+                    mark_escaped(d, node.scope, node, value, 0);
                 }
                 if (node instanceof AST_SymbolCatch) {
                     node.definition().fixed = false;
@@ -623,10 +623,12 @@ merge(Compressor.prototype, {
             }
         }
 
-        function mark_escaped(d, node, value, level) {
+        function mark_escaped(d, scope, node, value, level) {
             var parent = tw.parent(level);
-            if (value instanceof AST_Constant) return;
-            if (level > 0 && value instanceof AST_Function) return;
+            if (value) {
+                if (value.is_constant()) return;
+                if (level > 0 && value.is_constant_expression(scope)) return;
+            }
             if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
                 || parent instanceof AST_Call && node !== parent.expression
                 || parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
@@ -634,13 +636,13 @@ merge(Compressor.prototype, {
                 d.escaped = true;
                 return;
             } else if (parent instanceof AST_Array) {
-                mark_escaped(d, parent, parent, level + 1);
+                mark_escaped(d, scope, parent, parent, level + 1);
             } else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
                 var obj = tw.parent(level + 1);
-                mark_escaped(d, obj, obj, level + 2);
+                mark_escaped(d, scope, obj, obj, level + 2);
             } else if (parent instanceof AST_PropAccess && node === parent.expression) {
                 value = read_property(value, parent.property);
-                mark_escaped(d, parent, value, level + 1);
+                mark_escaped(d, scope, parent, value, level + 1);
                 if (value) return;
             }
             if (level == 0) d.direct_access = true;
diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js
index 40d36ac..b2dd027 100644
--- a/test/compress/hoist_props.js
+++ b/test/compress/hoist_props.js
@@ -500,3 +500,136 @@ issue_2473_4: {
     }
     expect_stdout: "1 2"
 }
+
+issue_2508_1: {
+    options = {
+        collapse_vars: true,
+        hoist_props: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = {
+            a: [ 1 ],
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect: {
+        (function(x) {
+            console.log(x);
+        })([ 1 ]);
+    }
+    expect_stdout: true
+}
+
+issue_2508_2: {
+    options = {
+        collapse_vars: true,
+        hoist_props: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = {
+            a: { b: 2 },
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect: {
+        (function(x) {
+            console.log(x);
+        })({ b: 2 });
+    }
+    expect_stdout: true
+}
+
+issue_2508_3: {
+    options = {
+        collapse_vars: true,
+        hoist_props: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = {
+            a: [ o ],
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect: {
+        var o = {
+            a: [ o ],
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect_stdout: true
+}
+
+issue_2508_4: {
+    options = {
+        collapse_vars: true,
+        hoist_props: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = {
+            a: { b: o },
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect: {
+        var o = {
+            a: { b: o },
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.a);
+    }
+    expect_stdout: true
+}
+
+issue_2508_5: {
+    options = {
+        collapse_vars: true,
+        hoist_props: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = {
+            f: function(x) {
+                console.log(x);
+            }
+        };
+        o.f(o.f);
+    }
+    expect: {
+        var o_f = function(x) {
+            console.log(x);
+        };
+        o_f(o_f);
+    }
+    expect_stdout: true
+}

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