[Pkg-javascript-commits] [uglifyjs] 86/491: enhance `if_return` to handle `return void...` (#1977)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:24 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 58fae7dc070449e650d1a48ad7144cb5571a510f
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sat May 20 15:58:46 2017 +0800

    enhance `if_return` to handle `return void...` (#1977)
    
    fixes #512
---
 lib/compress.js            | 19 ++++++++++---------
 test/compress/if_return.js | 22 ++++++++++++++++++++++
 test/mocha/cli.js          |  4 +++-
 test/mocha/spidermonkey.js |  4 +++-
 4 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index f6bf3d0..73ab21f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -914,12 +914,12 @@ merge(Compressor.prototype, {
                     continue loop;
                   case stat instanceof AST_If:
                     if (stat.body instanceof AST_Return) {
+                        var value = stat.body.value;
                         //---
                         // pretty silly case, but:
                         // if (foo()) return; return; ==> foo(); return;
-                        if (((in_lambda && ret.length == 0)
-                             || (ret[0] instanceof AST_Return && !ret[0].value))
-                            && !stat.body.value && !stat.alternative) {
+                        if ((in_lambda && ret.length == 0 || ret[0] instanceof AST_Return && !ret[0].value)
+                            && !value && !stat.alternative) {
                             CHANGED = true;
                             var cond = make_node(AST_SimpleStatement, stat.condition, {
                                 body: stat.condition
@@ -929,7 +929,7 @@ merge(Compressor.prototype, {
                         }
                         //---
                         // if (foo()) return x; return y; ==> return foo() ? x : y;
-                        if (ret[0] instanceof AST_Return && stat.body.value && ret[0].value && !stat.alternative) {
+                        if (ret[0] instanceof AST_Return && value && ret[0].value && !stat.alternative) {
                             CHANGED = true;
                             stat = stat.clone();
                             stat.alternative = ret[0];
@@ -939,7 +939,7 @@ merge(Compressor.prototype, {
                         //---
                         // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
                         if (multiple_if_returns && (ret.length == 0 || ret[0] instanceof AST_Return)
-                            && stat.body.value && !stat.alternative && in_lambda) {
+                            && value && !stat.alternative && in_lambda) {
                             CHANGED = true;
                             stat = stat.clone();
                             stat.alternative = ret[0] || make_node(AST_Return, stat, {
@@ -949,8 +949,8 @@ merge(Compressor.prototype, {
                             continue loop;
                         }
                         //---
-                        // if (foo()) return; [ else x... ]; y... ==> if (!foo()) { x...; y... }
-                        if (!stat.body.value && in_lambda) {
+                        // if (foo()) return [ void bar() ]; [ else x...; ] y... ==> if (!foo()) { x...; y... } else bar();
+                        if (in_lambda && (!value || value instanceof AST_UnaryPrefix && value.operator == "void")) {
                             CHANGED = true;
                             stat = stat.clone();
                             stat.condition = stat.condition.negate(compressor);
@@ -959,11 +959,12 @@ merge(Compressor.prototype, {
                             stat.body = make_node(AST_BlockStatement, stat, {
                                 body: body
                             });
-                            stat.alternative = null;
+                            stat.alternative = value ? make_node(AST_SimpleStatement, value, {
+                                body: value.expression
+                            }) : null;
                             ret = funs.concat([ stat.transform(compressor) ]);
                             continue loop;
                         }
-
                         //---
                         // if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e;
                         //
diff --git a/test/compress/if_return.js b/test/compress/if_return.js
index 0ac45c3..c09d67b 100644
--- a/test/compress/if_return.js
+++ b/test/compress/if_return.js
@@ -302,3 +302,25 @@ issue_1437_conditionals: {
         }
     }
 }
+
+issue_512: {
+    options = {
+        conditionals: true,
+        if_return: true,
+    }
+    input: {
+        function a() {
+            if (b()) {
+                c();
+                return;
+            }
+            throw e;
+        }
+    }
+    expect: {
+        function a() {
+            if (!b()) throw e;
+            c();
+        }
+    }
+}
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index 335b224..db4a2c3 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -19,7 +19,9 @@ describe("bin/uglifyjs", function () {
             eval(stdout);
 
             assert.strictEqual(typeof WrappedUglifyJS, 'object');
-            assert.strictEqual(WrappedUglifyJS.minify("foo([true,,2+3]);").code, "foo([!0,,5]);");
+            var result = WrappedUglifyJS.minify("foo([true,,2+3]);");
+            assert.strictEqual(result.error, undefined);
+            assert.strictEqual(result.code, "foo([!0,,5]);");
 
             done();
         });
diff --git a/test/mocha/spidermonkey.js b/test/mocha/spidermonkey.js
index a8a112d..9bddb53 100644
--- a/test/mocha/spidermonkey.js
+++ b/test/mocha/spidermonkey.js
@@ -15,7 +15,9 @@ describe("spidermonkey export/import sanity test", function() {
 
             eval(stdout);
             assert.strictEqual(typeof SpiderUglify, "object");
-            assert.strictEqual(SpiderUglify.minify("foo([true,,2+3]);").code, "foo([!0,,5]);");
+            var result = SpiderUglify.minify("foo([true,,2+3]);");
+            assert.strictEqual(result.error, undefined);
+            assert.strictEqual(result.code, "foo([!0,,5]);");
 
             done();
         });

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