mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Allow macro invocations as prop values (#2687)
* fix: macro invocations in props #2267 convert macro invocation Item statements to Exprs * remove now useless braces in docs
This commit is contained in:
parent
b82a5ba56b
commit
6f6c61b975
@ -120,6 +120,15 @@ fn strip_braces(block: ExprBlock) -> syn::Result<Expr> {
|
||||
let stmt = stmts.remove(0);
|
||||
match stmt {
|
||||
Stmt::Expr(expr) => Ok(expr),
|
||||
// See issue #2267, we want to parse macro invocations as expressions
|
||||
Stmt::Item(syn::Item::Macro(mac))
|
||||
if mac.ident.is_none() && mac.semi_token.is_none() =>
|
||||
{
|
||||
Ok(Expr::Macro(syn::ExprMacro {
|
||||
attrs: mac.attrs,
|
||||
mac: mac.mac,
|
||||
}))
|
||||
}
|
||||
Stmt::Semi(_expr, semi) => Err(syn::Error::new_spanned(
|
||||
semi,
|
||||
"only an expression may be assigned as a property. Consider removing this \
|
||||
|
||||
@ -118,7 +118,18 @@ fn compile_fail() {
|
||||
html! { <Child {std::f64::consts::PI} /> };
|
||||
html! { <Child {7 + 6} /> };
|
||||
html! { <Child {children.len()} /> };
|
||||
}
|
||||
|
||||
#[derive(Clone, Properties, PartialEq)]
|
||||
pub struct HtmlInPropsProperties {
|
||||
pub header: ::yew::Html,
|
||||
}
|
||||
#[function_component]
|
||||
fn HtmlInProps(props: &HtmlInPropsProperties) -> Html { let _ = (); unimplemented!() }
|
||||
|
||||
fn not_expressions() {
|
||||
html! { <HtmlInProps header={macro_rules! declare { }} /> };
|
||||
html! { <HtmlInProps header={format!("ending with semi");} /> };
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
@ -255,6 +255,18 @@ error: missing label for property value. If trying to use the shorthand property
|
||||
120 | html! { <Child {children.len()} /> };
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: only an expression may be assigned as a property
|
||||
--> tests/html_macro/component-fail.rs:131:34
|
||||
|
|
||||
131 | html! { <HtmlInProps header={macro_rules! declare { }} /> };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: only an expression may be assigned as a property. Consider removing this semicolon
|
||||
--> tests/html_macro/component-fail.rs:132:61
|
||||
|
|
||||
132 | html! { <HtmlInProps header={format!("ending with semi");} /> };
|
||||
| ^
|
||||
|
||||
error[E0425]: cannot find value `blah` in this scope
|
||||
--> tests/html_macro/component-fail.rs:68:22
|
||||
|
|
||||
|
||||
@ -43,6 +43,8 @@ pub struct ContainerProperties {
|
||||
pub int: ::std::primitive::i32,
|
||||
#[prop_or_default]
|
||||
pub children: ::yew::Children,
|
||||
#[prop_or_default]
|
||||
pub header: ::yew::Html,
|
||||
}
|
||||
|
||||
pub struct Container;
|
||||
@ -273,6 +275,9 @@ fn compile_pass() {
|
||||
<Container int=1 children={::yew::html::ChildrenRenderer::new(
|
||||
::std::vec![::yew::html!{ "::std::string::String" }]
|
||||
)} />
|
||||
<Container int=1 header={::yew::html!{
|
||||
<Child int=2 />
|
||||
}} />
|
||||
</>
|
||||
};
|
||||
|
||||
|
||||
@ -241,9 +241,9 @@ pub fn render_page(with_sidebar: bool) -> Html {
|
||||
if with_sidebar {
|
||||
// Page with sidebar
|
||||
html! {
|
||||
<Page sidebar={{html_nested! {
|
||||
<Page sidebar={html_nested! {
|
||||
<PageSideBar />
|
||||
}}} />
|
||||
}} />
|
||||
}
|
||||
} else {
|
||||
// Page without sidebar
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user