From 00d9aa208e12a1c61e5660ee6be88d35e1e711c8 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 16 Jul 2017 17:45:27 +1000 Subject: [PATCH] Trim trailing whitespace from comments, closes #2422 (#2494) --- src/comments.js | 6 +++--- tests/comments/__snapshots__/jsfmt.spec.js.snap | 15 +++++++++++++++ tests/comments/trailing_space.js | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 tests/comments/trailing_space.js diff --git a/src/comments.js b/src/comments.js index e0a32a57..c6c8e06d 100644 --- a/src/comments.js +++ b/src/comments.js @@ -831,7 +831,7 @@ function printComment(commentPath, options) { switch (comment.type || comment.kind) { case "Comment": - return "#" + comment.value; + return "#" + comment.value.trimRight(); case "CommentBlock": case "Block": return "/*" + comment.value + "*/"; @@ -839,9 +839,9 @@ function printComment(commentPath, options) { case "Line": // Print shebangs with the proper comment characters if (options.originalText.slice(util.locStart(comment)).startsWith("#!")) { - return "#!" + comment.value; + return "#!" + comment.value.trimRight(); } - return "//" + comment.value; + return "//" + comment.value.trimRight(); default: throw new Error("Not a comment: " + JSON.stringify(comment)); } diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 11e486f7..8b32979f 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -1249,6 +1249,21 @@ d //comment `; +exports[`trailing_space.js 1`] = ` +#!/there/is-space-here-> + +// Do not trim trailing whitespace from this source file! + +// There is some space here -> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +#!/there/is-space-here-> + +// Do not trim trailing whitespace from this source file! + +// There is some space here -> + +`; + exports[`try.js 1`] = ` // comment 1 try { diff --git a/tests/comments/trailing_space.js b/tests/comments/trailing_space.js new file mode 100644 index 00000000..c85ebdcb --- /dev/null +++ b/tests/comments/trailing_space.js @@ -0,0 +1,5 @@ +#!/there/is-space-here-> + +// Do not trim trailing whitespace from this source file! + +// There is some space here ->