Skip to content

Commit

Permalink
this should be better...
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Nov 26, 2023
1 parent 49ef2bb commit 77feaac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
25 changes: 8 additions & 17 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<Attribute>,
vis: Visibility,
sig: Signature,
body: TokenStream,
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub sig: Signature,
pub body: TokenStream,
}

impl Parse for DummyModel {
Expand Down Expand Up @@ -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())
}
22 changes: 9 additions & 13 deletions leptos_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -601,19 +600,17 @@ pub fn component(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
false
};

let mut fn_result = syn::parse::<ItemFn>(s.clone());
let mut dummy = syn::parse::<DummyModel>(s.clone());
let parse_result = syn::parse::<component::Model>(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)]
Expand All @@ -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 {
Expand Down

0 comments on commit 77feaac

Please sign in to comment.