Skip to content

Commit

Permalink
feat: support let bindings (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
bram209 authored Oct 7, 2024
1 parent b04f9a5 commit 53cbd8c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
53 changes: 44 additions & 9 deletions formatter/src/formatter/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rstml::node::{KeyedAttribute, NodeAttribute};
use rstml::node::{FnBinding, KeyedAttribute, KeyedAttributeValue, NodeAttribute};
use syn::{spanned::Spanned, Expr};

use crate::{formatter::Formatter, AttributeValueBraceStyle as Braces};
Expand All @@ -17,16 +17,33 @@ impl Formatter<'_> {
pub fn keyed_attribute(&mut self, attribute: &KeyedAttribute) {
self.node_name(&attribute.key);

if let Some(value) = attribute.value() {
let formatter = self
.settings
.attr_values
.get(&attribute.key.to_string())
.copied();
match &attribute.possible_value {
KeyedAttributeValue::None => {}
KeyedAttributeValue::Binding(binding) => self.attribute_binding(binding),
KeyedAttributeValue::Value(expr) => {
let formatter = self
.settings
.attr_values
.get(&attribute.key.to_string())
.copied();

self.printer.word("=");
self.attribute_value(&expr.value, formatter);
}
}
}

self.printer.word("=");
self.attribute_value(value, formatter);
fn attribute_binding(&mut self, binding: &FnBinding) {
self.printer.word("(");
let mut iterator = binding.inputs.iter().peekable();
while let Some(input) = iterator.next() {
self.format_syn_pat(input);
if iterator.peek().is_some() {
self.printer.word(",");
self.printer.space();
}
}
self.printer.word(")");
}

fn attribute_value(&mut self, value: &Expr, formatter: Option<ExpressionFormatter>) {
Expand Down Expand Up @@ -190,4 +207,22 @@ mod tests {
let f = format_attr_with_brace_style! { WhenRequired => alt={"test img"} };
assert_snapshot!(f, @r#"alt="test img""#);
}

#[test]
fn let_bindings_single() {
let f = format_attribute! { let(name) };
assert_snapshot!(f, @r#"let(name)"#)
}

#[test]
fn let_bindings_multiple() {
let f = format_attribute! { let(name, foo, bar) };
assert_snapshot!(f, @r#"let(name, foo, bar)"#)
}

#[test]
fn let_bindings_destructuring() {
let f = format_attribute! { let(Item { name, value }) };
assert_snapshot!(f, @r#"let(Item { name, value })"#)
}
}
2 changes: 1 addition & 1 deletion formatter/src/formatter/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Formatter<'_> {
fn opening_tag(&mut self, element: &NodeElement, is_self_closing: bool) {
self.printer.word("<");
self.node_name(&element.open_tag.name);
leptosfmt_prettyplease::unparse_generics(&element.open_tag.generics, self.printer);
self.format_syn_generics(&element.open_tag.generics);

self.attributes(element.attributes(), is_self_closing);

Expand Down
4 changes: 2 additions & 2 deletions formatter/src/formatter/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ impl Formatter<'_> {
.map(|line| (line, self.whitespace_and_comments.remove(&line).unwrap()))
.collect::<HashMap<_, _>>();

leptosfmt_prettyplease::unparse_expr(
expr,
leptosfmt_prettyplease::unparse_fn(
self.printer,
Some(&mut ViewMacroFormatter::new(
self.settings,
self.source,
&mut self.line_offset,
comments_or_whitespace,
)),
|p| p.expr(expr),
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions formatter/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use mac::{ParentIndent, ViewMacro};

use serde::Deserialize;
use serde::Serialize;
use syn::{Generics, Pat};

#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum ClosingTagStyle {
Expand Down Expand Up @@ -233,4 +234,12 @@ impl<'a> Formatter<'a> {

self.line_offset = Some(line_index);
}

pub fn format_syn_pat(&mut self, pat: &Pat) {
leptosfmt_prettyplease::unparse_fn(self.printer, None, |p| p.pat(pat));
}

pub fn format_syn_generics(&mut self, generics: &Generics) {
leptosfmt_prettyplease::unparse_fn(self.printer, None, |p| p.generics(generics));
}
}
2 changes: 1 addition & 1 deletion prettyplease
Submodule prettyplease updated 1 files
+4 −9 src/lib.rs

0 comments on commit 53cbd8c

Please sign in to comment.