Re-add parens after new expression in beautify mode

This commit is contained in:
Anthony Van de Gejuchte
2016-06-12 17:34:05 +02:00
committed by Richard van Velzen
parent bb9c9707aa
commit 5c4cfaa0a7
2 changed files with 55 additions and 11 deletions

View File

@@ -1287,7 +1287,9 @@ function OutputStream(options) {
// self should be AST_New. decide if we want to show parens or not. // self should be AST_New. decide if we want to show parens or not.
function need_constructor_parens(self, output) { function need_constructor_parens(self, output) {
// Always print parentheses with arguments // 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) { function best_of(a) {

View File

@@ -2,7 +2,49 @@ var assert = require("assert");
var uglify = require("../../"); var uglify = require("../../");
describe("New", function() { 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 = [ var tests = [
"new x(1);", "new x(1);",
"new x;", "new x;",
@@ -20,22 +62,22 @@ describe("New", function() {
var expected = [ var expected = [
"new x(1);", "new x(1);",
"new x;", "new x;",
"new (new x);", "new(new x);",
"new function(foo) {\n this.foo = foo;\n}(1);", "new function(foo){this.foo=foo}(1);",
"new function(foo) {\n this.foo = foo;\n};", "new function(foo){this.foo=foo};",
"new function test(foo) {\n this.foo = foo;\n}(1);", "new function test(foo){this.foo=foo}(1);",
"new function test(foo) {\n this.foo = foo;\n};", "new function test(foo){this.foo=foo};",
"new true;", "new true;",
"new 0;", "new 0;",
"new (!0);", "new(!0);",
"new (bar = function(foo) {\n this.foo = foo;\n})(123);", "new(bar=function(foo){this.foo=foo})(123);",
"new (bar = function(foo) {\n this.foo = foo;\n});" "new(bar=function(foo){this.foo=foo});"
]; ];
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i], { uglify.minify(tests[i], {
fromString: true, fromString: true,
output: {beautify: true}, output: {beautify: false},
compress: false, compress: false,
mangle: false mangle: false
}).code, }).code,