Skip to content

Commit

Permalink
fix: inline unquoted text + expr block combinations (#110)
Browse files Browse the repository at this point in the history
* fix: inline unquoted text + expr block combinations

* clippy

* clippy

* formatting
  • Loading branch information
bram209 authored Mar 2, 2024
1 parent 27d26e6 commit f50dd35
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "view macro formatter for the Leptos web framework"

[dependencies]
leptosfmt-pretty-printer.workspace = true
rstml = "0.10.6"
rstml = "0.11.2"
syn = { version = "2.0.18", features = ["visit", "full", "extra-traits"] }
leptosfmt-prettyplease = { features = ["verbatim"], version = "0.2.16" }
proc-macro2 = { version = "1.0.68", features = ["span-locations"] }
Expand Down
42 changes: 38 additions & 4 deletions formatter/src/formatter/element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::formatter::Formatter;

use rstml::node::{Node, NodeAttribute, NodeElement};
use syn::spanned::Spanned;

Expand Down Expand Up @@ -88,8 +89,17 @@ impl Formatter<'_> {
while let Some(child) = iter.next() {
self.node(child);

if iter.peek().is_some() {
self.printer.space()
if let Some(next_child) = iter.peek() {
let curr_end = child.span().end();
let next_start = next_child.span().start();
let consecutive =
curr_end.line == next_start.line && next_start.column == curr_end.column;

if !matches!(next_child, Node::RawText(_)) && !consecutive {
self.printer.space()
} else {
self.printer.zerobreak()
}
}
}

Expand Down Expand Up @@ -251,8 +261,8 @@ mod tests {

#[test]
fn child_element_two_textual() {
let formatted = format_element! { <div>"The count is" {count}</div> };
insta::assert_snapshot!(formatted, @r#"<div>"The count is" {count}</div>"#);
let formatted = format_element! { <div>"The count is " {count}</div> };
insta::assert_snapshot!(formatted, @r#"<div>"The count is " {count}</div>"#);
}

#[test]
Expand All @@ -266,6 +276,30 @@ mod tests {
"#);
}

#[test]
fn child_element_two_textual_unquoted() {
let formatted = format_element_from_string! { "<div>The count is {count}.</div>" };
insta::assert_snapshot!(formatted, @r#"<div>The count is {count}.</div>"#);
}

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

#[test]
fn child_element_many_textual_unquoted() {
let formatted = format_element_from_string! { "<div>The current count is: {count}. Increment by one is this: {count + 1}</div>" };
insta::assert_snapshot!(formatted, @r###"
<div>
The current count is: {count}. Increment by one is this:
{count + 1}
</div>
"###);
}
// view! { <p>Something: {something} .</p> }

#[test]
fn html_unquoted_text() {
let formatted = format_element_from_string!(r##"<div>Unquoted text</div>"##);
Expand Down
4 changes: 1 addition & 3 deletions formatter/src/formatter/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ impl<'a> ViewMacro<'a> {
(cx, comma)
};

let Some((tokens, global_class)) = extract_global_class(tokens) else {
return None;
};
let (tokens, global_class) = extract_global_class(tokens)?;

let span = mac.span();
let nodes = rstml::parse2(tokens).ok()?;
Expand Down

0 comments on commit f50dd35

Please sign in to comment.