Repository: jlongster/prettier
Pull request #404 (2017-01-22 23:43:32)

Fix various parenthesis issues on the left side of template


Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index bd288e3e0..0220b0ac2 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -241,6 +241,7 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "MemberExpression":
           return name === "object" && parent.object === node;
 
+        case "TaggedTemplateExpression":
         case "CallExpression":
         case "NewExpression":
           return true;
@@ -272,6 +273,7 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "NewExpression":
           return name === "callee" && parent.callee === node;
 
+        case "TaggedTemplateExpression":
         case "UnaryExpression":
         case "SpreadElement":
         case "SpreadProperty":
@@ -324,6 +326,7 @@ FPp.needsParens = function(assumeExpressionContext) {
     case "AwaitExpression":
     case "YieldExpression":
       switch (parent.type) {
+        case "TaggedTemplateExpression":
         case "BinaryExpression":
         case "LogicalExpression":
         case "UnaryExpression":
@@ -366,11 +369,13 @@ FPp.needsParens = function(assumeExpressionContext) {
     case "AssignmentExpression":
     case "ConditionalExpression":
       switch (parent.type) {
+        case "TaggedTemplateExpression":
         case "UnaryExpression":
         case "SpreadElement":
         case "SpreadProperty":
         case "BinaryExpression":
         case "LogicalExpression":
+        case "LogicalExpression":
         case "NewExpression":
           return true;
 
@@ -415,6 +420,7 @@ FPp.needsParens = function(assumeExpressionContext) {
 
     case "ClassExpression":
       switch (parent.type) {
+        case "TaggedTemplateExpression":
         case "BinaryExpression":
         case "ExportDefaultDeclaration":
         case "ExpressionStatement":


Test patch:
diff --git a/tests/template/__snapshots__/jsfmt.spec.js.snap b/tests/template/__snapshots__/jsfmt.spec.js.snap
index df29387a9..f5a6d457a 100644
--- a/tests/template/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/template/__snapshots__/jsfmt.spec.js.snap
@@ -31,6 +31,121 @@ var x = {
 "
 `;
 
+exports[`test parenthesis.js 1`] = `
+"// \"ArrowFunctionExpression\"
+(() => {})\`\`;
+
+// \"AssignmentExpression\"
+(b = c)\`\`;
+
+// \"AwaitExpression\"
+async function f() {
+  (await b)\`\`;
+}
+
+// \"BinaryExpression\"
+(b + c)\`\`;
+
+// \"CallExpression\"
+b()\`\`;
+
+// \"ClassExpression\"
+(class {})\`\`;
+
+// \"ConditionalExpression\"
+(b ? c : d)\`\`;
+
+// \"FunctionExpression\"
+(function() {})\`\`;
+
+// \"LogicalExpression\"
+(b || c)\`\`;
+
+// \"MemberExpression\"
+b.c\`\`;
+
+// \"NewExpression\"
+(new B())\`\`;
+
+// \"ObjectExpression\"
+({})\`\`;
+
+// \"SequenceExpression\"
+(b, c)\`\`;
+
+// \"TaggedTemplateExpression\"
+(\`\`)\`\`;
+
+// \"UnaryExpression\"
+(void b)\`\`;
+
+// \"UpdateExpression\"
+(++b)\`\`;
+
+// \"YieldExpression\"
+function* f() {
+  (yield 1)\`\`;
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// \"ArrowFunctionExpression\"
+(() => {
+})\`\`;
+
+// \"AssignmentExpression\"
+(b = c)\`\`;
+
+// \"AwaitExpression\"
+async function f() {
+  (await b)\`\`;
+}
+
+// \"BinaryExpression\"
+(b + c)\`\`;
+
+// \"CallExpression\"
+b()\`\`;
+
+// \"ClassExpression\"
+(class {})\`\`;
+
+// \"ConditionalExpression\"
+(b ? c : d)\`\`;
+
+// \"FunctionExpression\"
+(function() {
+})\`\`;
+
+// \"LogicalExpression\"
+(b || c)\`\`;
+
+// \"MemberExpression\"
+b.c\`\`;
+
+// \"NewExpression\"
+new B()\`\`;
+
+// \"ObjectExpression\"
+({})\`\`;
+
+// \"SequenceExpression\"
+(b, c)\`\`;
+
+// \"TaggedTemplateExpression\"
+\`\`\`\`;
+
+// \"UnaryExpression\"
+(void b)\`\`;
+
+// \"UpdateExpression\"
+(++b)\`\`;
+
+// \"YieldExpression\"
+function* f() {
+  (yield 1)\`\`;
+}
+"
+`;
+
 exports[`test template.js 1`] = `
 "/* @flow */
 
diff --git a/tests/template/parenthesis.js b/tests/template/parenthesis.js
new file mode 100644
index 000000000..aba408999
--- /dev/null
+++ b/tests/template/parenthesis.js
@@ -0,0 +1,54 @@
+// "ArrowFunctionExpression"
+(() => {})``;
+
+// "AssignmentExpression"
+(b = c)``;
+
+// "AwaitExpression"
+async function f() {
+  (await b)``;
+}
+
+// "BinaryExpression"
+(b + c)``;
+
+// "CallExpression"
+b()``;
+
+// "ClassExpression"
+(class {})``;
+
+// "ConditionalExpression"
+(b ? c : d)``;
+
+// "FunctionExpression"
+(function() {})``;
+
+// "LogicalExpression"
+(b || c)``;
+
+// "MemberExpression"
+b.c``;
+
+// "NewExpression"
+(new B())``;
+
+// "ObjectExpression"
+({})``;
+
+// "SequenceExpression"
+(b, c)``;
+
+// "TaggedTemplateExpression"
+(``)``;
+
+// "UnaryExpression"
+(void b)``;
+
+// "UpdateExpression"
+(++b)``;
+
+// "YieldExpression"
+function* f() {
+  (yield 1)``;
+}