diff --git a/lib/output.js b/lib/output.js index 7ddee484..6eae2f1f 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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) { diff --git a/test/mocha/new.js b/test/mocha/new.js index 8c0f24bc..083b9964 100644 --- a/test/mocha/new.js +++ b/test/mocha/new.js @@ -2,7 +2,49 @@ 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;", + "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) {\n this.foo = foo;\n}(1);", + "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 (bar = function(foo) {\n this.foo = foo;\n})(123);", + "new (bar = function(foo) {\n this.foo = foo;\n})();" + ]; + for (var i = 0; i < tests.length; i++) { + assert.strictEqual( + uglify.minify(tests[i], { + fromString: true, + output: {beautify: true}, + compress: false, + mangle: false + }).code, + expected[i] + ); + } + }); + + it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() { var tests = [ "new x(1);", "new x;", @@ -20,22 +62,22 @@ describe("New", function() { var expected = [ "new x(1);", "new x;", - "new (new x);", - "new function(foo) {\n this.foo = foo;\n}(1);", - "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(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) {\n this.foo = foo;\n})(123);", - "new (bar = function(foo) {\n this.foo = foo;\n});" + "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: true}, + output: {beautify: false}, compress: false, mangle: false }).code,