Repository: jlongster/prettier (js)

Issue:
Missing parenthesis for class expressions left of a member expression
```js
(class {})[1];
(class {}).a;
```

outputs

```js
class {}[1];
class {}.a;
```

which is invalid

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22(class%20%7B%7D)%5B1%5D%3B%5Cn(class%20%7B%7D).a%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Afalse%2C%22doc%22%3Afalse%7D%7D

Pull request description:
Add parenthesis for class expression on left of member expression
Fixes #414

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index a531a96fe..44800b3c4 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -392,6 +392,7 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "LogicalExpression":
         case "LogicalExpression":
         case "NewExpression":
+        case "ExportDefaultDeclaration":
           return true;
 
         case "CallExpression":
@@ -449,6 +450,8 @@ FPp.needsParens = function(assumeExpressionContext) {
           if (parent.callee === node) {
             return true;
           }
+        case "MemberExpression":
+          return name === "object" && parent.object === node;
       }
 
       return false;


Test patch:
diff --git a/tests/classes/__snapshots__/jsfmt.spec.js.snap b/tests/classes/__snapshots__/jsfmt.spec.js.snap
index 0a7f5ef2d..c3208a09b 100644
--- a/tests/classes/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/classes/__snapshots__/jsfmt.spec.js.snap
@@ -264,6 +264,15 @@ class Foo {} // error, shadows type Foo
 "
 `;
 
+exports[`test member.js 1`] = `
+"(class {})[1];
+(class {}).a;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+(class {})[1];
+(class {}).a;
+"
+`;
+
 exports[`test statics.js 1`] = `
 "/* @flow */
 
diff --git a/tests/classes/member.js b/tests/classes/member.js
new file mode 100644
index 000000000..128236eba
--- /dev/null
+++ b/tests/classes/member.js
@@ -0,0 +1,2 @@
+(class {})[1];
+(class {}).a;
diff --git a/tests/export_default/__snapshots__/jsfmt.spec.js.snap b/tests/export_default/__snapshots__/jsfmt.spec.js.snap
index 7a5c5da54..ac0cace2d 100644
--- a/tests/export_default/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/export_default/__snapshots__/jsfmt.spec.js.snap
@@ -5,6 +5,13 @@ module.exports = require(\"M\");
 "
 `;
 
+exports[`test body.js 1`] = `
+"export default (class {}[1] = 1);
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export default (class {}[1] = 1);
+"
+`;
+
 exports[`test test.js 1`] = `
 "var M = require(\'M\');
 var N = require(\'N\');
diff --git a/tests/export_default/body.js b/tests/export_default/body.js
new file mode 100644
index 000000000..7a2f3c475
--- /dev/null
+++ b/tests/export_default/body.js
@@ -0,0 +1,1 @@
+export default (class {}[1] = 1);