From 1a8e97f92bdb991fae1eb09c2333b6f99644bae2 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 13 Feb 2017 07:13:16 -0800 Subject: [PATCH] Fix trailing commas with a trailing comment (#664) The reason why trailing comments on a line work with commas is because of a special `lineSuffix` document that delays printing the content until a \n is printed. But `lineSuffix` only worked for pure string. By making it work with arbitrary documents we can fix trailing commas with trailing comments. Fixes #538 --- src/comments.js | 2 +- src/doc-builders.js | 6 +- src/doc-printer.js | 17 +- .../__snapshots__/jsfmt.spec.js.snap | 150 ++++++++++++++++++ tests/trailing_comma/trailing_whitespace.js | 35 ++++ 5 files changed, 198 insertions(+), 12 deletions(-) create mode 100644 tests/trailing_comma/trailing_whitespace.js diff --git a/src/comments.js b/src/comments.js index 695326cd..55d42a93 100644 --- a/src/comments.js +++ b/src/comments.js @@ -439,7 +439,7 @@ function printTrailingComment(commentPath, print, options, parentNode) { comment ); - return concat([hardline, isLineBeforeEmpty ? hardline : "", contents]); + return lineSuffix(concat([hardline, isLineBeforeEmpty ? hardline : "", contents])); } else if (isBlock) { // Trailing block comments never need a newline return concat([" ", contents]); diff --git a/src/doc-builders.js b/src/doc-builders.js index d25cd06a..9402f9da 100644 --- a/src/doc-builders.js +++ b/src/doc-builders.js @@ -63,11 +63,7 @@ function ifBreak(breakContents, flatContents) { } function lineSuffix(contents) { - if (typeof contents !== "string") { - throw new Error( - "lineSuffix only takes a string, but given: " + JSON.stringify(contents) - ); - } + assertDoc(contents); return { type: "line-suffix", contents }; } diff --git a/src/doc-printer.js b/src/doc-printer.js index 1e1bf276..e41ba094 100644 --- a/src/doc-printer.js +++ b/src/doc-printer.js @@ -87,7 +87,7 @@ function printDocToString(doc, width, newLine) { let cmds = [[0, MODE_BREAK, doc]]; let out = []; let shouldRemeasure = false; - let lineSuffix = ""; + let lineSuffix = []; while (cmds.length !== 0) { const x = cmds.pop(); @@ -190,7 +190,7 @@ function printDocToString(doc, width, newLine) { break; case "line-suffix": - lineSuffix += doc.contents; + lineSuffix.push([ind, mode, doc.contents]); break; case "line": switch (mode) { @@ -215,8 +215,15 @@ function printDocToString(doc, width, newLine) { } case MODE_BREAK: + if (lineSuffix.length) { + cmds.push([ind, mode, doc]); + [].push.apply(cmds, lineSuffix.reverse()); + lineSuffix = []; + break; + } + if (doc.literal) { - out.push(lineSuffix + newLine); + out.push(newLine); pos = 0; } else { if (out.length > 0) { @@ -227,11 +234,9 @@ function printDocToString(doc, width, newLine) { ); } - out.push(lineSuffix + newLine + " ".repeat(ind)); + out.push(newLine + " ".repeat(ind)); pos = ind; } - - lineSuffix = ""; break; } break; diff --git a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap index 2096a8ff..5f2e3336 100644 --- a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap +++ b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap @@ -153,3 +153,153 @@ const aLong = { }; " `; + +exports[`test trailing_whitespace.js 1`] = ` +"let example = [ + \'FOO\', + \'BAR\', + // Comment +]; + +foo({}, + // Comment +); + +o = { + state, + // Comment +}; + +o = { + state, + + // Comment +}; + +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB + // Comment +) { + a +} +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB, + // Comment +) { + a +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let example = [ + \"FOO\", + \"BAR\" + // Comment +]; + +foo( + {} + // Comment +); + +o = { + state + // Comment +}; + +o = { + state + // Comment +}; + +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB +) // Comment +{ + a; +} +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB +) // Comment +{ + a; +} +" +`; + +exports[`test trailing_whitespace.js 2`] = ` +"let example = [ + \'FOO\', + \'BAR\', + // Comment +]; + +foo({}, + // Comment +); + +o = { + state, + // Comment +}; + +o = { + state, + + // Comment +}; + +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB + // Comment +) { + a +} +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB, + // Comment +) { + a +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +let example = [ + \"FOO\", + \"BAR\", + // Comment +]; + +foo( + {}, + // Comment +); + +o = { + state, + // Comment +}; + +o = { + state, + // Comment +}; + +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB, +) // Comment +{ + a; +} +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB, +) // Comment +{ + a; +} +" +`; diff --git a/tests/trailing_comma/trailing_whitespace.js b/tests/trailing_comma/trailing_whitespace.js new file mode 100644 index 00000000..79756803 --- /dev/null +++ b/tests/trailing_comma/trailing_whitespace.js @@ -0,0 +1,35 @@ +let example = [ + 'FOO', + 'BAR', + // Comment +]; + +foo({}, + // Comment +); + +o = { + state, + // Comment +}; + +o = { + state, + + // Comment +}; + +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB + // Comment +) { + a +} +function supersupersupersuperLongF( + supersupersupersuperLongA, + supersupersupersuperLongB, + // Comment +) { + a +}