Repository: jlongster/prettier (js)

Issue:
Comments not emitted in chaining calls
before:

```js
request
  .post('/version')  // comment 1
  .set('Prefer', 'plurality=singular') /* comment 2 */
  .send()
  .end((error, response) => {});
```

after:

```js
request
  .post('/version')
  .set('Prefer', 'plurality=singular')
  .send()
  .end((error, response) => {});
```

Related issues:

https://github.com/jlongster/prettier/issues/31
https://github.com/jlongster/prettier/issues/87

Pull request description:
Make comments inside of MemberExpression look good
This is using the same technique as #544 and will conflict with it when we try to merge both :)

Fixes #200

Patch:
diff --git a/src/comments.js b/src/comments.js
index 1309a1889..9de74b035 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -151,8 +151,8 @@ function attach(comments, ast, text) {
     ) {
       // If a comment exists on its own line, prefer a leading comment.
       // We also need to check if it's the first line of the file.
-
-      if (handleIfStatementComments(enclosingNode, followingNode, comment)) {
+      if (handleMemberExpressionComment(enclosingNode, followingNode, comment) ||
+          handleIfStatementComments(enclosingNode, followingNode, comment)) {
         // We're good
       } else if (followingNode) {
         // Always a leading comment.
@@ -326,6 +326,18 @@ function handleIfStatementComments(enclosingNode, followingNode, comment) {
   return false;
 }
 
+function handleMemberExpressionComment(enclosingNode, followingNode, comment) {
+  if (enclosingNode &&
+      enclosingNode.type === "MemberExpression" &&
+      followingNode &&
+      followingNode.type === "Identifier") {
+    addLeadingComment(enclosingNode, comment);
+    return true;
+  }
+
+  return false;
+}
+
 function printComment(commentPath) {
   const comment = commentPath.getValue();
 


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index d4f993d89..3d1df16a7 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -319,8 +319,8 @@ export type BuckWebSocketMessage =
 const rootEpic = (actions, store) => combineEpics(...epics)(
   actions,
   store
-).// Log errors and continue.
-catch((err, stream) => {
+)// Log errors and continue.
+.catch((err, stream) => {
   getLogger().error(err);
   return stream;
 });
@@ -349,10 +349,10 @@ import path from \"path\"; // eslint-disable-line nuclide-internal/prefer-nuclid
 
 // Comments disappear in-between MemberExpressions
 Observable.of(process)
-  .// Don\'t complete until we say so!
-  merge(Observable.never())
-  .// Get the errors.
-  takeUntil(throwOnError ? errors.flatMap(Observable.throw) : errors)
+  // Don\'t complete until we say so!
+  .merge(Observable.never())
+  // Get the errors.
+  .takeUntil(throwOnError ? errors.flatMap(Observable.throw) : errors)
   .takeUntil(exit);
 
 /* Comments disappear inside of JSX*/
@@ -548,8 +548,8 @@ export type BuckWebSocketMessage =
 const rootEpic = (actions, store) => combineEpics(...epics)(
   actions,
   store
-).// Log errors and continue.
-catch((err, stream) => {
+)// Log errors and continue.
+.catch((err, stream) => {
   getLogger().error(err);
   return stream;
 });
@@ -578,10 +578,10 @@ import path from \"path\"; // eslint-disable-line nuclide-internal/prefer-nuclid
 
 // Comments disappear in-between MemberExpressions
 Observable.of(process)
-  .// Don\'t complete until we say so!
-  merge(Observable.never())
-  .// Get the errors.
-  takeUntil(throwOnError ? errors.flatMap(Observable.throw) : errors)
+  // Don\'t complete until we say so!
+  .merge(Observable.never())
+  // Get the errors.
+  .takeUntil(throwOnError ? errors.flatMap(Observable.throw) : errors)
   .takeUntil(exit);
 
 // Comments disappear inside of JSX