From 5db7366ad03905e9e4c60fd11f345d843ff36f2c Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Tue, 21 Feb 2023 23:56:50 +0100 Subject: [PATCH] fix(rome_js_formatter): mapped type formatting --- .../src/ts/types/mapped_type.rs | 10 +++-- .../tests/specs/ts/type/mapped_type.ts | 1 + .../tests/specs/ts/type/mapped_type.ts.snap | 39 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts create mode 100644 crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts.snap diff --git a/crates/rome_js_formatter/src/ts/types/mapped_type.rs b/crates/rome_js_formatter/src/ts/types/mapped_type.rs index f6811912b53..678961e3d37 100644 --- a/crates/rome_js_formatter/src/ts/types/mapped_type.rs +++ b/crates/rome_js_formatter/src/ts/types/mapped_type.rs @@ -28,9 +28,14 @@ impl FormatNodeRule for FormatTsMappedType { let property_name = property_name?; + // Check if the user introduced a new line inside the node. let should_expand = node .syntax() .tokens() + // Skip the first token to avoid formatter instability. See #4165. + // This also makes sense since leading trivia of the first token + // are not part of the interior of the node. + .skip(1) .flat_map(|token| { token .leading_trivia() @@ -47,8 +52,6 @@ impl FormatNodeRule for FormatTsMappedType { }); let format_inner = format_with(|f| { - write!(f, [soft_line_break_or_space()])?; - if let Some(readonly_modifier) = &readonly_modifier { write!(f, [readonly_modifier.format(), space()])?; } @@ -81,8 +84,7 @@ impl FormatNodeRule for FormatTsMappedType { f, [ &l_curly_token.format(), - group(&indent(&format_inner)).should_expand(should_expand), - soft_line_break_or_space(), + group(&soft_space_or_block_indent(&format_inner)).should_expand(should_expand), r_curly_token.format(), ] ) diff --git a/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts b/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts new file mode 100644 index 00000000000..b9fedaa846c --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts @@ -0,0 +1 @@ +type LongNameHereToCauseLineBreak_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ={ [K in "foo"]: string }; \ No newline at end of file diff --git a/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts.snap b/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts.snap new file mode 100644 index 00000000000..0dbf9013446 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/type/mapped_type.ts.snap @@ -0,0 +1,39 @@ +--- +source: crates/rome_formatter_test/src/snapshot_builder.rs +assertion_line: 212 +info: ts/type/mapped_type.ts +--- + +# Input + +```ts +type LongNameHereToCauseLineBreak_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ={ [K in "foo"]: string }; +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +Semicolons: Always +----- + +```ts +type LongNameHereToCauseLineBreak_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = + { [K in "foo"]: string }; +``` + +# Lines exceeding max width of 80 characters +``` + 1: type LongNameHereToCauseLineBreak_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = +``` + +