From 77feaac49d91aebe96898eaf09e460fe993a519f Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sun, 26 Nov 2023 14:07:18 -0500 Subject: [PATCH] this should be better... --- leptos_macro/src/component.rs | 25 ++++++++----------------- leptos_macro/src/lib.rs | 22 +++++++++------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/leptos_macro/src/component.rs b/leptos_macro/src/component.rs index dc9902b930..3c7b4d060b 100644 --- a/leptos_macro/src/component.rs +++ b/leptos_macro/src/component.rs @@ -137,7 +137,7 @@ impl ToTokens for Model { } } - let module_name = module_name_from_fn(body); + let module_name = module_name_from_fn_signature(&body.sig); #[allow(clippy::redundant_clone)] // false positive let body_name = body.sig.ident.clone(); @@ -544,10 +544,10 @@ impl Model { /// used to improve IDEs and rust-analyzer's auto-completion behavior in case /// of a syntax error. pub struct DummyModel { - attrs: Vec, - vis: Visibility, - sig: Signature, - body: TokenStream, + pub attrs: Vec, + pub vis: Visibility, + pub sig: Signature, + pub body: TokenStream, } impl Parse for DummyModel { @@ -1160,21 +1160,12 @@ fn is_valid_into_view_return_type(ty: &ReturnType) -> bool { .any(|test| ty == test) } -pub fn module_name_from_fn(body: &ItemFn) -> Ident { - let snake = &body - .sig +pub fn module_name_from_fn_signature(sig: &Signature) -> Ident { + let snake = &sig .ident .to_string() .from_case(Case::Camel) .to_case(Case::Snake); let name = format!("component_module_{snake}"); - Ident::new(&name, body.sig.ident.span()) -} - -pub fn strip_argument_attributes(fun: &mut ItemFn) { - for argument in fun.sig.inputs.iter_mut() { - if let FnArg::Typed(ref mut argument) = argument { - argument.attrs.clear(); - } - } + Ident::new(&name, sig.ident.span()) } diff --git a/leptos_macro/src/lib.rs b/leptos_macro/src/lib.rs index 822bdaf360..a7dd51f296 100644 --- a/leptos_macro/src/lib.rs +++ b/leptos_macro/src/lib.rs @@ -4,13 +4,12 @@ #[macro_use] extern crate proc_macro_error; +use component::DummyModel; use proc_macro::TokenStream; use proc_macro2::{Span, TokenTree}; use quote::ToTokens; use rstml::{node::KeyedAttribute, parse}; -use syn::{ - parse_macro_input, spanned::Spanned, token::Pub, ItemFn, Visibility, -}; +use syn::{parse_macro_input, spanned::Spanned, token::Pub, Visibility}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub(crate) enum Mode { @@ -33,7 +32,7 @@ impl Default for Mode { mod params; mod view; -use crate::component::{module_name_from_fn, strip_argument_attributes}; +use crate::component::module_name_from_fn_signature; use view::{client_template::render_template, render_view}; mod component; mod server; @@ -601,19 +600,17 @@ pub fn component(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream { false }; - let mut fn_result = syn::parse::(s.clone()); + let mut dummy = syn::parse::(s.clone()); let parse_result = syn::parse::(s); - if let (Ok(ref mut unexpanded), Ok(model)) = (&mut fn_result, parse_result) - { + if let (Ok(ref mut unexpanded), Ok(model)) = (&mut dummy, parse_result) { let expanded = model.is_transparent(is_transparent).into_token_stream(); if !matches!(unexpanded.vis, Visibility::Public(_)) { unexpanded.vis = Visibility::Public(Pub { span: unexpanded.vis.span(), }) } - let module_name = module_name_from_fn(unexpanded); - strip_argument_attributes(unexpanded); + let module_name = module_name_from_fn_signature(&unexpanded.sig); quote! { #expanded #[doc(hidden)] @@ -624,16 +621,15 @@ pub fn component(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream { #unexpanded } } - } else if let Ok(mut unexpanded) = fn_result { - let module_name = module_name_from_fn(&unexpanded); - strip_argument_attributes(&mut unexpanded); + } else if let Ok(dummy) = dummy { + let module_name = module_name_from_fn_signature(&dummy.sig); quote! { #[doc(hidden)] mod #module_name { use super::*; #[allow(non_snake_case, dead_code)] - #unexpanded + #dummy } } } else {