Re-add parens after new expression in beautify mode
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>
Sun, 12 Jun 2016 15:34:05 +0000 (17:34 +0200)
committerRichard van Velzen <rvanvelzen@experty.com>
Sun, 12 Jun 2016 18:03:48 +0000 (20:03 +0200)
lib/output.js
test/mocha/new.js

index 7ddee48..6eae2f1 100644 (file)
@@ -1287,7 +1287,9 @@ function OutputStream(options) {
     // self should be AST_New.  decide if we want to show parens or not.
     function need_constructor_parens(self, output) {
         // Always print parentheses with arguments
-        return self.args.length > 0;
+        if (self.args.length > 0) return true;
+
+        return output.option("beautify");
     };
 
     function best_of(a) {
index 8c0f24b..083b996 100644 (file)
@@ -2,7 +2,7 @@ var assert = require("assert");
 var uglify = require("../../");
 
 describe("New", function() {
-    it("Should attach callback parens after some tokens", function() {
+    it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
         var tests = [
             "new x(1);",
             "new x;",
@@ -19,17 +19,17 @@ describe("New", function() {
         ];
         var expected = [
             "new x(1);",
-            "new x;",
-            "new (new x);",
+            "new x();",
+            "new new x()();",
             "new function(foo) {\n    this.foo = foo;\n}(1);",
-            "new function(foo) {\n    this.foo = foo;\n};",
+            "new function(foo) {\n    this.foo = foo;\n}();",
             "new function test(foo) {\n    this.foo = foo;\n}(1);",
-            "new function test(foo) {\n    this.foo = foo;\n};",
-            "new true;",
-            "new 0;",
-            "new (!0);",
+            "new function test(foo) {\n    this.foo = foo;\n}();",
+            "new true();",
+            "new 0();",
+            "new (!0)();",
             "new (bar = function(foo) {\n    this.foo = foo;\n})(123);",
-            "new (bar = function(foo) {\n    this.foo = foo;\n});"
+            "new (bar = function(foo) {\n    this.foo = foo;\n})();"
         ];
         for (var i = 0; i < tests.length; i++) {
             assert.strictEqual(
@@ -43,4 +43,46 @@ describe("New", function() {
             );
         }
     });
+
+    it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() {
+        var tests = [
+            "new x(1);",
+            "new x;",
+            "new new x;",
+            "new (function(foo){this.foo=foo;})(1);",
+            "new (function(foo){this.foo=foo;})();",
+            "new (function test(foo){this.foo=foo;})(1);",
+            "new (function test(foo){this.foo=foo;})();",
+            "new true;",
+            "new (0);",
+            "new (!0);",
+            "new (bar = function(foo) {this.foo=foo;})(123);",
+            "new (bar = function(foo) {this.foo=foo;})();"
+        ];
+        var expected = [
+            "new x(1);",
+            "new x;",
+            "new(new x);",
+            "new function(foo){this.foo=foo}(1);",
+            "new function(foo){this.foo=foo};",
+            "new function test(foo){this.foo=foo}(1);",
+            "new function test(foo){this.foo=foo};",
+            "new true;",
+            "new 0;",
+            "new(!0);",
+            "new(bar=function(foo){this.foo=foo})(123);",
+            "new(bar=function(foo){this.foo=foo});"
+        ];
+        for (var i = 0; i < tests.length; i++) {
+            assert.strictEqual(
+                uglify.minify(tests[i], {
+                    fromString: true,
+                    output: {beautify: false},
+                    compress: false,
+                    mangle: false
+                }).code,
+                expected[i]
+            );
+        }
+    });
 });
\ No newline at end of file