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

feat: support let bindings #149

Merged
merged 2 commits into from
Oct 7, 2024
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
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 @@
.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 All @@ -157,7 +157,7 @@
use crate::formatter::*;
use crate::test_helpers::format_element_from_string;

macro_rules! format_element {

Check warning on line 160 in formatter/src/formatter/expr.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused macro definition: `format_element`
($($tt:tt)*) => {{
let settings = FormatterSettings {
max_width: 40,
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
Loading