[Pkg-javascript-commits] [uglifyjs] 171/228: improve beautified output of switch blocks (#1721)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:27 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 fef0bf9ee0367f07dfbca26b144c2995c2b5db5f
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Wed Mar 29 04:40:05 2017 +0800

    improve beautified output of switch blocks (#1721)
---
 lib/output.js           | 24 ++++++++++++------------
 test/compress/switch.js | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/lib/output.js b/lib/output.js
index c0f1052..5c11088 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -960,24 +960,24 @@ function OutputStream(options) {
             self.expression.print(output);
         });
         output.space();
-        if (self.body.length > 0) output.with_block(function(){
-            self.body.forEach(function(stmt, i){
-                if (i) output.newline();
+        var last = self.body.length - 1;
+        if (last < 0) output.print("{}");
+        else output.with_block(function(){
+            self.body.forEach(function(branch, i){
                 output.indent(true);
-                stmt.print(output);
+                branch.print(output);
+                if (i < last && branch.body.length > 0)
+                    output.newline();
             });
         });
-        else output.print("{}");
     });
     AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output){
-        if (this.body.length > 0) {
+        output.newline();
+        this.body.forEach(function(stmt){
+            output.indent();
+            stmt.print(output);
             output.newline();
-            this.body.forEach(function(stmt){
-                output.indent();
-                stmt.print(output);
-                output.newline();
-            });
-        }
+        });
     });
     DEFPRINT(AST_Default, function(self, output){
         output.print("default:");
diff --git a/test/compress/switch.js b/test/compress/switch.js
index 5c12449..82d725f 100644
--- a/test/compress/switch.js
+++ b/test/compress/switch.js
@@ -680,3 +680,44 @@ issue_1705_3: {
     }
     expect_stdout: true
 }
+
+beautify: {
+    beautify = {
+        beautify: true,
+    }
+    input: {
+        switch (a) {
+          case 0:
+          case 1:
+            break;
+          case 2:
+          default:
+        }
+        switch (b) {
+          case 3:
+            foo();
+            bar();
+          default:
+            break;
+        }
+    }
+    expect_exact: [
+        "switch (a) {",
+        "  case 0:",
+        "  case 1:",
+        "    break;",
+        "",
+        "  case 2:",
+        "  default:",
+        "}",
+        "",
+        "switch (b) {",
+        "  case 3:",
+        "    foo();",
+        "    bar();",
+        "",
+        "  default:",
+        "    break;",
+        "}",
+    ]
+}

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