From fcdfd617f530fa4d3463ee40bb368a9820679fd0 Mon Sep 17 00:00:00 2001 From: Chris <89366859+chrisp60@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:25:56 -0400 Subject: [PATCH] fix: redundant spans (#2836) Per https://docs.rs/quote/latest/quote/macro.quote.html#hygiene regarding token hygiene. > Any interpolated tokens preserve the Span information provided by their > `ToTokens` implementation. In many instances, the procedural macros are spanning tokens with their own span redundantly. --- leptos_macro/src/view/component_builder.rs | 26 +++++----------------- leptos_macro/src/view/mod.rs | 10 ++------- leptos_macro/src/view/slot_helper.rs | 13 +++++------ 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/leptos_macro/src/view/component_builder.rs b/leptos_macro/src/view/component_builder.rs index 129388824..baeb02b1a 100644 --- a/leptos_macro/src/view/component_builder.rs +++ b/leptos_macro/src/view/component_builder.rs @@ -78,10 +78,8 @@ pub(crate) fn component_to_tokens( }) .unwrap_or_else(|| quote! { #name }); - let value = quote_spanned!(value.span()=> { #value }); - - quote_spanned! {attr.span()=> - .#name(#[allow(unused_braces)] #value) + quote! { + .#name(#[allow(unused_braces)] { #value }) } }); @@ -262,30 +260,18 @@ pub(crate) fn component_to_tokens( quote! {} }; - let name_ref = quote_spanned! {name.span()=> - &#name - }; - - let build = quote_spanned! {name.span()=> - .build() - }; - - let component_props_builder = quote_spanned! {name.span()=> - ::leptos::component::component_props_builder(#name_ref #generics) - }; - #[allow(unused_mut)] // used in debug - let mut component = quote_spanned! {node.span()=> + let mut component = quote! { { #[allow(unreachable_code)] ::leptos::component::component_view( #[allow(clippy::needless_borrows_for_generic_args)] - #name_ref, - #component_props_builder + &#name, + ::leptos::component::component_props_builder(&#name #generics) #(#props)* #(#slots)* #children - #build + .build() ) #spreads } diff --git a/leptos_macro/src/view/mod.rs b/leptos_macro/src/view/mod.rs index f023cdd1f..77fbb7d56 100644 --- a/leptos_macro/src/view/mod.rs +++ b/leptos_macro/src/view/mod.rs @@ -695,20 +695,14 @@ pub(crate) fn event_type_and_handler( let event_type = if is_custom { event_type } else if let Some(ev_name) = event_name_ident { - let span = ev_name.span(); - quote_spanned! { - span => #ev_name - } + quote! { #ev_name } } else { event_type }; let event_type = if is_force_undelegated { let undelegated = if let Some(undelegated) = undelegated_ident { - let span = undelegated.span(); - quote_spanned! { - span => #undelegated - } + quote! { #undelegated } } else { quote! { undelegated } }; diff --git a/leptos_macro/src/view/slot_helper.rs b/leptos_macro/src/view/slot_helper.rs index bcaf6d3ea..0f292a9ce 100644 --- a/leptos_macro/src/view/slot_helper.rs +++ b/leptos_macro/src/view/slot_helper.rs @@ -59,10 +59,8 @@ pub(crate) fn slot_to_tokens( }) .unwrap_or_else(|| quote! { #name }); - let value = quote_spanned!(value.span()=> { #value }); - - quote_spanned! {attr.span()=> - .#name(#[allow(unused_braces)] #value) + quote! { + .#name(#[allow(unused_braces)] { #value }) } }); @@ -136,8 +134,9 @@ pub(crate) fn slot_to_tokens( items_to_bind.iter().map(|ident| quote! { #ident, }); let clonables = items_to_clone.iter().map(|ident| { - let ident_ref = quote_spanned!(ident.span()=> &#ident); - quote! { let #ident = ::core::clone::Clone::clone(#ident_ref); } + quote_spanned! {ident.span()=> + let #ident = ::core::clone::Clone::clone(&#ident); + } }); if bindables.len() > 0 { @@ -169,7 +168,7 @@ pub(crate) fn slot_to_tokens( .span(); let slot = Ident::new(&slot, span); let value = if values.len() > 1 { - quote_spanned! {span=> + quote! { ::std::vec![ #(#values)* ]