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

Add compile error when using comment and doctype tags within the view! macro #2355

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions leptos_macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Parse for Model {

// Make sure return type is correct
if !is_valid_into_view_return_type(&item.sig.output) {
abort!(
proc_macro_error::abort!(
item.sig,
"return type is incorrect";
help = "return signature must be `-> impl IntoView`"
Expand Down Expand Up @@ -628,19 +628,19 @@ impl Prop {
let typed = if let FnArg::Typed(ty) = arg {
ty
} else {
abort!(arg, "receiver not allowed in `fn`");
proc_macro_error::abort!(arg, "receiver not allowed in `fn`");
};

let prop_opts =
PropOpt::from_attributes(&typed.attrs).unwrap_or_else(|e| {
// TODO: replace with `.unwrap_or_abort()` once https://gitlab.com/CreepySkeleton/proc-macro-error/-/issues/17 is fixed
abort!(e.span(), e.to_string());
proc_macro_error::abort!(e.span(), e.to_string());
});

let name = if let Pat::Ident(i) = *typed.pat {
i
} else {
abort!(
proc_macro_error::abort!(
typed.pat,
"only `prop: bool` style types are allowed within the \
`#[component]` macro"
Expand Down Expand Up @@ -756,7 +756,7 @@ impl Docs {
}

let Some(val) = value_to_string(&attr.value) else {
abort!(
proc_macro_error::abort!(
attr,
"expected string literal in value of doc comment"
);
Expand Down Expand Up @@ -1075,7 +1075,7 @@ pub fn unwrap_option(ty: &Type) -> Type {
}
}

abort!(
proc_macro_error::abort!(
ty,
"`Option` must be `std::option::Option`";
help = STD_OPTION_MSG
Expand Down
18 changes: 8 additions & 10 deletions leptos_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#![allow(stable_features)]
// FIXME? every use of quote! {} is warning here -- false positive?
#![allow(unknown_lints)]
#![allow(private_macro_use)]

#[macro_use]
extern crate proc_macro_error;

use component::DummyModel;
use proc_macro::TokenStream;
Expand Down Expand Up @@ -339,7 +335,7 @@ pub fn view(tokens: TokenStream) -> TokenStream {
third.clone()
}
_ => {
abort!(
proc_macro_error::abort!(
second, "To create a scope class with the view! macro you must put a comma `,` after the value";
help = r#"e.g., view!{ class="my-class", <div>...</div>}"#
)
Expand Down Expand Up @@ -593,7 +589,7 @@ pub fn component(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
let transparent = parse_macro_input!(args as syn::Ident);

if transparent != "transparent" {
abort!(
proc_macro_error::abort!(
transparent,
"only `transparent` is supported";
help = "try `#[component(transparent)]` or `#[component]`"
Expand Down Expand Up @@ -846,7 +842,7 @@ pub fn island(_args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn slot(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
if !args.is_empty() {
abort!(
proc_macro_error::abort!(
Span::call_site(),
"no arguments are supported";
help = "try just `#[slot]`"
Expand All @@ -869,7 +865,7 @@ pub fn slot(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
/// You can provide any combination of the following named arguments:
/// - `name`: sets the identifier for the server function’s type, which is a struct created
/// to hold the arguments (defaults to the function identifier in PascalCase)
/// - `prefix`: a prefix at which the server function handler will be mounted (defaults to `/api`)
/// - `prefix`: a prefix at which the server function handler will be mounted (defaults to `/api`)
/// your prefix must begin with `/`. Otherwise your function won't be found.
/// - `endpoint`: specifies the exact path at which the server function handler will be mounted,
/// relative to the prefix (defaults to the function name followed by unique hash)
Expand Down Expand Up @@ -965,7 +961,7 @@ pub fn slot(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
/// are unique. You cannot define two server functions with the same URL prefix and endpoint path,
/// even if they have different URL encodings, e.g. a POST method at `/api/foo` and a GET method at `/api/foo`.
#[proc_macro_attribute]
#[proc_macro_error]
#[proc_macro_error::proc_macro_error]
pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
match server_fn_macro::server_macro_impl(
args.into(),
Expand Down Expand Up @@ -995,7 +991,9 @@ pub fn params_derive(
pub(crate) fn attribute_value(attr: &KeyedAttribute) -> &syn::Expr {
match attr.value() {
Some(value) => value,
None => abort!(attr.key, "attribute should have value"),
None => {
proc_macro_error::abort!(attr.key, "attribute should have value")
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions leptos_macro/src/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ impl Prop {
let prop_opts =
PropOpt::from_attributes(&arg.attrs).unwrap_or_else(|e| {
// TODO: replace with `.unwrap_or_abort()` once https://gitlab.com/CreepySkeleton/proc-macro-error/-/issues/17 is fixed
abort!(e.span(), e.to_string());
proc_macro_error::abort!(e.span(), e.to_string());
});

let name = if let Some(i) = arg.ident {
i
} else {
abort!(
proc_macro_error::abort!(
arg.ident,
"only `prop: bool` style types are allowed within the \
`#[slot]` macro"
Expand Down
34 changes: 32 additions & 2 deletions leptos_macro/src/view/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,22 @@ pub(crate) fn node_to_tokens(
global_class,
view_marker,
),
Node::Comment(_) | Node::Doctype(_) => Some(quote! {}),
Node::Comment(_) => {
proc_macro_error::emit_warning!(
node,
"comment tags do not show up in rendered output\nsupport for \
this is coming in leptos 0.7"
);
Some(quote! {})
}
Node::Doctype(_) => {
proc_macro_error::emit_warning!(
node,
"doctype tags do not show up in rendered output\nsupport for \
this is coming in leptos 0.7"
);
Some(quote! {})
}
Node::Text(node) => Some(quote! {
::leptos::leptos_dom::html::text(#node)
}),
Expand Down Expand Up @@ -333,7 +348,22 @@ pub(crate) fn element_to_tokens(
.unwrap_or_default(),
false,
),
Node::Comment(_) | Node::Doctype(_) => (quote! {}, false),
Node::Comment(_) => {
proc_macro_error::emit_warning!(
node,
"comment tags do not show up in rendered \
output\nsupport for this is coming in leptos 0.7"
);
(quote! {}, false)
}
Node::Doctype(_) => {
proc_macro_error::emit_warning!(
node,
"doctype tags do not show up in rendered \
output\nsupport for this is coming in leptos 0.7"
);
(quote! {}, false)
}
};
if is_static {
quote! {
Expand Down
17 changes: 13 additions & 4 deletions leptos_macro/src/view/client_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ pub(crate) fn render_template(nodes: &[Node]) -> TokenStream {
root_element_to_tokens(&template_uid, node)
}
_ => {
abort!(Span::call_site(), "template! takes a single root element.")
proc_macro_error::abort!(
Span::call_site(),
"template! takes a single root element."
)
}
}
}
Expand Down Expand Up @@ -201,7 +204,7 @@ fn element_to_tokens(
let next_sib =
match next_sibling_node(&node.children, idx + 1, next_el_id) {
Ok(next_sib) => next_sib,
Err(err) => abort!(span, "{}", err),
Err(err) => proc_macro_error::abort!(span, "{}", err),
};

let curr_id = child_to_tokens(
Expand Down Expand Up @@ -292,7 +295,10 @@ fn attr_to_tokens(

// refs
if name == "ref" {
abort!(span, "node_ref not yet supported in template! macro")
proc_macro_error::abort!(
span,
"node_ref not yet supported in template! macro"
)
}
// Event Handlers
else if name.starts_with("on:") {
Expand Down Expand Up @@ -457,7 +463,10 @@ fn child_to_tokens(
expressions,
navigations,
),
_ => abort!(Span::call_site(), "unexpected child node type"),
_ => proc_macro_error::abort!(
Span::call_site(),
"unexpected child node type"
),
}
}

Expand Down
2 changes: 1 addition & 1 deletion leptos_macro/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub(crate) fn parse_event_name(name: &str) -> (TokenStream, bool, bool) {
.unwrap_or((CUSTOM_EVENT, true));

let Ok(event_type) = event_type.parse::<TokenStream>() else {
abort!(event_type, "couldn't parse event name");
proc_macro_error::abort!(event_type, "couldn't parse event name");
};

let event_type = if is_custom {
Expand Down
36 changes: 33 additions & 3 deletions leptos_macro/src/view/server_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,22 @@ pub(crate) fn root_node_to_tokens_ssr(
global_class,
view_marker,
),
Node::Comment(_) | Node::Doctype(_) => quote! {},
Node::Comment(_) => {
proc_macro_error::emit_warning!(
node,
"comment tags do not show up in rendered output\nsupport for \
this is coming in leptos 0.7"
);
quote! {}
}
Node::Doctype(_) => {
proc_macro_error::emit_warning!(
node,
"doctype tags do not show up in rendered output\nsupport for \
this is coming in leptos 0.7"
);
quote! {}
}
Node::Text(node) => {
quote! {
::leptos::leptos_dom::html::text(#node)
Expand Down Expand Up @@ -382,11 +397,26 @@ fn element_to_tokens_ssr(
::leptos::IntoView::into_view(#[allow(unused_braces)] {#block})
}));
}
Node::Fragment(_) => abort!(
Node::Fragment(_) => proc_macro_error::abort!(
Span::call_site(),
"You can't nest a fragment inside an element."
),
Node::Comment(_) | Node::Doctype(_) => {}
Node::Comment(_) => {
proc_macro_error::emit_warning!(
child,
"comment tags do not show up in rendered \
output\nsupport for this is coming in leptos \
0.7"
);
}
Node::Doctype(_) => {
proc_macro_error::emit_warning!(
child,
"doctype tags do not show up in rendered \
output\nsupport for this is coming in leptos \
0.7"
);
}
}
}
}
Expand Down