Skip to content

Commit

Permalink
Simplify yew-macro a bit (#902)
Browse files Browse the repository at this point in the history
* yew-macro: Simplify Properties validation

* Fix most clippy warnings
  • Loading branch information
jplatte authored and jstarry committed Feb 10, 2020
1 parent ecf79b2 commit 9e603ce
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 43 deletions.
1 change: 0 additions & 1 deletion crates/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ trybuild = "1.0"
yew = { path = "../..", features = ["std_web"] }

[build-dependencies]
autocfg = "1.0.0"

[features]
doc_test = []
7 changes: 0 additions & 7 deletions crates/macro/build.rs

This file was deleted.

7 changes: 2 additions & 5 deletions crates/macro/src/derive_props/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ impl ToTokens for PropsBuilder<'_> {
// Each builder step implements the `BuilderStep` trait and `step_generics` is used to
// enforce that.
let step_generic_param = Ident::new("YEW_PROPS_BUILDER_STEP", Span::call_site());
let step_generics = with_param_bounds(
&generics,
step_generic_param.clone(),
step_trait.clone().to_owned(),
);
let step_generics =
with_param_bounds(&generics, step_generic_param.clone(), (*step_trait).clone());

let builder = quote! {
#(
Expand Down
39 changes: 11 additions & 28 deletions crates/macro/src/html_tree/html_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,21 @@ impl ToTokens for HtmlComponent {
} = self;

let validate_props = if let Props::List(ListProps { props, .. }) = props {
let prop_ref = Ident::new("__yew_prop_ref", Span::call_site());
let check_props = props.iter().map(|HtmlProp { label, .. }| {
quote! { #prop_ref.#label; }
quote! { props.#label; }
});

let check_children = if !children.is_empty() {
quote! { #prop_ref.children; }
quote! { props.children; }
} else {
quote! {}
};

// This is a hack to avoid allocating memory but still have a reference to a props
// struct so that attributes can be checked against it

#[cfg(has_maybe_uninit)]
let unallocated_prop_ref = quote! {
let #prop_ref: <#ty as ::yew::html::Component>::Properties = unsafe {
::std::mem::MaybeUninit::uninit().assume_init()
};
};

#[cfg(not(has_maybe_uninit))]
let unallocated_prop_ref = quote! {
let #prop_ref: <#ty as ::yew::html::Component>::Properties = unsafe {
::std::mem::uninitialized()
};
};

quote! {
#unallocated_prop_ref
#check_children
#(#check_props)*
let _ = |props: <#ty as ::yew::html::Component>::Properties| {
#check_children
#(#check_props)*
};
}
} else {
quote! {}
Expand Down Expand Up @@ -283,7 +266,7 @@ impl PeekValue<Type> for HtmlComponentOpen {
let (punct, cursor) = cursor.punct()?;
(punct.as_char() == '<').as_option()?;
let (typ, _) = HtmlComponent::peek_type(cursor)?;
return Some(typ);
Some(typ)
}
}

Expand Down Expand Up @@ -333,7 +316,7 @@ impl PeekValue<Type> for HtmlComponentClose {
let (punct, _) = cursor.punct()?;
(punct.as_char() == '>').as_option()?;

return Some(typ);
Some(typ)
}
}
impl Parse for HtmlComponentClose {
Expand Down Expand Up @@ -378,7 +361,7 @@ impl Props {
impl PeekValue<PropType> for Props {
fn peek(cursor: Cursor) -> Option<PropType> {
let (ident, _) = cursor.ident()?;
let prop_type = if ident.to_string() == "with" {
let prop_type = if ident == "with" {
PropType::With
} else {
PropType::List
Expand Down Expand Up @@ -411,7 +394,7 @@ impl Parse for ListProps {
}

let ref_position = props.iter().position(|p| p.label.to_string() == "ref");
let node_ref = ref_position.and_then(|i| Some(props.remove(i).value));
let node_ref = ref_position.map(|i| props.remove(i).value);
for prop in &props {
if prop.label.to_string() == "ref" {
return Err(syn::Error::new_spanned(&prop.label, "too many refs set"));
Expand Down Expand Up @@ -452,7 +435,7 @@ struct WithProps {
impl Parse for WithProps {
fn parse(input: ParseStream) -> ParseResult<Self> {
let with = input.parse::<Ident>()?;
if with.to_string() != "with" {
if with != "with" {
return Err(input.error("expected to find `with` token"));
}
let props = input.parse::<Ident>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/macro/src/html_tree/html_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct HtmlIterable(Expr);
impl PeekValue<()> for HtmlIterable {
fn peek(cursor: Cursor) -> Option<()> {
let (ident, _) = cursor.ident()?;
(ident.to_string() == "for").as_option()
(ident == "for").as_option()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/macro/src/html_tree/html_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl PeekValue<()> for HtmlProp {
fn peek(cursor: Cursor) -> Option<()> {
let (_, cursor) = HtmlPropLabel::peek(cursor)?;
let (punct, _) = cursor.punct()?;
return (punct.as_char() == '=').as_option();
(punct.as_char() == '=').as_option()
}
}

Expand Down

0 comments on commit 9e603ce

Please sign in to comment.