Repository: jlongster/prettier
Pull request #570 (2017-02-02 15:04:56)

Support dangling comments in ClassBody
Comments were dropped there

vjeux: Just updated with the suggested changes

Patch:
diff --git a/src/printer.js b/src/printer.js
index 941c9bb0a..ca6fea225 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -17,6 +17,7 @@ var group = docBuilders.group;
 var indent = docBuilders.indent;
 var conditionalGroup = docBuilders.conditionalGroup;
 var ifBreak = docBuilders.ifBreak;
+var breakParent = docBuilders.breakParent;
 
 var docUtils = require("./doc-utils");
 var willBreak = docUtils.willBreak;
@@ -261,6 +262,8 @@ function genericPrintNoParens(path, options, print) {
           !n.rest &&
           n.params[0].type === "Identifier" &&
           !n.params[0].typeAnnotation &&
+          !n.params[0].leadingComments &&
+          !n.params[0].trailingComments &&
           !n.predicate &&
           !n.returnType
       ) {
@@ -1187,7 +1190,14 @@ function genericPrintNoParens(path, options, print) {
       ]);
     case "ClassBody":
       if (n.body.length === 0) {
-        return "{}";
+        return group(
+          concat([
+            "{",
+            comments.printDanglingComments(path, options),
+            softline,
+            "}"
+          ])
+        );
       }
 
       return concat([
@@ -1743,32 +1753,35 @@ function printArgumentsList(path, options, print) {
 
   if (groupLastArg) {
     const shouldBreak = printed.slice(0, -1).some(willBreak);
-    return conditionalGroup(
-      [
-        concat(["(", join(concat([", "]), printed), ")"]),
-        concat([
-          "(",
-          join(concat([",", line]), printed.slice(0, -1)),
-          printed.length > 1 ? ", " : "",
-          group(util.getLast(printed), { shouldBreak: true }),
-          ")"
-        ]),
-        group(
+    return concat([
+      printed.some(willBreak) ? breakParent : "",
+      conditionalGroup(
+        [
+          concat(["(", join(concat([", "]), printed), ")"]),
           concat([
             "(",
-            indent(
-              options.tabWidth,
-              concat([line, join(concat([",", line]), printed)])
-            ),
-            options.trailingComma ? "," : "",
-            line,
+            join(concat([",", line]), printed.slice(0, -1)),
+            printed.length > 1 ? ", " : "",
+            group(util.getLast(printed), { shouldBreak: true }),
             ")"
           ]),
-          { shouldBreak: true }
-        )
-      ],
-      { shouldBreak }
-    );
+          group(
+            concat([
+              "(",
+              indent(
+                options.tabWidth,
+                concat([line, join(concat([",", line]), printed)])
+              ),
+              options.trailingComma ? "," : "",
+              line,
+              ")"
+            ]),
+            { shouldBreak: true }
+          )
+        ],
+        { shouldBreak }
+      )
+    ]);
   }
 
   return group(


Test patch:
diff --git a/tests/classes/__snapshots__/jsfmt.spec.js.snap b/tests/classes/__snapshots__/jsfmt.spec.js.snap
index d5a3c2580..676701dae 100644
--- a/tests/classes/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/classes/__snapshots__/jsfmt.spec.js.snap
@@ -31,6 +31,27 @@ exports[`test call.js 1`] = `
 "
 `;
 
+exports[`test empty.js 1`] = `
+"class A {
+  // comment
+}
+
+class A { // comment
+}
+
+class A {
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+class A {
+  // comment
+}
+
+class A {} // comment
+
+class A {}
+"
+`;
+
 exports[`test member.js 1`] = `
 "(class {})[1];
 (class {}).a;
diff --git a/tests/classes/empty.js b/tests/classes/empty.js
new file mode 100644
index 000000000..c4141e05c
--- /dev/null
+++ b/tests/classes/empty.js
@@ -0,0 +1,9 @@
+class A {
+  // comment
+}
+
+class A { // comment
+}
+
+class A {
+}
diff --git a/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap b/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000..bdc17200d
--- /dev/null
+++ b/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,29 @@
+exports[`test arrow.js 1`] = `
+"// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Error
+const beep = (data: Object) => {};
+
+// OK
+const beep = (data: Object, secondData: Object) => {};
+"
+`;
+
+exports[`test arrow.js 2`] = `
+"// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Error
+const beep = (data /*: Object*/) => {};
+
+// OK
+const beep = (data /*: Object*/, secondData /*: Object*/) => {};
+"
+`;
diff --git a/tests/flow_comments/arrow.js b/tests/flow_comments/arrow.js
new file mode 100644
index 000000000..7f5d39d75
--- /dev/null
+++ b/tests/flow_comments/arrow.js
@@ -0,0 +1,5 @@
+// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
diff --git a/tests/flow_comments/jsfmt.spec.js b/tests/flow_comments/jsfmt.spec.js
new file mode 100644
index 000000000..d5ec770a9
--- /dev/null
+++ b/tests/flow_comments/jsfmt.spec.js
@@ -0,0 +1,2 @@
+run_spec(__dirname);
+run_spec(__dirname, {parser: 'babylon'});
diff --git a/tests/last_argument_expansion/__snapshots__/jsfmt.spec.js.snap b/tests/last_argument_expansion/__snapshots__/jsfmt.spec.js.snap
index 93bce8cbf..d298b8359 100644
--- a/tests/last_argument_expansion/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/last_argument_expansion/__snapshots__/jsfmt.spec.js.snap
@@ -35,6 +35,61 @@ export default function searchUsers(action$) {
 "
 `;
 
+exports[`test break-parent.js 1`] = `
+"({
+  processors: [
+    require(\"autoprefixer\", {
+      browsers: [\"> 1%\", \"last 2 versions\", \"ie >= 11\", \"Firefox ESR\"]
+    }),
+    require(\"postcss-url\")({
+      url: url =>
+        url.startsWith(\"/\") || /^[a-z]+:/.test(url) ? url : \`/static/\${url}\`
+    })
+  ]
+});
+
+true
+  ? test({
+      a: 1
+    })
+  : <div
+      a={123412342314}
+      b={123412341234}
+      c={123412341234}
+      d={123412341234}
+      e={123412341234}
+      f={123412341234}
+      g={123412341234}
+    />;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+({
+  processors: [
+    require(\"autoprefixer\", {
+      browsers: [\"> 1%\", \"last 2 versions\", \"ie >= 11\", \"Firefox ESR\"]
+    }),
+    require(\"postcss-url\")({
+      url: url =>
+        url.startsWith(\"/\") || /^[a-z]+:/.test(url) ? url : \`/static/\${url}\`
+    })
+  ]
+});
+
+true
+  ? test({
+      a: 1
+    })
+  : <div
+      a={123412342314}
+      b={123412341234}
+      c={123412341234}
+      d={123412341234}
+      e={123412341234}
+      f={123412341234}
+      g={123412341234}
+    />;
+"
+`;
+
 exports[`test jsx.js 1`] = `
 "const els = items.map(item => (
   <div className=\"whatever\">
diff --git a/tests/last_argument_expansion/break-parent.js b/tests/last_argument_expansion/break-parent.js
new file mode 100644
index 000000000..698f0a022
--- /dev/null
+++ b/tests/last_argument_expansion/break-parent.js
@@ -0,0 +1,25 @@
+({
+  processors: [
+    require("autoprefixer", {
+      browsers: ["> 1%", "last 2 versions", "ie >= 11", "Firefox ESR"]
+    }),
+    require("postcss-url")({
+      url: url =>
+        url.startsWith("/") || /^[a-z]+:/.test(url) ? url : `/static/${url}`
+    })
+  ]
+});
+
+true
+  ? test({
+      a: 1
+    })
+  : <div
+      a={123412342314}
+      b={123412341234}
+      c={123412341234}
+      d={123412341234}
+      e={123412341234}
+      f={123412341234}
+      g={123412341234}
+    />;