Repository: jlongster/prettier (js)

Issue:
Missing parenthesis for typeof and arrow functions
```js
typeof (() => {});
```

outputs

```
typeof () => {};
```

which is invalid

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22typeof%20(()%20%3D%3E%20%7B%7D)%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 description:
Fix missing parenthesis for typeof and arrow functions
Fixes #242

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index af4392cb6..dd4b2b55e 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -371,11 +371,14 @@ FPp.needsParens = function(assumeExpressionContext) {
         return true;
       }
 
-      if (parent.type === "MemberExpression") {
-        return true;
-      }
+      switch (parent.type) {
+        case "MemberExpression":
+        case "UnaryExpression":
+          return true;
 
-      return isBinary(parent);
+        default:
+          return isBinary(parent);
+      }
 
     case "ClassExpression":
       return parent.type === "ExpressionStatement";


Test patch:
diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap
index 728c2470a..bf224ddd7 100644
--- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap
@@ -27,9 +27,12 @@ var ident = <T>(x: T): T => x;
 
 exports[`test arrow_function_expression.js 1`] = `
 "(a => {}).length
+typeof (() => {});
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 (a => {
 }).length;
+typeof (() => {
+});
 "
 `;
 
diff --git a/tests/arrows/arrow_function_expression.js b/tests/arrows/arrow_function_expression.js
index ecea2142f..57a6e9209 100644
--- a/tests/arrows/arrow_function_expression.js
+++ b/tests/arrows/arrow_function_expression.js
@@ -1,1 +1,2 @@
 (a => {}).length
+typeof (() => {});