Repository: prettier/prettier (JavaScript) — Bug Fix

Issue:
End-of-line comments aren't formatted properly if followed by trailing whitespace
**Prettier 2.0.4**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEA3AhgJwATuwXm2GwHoAqbOADxkzwHcALAS3gGcAHdMObAWj4A+bGRLZsAHSjjxAI2wBfANxSpGHHkLFy2KBGxNWcTt14DhoqTOzzlIADQgIHGM2htkoLJgj0AClgIHijoADb06ACeHo6ydGAA1nAwAMpcYMxQAObItACucI6MMAC2oQDqLOzpcClBrMyorJHI4GwxIJlscJgwfnRZJejIAGZh3Y4AVmxUAELxSanoJXAAMplwo+OFINNUKZlZoXAAinkQ8FuhEyBcmN2YrbLosnChDreYmTDlzAAmMEYyAAHAAGRwcHzdcp0DitSHGHqoTaOACO53g-WcwRA6DYfCgcDgf2JH0wcHRzHJ-XQg2GSDG1x23RKzFymAKjjYh2OZwumwZ20cMBevwBQKQACZhXRmKFDgBhCAlIatYwAVg+eW6ABUXsFGTdUAUAJJQEmwFJgL4uACC5pSMEixyu3QUCiAA)
```sh
--parser babel
```

**Input:**
```jsx
var a = { /* extra whitespace --> */  
    b };

var a = { /* no whitespace --> */
    b };
```

**Output:**
```jsx
var a = {
  /* extra whitespace --> */

  b,
};

var a = {
  /* no whitespace --> */ b,
};

```

**Expected behavior:**
```jsx
var a = {
  /* extra whitespace --> */ b,
};

var a = {
  /* no whitespace --> */ b,
};

```



Discussion:

Resolving patch:
diff --git a/changelog_unreleased/javascript/pr-8069.md b/changelog_unreleased/javascript/pr-8069.md
new file mode 100644
index 000000000000..121f9a54dcfa
--- /dev/null
+++ b/changelog_unreleased/javascript/pr-8069.md
@@ -0,0 +1,33 @@
+#### Fix end-of-line comments if followed by trailing whitespace ([#8069](https://github.com/prettier/prettier/pull/8069) by [@shisama](https://github.com/shisama))
+
+If comments line has trailing whitespaces, comments aren't detected as end-of-line.
+
+<!-- prettier-ignore -->
+```jsx
+// Input
+var a = { /* extra whitespace --> */  
+    b };
+
+var a = { /* no whitespace --> */
+    b };
+
+// Prettier stable
+var a = {
+  /* extra whitespace --> */
+
+  b,
+};
+
+var a = {
+  /* no whitespace --> */ b,
+};
+
+// Prettier master
+var a = {
+  /* extra whitespace --> */ b,
+};
+
+var a = {
+  /* no whitespace --> */ b,
+};
+```
diff --git a/src/main/comments.js b/src/main/comments.js
index 385b0c3ca402..c9e9d6e6acc3 100644
--- a/src/main/comments.js
+++ b/src/main/comments.js
@@ -14,6 +14,7 @@ const {
 const {
   hasNewline,
   skipNewline,
+  skipSpaces,
   isPreviousLineEmpty,
 } = require("../common/util");
 const {
@@ -520,7 +521,10 @@ function printComments(path, print, options, needsSemi) {
       leadingParts.push(contents);
 
       const text = options.originalText;
-      const index = skipNewline(text, options.locEnd(comment));
+      const index = skipNewline(
+        text,
+        skipSpaces(text, options.locEnd(comment))
+      );
       if (index !== false && hasNewline(text, index)) {
         leadingParts.push(hardline);
       }


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 2963f6a9b6d3..43c395915526 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -3835,6 +3835,72 @@ if (true) {
 ================================================================================
 `;
 
+exports[`snippet: #0 - {"semi":false} format 1`] = `
+====================================options=====================================
+parsers: ["flow", "babel", "typescript"]
+printWidth: 80
+semi: false
+                                                                                | printWidth
+=====================================input======================================
+var a = { /* comment */      
+b };
+=====================================output=====================================
+var a = {
+  /* comment */ b,
+}
+
+================================================================================
+`;
+
+exports[`snippet: #0 format 1`] = `
+====================================options=====================================
+parsers: ["flow", "babel", "typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+var a = { /* comment */      
+b };
+=====================================output=====================================
+var a = {
+  /* comment */ b,
+};
+
+================================================================================
+`;
+
+exports[`snippet: #1 - {"semi":false} format 1`] = `
+====================================options=====================================
+parsers: ["flow", "babel", "typescript"]
+printWidth: 80
+semi: false
+                                                                                | printWidth
+=====================================input======================================
+var a = { /* comment */
+b };
+=====================================output=====================================
+var a = {
+  /* comment */ b,
+}
+
+================================================================================
+`;
+
+exports[`snippet: #1 format 1`] = `
+====================================options=====================================
+parsers: ["flow", "babel", "typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+var a = { /* comment */
+b };
+=====================================output=====================================
+var a = {
+  /* comment */ b,
+};
+
+================================================================================
+`;
+
 exports[`switch.js - {"semi":false} format 1`] = `
 ====================================options=====================================
 parsers: ["flow", "babel", "typescript"]
diff --git a/tests/comments/jsfmt.spec.js b/tests/comments/jsfmt.spec.js
index 133b453c7029..74a0652d63c3 100644
--- a/tests/comments/jsfmt.spec.js
+++ b/tests/comments/jsfmt.spec.js
@@ -1,2 +1,10 @@
-run_spec(__dirname, ["flow", "babel", "typescript"]);
-run_spec(__dirname, ["flow", "babel", "typescript"], { semi: false });
+const fixtures = {
+  dirname: __dirname,
+  snippets: [
+    "var a = { /* comment */      \nb };", // trailing whitespace after comment
+    "var a = { /* comment */\nb };",
+  ],
+};
+
+run_spec(fixtures, ["flow", "babel", "typescript"]);
+run_spec(fixtures, ["flow", "babel", "typescript"], { semi: false });
diff --git a/tests/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap b/tests/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap
index ca9c14fc8bf3..3102f4f36dd0 100644
--- a/tests/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap
@@ -26,7 +26,6 @@ function foo(bar) {
 /**
  * Some notes that should not be appended to
  */
-
 const fruit = "tomatoes";
 
 ================================================================================