From 93cad97b14c1a404d0dadfcb73f291bbdbaff3d3 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 1 May 2017 14:29:03 -0700 Subject: [PATCH] Preserve inline comment as last argument (#1390) We already had the logic but only applied it for comments on their own line, we should also do it if they are inline. Fixes #1380 --- src/comments.js | 7 ++ .../comments/__snapshots__/jsfmt.spec.js.snap | 77 +++++++++++++++++++ tests/comments/last-arg.js | 41 ++++++++++ 3 files changed, 125 insertions(+) diff --git a/src/comments.js b/src/comments.js index 66bb0576..a511491a 100644 --- a/src/comments.js +++ b/src/comments.js @@ -192,6 +192,13 @@ function attach(comments, ast, text) { } } else if (util.hasNewline(text, locEnd(comment))) { if ( + handleLastFunctionArgComments( + text, + precedingNode, + enclosingNode, + followingNode, + comment + ) || handleConditionalExpressionComments( enclosingNode, precedingNode, diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 6482cb09..58761d44 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -714,6 +714,47 @@ class X { ): $Enum { } } + +class Foo { + a(lol /*string*/) {} + + b(lol /*string*/ + ) {} + + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} + + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) /*string*/ {} + + // prettier-ignore + c(lol /*string*/ + ) {} + + // prettier-ignore + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} + + // prettier-ignore + e( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} /* string*/ +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ type f = ( currentRequest: { a: number } @@ -767,6 +808,42 @@ class X { ): $Enum {} } +class Foo { + a(lol /*string*/) {} + + b(lol /*string*/) {} + + d(lol /*string*/, lol2 /*string*/, lol3 /*string*/, lol4 /*string*/) {} + + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) /*string*/ { + } + + // prettier-ignore + c(lol /*string*/ + ) {} + + // prettier-ignore + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} + + // prettier-ignore + e( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} /* string*/ +} + `; exports[`preserve-new-line-last.js 1`] = ` diff --git a/tests/comments/last-arg.js b/tests/comments/last-arg.js index 5a3052bd..45c69c81 100644 --- a/tests/comments/last-arg.js +++ b/tests/comments/last-arg.js @@ -51,3 +51,44 @@ class X { ): $Enum { } } + +class Foo { + a(lol /*string*/) {} + + b(lol /*string*/ + ) {} + + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} + + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) /*string*/ {} + + // prettier-ignore + c(lol /*string*/ + ) {} + + // prettier-ignore + d( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} + + // prettier-ignore + e( + lol /*string*/, + lol2 /*string*/, + lol3 /*string*/, + lol4 /*string*/ + ) {} /* string*/ +}