From ec6ffbe063a19ae05f211bd26e7fcfcf33ba85bc Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 13 Feb 2017 09:05:18 -0800 Subject: [PATCH] Properly support member chains comments (#668) When there is a comment right before the first `.`, we want to force break such that the comment is printed on its own line. Note: this needs to be based on-top of #667 as it always indent the first `.` Fixes #613 --- src/printer.js | 9 ++++-- .../comments/__snapshots__/jsfmt.spec.js.snap | 28 ++++++++----------- .../__snapshots__/jsfmt.spec.js.snap | 17 +++++++++++ tests/method-chain/comment.js | 6 ++++ 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 tests/method-chain/comment.js diff --git a/src/printer.js b/src/printer.js index 32301d62a..b99391c46 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2253,12 +2253,12 @@ function printMemberChain(path, options, print) { } const printedGroups = groups.map(printGroup); - const oneLine = concat(printedGroups); + const hasComment = groups.length >= 2 && groups[1][0].node.comments; // If we only have a single `.`, we shouldn't do anything fancy and just // render everything concatenated together. - if (groups.length <= 2) { + if (groups.length <= 2 && !hasComment) { return group(oneLine); } @@ -2268,6 +2268,11 @@ function printMemberChain(path, options, print) { printIndentedGroup(groups.slice(shouldMerge ? 2 : 1), hardline) ]); + // If there's a comment, we don't want to print in one line. + if (hasComment) { + return group(expanded); + } + // If any group but the last one has a hard line, we want to force expand // it. If the last group is a function it's okay to inline if it fits. if (printedGroups.slice(0, -1).some(willBreak)) { diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 1b6ba4899..246208c14 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -364,14 +364,12 @@ export type BuckWebSocketMessage = }; // Missing one level of indentation because of the comment -const rootEpic = (actions, store) => combineEpics(...epics)( - actions, - store -)// Log errors and continue. -.catch((err, stream) => { - getLogger().error(err); - return stream; -}); +const rootEpic = (actions, store) => combineEpics(...epics)(actions, store) + // Log errors and continue. + .catch((err, stream) => { + getLogger().error(err); + return stream; + }); // Two extra levels of indentation because of the comment export type AsyncExecuteOptions = @@ -593,14 +591,12 @@ export type BuckWebSocketMessage = }; // Missing one level of indentation because of the comment -const rootEpic = (actions, store) => combineEpics(...epics)( - actions, - store -)// Log errors and continue. -.catch((err, stream) => { - getLogger().error(err); - return stream; -}); +const rootEpic = (actions, store) => combineEpics(...epics)(actions, store) + // Log errors and continue. + .catch((err, stream) => { + getLogger().error(err); + return stream; + }); // Two extra levels of indentation because of the comment export type AsyncExecuteOptions = diff --git a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap index daa999035..72838e2d1 100644 --- a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap +++ b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap @@ -115,6 +115,23 @@ it(\"should group messages with same created time\", () => { " `; +exports[`test comment.js 1`] = ` +"function f() { + return observableFromSubscribeFunction() + // Debounce manually rather than using editor.onDidStopChanging so that the debounce time is + // configurable. + .debounceTime(debounceInterval); +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function f() { + return observableFromSubscribeFunction() + // Debounce manually rather than using editor.onDidStopChanging so that the debounce time is + // configurable. + .debounceTime(debounceInterval); +} +" +`; + exports[`test first_long.js 1`] = ` "export default function theFunction(action$, store) { return action$.ofType(THE_ACTION).switchMap(action => Observable diff --git a/tests/method-chain/comment.js b/tests/method-chain/comment.js new file mode 100644 index 000000000..7cc47760d --- /dev/null +++ b/tests/method-chain/comment.js @@ -0,0 +1,6 @@ +function f() { + return observableFromSubscribeFunction() + // Debounce manually rather than using editor.onDidStopChanging so that the debounce time is + // configurable. + .debounceTime(debounceInterval); +}