Repository: jlongster/prettier (js)

Issue:
Unprinted comment in object destructuring
```js
const {a /* missing comment */ = 1} = b
```

```
Error: Comment "missing comment" was not printed. Please report this error!
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22const%20%7Ba%20%2F*%20missing%20comment%20*%2F%20%3D%201%7D%20%3D%20b%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 missing comments in assignment pattern
This PR should fix #699.

Patch:
diff --git a/src/comments.js b/src/comments.js
index d2f1f99f9..c630afa31 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -179,7 +179,8 @@ function attach(comments, ast, text) {
         addDanglingComment(ast, comment);
       }
     } else {
-      if (handleIfStatementComments(enclosingNode, followingNode, comment)) {
+      if (handleIfStatementComments(enclosingNode, followingNode, comment) ||
+          handleObjectProperty(enclosingNode, precedingNode, comment)) {
         // We're good
       } else if (precedingNode && followingNode) {
         // Otherwise, text exists both before and after the comment on
@@ -395,6 +396,19 @@ function handleConditionalExpressionComments(
   return false;
 }
 
+function handleObjectProperty(enclosingNode, precedingNode, comment) {
+  if (enclosingNode &&
+      (enclosingNode.type === "ObjectProperty" ||
+       enclosingNode.type === "Property") &&
+      enclosingNode.shorthand &&
+      enclosingNode.key === precedingNode &&
+      enclosingNode.value.type === "AssignmentPattern") {
+    addTrailingComment(enclosingNode.value.left, comment);
+    return true;
+  }
+  return false;
+}
+
 function printComment(commentPath) {
   const comment = commentPath.getValue();
   comment.printed = true;


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 1c50b6e53..e300d1965 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1087,3 +1087,25 @@ try {
 }
 "
 `;
+
+exports[`test assignment-pattern.js 1`] = `
+"const { a /* comment */ = 1 } = b;
+
+const { c = 1 /* comment */ } = d;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+const { a /* comment */ = 1 } = b;
+
+const { c = 1 /* comment */ } = d;
+"
+`;
+
+exports[`test assignment-pattern.js 2`] = `
+"const { a /* comment */ = 1 } = b;
+
+const { c = 1 /* comment */ } = d;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+const { a /* comment */ = 1 } = b;
+
+const { c = 1 /* comment */ } = d;
+"
+`;
diff --git a/tests/comments/assignment-pattern.js b/tests/comments/assignment-pattern.js
new file mode 100644
index 000000000..52e0544c7
--- /dev/null
+++ b/tests/comments/assignment-pattern.js
@@ -0,0 +1,3 @@
+const { a /* comment */ = 1 } = b;
+
+const { c = 1 /* comment */ } = d;