Skip to content

Commit

Permalink
fix: macro invocations in props #2267
Browse files Browse the repository at this point in the history
convert macro invocation Item statements to Exprs
  • Loading branch information
WorldSEnder committed May 16, 2022
1 parent b82a5ba commit f61439e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/yew-macro/src/props/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
11 changes: 11 additions & 0 deletions packages/yew-macro/tests/html_macro/component-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
12 changes: 12 additions & 0 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
5 changes: 5 additions & 0 deletions packages/yew-macro/tests/html_macro/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 />
}} />
</>
};

Expand Down

0 comments on commit f61439e

Please sign in to comment.