From 1528e45ff2919e4115d322fec74bd0dcea7367c4 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Sat, 25 Apr 2020 11:38:06 +0900 Subject: [PATCH] JS: Fix end-of-line comments if followed by trailing whitespace (#8069) Co-authored-by: fisker Cheung --- changelog_unreleased/javascript/pr-8069.md | 33 ++++++++++ src/main/comments.js | 6 +- .../comments/__snapshots__/jsfmt.spec.js.snap | 66 +++++++++++++++++++ tests/comments/jsfmt.spec.js | 12 +++- .../js/__snapshots__/jsfmt.spec.js.snap | 1 - 5 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 changelog_unreleased/javascript/pr-8069.md diff --git a/changelog_unreleased/javascript/pr-8069.md b/changelog_unreleased/javascript/pr-8069.md new file mode 100644 index 000000000..121f9a54d --- /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. + + +```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 385b0c3ca..c9e9d6e6a 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); } diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 2963f6a9b..43c395915 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 133b453c7..74a0652d6 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 ca9c14fc8..3102f4f36 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"; ================================================================================