Repository: jlongster/prettier (js)

Issue:
Ternary formatting with `await` changes behavior
```
(async function() {
  console.log(
    await (true ? Promise.resolve('A') : Promise.resolve('B'))
  );
})()
```

The expectation in this code is that the ternary will evaluate to a Promise, which is then awaited. Prettier removes the paren, though, so instead of awaiting a valid Promise it awaits `true`.

The output should be `"A"`, but instead its `Promise {}`

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22(async%20function()%20%7B%5Cn%20%20console.log(%5Cn%20%20%20%20await%20(true%20%3F%20Promise.resolve('A')%20%3A%20Promise.resolve('B'))%5Cn%20%20)%3B%5Cn%7D)()%5Cn%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%22jsxBracketSameLine%22%3Afalse%2C%22doc%22%3Afalse%7D%7D

Pull request description:
Fix await ternary parenthesis
This should fix #739.

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index 84afe6be3..ef6164a30 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -419,6 +419,7 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "LogicalExpression":
         case "NewExpression":
         case "ExportDefaultDeclaration":
+        case "AwaitExpression":
           return true;
 
         case "CallExpression":


Test patch:
diff --git a/tests/async/__snapshots__/jsfmt.spec.js.snap b/tests/async/__snapshots__/jsfmt.spec.js.snap
index 208c7fca7..4496f3b53 100644
--- a/tests/async/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/async/__snapshots__/jsfmt.spec.js.snap
@@ -18,9 +18,19 @@ exports[`test conditional-expression.js 1`] = `
 "async function f() {
   const result = typeof fn === \'function\' ? await fn() : null;
 }
+
+(async function() {
+  console.log(
+    await (true ? Promise.resolve(\"A\") : Promise.resolve(\"B\"))
+  );
+})()
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 async function f() {
   const result = typeof fn === \"function\" ? await fn() : null;
 }
+
+(async function() {
+  console.log(await (true ? Promise.resolve(\"A\") : Promise.resolve(\"B\")));
+})();
 "
 `;
diff --git a/tests/async/conditional-expression.js b/tests/async/conditional-expression.js
index 532c66d6a..73d846394 100644
--- a/tests/async/conditional-expression.js
+++ b/tests/async/conditional-expression.js
@@ -1,3 +1,9 @@
 async function f() {
   const result = typeof fn === 'function' ? await fn() : null;
 }
+
+(async function() {
+  console.log(
+    await (true ? Promise.resolve("A") : Promise.resolve("B"))
+  );
+})()