diff --git a/src/printer.js b/src/printer.js index ba028f26f..96097485c 100644 --- a/src/printer.js +++ b/src/printer.js @@ -801,16 +801,21 @@ function genericPrintNoParens(path, options, print, args) { parts.push(semi); return concat(parts); + case "NewExpression": case "CallExpression": { + const isNew = n.type === "NewExpression"; if ( // We want to keep require calls as a unit - (n.callee.type === "Identifier" && n.callee.name === "require") || + (!isNew && + n.callee.type === "Identifier" && + n.callee.name === "require") || // Template literals as single arguments (n.arguments.length === 1 && isTemplateOnItsOwnLine(n.arguments[0], options.originalText)) || // Keep test declarations on a single line // e.g. `it('long name', () => {` - (n.callee.type === "Identifier" && + (!isNew && + n.callee.type === "Identifier" && (n.callee.name === "it" || n.callee.name === "test" || n.callee.name === "describe") && @@ -824,6 +829,7 @@ function genericPrintNoParens(path, options, print, args) { n.arguments[1].params.length <= 1) ) { return concat([ + isNew ? "new " : "", path.call(print, "callee"), path.call(print, "typeParameters"), concat(["(", join(", ", path.map(print, "arguments")), ")"]) @@ -832,11 +838,12 @@ function genericPrintNoParens(path, options, print, args) { // We detect calls on member lookups and possibly print them in a // special chain format. See `printMemberChain` for more info. - if (n.callee.type === "MemberExpression") { + if (!isNew && n.callee.type === "MemberExpression") { return printMemberChain(path, options, print); } return concat([ + isNew ? "new " : "", path.call(print, "callee"), printFunctionTypeParameters(path, options, print), printArgumentsList(path, options, print) @@ -1216,18 +1223,6 @@ function genericPrintNoParens(path, options, print, args) { ]) ); } - case "NewExpression": - parts.push( - "new ", - path.call(print, "callee"), - printFunctionTypeParameters(path, options, print) - ); - - if (n.arguments) { - parts.push(printArgumentsList(path, options, print)); - } - - return concat(parts); case "VariableDeclaration": { const printed = path.map(childPath => { return print(childPath); diff --git a/tests/template/__snapshots__/jsfmt.spec.js.snap b/tests/template/__snapshots__/jsfmt.spec.js.snap index 34df98f8c..cbcf31d1b 100644 --- a/tests/template/__snapshots__/jsfmt.spec.js.snap +++ b/tests/template/__snapshots__/jsfmt.spec.js.snap @@ -27,6 +27,11 @@ insertRule(\`*, *:before, *:after { insertRule\`*, *:before, *:after { box-sizing: inherit; }\`; + +new Error(formatErrorMessage\` + This a really bad error. + Which has more than one line. +\`); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ insertRule(\`*, *:before, *:after { box-sizing: inherit; @@ -36,6 +41,11 @@ insertRule\`*, *:before, *:after { box-sizing: inherit; }\`; +new Error(formatErrorMessage\` + This a really bad error. + Which has more than one line. +\`); + `; exports[`comment.js 1`] = ` diff --git a/tests/template/call.js b/tests/template/call.js index 2f728a10e..154c26e71 100644 --- a/tests/template/call.js +++ b/tests/template/call.js @@ -5,3 +5,8 @@ insertRule(`*, *:before, *:after { insertRule`*, *:before, *:after { box-sizing: inherit; }`; + +new Error(formatErrorMessage` + This a really bad error. + Which has more than one line. +`);