diff options
-rw-r--r-- | lib/output.js | 14 | ||||
-rw-r--r-- | test/compress/new.js | 32 | ||||
-rw-r--r-- | test/mocha/new.js | 16 |
3 files changed, 51 insertions, 11 deletions
diff --git a/lib/output.js b/lib/output.js index 907818e0..7ddee484 100644 --- a/lib/output.js +++ b/lib/output.js @@ -594,7 +594,7 @@ function OutputStream(options) { PARENS(AST_New, function(output){ var p = output.parent(); - if (no_constructor_parens(this, output) + if (!need_constructor_parens(this, output) && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]() || p instanceof AST_Call && p.expression === this)) // (new foo)(bar) return true; @@ -995,7 +995,7 @@ function OutputStream(options) { /* -----[ other expressions ]----- */ DEFPRINT(AST_Call, function(self, output){ self.expression.print(output); - if (self instanceof AST_New && no_constructor_parens(self, output)) + if (self instanceof AST_New && !need_constructor_parens(self, output)) return; output.with_parens(function(){ self.args.forEach(function(expr, i){ @@ -1285,13 +1285,9 @@ function OutputStream(options) { }; // self should be AST_New. decide if we want to show parens or not. - function no_constructor_parens(self, output) { - return self.args.length == 0 && !output.option("beautify") || - !(self.expression instanceof AST_SymbolRef || - self.expression instanceof AST_Call || - self.expression instanceof AST_Function || - self.expression instanceof AST_Assign - ); + function need_constructor_parens(self, output) { + // Always print parentheses with arguments + return self.args.length > 0; }; function best_of(a) { diff --git a/test/compress/new.js b/test/compress/new.js index d956ae27..78a1026e 100644 --- a/test/compress/new.js +++ b/test/compress/new.js @@ -11,6 +11,30 @@ new_statement: { expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);" } +new_statements_2: { + input: { + new x; + new new x; + new new new x; + new true; + new (0); + new (!0); + new (bar = function(foo) {this.foo=foo;})(123); + new (bar = function(foo) {this.foo=foo;})(); + } + expect_exact: "new x;new(new x);new(new(new x));new true;new 0;new(!0);new(bar=function(foo){this.foo=foo})(123);new(bar=function(foo){this.foo=foo});" +} + +new_statements_3: { + input: { + 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;})(); + } + expect_exact: "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_with_rewritten_true_value: { options = { booleans: true } input: { @@ -18,3 +42,11 @@ new_with_rewritten_true_value: { } expect_exact: "new(!0);" } + +new_with_many_parameters: { + input: { + new foo.bar("baz"); + new x(/123/, 456); + } + expect_exact: 'new foo.bar("baz");new x(/123/,456);' +} diff --git a/test/mocha/new.js b/test/mocha/new.js index 7e7aea7d..8c0f24bc 100644 --- a/test/mocha/new.js +++ b/test/mocha/new.js @@ -5,19 +5,31 @@ describe("New", function() { it("Should attach callback parens after some tokens", 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;})(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})(123);", + "new (bar = function(foo) {\n this.foo = foo;\n});" ]; for (var i = 0; i < tests.length; i++) { assert.strictEqual( |