[Pkg-javascript-commits] [uglifyjs] 140/190: Hoist functions when reversing if (x) return; ... vs. if (!x) ...

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Aug 7 23:17:20 UTC 2016


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

terceiro pushed a commit to annotated tag upstream/2.7.0
in repository uglifyjs.

commit 4fe630431c37ffb81466959e9ea9797d6d2de21a
Author: Richard van Velzen <rvanvelzen at experty.com>
Date:   Sat Apr 23 23:48:33 2016 +0200

    Hoist functions when reversing if (x) return; ... vs. if (!x) ...
    
    Fixes #1052
---
 lib/compress.js             | 18 ++++++++++++++++--
 test/compress/issue-1052.js | 27 +++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 53618ae..2bcfcf3 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -578,11 +578,13 @@ merge(Compressor.prototype, {
                             CHANGED = true;
                             stat = stat.clone();
                             stat.condition = stat.condition.negate(compressor);
+                            var body = as_statement_array(stat.alternative).concat(ret);
+                            var funs = extract_functions_from_statement_array(body);
                             stat.body = make_node(AST_BlockStatement, stat, {
-                                body: as_statement_array(stat.alternative).concat(ret)
+                                body: body
                             });
                             stat.alternative = null;
-                            ret = [ stat.transform(compressor) ];
+                            ret = funs.concat([ stat.transform(compressor) ]);
                             continue loop;
                         }
                         //---
@@ -840,6 +842,18 @@ merge(Compressor.prototype, {
 
     };
 
+    function extract_functions_from_statement_array(statements) {
+        var funs = [];
+        for (var i = statements.length - 1; i >= 0; --i) {
+            var stat = statements[i];
+            if (stat instanceof AST_Defun) {
+                statements.splice(i, 1);
+                funs.unshift(stat);
+            }
+        }
+        return funs;
+    }
+
     function extract_declarations_from_unreachable_code(compressor, stat, target) {
         if (!(stat instanceof AST_Defun)) {
             compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
diff --git a/test/compress/issue-1052.js b/test/compress/issue-1052.js
new file mode 100644
index 0000000..067eea4
--- /dev/null
+++ b/test/compress/issue-1052.js
@@ -0,0 +1,27 @@
+hoist_funs_when_handling_if_return_rerversal: {
+    options = { if_return: true, hoist_funs: false };
+    input: {
+        "use strict";
+
+        ( function() {
+            if ( !window ) {
+                return;
+            }
+
+            function f() {}
+            function g() {}
+        } )();
+    }
+    expect: {
+        "use strict";
+
+        ( function() {
+            function f() {}
+            function g() {}
+
+            // NOTE: other compression steps will reduce this
+            // down to just `window`.
+            if ( window );
+        } )();
+    }
+}

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