[Pkg-javascript-commits] [uglifyjs] 31/228: improve `--beautify bracketize` reduce whitespaces from if-else statements

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:14 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 09f9ae2de9fb2a328e088ab82ad1e49731c971fb
Author: alexlamsl <alexlamsl at gmail.com>
Date:   Sat Feb 18 19:30:33 2017 +0800

    improve `--beautify bracketize`
    reduce whitespaces from if-else statements
    
    fixes #1482
    closes #1483
---
 lib/output.js                       |  5 ++-
 test/input/issue-1482/bracketize.js | 73 +++++++++++++++++++++++++++++++++++++
 test/input/issue-1482/default.js    | 17 +++++++++
 test/input/issue-1482/input.js      | 12 ++++++
 test/mocha/cli.js                   | 21 +++++++++++
 5 files changed, 127 insertions(+), 1 deletion(-)

diff --git a/lib/output.js b/lib/output.js
index b6f0a87..4f576c9 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -938,7 +938,10 @@ function OutputStream(options) {
             output.space();
             output.print("else");
             output.space();
-            force_statement(self.alternative, output);
+            if (self.alternative instanceof AST_If)
+                self.alternative.print(output);
+            else
+                force_statement(self.alternative, output);
         } else {
             self._do_print_body(output);
         }
diff --git a/test/input/issue-1482/bracketize.js b/test/input/issue-1482/bracketize.js
new file mode 100644
index 0000000..2c2b103
--- /dev/null
+++ b/test/input/issue-1482/bracketize.js
@@ -0,0 +1,73 @@
+if (x) {
+    foo();
+}
+
+if (x) {
+    foo();
+} else {
+    baz();
+}
+
+if (x) {
+    foo();
+} else if (y) {
+    bar();
+} else {
+    baz();
+}
+
+if (x) {
+    if (y) {
+        foo();
+    } else {
+        bar();
+    }
+} else {
+    baz();
+}
+
+if (x) {
+    foo();
+} else if (y) {
+    bar();
+} else if (z) {
+    baz();
+} else {
+    moo();
+}
+
+function f() {
+    if (x) {
+        foo();
+    }
+    if (x) {
+        foo();
+    } else {
+        baz();
+    }
+    if (x) {
+        foo();
+    } else if (y) {
+        bar();
+    } else {
+        baz();
+    }
+    if (x) {
+        if (y) {
+            foo();
+        } else {
+            bar();
+        }
+    } else {
+        baz();
+    }
+    if (x) {
+        foo();
+    } else if (y) {
+        bar();
+    } else if (z) {
+        baz();
+    } else {
+        moo();
+    }
+}
diff --git a/test/input/issue-1482/default.js b/test/input/issue-1482/default.js
new file mode 100644
index 0000000..14054e9
--- /dev/null
+++ b/test/input/issue-1482/default.js
@@ -0,0 +1,17 @@
+if (x) foo();
+
+if (x) foo(); else baz();
+
+if (x) foo(); else if (y) bar(); else baz();
+
+if (x) if (y) foo(); else bar(); else baz();
+
+if (x) foo(); else if (y) bar(); else if (z) baz(); else moo();
+
+function f() {
+    if (x) foo();
+    if (x) foo(); else baz();
+    if (x) foo(); else if (y) bar(); else baz();
+    if (x) if (y) foo(); else bar(); else baz();
+    if (x) foo(); else if (y) bar(); else if (z) baz(); else moo();
+}
diff --git a/test/input/issue-1482/input.js b/test/input/issue-1482/input.js
new file mode 100644
index 0000000..0186e82
--- /dev/null
+++ b/test/input/issue-1482/input.js
@@ -0,0 +1,12 @@
+if (x) foo();
+if (x) foo(); else baz();
+if (x) foo(); else if (y) bar(); else baz();
+if (x) if (y) foo(); else bar(); else baz();
+if (x) foo(); else if (y) bar(); else if (z) baz(); else moo();
+function f() {
+if (x) foo();
+if (x) foo(); else baz();
+if (x) foo(); else if (y) bar(); else baz();
+if (x) if (y) foo(); else bar(); else baz();
+if (x) foo(); else if (y) bar(); else if (z) baz(); else moo();
+}
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index 64599c5..450df1f 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -1,5 +1,6 @@
 var assert = require("assert");
 var exec = require("child_process").exec;
+var readFileSync = require("fs").readFileSync;
 
 describe("bin/uglifyjs", function () {
     var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
@@ -130,4 +131,24 @@ describe("bin/uglifyjs", function () {
            done();
        });
     });
+    it("Should work with `--beautify`", function (done) {
+       var command = uglifyjscmd + ' test/input/issue-1482/input.js -b';
+
+       exec(command, function (err, stdout) {
+           if (err) throw err;
+
+           assert.strictEqual(stdout, readFileSync("test/input/issue-1482/default.js", "utf8"));
+           done();
+       });
+    });
+    it("Should work with `--beautify bracketize`", function (done) {
+       var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize';
+
+       exec(command, function (err, stdout) {
+           if (err) throw err;
+
+           assert.strictEqual(stdout, readFileSync("test/input/issue-1482/bracketize.js", "utf8"));
+           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