From d7fbddcd7f99147b359000d55bf1276e38e3cd75 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 11 Oct 2022 12:35:12 +0200 Subject: [PATCH] fix(rome_js_formatter): Arrow chain trailing comments This PR fixes an issue where trailing comments of arrow expressions that are part of an arrow chain (arrow expressions that are the body of another arrow) were dropped. The manual handling of comments is necessary because the arrow chain formatting doesn't call into the `FormatJsArrowFunctionExpression` of nested arrow functions. The implementation already handles the formatting of leading comments correctly. ## Tests I added a new snapshot test and verified that running rome format on prettier yields no more "dropped comments" errors. --- .../expressions/arrow_function_expression.rs | 14 +++++-- .../js/module/arrow/arrow_chain_comments.js | 9 ++++ .../module/arrow/arrow_chain_comments.js.snap | 41 +++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js create mode 100644 crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js.snap diff --git a/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs b/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs index f3fc43b106a..ead14a5d7e3 100644 --- a/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs +++ b/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs @@ -458,10 +458,18 @@ impl Format for ArrowChain { soft_block_indent(&format_tail_body), text(")") ])] - ) + )?; } else { - write!(f, [format_tail_body]) + write!(f, [format_tail_body])?; + } + + // Format the trailing comments of all arrow function EXCEPT the first one because + // the comments of the head get formatted as part of the `FormatJsArrowFunctionExpression` call. + for arrow in self.arrows().skip(1) { + write!(f, [format_trailing_comments(arrow.syntax())])?; } + + Ok(()) }); let format_tail_body = format_with(|f| { @@ -489,7 +497,7 @@ impl Format for ArrowChain { .should_expand(break_before_chain), space(), tail.fat_arrow_token().format(), - indent_if_group_breaks(&format_tail_body, group_id) + indent_if_group_breaks(&format_tail_body, group_id), ] )?; diff --git a/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js b/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js new file mode 100644 index 00000000000..f1832c26f3f --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js @@ -0,0 +1,9 @@ +x = (bifornCringerMoshedPerplexSawder) => ((askTrovenaBeenaDependsRowans, glimseGlyphsHazardNoopsTieTie) => (f00) => { + averredBathersBoxroomBuggyNurl(); + } // BOOM +) + +x2 = (a) => ((askTrovenaBeenaDependsRowans1, askTrovenaBeenaDependsRowans2, askTrovenaBeenaDependsRowans3) => { + c(); + } /* ! */ // KABOOM +) diff --git a/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js.snap b/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js.snap new file mode 100644 index 00000000000..b973e691240 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_chain_comments.js.snap @@ -0,0 +1,41 @@ +--- +source: crates/rome_js_formatter/tests/spec_test.rs +expression: arrow_chain_comments.js +--- +# Input +x = (bifornCringerMoshedPerplexSawder) => ((askTrovenaBeenaDependsRowans, glimseGlyphsHazardNoopsTieTie) => (f00) => { + averredBathersBoxroomBuggyNurl(); + } // BOOM +) + +x2 = (a) => ((askTrovenaBeenaDependsRowans1, askTrovenaBeenaDependsRowans2, askTrovenaBeenaDependsRowans3) => { + c(); + } /* ! */ // KABOOM +) + +============================= +# Outputs +## Output 1 +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +Quote properties: As needed +----- +x = + (bifornCringerMoshedPerplexSawder) => + (askTrovenaBeenaDependsRowans, glimseGlyphsHazardNoopsTieTie) => + (f00) => { + averredBathersBoxroomBuggyNurl(); + }; // BOOM + +x2 = + (a) => + ( + askTrovenaBeenaDependsRowans1, + askTrovenaBeenaDependsRowans2, + askTrovenaBeenaDependsRowans3, + ) => { + c(); + } /* ! */; // KABOOM +