Skip to content

Commit

Permalink
Destructuring let (0.7) (#2655)
Browse files Browse the repository at this point in the history
* Use `let()` syntax for bindings

This lets users use destructuring when binding more complex values, and we also get better IDE support.

* Update rstml
  • Loading branch information
Jinxit authored and gbj committed Jul 4, 2024
1 parent 5ffcfc4 commit 3797a41
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion leptos_hot_reload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ syn = { version = "2", features = [
"printing",
] }
quote = "1"
rstml = "0.11.0"
rstml = "0.11.2"
proc-macro2 = { version = "1", features = ["span-locations", "nightly"] }
parking_lot = "0.12"
walkdir = "2"
Expand Down
2 changes: 1 addition & 1 deletion leptos_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ proc-macro-error = { version = "1", default-features = false }
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
rstml = "0.11.0"
rstml = "0.11.2"
leptos_hot_reload = { workspace = true }
server_fn_macro = { workspace = true }
convert_case = "0.6.0"
Expand Down
34 changes: 27 additions & 7 deletions leptos_macro/src/view/component_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{fragment_to_tokens, TagType};
use crate::view::attribute_absolute;
use proc_macro2::{Ident, TokenStream, TokenTree};
use quote::{format_ident, quote, quote_spanned};
use rstml::node::{NodeAttribute, NodeBlock, NodeElement};
use rstml::node::{KeyedAttributeValue, NodeAttribute, NodeBlock, NodeElement, NodeName};
use std::collections::HashMap;
use syn::{spanned::Spanned, Expr, ExprRange, RangeLimits, Stmt};
use syn::{spanned::Spanned, Expr, ExprRange, RangeLimits, Stmt, ExprPath};

pub(crate) fn component_to_tokens(
node: &NodeElement,
Expand Down Expand Up @@ -56,7 +56,7 @@ pub(crate) fn component_to_tokens(
.filter(|(idx, attr)| {
idx < &spread_marker && {
let attr_key = attr.key.to_string();
!attr_key.starts_with("let:")
!is_attr_let(&attr.key)
&& !attr_key.starts_with("clone:")
&& !attr_key.starts_with("class:")
&& !attr_key.starts_with("style:")
Expand Down Expand Up @@ -88,10 +88,20 @@ pub(crate) fn component_to_tokens(
let items_to_bind = attrs
.clone()
.filter_map(|attr| {
attr.key
.to_string()
.strip_prefix("let:")
.map(|ident| format_ident!("{ident}", span = attr.key.span()))
if !is_attr_let(&attr.key) {
return None;
}

let KeyedAttributeValue::Binding(binding) = &attr.possible_value else {
if let Some(ident) = attr.key.to_string().strip_prefix("let:") {
let ident1 = format_ident!("{ident}", span = attr.key.span());
return Some(quote! { #ident1 });
} else {
return None;
}
};
let inputs = &binding.inputs;
Some(quote! { #inputs })
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -288,3 +298,13 @@ pub(crate) fn component_to_tokens(

component
}

fn is_attr_let(key: &NodeName) -> bool {
if key.to_string().starts_with("let:") {
true
} else if let NodeName::Path(ExprPath { path, .. }) = key {
path.segments.len() == 1 && path.segments[0].ident == "let"
} else {
false
}
}

0 comments on commit 3797a41

Please sign in to comment.