diff --git a/tests/build/property_macro/fail/incorredt_attr_args.rs b/tests/build/property_macro/fail/incorredt_attr_args.rs new file mode 100644 index 000000000..f97cf5296 --- /dev/null +++ b/tests/build/property_macro/fail/incorredt_attr_args.rs @@ -0,0 +1,23 @@ +use zero_ui::core::{property, UiNode}; + +#[property(context, unknown: true)] +fn unknown_arg(child: impl UiNode, input: bool) -> impl UiNode { + child +} + +#[property(context, allowed_in_when: "false")] +fn invalid_allowed_in_when_value(child: impl UiNode, input: bool) -> impl UiNode { + child +} + +#[property(context, allowed_in_when)] +fn missing_allowed_in_when_value_1(child: impl UiNode, input: bool) -> impl UiNode { + child +} + +#[property(context, allowed_in_when: )] +fn missing_allowed_in_when_value_2(child: impl UiNode, input: bool) -> impl UiNode { + child +} + +fn main() {} diff --git a/tests/build/property_macro/pass/attr_args.rs b/tests/build/property_macro/pass/attr_args.rs new file mode 100644 index 000000000..6123e8d02 --- /dev/null +++ b/tests/build/property_macro/pass/attr_args.rs @@ -0,0 +1,27 @@ +use zero_ui::core::{property, UiNode}; + +#[property(context)] +fn trailing_comma_1(child: impl UiNode, input: bool) -> impl UiNode { + let _ = input; + child +} + +#[property(context, allowed_in_when: true)] +fn allowed_in_when(child: impl UiNode, input: bool) -> impl UiNode { + let _ = input; + child +} + +#[property(context, allowed_in_when: false)] +fn not_allowed_in_when(child: impl UiNode, input: bool) -> impl UiNode { + let _ = input; + child +} + +#[property(context, allowed_in_when: false,)] +fn trailing_comma_2(child: impl UiNode, input: bool) -> impl UiNode { + let _ = input; + child +} + +fn main() {} diff --git a/zero-ui-proc-macros/src/property.rs b/zero-ui-proc-macros/src/property.rs index 8d929c0c5..27df700ca 100644 --- a/zero-ui-proc-macros/src/property.rs +++ b/zero-ui-proc-macros/src/property.rs @@ -27,7 +27,7 @@ pub use input::keyword; pub use input::Priority; mod input { - use syn::{parse::*, *}; + use syn::{parse::*, spanned::Spanned, *}; pub mod keyword { syn::custom_keyword!(context); @@ -51,8 +51,21 @@ mod input { Ok(MacroArgs { priority: input.parse()?, allowed_in_when: { - if input.peek(Token![,]) { - Some((input.parse()?, input.parse()?, input.parse()?, input.parse()?)) + if input.peek(Token![,]) && input.peek2(keyword::allowed_in_when) { + let comma = input.parse().unwrap(); + let allowed_in_when = input.parse::().unwrap(); + + if input.is_empty() { + return Err(syn::Error::new(allowed_in_when.span(), "expected `allowed_in_when : `")); + } + let colon = input.parse::()?; + + if input.is_empty() { + return Err(syn::Error::new(colon.span(), "expected `: `")); + } + let bool_ = input.parse()?; + + Some((comma, allowed_in_when, colon, bool_)) } else { None }