Skip to content

Commit

Permalink
feat: improved macro hygiene (#2084)
Browse files Browse the repository at this point in the history
  • Loading branch information
blorbb authored Nov 29, 2023
1 parent 1272bd1 commit 19711e1
Show file tree
Hide file tree
Showing 18 changed files with 1,128 additions and 346 deletions.
24 changes: 13 additions & 11 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl ToTokens for Model {
} else {
quote! {
::leptos::leptos_dom::Component::new(
stringify!(#name),
::std::stringify!(#name),
move || {
#tracing_guard_expr
#tracing_props_expr
Expand Down Expand Up @@ -291,13 +291,14 @@ impl ToTokens for Model {
&& cfg!(feature = "ssr")
{
quote! {
let children = Box::new(|| ::leptos::Fragment::lazy(|| vec![
let children = ::std::boxed::Box::new(|| ::leptos::Fragment::lazy(|| ::std::vec![
::leptos::SharedContext::with_hydration(move || {
::leptos::leptos_dom::html::custom(
::leptos::leptos_dom::html::Custom::new("leptos-children"),
::leptos::IntoView::into_view(
::leptos::leptos_dom::html::custom(
::leptos::leptos_dom::html::Custom::new("leptos-children"),
)
.child(::leptos::SharedContext::no_hydration(children))
)
.child(::leptos::SharedContext::no_hydration(children))
.into_view()
})
]));
}
Expand Down Expand Up @@ -411,13 +412,14 @@ impl ToTokens for Model {
};
let children = if is_island_with_children {
quote! {
.children(Box::new(move || ::leptos::Fragment::lazy(|| vec![
.children(::std::boxed::Box::new(move || ::leptos::Fragment::lazy(|| ::std::vec![
::leptos::SharedContext::with_hydration(move || {
::leptos::leptos_dom::html::custom(
::leptos::leptos_dom::html::Custom::new("leptos-children"),
::leptos::IntoView::into_view(
::leptos::leptos_dom::html::custom(
::leptos::leptos_dom::html::Custom::new("leptos-children"),
)
.prop("$$owner", ::leptos::Owner::current().map(|n| n.as_ffi()))
)
.prop("$$owner", ::leptos::Owner::current().map(|n| n.as_ffi()))
.into_view()
})])))
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion leptos_macro/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn params_impl(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
let span = field.span();

quote_spanned! {
span => #ident: <#ty>::into_param(map.get(#field_name_string).map(::std::string::String::as_str), #field_name_string)?
span=> #ident: <#ty as ::leptos_router::IntoParam>::into_param(
map.get(#field_name_string).map(::std::string::String::as_str),
#field_name_string
)?
}
})
.collect()
Expand Down
50 changes: 16 additions & 34 deletions leptos_macro/src/view/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn fragment_to_tokens(
)?;

Some(quote! {
#node.into_view()
::leptos::IntoView::into_view(#[allow(unused_braces)] {#node})
})
})
.peekable();
Expand Down Expand Up @@ -305,11 +305,8 @@ pub(crate) fn element_to_tokens(
global_class,
None,
)
.unwrap_or({
let span = Span::call_site();
quote_spanned! {
span => ::leptos::leptos_dom::Unit
}
.unwrap_or(quote_spanned! {
Span::call_site()=> ::leptos::leptos_dom::Unit
}),
false,
),
Expand Down Expand Up @@ -378,7 +375,7 @@ pub(crate) fn attribute_to_tokens(
let name = node.key.to_string();
if name == "ref" || name == "_ref" || name == "ref_" || name == "node_ref" {
let value = expr_to_ident(attribute_value(node));
let node_ref = quote_spanned! { span => node_ref };
let node_ref = quote_spanned! { span=> node_ref };

quote! {
.#node_ref(#value)
Expand Down Expand Up @@ -415,28 +412,23 @@ pub(crate) fn attribute_to_tokens(
NodeName::Punctuated(parts) => &parts[0],
_ => unreachable!(),
};
let on = {
let span = on.span();
quote_spanned! {
span => .on
}
let on = quote_spanned! {
on.span()=> .on
};
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
ev_name.span()=> #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
undelegated.span()=> #undelegated
}
} else {
quote! { undelegated }
Expand All @@ -455,11 +447,8 @@ pub(crate) fn attribute_to_tokens(
NodeName::Punctuated(parts) => &parts[0],
_ => unreachable!(),
};
let prop = {
let span = prop.span();
quote_spanned! {
span => .prop
}
let prop = quote_spanned! {
prop.span()=> .prop
};
quote! {
#prop(#name, #[allow(unused_braces)] {#value})
Expand All @@ -470,11 +459,8 @@ pub(crate) fn attribute_to_tokens(
NodeName::Punctuated(parts) => &parts[0],
_ => unreachable!(),
};
let class = {
let span = class.span();
quote_spanned! {
span => .class
}
let class = quote_spanned! {
class.span()=> .class
};
quote! {
#class(#name, #[allow(unused_braces)] {#value})
Expand All @@ -485,11 +471,8 @@ pub(crate) fn attribute_to_tokens(
NodeName::Punctuated(parts) => &parts[0],
_ => unreachable!(),
};
let style = {
let span = style.span();
quote_spanned! {
span => .style
}
let style = quote_spanned! {
style.span()=> .style
};
quote! {
#style(#name, #[allow(unused_braces)] {#value})
Expand Down Expand Up @@ -518,17 +501,16 @@ pub(crate) fn attribute_to_tokens(
Some(value) => {
quote! { #value }
}
None => quote_spanned! { span => "" },
None => quote_spanned! { span=> "" },
};

let attr = match &node.key {
NodeName::Punctuated(parts) => Some(&parts[0]),
_ => None,
};
let attr = if let Some(attr) = attr {
let span = attr.span();
quote_spanned! {
span => .attr
attr.span()=> .attr
}
} else {
quote! {
Expand Down
52 changes: 35 additions & 17 deletions leptos_macro/src/view/client_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,25 @@ fn element_to_tokens(
let debug_name = node.name().to_string();
let this_nav = if is_root_el {
quote_spanned! {
span => let #this_el_ident = #debug_name;
span=> let #this_el_ident = #debug_name;
let #this_el_ident =
::leptos::wasm_bindgen::JsCast::unchecked_into::<leptos::web_sys::Node>(#parent.clone());
::leptos::wasm_bindgen::JsCast::unchecked_into::<::leptos::web_sys::Node>(
#parent.clone()
);
//debug!("=> got {}", #this_el_ident.node_name());
}
} else if let Some(prev_sib) = &prev_sib {
quote_spanned! {
span => let #this_el_ident = #debug_name;
span=> let #this_el_ident = #debug_name;
//log::debug!("next_sibling ({})", #debug_name);
let #this_el_ident = #prev_sib.next_sibling().unwrap_or_else(|| panic!("error : {} => {} ", #debug_name, "nextSibling"));
let #this_el_ident = #prev_sib.next_sibling().unwrap_or_else(|| ::std::panic!("error : {} => {} ", #debug_name, "nextSibling"));
//log::debug!("=> got {}", #this_el_ident.node_name());
}
} else {
quote_spanned! {
span => let #this_el_ident = #debug_name;
span=> let #this_el_ident = #debug_name;
//log::debug!("first_child ({})", #debug_name);
let #this_el_ident = #parent.first_child().unwrap_or_else(|| panic!("error: {} => {}", #debug_name, "firstChild"));
let #this_el_ident = #parent.first_child().unwrap_or_else(|| ::std::panic!("error: {} => {}", #debug_name, "firstChild"));
//log::debug!("=> got {}", #this_el_ident.node_name());
}
};
Expand Down Expand Up @@ -297,23 +299,35 @@ fn attr_to_tokens(
let (event_type, handler) =
crate::view::event_from_attribute_node(node, false);
expressions.push(quote! {
::leptos::leptos_dom::add_event_helper(::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id), #event_type, #handler);
::leptos::leptos_dom::add_event_helper(
::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id),
#event_type,
#handler,
);
})
}
// Properties
else if let Some(name) = name.strip_prefix("prop:") {
let value = attribute_value(node);

expressions.push(quote_spanned! {
span => ::leptos::leptos_dom::property(::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id), #name, #value.into_property())
span=> ::leptos::leptos_dom::property(
::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id),
#name,
::leptos::IntoProperty::into_property(#value),
)
});
}
// Classes
else if let Some(name) = name.strip_prefix("class:") {
let value = attribute_value(node);

expressions.push(quote_spanned! {
span => ::leptos::leptos_dom::class_helper(leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id), #name.into(), #value.into_class())
span=> ::leptos::leptos_dom::class_helper(
::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id),
#name.into(),
::leptos::IntoClass::into_class(#value),
)
});
}
// Attributes
Expand All @@ -337,7 +351,11 @@ fn attr_to_tokens(
// For client-side rendering, dynamic attributes don't need to be rendered in the template
// They'll immediately be set synchronously before the cloned template is mounted
expressions.push(quote_spanned! {
span => ::leptos::leptos_dom::attribute_helper(leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id), #name.into(), {#value}.into_attribute())
span=> ::leptos::leptos_dom::attribute_helper(
::leptos::wasm_bindgen::JsCast::unchecked_ref(&#el_id),
#name.into(),
::leptos::IntoAttribute::into_attribute(#[allow(unused_braces)] {#value}),
)
});
}
}
Expand Down Expand Up @@ -465,15 +483,15 @@ fn block_to_tokens(
let name = child_ident(*next_el_id, span);
let location = if let Some(sibling) = &prev_sib {
quote_spanned! {
span => //log::debug!("-> next sibling");
let #name = #sibling.next_sibling().unwrap_or_else(|| panic!("error : {} => {} ", "{block}", "nextSibling"));
//log::debug!("\tnext sibling = {}", #name.node_name());
span=> //log::debug!("-> next sibling");
let #name = #sibling.next_sibling().unwrap_or_else(|| ::std::panic!("error : {} => {} ", "{block}", "nextSibling"));
//log::debug!("\tnext sibling = {}", #name.node_name());
}
} else {
quote_spanned! {
span => //log::debug!("\\|/ first child on {}", #parent.node_name());
let #name = #parent.first_child().unwrap_or_else(|| panic!("error : {} => {} ", "{block}", "firstChild"));
//log::debug!("\tfirst child = {}", #name.node_name());
span=> //log::debug!("\\|/ first child on {}", #parent.node_name());
let #name = #parent.first_child().unwrap_or_else(|| ::std::panic!("error : {} => {} ", "{block}", "firstChild"));
//log::debug!("\tfirst child = {}", #name.node_name());
}
};
(Some(name), location)
Expand Down Expand Up @@ -504,7 +522,7 @@ fn block_to_tokens(
navigations.push(location);

expressions.push(quote! {
::leptos::leptos_dom::mount_child(#mount_kind, &{#value}.into_view());
::leptos::leptos_dom::mount_child(#mount_kind, &::leptos::IntoView::into_view(#[allow(unused_braces)] {#value}));
});

if let Some(name) = name {
Expand Down
6 changes: 3 additions & 3 deletions leptos_macro/src/view/component_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) fn component_to_tokens(
let value = attr.value().map(|v| {
quote! { #v }
})?;
Some(quote! { (#name, #value.into_attribute()) })
Some(quote! { (#name, ::leptos::IntoAttribute::into_attribute(#value)) })
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -172,7 +172,7 @@ pub(crate) fn component_to_tokens(
let slot = Ident::new(&slot, span);
if values.len() > 1 {
quote! {
.#slot(vec![
.#slot(::std::vec![
#(#values)*
])
}
Expand Down Expand Up @@ -211,7 +211,7 @@ pub(crate) fn component_to_tokens(
component
} else {
quote! {
#component.into_view()
::leptos::IntoView::into_view(#[allow(unused_braces)] {#component})
#(#events_and_directives)*
}
}
Expand Down
4 changes: 2 additions & 2 deletions leptos_macro/src/view/ide_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ impl IdeTagHelper {
// todo: check is html, and emit_warning in case of custom tag
quote! { ::leptos::leptos_dom::html }
};
quote!( #namespace::#name)
quote! { #namespace::#name }
}

/// Returns `syn::Path`-like `TokenStream` to the `custom` section in docs.
fn create_custom_tag_fn_path(span: Span) -> TokenStream {
let custom_ident = Ident::new("custom", span);
quote! {leptos::leptos_dom::html::#custom_ident::<leptos::leptos_dom::html::Custom>}
quote! { ::leptos::leptos_dom::html::#custom_ident::<::leptos::leptos_dom::html::Custom> }
}

// Extract from NodeName completion idents.
Expand Down
8 changes: 3 additions & 5 deletions leptos_macro/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn render_view(
let empty = {
let span = Span::call_site();
quote_spanned! {
span => leptos::leptos_dom::Unit
span=> ::leptos::leptos_dom::Unit
}
};

Expand Down Expand Up @@ -402,9 +402,8 @@ fn fancy_class_name<'a>(
if name == "class" {
if let Some(Tuple(tuple)) = node.value() {
if tuple.elems.len() == 2 {
let span = node.key.span();
let class = quote_spanned! {
span => .class
node.key.span()=> .class
};
let class_name = &tuple.elems[0];
let class_name = if let Expr::Lit(ExprLit {
Expand Down Expand Up @@ -472,9 +471,8 @@ fn fancy_style_name<'a>(
if name == "style" {
if let Some(Tuple(tuple)) = node.value() {
if tuple.elems.len() == 2 {
let span = node.key.span();
let style = quote_spanned! {
span => .style
node.key.span()=> .style
};
let style_name = &tuple.elems[0];
let style_name = if let Expr::Lit(ExprLit {
Expand Down
Loading

0 comments on commit 19711e1

Please sign in to comment.