[Pkg-javascript-commits] [uglifyjs] 311/491: improve `if_return` (#2558)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:48 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 3dd495ecdd231a6b245ab002f61805b21114924b
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Dec 7 01:01:52 2017 +0800

    improve `if_return` (#2558)
    
    `return void x()` => `x()`
---
 lib/compress.js                   | 30 +++++++++++++++++++++++-------
 test/compress/return_undefined.js | 22 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 914fdd0..3f0414e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1211,10 +1211,19 @@ merge(Compressor.prototype, {
                 var stat = statements[i];
                 var next = statements[i + 1];
 
-                if (in_lambda && stat instanceof AST_Return && !stat.value && !next) {
-                    CHANGED = true;
-                    statements.length--;
-                    continue;
+                if (in_lambda && !next && stat instanceof AST_Return) {
+                    if (!stat.value) {
+                        CHANGED = true;
+                        statements.length--;
+                        continue;
+                    }
+                    if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") {
+                        CHANGED = true;
+                        statements[i] = make_node(AST_SimpleStatement, stat, {
+                            body: stat.value.expression
+                        });
+                        continue;
+                    }
                 }
 
                 if (stat instanceof AST_If) {
@@ -1301,9 +1310,16 @@ merge(Compressor.prototype, {
                         && prev instanceof AST_If && prev.body instanceof AST_Return
                         && i + 2 == statements.length && next instanceof AST_SimpleStatement) {
                         CHANGED = true;
-                        statements.push(make_node(AST_Return, next, {
-                            value: null
-                        }).transform(compressor));
+                        stat = stat.clone();
+                        stat.alternative = make_node(AST_BlockStatement, next, {
+                            body: [
+                                next,
+                                make_node(AST_Return, next, {
+                                    value: null
+                                })
+                            ]
+                        });
+                        statements.splice(i, 2, stat.transform(compressor));
                         continue;
                     }
                 }
diff --git a/test/compress/return_undefined.js b/test/compress/return_undefined.js
index 9662aa5..4d2b425 100644
--- a/test/compress/return_undefined.js
+++ b/test/compress/return_undefined.js
@@ -122,3 +122,25 @@ return_undefined: {
         }
     }
 }
+
+return_void: {
+    options = {
+        if_return: true,
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            function g() {
+                h();
+            }
+            return g();
+        }
+    }
+    expect: {
+        function f() {
+            h();
+        }
+    }
+}

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