Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: softbreak when elem has single raw text child node #73

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions formatter/src/formatter/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Formatter<'_> {

let is_textual = children
.first()
.map(|n| matches!(n, Node::Text(_) | Node::Block(_)))
.map(|n| matches!(n, Node::Text(_) | Node::RawText(_) | Node::Block(_)))
.unwrap_or_default();

let soft_break = is_textual && attribute_count <= 1;
Expand Down Expand Up @@ -230,6 +230,12 @@ mod tests {
insta::assert_snapshot!(formatted, @r###"<div>"hello"</div>"###);
}

#[test]
fn child_element_single_textual_unquoted() {
let formatted = format_element_from_string!("<div>hello</div>");
insta::assert_snapshot!(formatted, @r###"<div>hello</div>"###);
}

#[test]
fn child_element_single_textual_single_attr() {
let formatted = format_element! { <div key=12>"hello"</div> };
Expand Down Expand Up @@ -266,19 +272,13 @@ mod tests {
#[test]
fn html_unquoted_text() {
let formatted = format_element_from_string!(r##"<div>Unquoted text</div>"##);
insta::assert_snapshot!(formatted, @r#"
<div>
Unquoted text
</div>"#);
insta::assert_snapshot!(formatted, @"<div>Unquoted text</div>");
}

#[test]
fn html_unquoted_text_with_surrounding_spaces() {
let formatted = format_element_from_string!(r##"<div> Unquoted text with spaces </div>"##);
insta::assert_snapshot!(formatted, @r#"
<div>
Unquoted text with spaces
</div>"#);
insta::assert_snapshot!(formatted, @"<div>Unquoted text with spaces</div>");
}

#[test]
Expand Down
10 changes: 2 additions & 8 deletions formatter/src/source_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ mod tests {
// Valid Rust formatted code
#[component]
pub(crate) fn Error(cx: Scope, message: Option<String>) -> impl IntoView {
view! { cx,
<div>
Example
</div>
}
view! { cx, <div>Example</div> }
}
"###);
}
Expand Down Expand Up @@ -349,9 +345,7 @@ mod tests {
fn test2(cx: Scope) -> impl IntoView {
let x = view! { cx,
<div>
<span>
Hello
</span>
<span>Hello</span>
</div>
};
}
Expand Down