From 0d6fed9416799968a203aea990ed42b0d5051d77 Mon Sep 17 00:00:00 2001 From: Jon Egeland Date: Tue, 12 Dec 2023 20:49:24 +0000 Subject: [PATCH] fix(js_formatter): fix some random indention inconsistencies --- .../expressions/arrow_function_expression.rs | 56 +++++++++++-------- .../src/utils/binary_like_expression.rs | 1 + .../prettier/js/ternaries/func-call.js.snap | 52 ----------------- 3 files changed, 34 insertions(+), 75 deletions(-) delete mode 100644 crates/biome_js_formatter/tests/specs/prettier/js/ternaries/func-call.js.snap diff --git a/crates/biome_js_formatter/src/js/expressions/arrow_function_expression.rs b/crates/biome_js_formatter/src/js/expressions/arrow_function_expression.rs index fd802121a604..3a32f21bed66 100644 --- a/crates/biome_js_formatter/src/js/expressions/arrow_function_expression.rs +++ b/crates/biome_js_formatter/src/js/expressions/arrow_function_expression.rs @@ -539,30 +539,40 @@ impl Format for ArrowChain { // rest of the arrows in the chain need to format their // comments manually, since they won't have their own // Format node to handle it. - if !is_first_in_chain - && f.context().comments().has_leading_comments(arrow.syntax()) - { - // A grouped layout implies that the arrow chain is trying to be rendered - // in a condensend, single-line format (at least the signatures, not - // necessarily the body). In that case, we _need_ to prevent the leading - // comments from inserting line breaks. But if it's _not_ a grouped layout, - // then we want to _force_ the line break so that the leading comments - // don't inadvertently end up on the previous line after the fat arrow. - if is_grouped_call_arg_layout { - write!(f, [space(), format_leading_comments(arrow.syntax())])?; - } else { - write!( - f, - [ - soft_line_break_or_space(), - format_leading_comments(arrow.syntax()) - ] - )?; + let should_format_comments = !is_first_in_chain + && f.context().comments().has_leading_comments(arrow.syntax()); + let is_first = is_first_in_chain; + + let formatted_signature = format_with(|f: &mut JsFormatter| { + if should_format_comments { + // A grouped layout implies that the arrow chain is trying to be rendered + // in a condensend, single-line format (at least the signatures, not + // necessarily the body). In that case, we _need_ to prevent the leading + // comments from inserting line breaks. But if it's _not_ a grouped layout, + // then we want to _force_ the line break so that the leading comments + // don't inadvertently end up on the previous line after the fat arrow. + if is_grouped_call_arg_layout { + write!(f, [space(), format_leading_comments(arrow.syntax())])?; + } else { + write!( + f, + [ + soft_line_break_or_space(), + format_leading_comments(arrow.syntax()) + ] + )?; + } } - } - let formatted_signature = - format_signature(arrow, is_grouped_call_arg_layout, is_first_in_chain); + write!( + f, + [format_signature( + arrow, + is_grouped_call_arg_layout, + is_first + )] + ) + }); // Arrow chains indent a second level for every item other than the first: // (a) => @@ -578,7 +588,7 @@ impl Format for ArrowChain { write!(f, [formatted_signature])?; } else { write!(f, [indent(&formatted_signature)])?; - } + }; // The arrow of the tail is formatted outside of the group to ensure it never // breaks from the body diff --git a/crates/biome_js_formatter/src/utils/binary_like_expression.rs b/crates/biome_js_formatter/src/utils/binary_like_expression.rs index d1af55e2f8f3..5e7b0ec96229 100644 --- a/crates/biome_js_formatter/src/utils/binary_like_expression.rs +++ b/crates/biome_js_formatter/src/utils/binary_like_expression.rs @@ -530,6 +530,7 @@ impl AnyJsBinaryLikeExpression { | JsSyntaxKind::JS_THROW_STATEMENT | JsSyntaxKind::JS_CALL_EXPRESSION | JsSyntaxKind::JS_IMPORT_CALL_EXPRESSION + | JsSyntaxKind::JS_CALL_ARGUMENT_LIST | JsSyntaxKind::META ) }) diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/ternaries/func-call.js.snap b/crates/biome_js_formatter/tests/specs/prettier/js/ternaries/func-call.js.snap deleted file mode 100644 index bc9d3853c1d6..000000000000 --- a/crates/biome_js_formatter/tests/specs/prettier/js/ternaries/func-call.js.snap +++ /dev/null @@ -1,52 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: js/ternaries/func-call.js ---- - -# Input - -```js -fn( - bifornCringerMoshedPerplexSawder, - askTrovenaBeenaDependsRowans, - glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && - anodyneCondosMalateOverateRetinol - ? annularCooeedSplicesWalksWayWay - : kochabCooieGameOnOboleUnweave -); -// TODO(rattrayalex): try to indent consequent/alternate here. - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -2,7 +2,7 @@ - bifornCringerMoshedPerplexSawder, - askTrovenaBeenaDependsRowans, - glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && -- anodyneCondosMalateOverateRetinol -+ anodyneCondosMalateOverateRetinol - ? annularCooeedSplicesWalksWayWay - : kochabCooieGameOnOboleUnweave, - ); -``` - -# Output - -```js -fn( - bifornCringerMoshedPerplexSawder, - askTrovenaBeenaDependsRowans, - glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && - anodyneCondosMalateOverateRetinol - ? annularCooeedSplicesWalksWayWay - : kochabCooieGameOnOboleUnweave, -); -// TODO(rattrayalex): try to indent consequent/alternate here. -``` - -