Skip to content

Commit

Permalink
chore: upgrade to rstml 12
Browse files Browse the repository at this point in the history
  • Loading branch information
bram209 committed Feb 2, 2025
1 parent 2c42b64 commit d02647f
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 56 deletions.
68 changes: 49 additions & 19 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 @@ -10,7 +10,7 @@ description = "view macro formatter for the Leptos web framework"
[dependencies]
leptosfmt-pretty-printer.workspace = true
leptosfmt-prettyplease.workspace = true
rstml = "0.11.2"
rstml = "0.12.1"
syn = { workspace = true }
proc-macro2 = { workspace = true }
thiserror = "1.0.61"
Expand Down
53 changes: 32 additions & 21 deletions formatter/src/formatter/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use rstml::node::{FnBinding, KeyedAttribute, KeyedAttributeValue, NodeAttribute, NodeBlock};
use rstml::node::{
FnBinding, KVAttributeValue, KeyedAttribute, KeyedAttributeValue, NodeAttribute, NodeBlock,
};
use syn::{spanned::Spanned, Expr, RangeLimits, Stmt};

use crate::{formatter::Formatter, AttributeValueBraceStyle as Braces};
Expand Down Expand Up @@ -52,31 +54,40 @@ impl Formatter<'_> {

fn attribute_value(
&mut self,
value: &Expr,
value: &KVAttributeValue,
formatter: Option<ExpressionFormatter>,
next_attribute: Option<&NodeAttribute>,
) {
match (self.settings.attr_value_brace_style, value, next_attribute) {
(Braces::WhenRequired, syn::Expr::Block(_), Some(next))
if is_spread_attribute(next) =>
{
// If the next attribute is a spread attribute, make sure that the braces are not stripped from the expression
// to avoid an ambiguity in the parser (i.e. `foo=bar {..}` could be interpreted as initialization of a struct called `bar`, instead of two separate attributes)
self.node_value_expr(value, false, true, formatter)
match value {
KVAttributeValue::InvalidBraced(invalid) => {
self.printer.word(invalid.span().source_text().unwrap());
}
(Braces::Always, syn::Expr::Block(_), _) => {
self.node_value_expr(value, false, false, formatter)
KVAttributeValue::Expr(expr) => {
match (self.settings.attr_value_brace_style, expr, next_attribute) {
(Braces::WhenRequired, syn::Expr::Block(_), Some(next))
if is_spread_attribute(next) =>
{
// If the next attribute is a spread attribute, make sure that the braces are not stripped from the expression
// to avoid an ambiguity in the parser (i.e. `foo=bar {..}` could be interpreted as initialization of a struct called `bar`, instead of two separate attributes)
self.node_value_expr(expr, false, true, formatter)
}
(Braces::Always, syn::Expr::Block(_), _) => {
self.node_value_expr(expr, false, false, formatter)
}
(Braces::AlwaysUnlessLit, syn::Expr::Block(_) | syn::Expr::Lit(_), _) => {
self.node_value_expr(expr, false, true, formatter)
}
(Braces::Always | Braces::AlwaysUnlessLit, _, _) => {
self.printer.word("{");
self.node_value_expr(expr, false, false, formatter);
self.printer.word("}");
}
(Braces::WhenRequired, _, _) => {
self.node_value_expr(expr, true, true, formatter)
}
(Braces::Preserve, _, _) => self.node_value_expr(expr, false, false, formatter),
}
}
(Braces::AlwaysUnlessLit, syn::Expr::Block(_) | syn::Expr::Lit(_), _) => {
self.node_value_expr(value, false, true, formatter)
}
(Braces::Always | Braces::AlwaysUnlessLit, _, _) => {
self.printer.word("{");
self.node_value_expr(value, false, false, formatter);
self.printer.word("}");
}
(Braces::WhenRequired, _, _) => self.node_value_expr(value, true, true, formatter),
(Braces::Preserve, _, _) => self.node_value_expr(value, false, false, formatter),
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions formatter/src/formatter/element.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::{formatter::Formatter, ClosingTagStyle};

use rstml::node::{Node, NodeAttribute, NodeElement};
use rstml::{
atoms::{CloseTag, OpenTag},

Check warning on line 4 in formatter/src/formatter/element.rs

View workflow job for this annotation

GitHub Actions / Check

unused imports: `CloseTag` and `CustomNode`

Check warning on line 4 in formatter/src/formatter/element.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `CloseTag` and `CustomNode`
node::{CustomNode, Node, NodeAttribute, NodeElement},
Infallible,
};
use syn::spanned::Spanned;

impl Formatter<'_> {
pub fn element(&mut self, element: &NodeElement) {
pub fn element(&mut self, element: &NodeElement<Infallible>) {
let name = element.name().to_string();
let is_self_closing = is_self_closing(element, &name, self.settings.closing_tag_style);

Expand All @@ -13,11 +17,12 @@ impl Formatter<'_> {
if !is_self_closing {
self.children(&element.children, element.attributes().len());
self.flush_comments(element.close_tag.span().end().line - 1, true);
self.closing_tag(element)
// Note: we pass open_tag instead of close_tag, such that we may auto-close non-self-closing elements
self.closing_tag(&element.open_tag);
}
}

fn opening_tag(&mut self, element: &NodeElement, is_self_closing: bool) {
fn opening_tag(&mut self, element: &NodeElement<Infallible>, is_self_closing: bool) {
self.printer.word("<");
self.node_name(&element.open_tag.name);
self.format_syn_generics(&element.open_tag.generics);
Expand All @@ -31,10 +36,10 @@ impl Formatter<'_> {
}
}

fn closing_tag(&mut self, element: &NodeElement) {
fn closing_tag(&mut self, open_tag: &OpenTag) {
self.printer.word("</");
self.node_name(element.name());
self.format_syn_generics(&element.open_tag.generics);
self.node_name(&open_tag.name);
self.format_syn_generics(&open_tag.generics);
self.printer.word(">");
}

Expand Down Expand Up @@ -149,7 +154,11 @@ fn is_void_element(name: &str) -> bool {
)
}

fn is_self_closing(element: &NodeElement, name: &str, closing_tag_style: ClosingTagStyle) -> bool {
fn is_self_closing(
element: &NodeElement<Infallible>,
name: &str,
closing_tag_style: ClosingTagStyle,
) -> bool {
if !element.children.is_empty() {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions formatter/src/formatter/fragment.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rstml::node::NodeFragment;
use rstml::{node::NodeFragment, Infallible};

use crate::formatter::Formatter;

impl Formatter<'_> {
pub fn fragment(&mut self, fragment: &NodeFragment) {
pub fn fragment(&mut self, fragment: &NodeFragment<Infallible>) {
self.printer.word("<>");
self.children(&fragment.children, 0);
self.printer.word("</>");
Expand Down
6 changes: 5 additions & 1 deletion formatter/src/formatter/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use rstml::node::{Node, NodeBlock, NodeComment, NodeDoctype, NodeName, NodeText, RawText};
use rstml::{
node::{CustomNode, Node, NodeBlock, NodeComment, NodeDoctype, NodeName, NodeText, RawText},

Check warning on line 2 in formatter/src/formatter/node.rs

View workflow job for this annotation

GitHub Actions / Check

unused imports: `CustomNode` and `Infallible`

Check warning on line 2 in formatter/src/formatter/node.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `CustomNode` and `Infallible`
Infallible,
};
use syn::spanned::Spanned;

use crate::{formatter::Formatter, get_text_beween_spans};
Expand All @@ -15,6 +18,7 @@ impl Formatter<'_> {
Node::Comment(comment) => self.comment(comment),
Node::Doctype(doctype) => self.doctype(doctype),
Node::Block(block) => self.node_block(block),
Node::Custom(_) => todo!(),
};
}

Expand Down
9 changes: 6 additions & 3 deletions formatter/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::str::FromStr;

use crop::Rope;
use leptosfmt_pretty_printer::Printer;
use rstml::node::{Node, NodeAttribute, NodeComment, NodeDoctype, NodeElement, NodeFragment};
use rstml::{
node::{Node, NodeAttribute, NodeComment, NodeDoctype, NodeElement, NodeFragment},
Infallible,
};

macro_rules! attribute {
($($tt:tt)*) => {
Expand Down Expand Up @@ -85,14 +88,14 @@ pub fn get_element_attribute(
.clone()
}

pub fn get_element(mut nodes: Vec<Node>, element_index: usize) -> NodeElement {
pub fn get_element(mut nodes: Vec<Node>, element_index: usize) -> NodeElement<Infallible> {
let Node::Element(element) = nodes.swap_remove(element_index) else {
panic!("expected element")
};
element
}

pub fn get_fragment(mut nodes: Vec<Node>, fragment_index: usize) -> NodeFragment {
pub fn get_fragment(mut nodes: Vec<Node>, fragment_index: usize) -> NodeFragment<Infallible> {
let Node::Fragment(fragment) = nodes.swap_remove(fragment_index) else {
panic!("expected fragment")
};
Expand Down
2 changes: 1 addition & 1 deletion printer/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl Printer {
}
self.out.push('\n');
let indent = self.indent as isize + token.offset;
self.pending_indentation = usize::try_from(indent).unwrap();
self.pending_indentation = usize::try_from(indent).unwrap_or(0);
self.space = cmp::max(self.settings.margin - indent, self.settings.min_space);
if !token.post_break.is_empty() {
self.print_indent();
Expand Down

0 comments on commit d02647f

Please sign in to comment.