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

fix: restore array syntax for setting multiple classes dynamically (closes #3151) #3152

Merged
merged 1 commit into from
Oct 24, 2024
Merged
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
79 changes: 71 additions & 8 deletions leptos_macro/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use std::{
collections::{HashMap, HashSet, VecDeque},
};
use syn::{
spanned::Spanned, Expr, Expr::Tuple, ExprLit, ExprRange, Lit, LitStr,
RangeLimits, Stmt,
spanned::Spanned, Expr, Expr::Tuple, ExprArray, ExprLit, ExprRange, Lit,
LitStr, RangeLimits, Stmt,
};

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -650,6 +650,9 @@ pub(crate) fn element_to_tokens(
_ => None,
};
match (key_a.as_deref(), key_b.as_deref()) {
(Some("class"), Some("class")) | (Some("style"), Some("style")) => {
Ordering::Equal
}
(Some("class"), _) | (Some("style"), _) => Ordering::Less,
(_, Some("class")) | (_, Some("style")) => Ordering::Greater,
_ => Ordering::Equal,
Expand All @@ -661,9 +664,18 @@ pub(crate) fn element_to_tokens(
for attr in node.attributes() {
if let NodeAttribute::Attribute(attr) = attr {
let mut name = attr.key.to_string();
if let Some(tuple_name) = tuple_name(&name, attr) {
name.push(':');
name.push_str(&tuple_name);
match tuple_name(&name, attr) {
TupleName::None => {}
TupleName::Str(tuple_name) => {
name.push(':');
name.push_str(&tuple_name);
}
TupleName::Array(names) => {
for tuple_name in names {
name.push(':');
name.push_str(&tuple_name);
}
}
}
if names.contains(&name) {
proc_macro_error2::emit_error!(
Expand Down Expand Up @@ -1183,6 +1195,32 @@ fn class_to_tokens(
class: TokenStream,
class_name: Option<&str>,
) -> TokenStream {
// case of class=(["foo", "bar"], /* something */)
// just expands to multiple uses of class:
if let Some(Tuple(tuple)) = node.value() {
if tuple.elems.len() == 2 {
let name = &tuple.elems[0];
let value = &tuple.elems[1];
if let Expr::Array(ExprArray { elems, .. }) = name {
return elems
.iter()
.map(|elem| match elem {
Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) => quote! {
.#class((#s, #value))
},
_ => proc_macro_error2::abort!(
elem.span(),
"invalid name"
),
})
.collect();
}
}
}

// default case
let value = attribute_value(node, false);
if let Some(class_name) = class_name {
quote! {
Expand Down Expand Up @@ -1617,7 +1655,7 @@ pub(crate) fn directive_call_from_attribute_node(
quote! { .directive(#handler, #[allow(clippy::useless_conversion)] #param) }
}

fn tuple_name(name: &str, node: &KeyedAttribute) -> Option<String> {
fn tuple_name(name: &str, node: &KeyedAttribute) -> TupleName {
if name == "style" || name == "class" {
if let Some(Tuple(tuple)) = node.value() {
{
Expand All @@ -1627,12 +1665,37 @@ fn tuple_name(name: &str, node: &KeyedAttribute) -> Option<String> {
lit: Lit::Str(s), ..
}) = style_name
{
return Some(s.value());
return TupleName::Str(s.value());
} else if let Expr::Array(ExprArray { elems, .. }) =
style_name
{
return TupleName::Array(
elems
.iter()
.filter_map(|elem| match elem {
Expr::Lit(ExprLit {
lit: Lit::Str(s),
..
}) => Some(s.value()),
_ => proc_macro_error2::abort!(
elem.span(),
"invalid name"
),
})
.collect(),
);
}
}
}
}
}

None
TupleName::None
}

#[derive(Debug)]
enum TupleName {
None,
Str(String),
Array(Vec<String>),
}