2021-01-20 17:04:27 +00:00
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::{quote, quote_spanned, ToTokens};
|
|
|
|
use syn::spanned::Spanned;
|
2021-01-21 07:25:44 +00:00
|
|
|
use syn::{
|
|
|
|
parse::{Parse, ParseStream},
|
|
|
|
Signature,
|
|
|
|
};
|
2021-01-20 17:04:27 +00:00
|
|
|
use syn::{
|
|
|
|
parse_macro_input, Attribute, Block, FnArg, Ident, Item, ItemFn, ReturnType, Type, Visibility,
|
|
|
|
};
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
/// Label a function or static closure as a functional component.
|
|
|
|
/// This macro reduces the need to create a separate properties struct.
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn fc(attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
|
let item = parse_macro_input!(item as FunctionComponent);
|
|
|
|
// let attr = parse_macro_input!(attr as FunctionComponentName);
|
|
|
|
|
|
|
|
function_component_impl(item)
|
|
|
|
// function_component_impl(attr, item)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn function_component_impl(
|
|
|
|
// name: FunctionComponentName,
|
|
|
|
component: FunctionComponent,
|
|
|
|
) -> syn::Result<TokenStream> {
|
|
|
|
// let FunctionComponentName { component_name } = name;
|
|
|
|
|
|
|
|
let FunctionComponent {
|
|
|
|
block,
|
|
|
|
props_type,
|
|
|
|
arg,
|
|
|
|
vis,
|
|
|
|
attrs,
|
|
|
|
name: function_name,
|
|
|
|
return_type,
|
|
|
|
} = component;
|
|
|
|
|
|
|
|
// if function_name == component_name {
|
|
|
|
// return Err(syn::Error::new_spanned(
|
|
|
|
// component_name,
|
|
|
|
// "the component must not have the same name as the function",
|
|
|
|
// ));
|
|
|
|
// }
|
|
|
|
|
|
|
|
let ret_type = quote_spanned!(return_type.span()=> VNode);
|
|
|
|
|
|
|
|
let quoted = quote! {
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
mod __component_blah {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub struct Props {
|
|
|
|
name: String
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn component(ctx: &mut Context<Props>) -> #ret_type {
|
|
|
|
let Props {
|
|
|
|
name
|
|
|
|
} = ctx.props;
|
|
|
|
#block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub use __component_blah::component as #function_name;
|
|
|
|
};
|
|
|
|
// let quoted = quote! {
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// #[allow(non_camel_case_types)]
|
|
|
|
// #vis struct #function_name;
|
|
|
|
|
|
|
|
// impl ::yew_functional::FunctionProvider for #function_name {
|
|
|
|
// type TProps = #props_type;
|
|
|
|
|
|
|
|
// fn run(#arg) -> #ret_type {
|
|
|
|
// #block
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #(#attrs)*
|
|
|
|
// #vis type #component_name = ::yew_functional::FunctionComponent<#function_name>;
|
|
|
|
// };
|
|
|
|
Ok(quoted)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A parsed version of the user's input
|
2021-01-20 17:04:27 +00:00
|
|
|
struct FunctionComponent {
|
2021-01-21 07:25:44 +00:00
|
|
|
// The actual contents of the function
|
2021-01-20 17:04:27 +00:00
|
|
|
block: Box<Block>,
|
2021-01-21 07:25:44 +00:00
|
|
|
|
|
|
|
// The user's props type
|
2021-01-20 17:04:27 +00:00
|
|
|
props_type: Box<Type>,
|
2021-01-21 07:25:44 +00:00
|
|
|
|
2021-01-20 17:04:27 +00:00
|
|
|
arg: FnArg,
|
|
|
|
vis: Visibility,
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
name: Ident,
|
|
|
|
return_type: Box<Type>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for FunctionComponent {
|
|
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
|
|
|
let parsed: Item = input.parse()?;
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Convert the parsed input into the Function block
|
|
|
|
let ItemFn {
|
|
|
|
attrs,
|
|
|
|
vis,
|
|
|
|
sig,
|
|
|
|
block,
|
|
|
|
} = ensure_fn_block(parsed)?;
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Validate the user's signature
|
|
|
|
let sig = validate_signature(sig)?;
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Validate the return type is actually something
|
|
|
|
let return_type = ensure_return_type(sig.output)?;
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Get all the function args
|
|
|
|
let mut inputs = sig.inputs.into_iter();
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Collect the first arg
|
|
|
|
let first_arg: FnArg = inputs
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| syn::parse_quote! { _: &() });
|
|
|
|
|
|
|
|
// Extract the "context" object
|
|
|
|
let props_type = validate_context_arg(&first_arg)?;
|
|
|
|
|
2021-01-21 08:22:08 +00:00
|
|
|
/*
|
|
|
|
Extract the rest of the function arguments into a struct body
|
|
|
|
We require all inputs are strongly typed with names so we can destructure into the function body when expanded
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
// let rest = inputs
|
|
|
|
// .map(|f| {
|
|
|
|
// //
|
|
|
|
// match f {
|
|
|
|
// FnArg::Typed(pat) => {
|
|
|
|
// match *pat.pat {
|
|
|
|
// syn::Pat::Type(asd) => {}
|
|
|
|
// _ => {}
|
|
|
|
// };
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
// FnArg::Receiver(_) => {}
|
|
|
|
// }
|
|
|
|
// // let name = f
|
|
|
|
// let stream = f.into_token_stream();
|
|
|
|
// (stream)
|
|
|
|
// })
|
|
|
|
// .collect::<Vec<_>>();
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
// Collect the rest of the args into a list of definitions to be used by the inline struct
|
|
|
|
|
|
|
|
// Checking after param parsing may make it a little inefficient
|
|
|
|
// but that's a requirement for better error messages in case of receivers
|
|
|
|
// `>0` because first one is already consumed.
|
|
|
|
// if inputs.len() > 0 {
|
|
|
|
// let params: TokenStream = inputs.map(|it| it.to_token_stream()).collect();
|
|
|
|
// return Err(syn::Error::new_spanned(
|
|
|
|
// params,
|
|
|
|
// "function components can accept at most one parameter for the props",
|
|
|
|
// ));
|
|
|
|
// }
|
|
|
|
let name = sig.ident;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
props_type,
|
|
|
|
block,
|
|
|
|
arg: first_arg,
|
|
|
|
vis,
|
|
|
|
attrs,
|
|
|
|
name,
|
|
|
|
return_type,
|
|
|
|
})
|
2021-01-20 17:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
/// Ensure the user's input is actually a functional component
|
|
|
|
fn ensure_fn_block(item: Item) -> syn::Result<ItemFn> {
|
|
|
|
match item {
|
|
|
|
Item::Fn(it) => Ok(it),
|
2021-02-03 07:26:04 +00:00
|
|
|
Item::Static(it) => {
|
|
|
|
let syn::ItemStatic {
|
|
|
|
attrs,
|
|
|
|
vis,
|
|
|
|
static_token,
|
|
|
|
mutability,
|
|
|
|
ident,
|
|
|
|
colon_token,
|
|
|
|
ty,
|
|
|
|
eq_token,
|
|
|
|
expr,
|
|
|
|
semi_token,
|
|
|
|
} = ⁢
|
|
|
|
match ty.as_ref() {
|
|
|
|
Type::BareFn(bare) => {}
|
|
|
|
// Type::Array(_)
|
|
|
|
// | Type::Group(_)
|
|
|
|
// | Type::ImplTrait(_)
|
|
|
|
// | Type::Infer(_)
|
|
|
|
// | Type::Macro(_)
|
|
|
|
// | Type::Never(_)
|
|
|
|
// | Type::Paren(_)
|
|
|
|
// | Type::Path(_)
|
|
|
|
// | Type::Ptr(_)
|
|
|
|
// | Type::Reference(_)
|
|
|
|
// | Type::Slice(_)
|
|
|
|
// | Type::TraitObject(_)
|
|
|
|
// | Type::Tuple(_)
|
|
|
|
// | Type::Verbatim(_)
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Add support for static block
|
|
|
|
// Ensure that the contents of the static block can be extracted to a function
|
|
|
|
// TODO: @Jon
|
|
|
|
// Decide if statics should be converted to functions (under the hood) or stay as statics
|
|
|
|
// They _do_ get promoted, but also have a &'static ref
|
|
|
|
Err(syn::Error::new_spanned(
|
|
|
|
it,
|
|
|
|
"`function_component` attribute not ready for statics",
|
|
|
|
))
|
|
|
|
}
|
2021-01-21 07:25:44 +00:00
|
|
|
other => Err(syn::Error::new_spanned(
|
|
|
|
other,
|
|
|
|
"`function_component` attribute can only be applied to functions",
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
/// Ensure the user's function actually returns a VNode
|
|
|
|
fn ensure_return_type(output: ReturnType) -> syn::Result<Box<Type>> {
|
|
|
|
match output {
|
|
|
|
ReturnType::Default => Err(syn::Error::new_spanned(
|
|
|
|
output,
|
|
|
|
"function components must return `dioxus::VNode`",
|
|
|
|
)),
|
|
|
|
ReturnType::Type(_, ty) => Ok(ty),
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
/// Validate the users's input signature for the function component.
|
|
|
|
/// Returns an error if any of the conditions prove to be wrong;
|
|
|
|
fn validate_signature(sig: Signature) -> syn::Result<Signature> {
|
|
|
|
if !sig.generics.params.is_empty() {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
sig.generics,
|
|
|
|
"function components can't contain generics",
|
|
|
|
));
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
if sig.asyncness.is_some() {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
sig.asyncness,
|
|
|
|
"function components can't be async",
|
|
|
|
));
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
if sig.constness.is_some() {
|
2021-01-20 17:04:27 +00:00
|
|
|
return Err(syn::Error::new_spanned(
|
2021-01-21 07:25:44 +00:00
|
|
|
sig.constness,
|
|
|
|
"const functions can't be function components",
|
2021-01-20 17:04:27 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
if sig.abi.is_some() {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
sig.abi,
|
|
|
|
"extern functions can't be function components",
|
|
|
|
));
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
Ok(sig)
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
fn validate_context_arg(first_arg: &FnArg) -> syn::Result<Box<Type>> {
|
|
|
|
if let FnArg::Typed(arg) = first_arg {
|
|
|
|
// Input arg is a reference to an &mut Context
|
|
|
|
if let Type::Reference(ty) = &*arg.ty {
|
|
|
|
if ty.lifetime.is_some() {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
&ty.lifetime,
|
|
|
|
"reference must not have a lifetime",
|
|
|
|
));
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
if ty.mutability.is_some() {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
&ty.mutability,
|
|
|
|
"reference must not be mutable",
|
|
|
|
));
|
2021-01-20 17:04:27 +00:00
|
|
|
}
|
2021-01-21 07:25:44 +00:00
|
|
|
|
|
|
|
Ok(ty.elem.clone())
|
|
|
|
} else {
|
|
|
|
let msg = format!(
|
|
|
|
"expected a reference to a `Context` object (try: `&mut {}`)",
|
|
|
|
arg.ty.to_token_stream()
|
|
|
|
);
|
|
|
|
return Err(syn::Error::new_spanned(arg.ty.clone(), msg));
|
2021-01-20 17:04:27 +00:00
|
|
|
}
|
2021-01-21 07:25:44 +00:00
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
first_arg,
|
|
|
|
"function components can't accept a receiver",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
|
2021-01-21 07:25:44 +00:00
|
|
|
fn collect_inline_args() {}
|
|
|
|
|
|
|
|
/// The named specified in the macro usage.
|
|
|
|
struct FunctionComponentName {
|
|
|
|
component_name: Ident,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for FunctionComponentName {
|
|
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
|
|
|
if input.is_empty() {
|
|
|
|
return Err(input.error("expected identifier for the component"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let component_name = input.parse()?;
|
|
|
|
|
|
|
|
Ok(Self { component_name })
|
|
|
|
}
|
2021-01-20 17:04:27 +00:00
|
|
|
}
|