Repository: jlongster/prettier (js)

Issue:
Missing parenthesis for new with an arrow function
```js
new (() => {});
```

outputs

```js
new () => {};
```

which is invalid. I'm not sure if this can even result into a meaningful result, but it parses in Chrome at least.

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22new%20(()%20%3D%3E%20%7B%7D)%3B%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%7D%7D

Pull request:
Ensure parens on NewExpression with function callee
Fixes #258

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index 94b2fcf44..ca3e73e76 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -378,6 +378,9 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "UnaryExpression":
           return true;
 
+        case "NewExpression":
+          return name === "callee";
+
         default:
           return isBinary(parent);
       }


Test patch:
diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap
index 4858de59f..424b07289 100644
--- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap
@@ -32,6 +32,7 @@ typeof (() => {});
 export default (() => {})();
 (() => {})()\`\`;
 (() => {})\`\`;
+new (() => {});
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 (a => {
 }).length;
@@ -43,6 +44,8 @@ export default (() => {
 })()\`\`;
 (() => {
 })\`\`;
+new (() => {
+})();
 "
 `;
 
diff --git a/tests/arrows/arrow_function_expression.js b/tests/arrows/arrow_function_expression.js
index dac425060..9b579fe04 100644
--- a/tests/arrows/arrow_function_expression.js
+++ b/tests/arrows/arrow_function_expression.js
@@ -3,3 +3,4 @@ typeof (() => {});
 export default (() => {})();
 (() => {})()``;
 (() => {})``;
+new (() => {});
diff --git a/tests/async/__snapshots__/jsfmt.spec.js.snap b/tests/async/__snapshots__/jsfmt.spec.js.snap
index ab791989f..d4b1a9e02 100644
--- a/tests/async/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/async/__snapshots__/jsfmt.spec.js.snap
@@ -192,8 +192,8 @@ var e = async function() {
 var et = async function<T>(a: T) {
 };
 
-var n = new async function() {
-}();
+var n = new (async function() {
+})();
 
 var o = {
   async m() {
@@ -516,9 +516,9 @@ var et = async function<T>(a: T) {
   await 1;
 };
 
-var n = new async function() {
+var n = new (async function() {
   await 1;
-}();
+})();
 
 var o = {
   async m() {
diff --git a/tests/function/__snapshots__/jsfmt.spec.js.snap b/tests/function/__snapshots__/jsfmt.spec.js.snap
index 8c581f246..8ad3ba76d 100644
--- a/tests/function/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/function/__snapshots__/jsfmt.spec.js.snap
@@ -369,6 +369,7 @@ typeof (function() {});
 export default (function() {})();
 (function() {})()\`\`;
 (function() {})\`\`;
+new (function() {});
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 (function() {
 }).length;
@@ -380,6 +381,8 @@ export default (function() {
 })()\`\`;
 (function() {
 })\`\`;
+new (function() {
+})();
 "
 `;
 
diff --git a/tests/function/function_expression.js b/tests/function/function_expression.js
index 93c6da8ec..e680a3d1a 100644
--- a/tests/function/function_expression.js
+++ b/tests/function/function_expression.js
@@ -3,3 +3,4 @@ typeof (function() {});
 export default (function() {})();
 (function() {})()``;
 (function() {})``;
+new (function() {});