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;
/* -----[ 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){
};
// 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) {
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: {
}
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);'
+}
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(