From 9b418bf34905752d42f7fd33ecd100465588075d Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 8 Aug 2022 23:56:34 +0500 Subject: [PATCH] Make test pass, fmt + clippy --- .../yew-macro/src/html_tree/html_element.rs | 84 +++++++++++-------- packages/yew-macro/src/props/prop.rs | 17 +++- packages/yew-macro/src/props/prop_macro.rs | 6 +- .../yew/src/dom_bundle/btag/attributes.rs | 19 +++-- packages/yew/src/dom_bundle/btag/mod.rs | 2 +- 5 files changed, 82 insertions(+), 46 deletions(-) diff --git a/packages/yew-macro/src/html_tree/html_element.rs b/packages/yew-macro/src/html_tree/html_element.rs index 8afa2e0ab37..5cb4fb106d1 100644 --- a/packages/yew-macro/src/html_tree/html_element.rs +++ b/packages/yew-macro/src/html_tree/html_element.rs @@ -135,40 +135,58 @@ impl ToTokens for HtmlElement { // other attributes let attributes = { - let normal_attrs = attributes.iter().map(|Prop { label, value, is_forced_attribute, .. }| { - (label.to_lit_str(), value.optimize_literals_tagged(), *is_forced_attribute) - }); - let boolean_attrs = booleans.iter().filter_map(|Prop { label, value, is_forced_attribute, .. }| { - let key = label.to_lit_str(); - Some(( - key.clone(), - match value { - Expr::Lit(e) => match &e.lit { - Lit::Bool(b) => Value::Static(if b.value { - quote! { #key } - } else { - return None; - }), - _ => Value::Dynamic(quote_spanned! {value.span()=> { - ::yew::utils::__ensure_type::<::std::primitive::bool>(#value); - #key - }}), - }, - expr => Value::Dynamic( - quote_spanned! {expr.span().resolved_at(Span::call_site())=> - if #expr { - ::std::option::Option::Some( - ::yew::virtual_dom::AttrValue::Static(#key) - ) + let normal_attrs = attributes.iter().map( + |Prop { + label, + value, + is_forced_attribute, + .. + }| { + ( + label.to_lit_str(), + value.optimize_literals_tagged(), + *is_forced_attribute, + ) + }, + ); + let boolean_attrs = booleans.iter().filter_map( + |Prop { + label, + value, + is_forced_attribute, + .. + }| { + let key = label.to_lit_str(); + Some(( + key.clone(), + match value { + Expr::Lit(e) => match &e.lit { + Lit::Bool(b) => Value::Static(if b.value { + quote! { #key } } else { - ::std::option::Option::None - } + return None; + }), + _ => Value::Dynamic(quote_spanned! {value.span()=> { + ::yew::utils::__ensure_type::<::std::primitive::bool>(#value); + #key + }}), }, - ), - }, - *is_forced_attribute - )) - }); + expr => Value::Dynamic( + quote_spanned! {expr.span().resolved_at(Span::call_site())=> + if #expr { + ::std::option::Option::Some( + ::yew::virtual_dom::AttrValue::Static(#key) + ) + } else { + ::std::option::Option::None + } + }, + ), + }, + *is_forced_attribute, + )) + }, + ); let class_attr = classes.as_ref().and_then(|classes| match classes { ClassesForm::Tuple(classes) => { let span = classes.span(); @@ -259,7 +277,7 @@ impl ToTokens for HtmlElement { quote! { ::yew::virtual_dom::ApplyAttributeAs::Property } }; let value = wrap_attr_value(v); - quote! { ::std::option::Option::map(#value, |it| (it, ::yew::virtual_dom::ApplyAttributeAs::Property)) } + quote! { ::std::option::Option::map(#value, |it| (it, #apply_as)) } }); quote! { ::yew::virtual_dom::Attributes::Dynamic{ diff --git a/packages/yew-macro/src/props/prop.rs b/packages/yew-macro/src/props/prop.rs index 21adb48b75d..5d840032ec2 100644 --- a/packages/yew-macro/src/props/prop.rs +++ b/packages/yew-macro/src/props/prop.rs @@ -35,7 +35,10 @@ impl Prop { /// Parse a prop using the shorthand syntax `{value}`, short for `value={value}` /// This only allows for labels with no hyphens, as it would otherwise create /// an ambiguity in the syntax - fn parse_shorthand_prop_assignment(input: ParseStream, is_forced_attribute: bool) -> syn::Result { + fn parse_shorthand_prop_assignment( + input: ParseStream, + is_forced_attribute: bool, + ) -> syn::Result { let value; let _brace = braced!(value in input); let expr = value.parse::()?; @@ -61,7 +64,11 @@ impl Prop { )); }?; - Ok(Self { label, value: expr, is_forced_attribute }) + Ok(Self { + label, + value: expr, + is_forced_attribute, + }) } /// Parse a prop of the form `label={value}` @@ -85,7 +92,11 @@ impl Prop { } let value = parse_prop_value(input)?; - Ok(Self { label, value, is_forced_attribute }) + Ok(Self { + label, + value, + is_forced_attribute, + }) } } diff --git a/packages/yew-macro/src/props/prop_macro.rs b/packages/yew-macro/src/props/prop_macro.rs index 060b60f3259..99e8948fb49 100644 --- a/packages/yew-macro/src/props/prop_macro.rs +++ b/packages/yew-macro/src/props/prop_macro.rs @@ -61,7 +61,11 @@ impl Parse for PropValue { impl From for Prop { fn from(prop_value: PropValue) -> Prop { let PropValue { label, value } = prop_value; - Prop { label, value, is_forced_attribute: false } + Prop { + label, + value, + is_forced_attribute: false, + } } } diff --git a/packages/yew/src/dom_bundle/btag/attributes.rs b/packages/yew/src/dom_bundle/btag/attributes.rs index 3aaf1723cb5..d8c0fe86f5b 100644 --- a/packages/yew/src/dom_bundle/btag/attributes.rs +++ b/packages/yew/src/dom_bundle/btag/attributes.rs @@ -295,11 +295,13 @@ impl Apply for Attributes { #[cfg(test)] mod tests { use std::time::Duration; + use gloo::utils::document; use js_sys::Reflect; use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; - use crate::{html, Html, function_component}; + use super::*; + use crate::{function_component, html, Html}; wasm_bindgen_test_configure!(run_in_browser); @@ -345,7 +347,11 @@ mod tests { ]); let (element, btree) = create_element(); attrs.apply(&btree, &element); - assert_eq!(element.outer_html(), "", "should be set as attribute"); + assert_eq!( + element.outer_html(), + "", + "should be set as attribute" + ); assert_eq!( Reflect::get(element.as_ref(), &JsValue::from_str("alt")) .expect("no alt") @@ -358,13 +364,10 @@ mod tests { #[test] fn class_is_always_attrs() { - let attrs = Attributes::Static(&[ - ("class", "thing", ApplyAttributeAs::Attribute), - ]); + let attrs = Attributes::Static(&[("class", "thing", ApplyAttributeAs::Attribute)]); let (element, btree) = create_element(); attrs.apply(&btree, &element); - assert_eq!(element.get_attribute("id").unwrap(), "foo"); assert_eq!(element.get_attribute("class").unwrap(), "thing"); } @@ -376,8 +379,8 @@ mod tests { } let output = gloo::utils::document().get_element_by_id("output").unwrap(); - yew::Renderer::::with_root(output.clone()) - .render(); + yew::Renderer::::with_root(output.clone()).render(); + gloo::timers::future::sleep(Duration::from_secs(1)).await; let element = output.query_selector("a").unwrap().unwrap(); assert_eq!(element.get_attribute("alt").unwrap(), "abc"); diff --git a/packages/yew/src/dom_bundle/btag/mod.rs b/packages/yew/src/dom_bundle/btag/mod.rs index 3a30874bea2..d504ea0386b 100644 --- a/packages/yew/src/dom_bundle/btag/mod.rs +++ b/packages/yew/src/dom_bundle/btag/mod.rs @@ -386,7 +386,7 @@ mod feat_hydration { #[cfg(test)] mod tests { use gloo::utils::document; - use wasm_bindgen::{JsCast}; + use wasm_bindgen::JsCast; use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; use web_sys::HtmlInputElement as InputElement;