Repository: jlongster/prettier
Pull request #530 (2017-01-31 08:05:40)

Fix closing call expression commented out
```js
foo({}
  // Hi
);
```

prints

```js
foo({}
  // Hi);
```

Edit: change from a test failure PR to a fix PR


Patch:
diff --git a/src/printer.js b/src/printer.js
index 75472aab8..0eca71aed 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -588,11 +588,14 @@ function genericPrintNoParens(path, options, print) {
       );
 
       if (props.length === 0) {
-        return concat([
-          "{",
-          comments.printDanglingComments(path, options),
-          "}"
-        ]);
+        return group(
+          concat([
+            "{",
+            comments.printDanglingComments(path, options),
+            softline,
+            "}"
+          ])
+        );
       } else {
         return group(
           concat([
@@ -1708,17 +1711,18 @@ function printArgumentsList(path, options, print) {
   // This is just an optimization; I think we could return the
   // conditional group for all function calls, but it's more expensive
   // so only do it for specific forms.
-  const groupLastArg = lastArg.type === "ObjectExpression" ||
-    lastArg.type === "ArrayExpression" ||
-    lastArg.type === "FunctionExpression" ||
-    lastArg.type === "ArrowFunctionExpression" &&
-      (lastArg.body.type === "BlockStatement" ||
-        lastArg.body.type === "ArrowFunctionExpression" ||
-        lastArg.body.type === "ObjectExpression" ||
-        lastArg.body.type === "ArrayExpression" ||
-        lastArg.body.type === "CallExpression" ||
-        lastArg.body.type === "JSXElement") ||
-    lastArg.type === "NewExpression";
+  const groupLastArg = (!lastArg.comments || !lastArg.comments.length) &&
+    (lastArg.type === "ObjectExpression" ||
+      lastArg.type === "ArrayExpression" ||
+      lastArg.type === "FunctionExpression" ||
+      lastArg.type === "ArrowFunctionExpression" &&
+        (lastArg.body.type === "BlockStatement" ||
+          lastArg.body.type === "ArrowFunctionExpression" ||
+          lastArg.body.type === "ObjectExpression" ||
+          lastArg.body.type === "ArrayExpression" ||
+          lastArg.body.type === "CallExpression" ||
+          lastArg.body.type === "JSXElement") ||
+      lastArg.type === "NewExpression");
 
   if (groupLastArg) {
     const shouldBreak = printed.slice(0, -1).some(willBreak);


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 6888c6572..dbed6c7e6 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -26,6 +26,9 @@ exports[`test blank.js 2`] = `
 
 exports[`test dangling.js 1`] = `
 "var x = {/* dangling */};
+var x = {
+  // dangling
+};
 var x = [/* dangling */];
 function x() {
   /* dangling */
@@ -33,6 +36,9 @@ function x() {
 declare class Foo extends Qux<string> {/* dangling */}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 var x = {/* dangling */};
+var x = {
+  // dangling
+};
 var x = [/* dangling */];
 function x() {
   /* dangling */
@@ -43,6 +49,9 @@ declare class Foo extends Qux<string> {/* dangling */}
 
 exports[`test dangling.js 2`] = `
 "var x = {/* dangling */};
+var x = {
+  // dangling
+};
 var x = [/* dangling */];
 function x() {
   /* dangling */
@@ -50,6 +59,9 @@ function x() {
 declare class Foo extends Qux<string> {/* dangling */}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 var x = {/* dangling */};
+var x = {
+  // dangling
+};
 var x = [/* dangling */];
 function x() {
   /* dangling */
@@ -161,6 +173,11 @@ else {
 
 // The comment makes the line break in a weird way
 const result = asyncExecute(\'non_existing_command\', /* args */ []);
+
+// The closing paren is printed on the same line as the comment
+foo({}
+  // Hi
+);
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 // Does not need to break as it fits in 80 columns
 this.call(a, /* comment */ b);
@@ -270,6 +287,12 @@ if (1) {
 
 // The comment makes the line break in a weird way
 const result = asyncExecute(\"non_existing_command\", /* args */ []);
+
+// The closing paren is printed on the same line as the comment
+foo(
+  {}
+  // Hi
+);
 "
 `;
 
@@ -376,6 +399,11 @@ else {
 
 // The comment makes the line break in a weird way
 const result = asyncExecute(\'non_existing_command\', /* args */ []);
+
+// The closing paren is printed on the same line as the comment
+foo({}
+  // Hi
+);
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 // Does not need to break as it fits in 80 columns
 this.call(a, /* comment */ b);
@@ -485,5 +513,11 @@ if (1) {
 
 // The comment makes the line break in a weird way
 const result = asyncExecute(\"non_existing_command\", /* args */ []);
+
+// The closing paren is printed on the same line as the comment
+foo(
+  {}
+  // Hi
+);
 "
 `;
diff --git a/tests/comments/dangling.js b/tests/comments/dangling.js
index 7049ef07f..22208dee5 100644
--- a/tests/comments/dangling.js
+++ b/tests/comments/dangling.js
@@ -1,4 +1,7 @@
 var x = {/* dangling */};
+var x = {
+  // dangling
+};
 var x = [/* dangling */];
 function x() {
   /* dangling */
diff --git a/tests/comments/issues.js b/tests/comments/issues.js
index e48543cd2..c95f79c4a 100644
--- a/tests/comments/issues.js
+++ b/tests/comments/issues.js
@@ -100,3 +100,8 @@ else {
 
 // The comment makes the line break in a weird way
 const result = asyncExecute('non_existing_command', /* args */ []);
+
+// The closing paren is printed on the same line as the comment
+foo({}
+  // Hi
+);
diff --git a/tests/flow/this_type/lib/__snapshots__/jsfmt.spec.js.snap b/tests/flow/this_type/lib/__snapshots__/jsfmt.spec.js.snap
index 9ba16e9c0..8dc8bb152 100644
--- a/tests/flow/this_type/lib/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/flow/this_type/lib/__snapshots__/jsfmt.spec.js.snap
@@ -35,7 +35,8 @@ declare module \"mini-immutable\" {
     set(key: K, value: V): this /* more precise than Map<K,V> (see below)*/
   }
   declare class OrderedMap<K, V> extends Map<K, V> {
-    /* inherits set method returning OrderedMap<K,V> instead of Map<K,V>*/}
+    /* inherits set method returning OrderedMap<K,V> instead of Map<K,V>*/
+  }
 }
 "
 `;
diff --git a/tests/flow/traits/__snapshots__/jsfmt.spec.js.snap b/tests/flow/traits/__snapshots__/jsfmt.spec.js.snap
index 409328331..8e66f054d 100644
--- a/tests/flow/traits/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/flow/traits/__snapshots__/jsfmt.spec.js.snap
@@ -22,7 +22,8 @@ declare class Baz<T> {
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 declare class Foo extends Qux<string> {
   /* KeyedCollection <: Collection*/
-  /* ...KeyedIterable*/}
+  /* ...KeyedIterable*/
+}
 declare class Bar<T> extends Baz<T> {
   // KeyedIterable <: Iterable
   y: T