Repository: jlongster/prettier (js)

Issue:
Missing parentheses around class expressions used with operators
Input:

```js
(class extends b {}) + 1
```

Output:

```js
class extends b {} + 1;
```

... which is invalid JavaScript.

Expected:

```js
(class extends b {}) + 1;
```

Or:

```js
(class extends b {} + 1);
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22(class%20extends%20b%20%7B%7D)%20%2B%201%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

---

Input:

```js
(class a extends b {}) + 1
```

Output:

```js
class a extends b {} + 1;
```

... which means:

```js
class a extends b {}
+1;
```

Expected:

```js
(class a extends b {}) + 1;
```

Or:

```js
(class a extends b {} + 1);
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22(class%20a%20extends%20b%20%7B%7D)%20%2B%201%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 class inside of binary expression missing parenthesis
Fixes #395

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index 74e994d07..990d7ed0c 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;


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;