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 error reporting in view macro #2289

Merged
merged 34 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8461fba
Added name span to .build in component_to_tokens
Ar4ys Jan 31, 2024
f970bcc
Added #[allow(unreachable_code)] to leptos::component_view inside co…
Ar4ys Jan 31, 2024
5b3e2b2
Added span to name reference in component_to_tokens
Ar4ys Jan 31, 2024
29fb65e
Added span to leptos::component_props_builder in component_to_tokens
Ar4ys Jan 31, 2024
34c88c7
Added span to props in component_to_tokens
Ar4ys Jan 31, 2024
78216c7
Added span to "on" method in events component_to_tokens
Ar4ys Jan 31, 2024
6db92a0
Added spans in directive_call_from_attribute_node
Ar4ys Jan 31, 2024
c8c0732
Added spans in fragment_to_tokens and it's ssr version
Ar4ys Feb 2, 2024
5c33719
Added span to props in slot_to_tokens
Ar4ys Feb 2, 2024
f7ba3a2
Added span to the whole slot quote
Ar4ys Feb 2, 2024
c67d66d
Changed slots's name span to last slot node
Ar4ys Feb 2, 2024
e2cf8ef
Added span to the slot vec
Ar4ys Feb 2, 2024
e574956
Added #[allow(unreachable_code)] to `.into()` in slot_to_tokens
Ar4ys Feb 2, 2024
4657222
Added span to `.build()` in slot_to_tokens
Ar4ys Feb 2, 2024
dc1015d
Added span for the whole component
Ar4ys Feb 2, 2024
42dc2cf
Added span to "clone:" directive
Ar4ys Feb 3, 2024
7b56105
Added span to ".children()"
Ar4ys Feb 3, 2024
ae841f5
Removed unused "_span" param from fragment_to_tokens and fragment_to_…
Ar4ys Feb 3, 2024
030227e
Removed unnecessary parenthesis around values in `attribute_to_tokens`
Ar4ys Feb 4, 2024
8c581c7
Removed unnecessary curly braces around value in `spread_attrs`
Ar4ys Feb 5, 2024
748ebe5
Removed unnecessary parenthesis around children in `element_to_tokens`
Ar4ys Feb 5, 2024
e7de98f
Added catch-all span to element_to_tokens
Ar4ys Feb 5, 2024
d11a907
Formatted `quote!` according to official guidelines
Ar4ys Feb 5, 2024
e8ed2d1
Updated view/snapshots in leptos_macro
Ar4ys Feb 8, 2024
1958819
Added span to spread props in element_to_tokens_ssr
Ar4ys Feb 8, 2024
f67b47b
Removed unnecessary curly braces in element_to_token_ssr
Ar4ys Feb 8, 2024
0ab1953
Updated view/snapshots in leptos_macro
Ar4ys Feb 8, 2024
2c3c790
Added view macro tests to leptos_macro
Ar4ys Feb 10, 2024
9da4b7b
Fixed clippy warnings in view macro output
Ar4ys Feb 10, 2024
4a5662e
Updated view snapshots in tests
Ar4ys Feb 10, 2024
550068b
Fixed expected_one_let_bind_got_none test in leptos_macro
Ar4ys Feb 25, 2024
6bf2897
Removed snapshot tests in leptos_macro/tests/ui/view
Ar4ys Feb 27, 2024
e8adfa0
Merge branch 'main' into fix-view-error-reporting
gbj Mar 8, 2024
cf6f6d5
Merge branch 'main' into fix-view-error-reporting
gbj Apr 8, 2024
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
123 changes: 53 additions & 70 deletions leptos_macro/src/view/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub(crate) enum TagType {

#[allow(clippy::too_many_arguments)]
pub(crate) fn fragment_to_tokens(
_span: Span,
nodes: &[Node],
lazy: bool,
parent_type: TagType,
Expand All @@ -35,9 +34,16 @@ pub(crate) fn fragment_to_tokens(
let mut slots = HashMap::new();
let has_slots = parent_slots.is_some();

let original_span = nodes
.first()
.zip(nodes.last())
.and_then(|(first, last)| first.span().join(last.span()))
.unwrap_or_else(Span::call_site);

let mut nodes = nodes
.iter()
.filter_map(|node| {
let span = node.span();
let node = node_to_tokens(
node,
parent_type,
Expand All @@ -46,8 +52,12 @@ pub(crate) fn fragment_to_tokens(
None,
)?;

let node = quote_spanned! {span=>
#[allow(unused_braces)] {#node}
};

Some(quote! {
::leptos::IntoView::into_view(#[allow(unused_braces)] {#node})
::leptos::IntoView::into_view(#node)
})
})
.peekable();
Expand All @@ -72,7 +82,7 @@ pub(crate) fn fragment_to_tokens(
};

let tokens = if lazy {
quote! {
quote_spanned! {original_span=>
{
::leptos::Fragment::lazy(|| ::std::vec![
#(#nodes),*
Expand All @@ -81,7 +91,7 @@ pub(crate) fn fragment_to_tokens(
}
}
} else {
quote! {
quote_spanned! {original_span=>
{
::leptos::Fragment::new(::std::vec![
#(#nodes),*
Expand Down Expand Up @@ -112,7 +122,6 @@ pub(crate) fn node_to_tokens(
) -> Option<TokenStream> {
match node {
Node::Fragment(fragment) => fragment_to_tokens(
Span::call_site(),
&fragment.children,
true,
parent_type,
Expand Down Expand Up @@ -169,7 +178,7 @@ pub(crate) fn element_to_tokens(
let name = if is_custom_element(&tag) {
let name = node.name().to_string();
// link custom ident to name span for IDE docs
let custom = Ident::new("custom", name.span());
let custom = Ident::new("custom", node.name().span());
quote! { ::leptos::leptos_dom::html::#custom(::leptos::leptos_dom::html::Custom::new(#name)) }
} else if is_svg_element(&tag) {
parent_type = TagType::Svg;
Expand Down Expand Up @@ -237,9 +246,7 @@ pub(crate) fn element_to_tokens(
..
}),
_,
) => Some(
quote! { .bindings(#[allow(unused_brace)] {#end}) },
),
) => Some(quote! { .bindings(#end) }),
_ => None,
}
} else {
Expand Down Expand Up @@ -276,14 +283,7 @@ pub(crate) fn element_to_tokens(
});
let global_class_expr = match global_class {
None => quote! {},
Some(class) => {
quote! {
.classes(
#[allow(unused_braces)]
{#class}
)
}
}
Some(class) => quote! { .classes(#class) },
};

if is_self_closing(node) && !node.children.is_empty() {
Expand All @@ -295,65 +295,48 @@ pub(crate) fn element_to_tokens(
);
}

let children = node.children.iter().map(|node| {
let (child, is_static) = match node {
Node::Fragment(fragment) => (
fragment_to_tokens(
Span::call_site(),
&fragment.children,
true,
parent_type,
None,
global_class,
None,
)
.unwrap_or(quote_spanned! {
Span::call_site()=> ::leptos::leptos_dom::Unit
}),
false,
),
Node::Text(node) => (quote! { #node }, true),
let children = node
.children
.iter()
.map(|node| match node {
Node::Fragment(fragment) => fragment_to_tokens(
&fragment.children,
true,
parent_type,
None,
global_class,
None,
)
.unwrap_or(quote_spanned! {
Span::call_site()=> ::leptos::leptos_dom::Unit
}),
Node::Text(node) => quote! { #node },
Node::RawText(node) => {
let text = node.to_string_best();
let text = syn::LitStr::new(&text, node.span());
(quote! { #text }, true)
}
Node::Block(node) => (
quote! {
#node
},
false,
),
Node::Element(node) => (
element_to_tokens(
node,
parent_type,
None,
global_class,
None,
)
.unwrap_or_default(),
false,
),
Node::Comment(_) | Node::Doctype(_) => (quote! {}, false),
};
if is_static {
quote! {
.child(#child)
quote! { #text }
}
} else {
quote! {
.child((#child))
}
}
});
Node::Block(node) => quote! { #node },
Node::Element(node) => element_to_tokens(
node,
parent_type,
None,
global_class,
None,
)
.unwrap_or_default(),
Node::Comment(_) | Node::Doctype(_) => quote! {},
})
.map(|node| quote!(.child(#node)));

let view_marker = if let Some(marker) = view_marker {
quote! { .with_view_marker(#marker) }
} else {
quote! {}
};
let ide_helper_close_tag = ide_helper_close_tag.into_iter();
Some(quote! {
Some(quote_spanned! {node.span()=>
#[allow(unused_braces)]
{
#(#ide_helper_close_tag)*
#name
Expand Down Expand Up @@ -453,7 +436,7 @@ pub(crate) fn attribute_to_tokens(
prop.span()=> .prop
};
quote! {
#prop(#name, #[allow(unused_braces)] {#value})
#prop(#name, #value)
}
} else if let Some(name) = name.strip_prefix("class:") {
let value = attribute_value(node);
Expand All @@ -465,7 +448,7 @@ pub(crate) fn attribute_to_tokens(
class.span()=> .class
};
quote! {
#class(#name, #[allow(unused_braces)] {#value})
#class(#name, #value)
}
} else if let Some(name) = name.strip_prefix("style:") {
let value = attribute_value(node);
Expand All @@ -477,7 +460,7 @@ pub(crate) fn attribute_to_tokens(
style.span()=> .style
};
quote! {
#style(#name, #[allow(unused_braces)] {#value})
#style(#name, #value)
}
} else {
let name = name.replacen("attr:", "", 1);
Expand Down Expand Up @@ -520,7 +503,7 @@ pub(crate) fn attribute_to_tokens(
}
};
quote! {
#attr(#name, (#value))
#attr(#name, #value)
}
}
}
Loading
Loading