From e1e1f01e747ec4bef338fe9e74222d9c173f1522 Mon Sep 17 00:00:00 2001 From: Denis Bezrukov <6227442+denbezrukov@users.noreply.github.com> Date: Thu, 24 Nov 2022 10:06:12 +0200 Subject: [PATCH 1/5] fix(rome_js_formatter): jsx punctuation --- .../src/jsx/lists/child_list.rs | 71 +++++++--- crates/rome_js_formatter/src/utils/jsx.rs | 33 ++++- .../tests/specs/jsx/new-lines.jsx | 50 +++++++ .../tests/specs/jsx/new-lines.jsx.snap | 126 ++++++++++++++++++ .../tests/specs/prettier/jsx/fbt/test.js.snap | 63 ++++++--- .../specs/prettier/jsx/text-wrap/test.js.snap | 96 ++----------- 6 files changed, 312 insertions(+), 127 deletions(-) create mode 100644 crates/rome_js_formatter/tests/specs/jsx/new-lines.jsx create mode 100644 crates/rome_js_formatter/tests/specs/jsx/new-lines.jsx.snap diff --git a/crates/rome_js_formatter/src/jsx/lists/child_list.rs b/crates/rome_js_formatter/src/jsx/lists/child_list.rs index 9987c4b389f..3ee70debf96 100644 --- a/crates/rome_js_formatter/src/jsx/lists/child_list.rs +++ b/crates/rome_js_formatter/src/jsx/lists/child_list.rs @@ -102,11 +102,11 @@ impl FormatJsxChildList { } // Last word or last word before an element without any whitespace in between - Some(JsxChild::NonText(child)) => Some(WordSeparator::EndOfText { - is_next_self_closing: matches!( - child, + Some(JsxChild::NonText(next_child)) => Some(WordSeparator::EndOfText { + is_soft_line_break: !matches!( + next_child, JsxAnyChild::JsxSelfClosingElement(_) - ), + ) || word.is_ascii_punctuation(), }), Some(JsxChild::Newline | JsxChild::Whitespace | JsxChild::EmptyLine) => { @@ -165,9 +165,45 @@ impl FormatJsxChildList { // A new line between some JSX text and an element JsxChild::Newline => { - child_breaks = true; + // Here we handle the case when we have a newline between a ascii punctuation word and a jsx element + let is_soft_break = { + if let Some(JsxChild::Word(word)) = last { + let is_next_element_self_closing = matches!( + children_iter.peek(), + Some(JsxChild::NonText(JsxAnyChild::JsxSelfClosingElement(_))) + ); - multiline.write_separator(&hard_line_break(), f); + // Here we need to use the previous and the next element + // [JsxChild::Word, JsxChild::Newline, JsxChild::NonText] + // ``` + //
Use the Button
's
Use the Button
's