Repository: jlongster/prettier (js)

Issue:
Missing parentheses around weird arrow function body
Input: (using an object as a template literal tagger)

```js
() => ({}`a`)
```

Output:

```js
(() => {}`a`);
```

... which is invalid JS.

Expected:

```js
(() => ({}`a`));
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22()%20%3D%3E%20(%7B%7D%60a%60)%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%2C%22doc%22%3Afalse%7D%7D

Found using the fuzzer.

Pull request description:
Fix parenthesis in object as left-hand-side of template
Fixes #392

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index 74e994d07..1d34e7421 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -405,6 +405,7 @@ FPp.needsParens = function(assumeExpressionContext) {
 
     case "ClassExpression":
       switch (parent.type) {
+        case "BinaryExpression":
         case "ExportDefaultDeclaration":
         case "ExpressionStatement":
           return true;
@@ -416,6 +417,9 @@ FPp.needsParens = function(assumeExpressionContext) {
       if (parent.type === "ArrowFunctionExpression" && name === "body") {
         return true;
       }
+      if (parent.type === "TaggedTemplateExpression") {
+        return true;
+      }
 
     default:
       if (


Test patch:
diff --git a/tests/classes/__snapshots__/jsfmt.spec.js.snap b/tests/classes/__snapshots__/jsfmt.spec.js.snap
index 4644dbbd2..d2de13bf1 100644
--- a/tests/classes/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/classes/__snapshots__/jsfmt.spec.js.snap
@@ -74,6 +74,19 @@ new E().x;
 "
 `;
 
+exports[`test binary.js 1`] = `
+"(class {}) + 1;
+(class a {}) + 1;
+(class extends b {}) + 1;
+(class a extends b {}) + 1;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+(class {}) + 1;
+(class a {}) + 1;
+(class extends b {}) + 1;
+(class a extends b {}) + 1;
+"
+`;
+
 exports[`test class_shapes.js 1`] = `
 "/* @flow */
 
diff --git a/tests/classes/binary.js b/tests/classes/binary.js
new file mode 100644
index 000000000..f60f9c8f7
--- /dev/null
+++ b/tests/classes/binary.js
@@ -0,0 +1,4 @@
+(class {}) + 1;
+(class a {}) + 1;
+(class extends b {}) + 1;
+(class a extends b {}) + 1;
diff --git a/tests/objects/__snapshots__/jsfmt.spec.js.snap b/tests/objects/__snapshots__/jsfmt.spec.js.snap
index 74d07ce15..c3fa28ebb 100644
--- a/tests/objects/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/objects/__snapshots__/jsfmt.spec.js.snap
@@ -43,6 +43,15 @@ var z = Object(123);
 "
 `;
 
+exports[`test expression.js 1`] = `
+"() => ({}\`\`);
+({})\`\`;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+(() => ({})\`\`);
+({})\`\`;
+"
+`;
+
 exports[`test objects.js 1`] = `
 "/* @flow */
 
diff --git a/tests/objects/expression.js b/tests/objects/expression.js
new file mode 100644
index 000000000..fd92a69bf
--- /dev/null
+++ b/tests/objects/expression.js
@@ -0,0 +1,2 @@
+() => ({}``);
+({})``;