From c311de79b1914e984c824535be0c7afe4ff78b7a Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 6 Oct 2022 23:59:30 -0400 Subject: [PATCH] Make all the attribute args optional --- macros/src/lib.rs | 17 ++++++++++------- wasm-web-component/src/lib.rs | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 94dea5c..5419f7c 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -33,7 +33,10 @@ fn expand_crate_ref(name: &str, path: Path) -> syn::Path { } } -fn get_class_and_element_names(args: Vec) -> (Literal, Literal, Literal) { +fn get_class_and_element_names( + args: Vec, + struct_name: &Ident, +) -> (Literal, Literal, Literal) { let mut class_name = None; let mut element_name = None; let mut observed_attributes = None; @@ -54,12 +57,11 @@ fn get_class_and_element_names(args: Vec) -> (Literal, Literal, Lite } } } - // TODO(jwall): it should be a compile error if this is missing. - let class_name = class_name - .map(|n| n.token()) - .unwrap_or_else(|| LitStr::new("", Span::call_site()).token()); - // TODO(jwall): if Missing we should derive this from the class name. + let class_name = class_name.map(|n| n.token()).unwrap_or_else(|| { + LitStr::new(struct_name.to_string().as_ref(), Span::call_site()).token() + }); + let element_name = match element_name.map(|n| n.token()) { Some(n) => n, None => { @@ -266,7 +268,8 @@ pub fn web_component(attr: TokenStream, item: TokenStream) -> TokenStream { let args = parse_macro_input!(attr as AttributeArgs); let item_struct = parse_macro_input!(item as ItemStruct); - let (class_name, element_name, observed_attributes) = get_class_and_element_names(args); + let (class_name, element_name, observed_attributes) = + get_class_and_element_names(args, &item_struct.ident); expand_struct(item_struct, class_name, element_name, observed_attributes) } diff --git a/wasm-web-component/src/lib.rs b/wasm-web-component/src/lib.rs index 80eeccb..e06ba10 100644 --- a/wasm-web-component/src/lib.rs +++ b/wasm-web-component/src/lib.rs @@ -11,10 +11,10 @@ use web_sys::{window, Element, HtmlElement}; /// It expects you to implement the [WebComponentBinding](trait@WebComponentBinding) /// trait in order to implement the callbacks. /// -/// It supports three attribute `name = value` parameters. -/// * `class_name = "ClassName"` - Required. The class name to use for the javascript shim. -/// * `element_name = "class-name"` - Optional. A valid custom element name to use for the element. -/// * `observed_attrs = "['attr1', attr2']"` - Optional. A javascript array with a list of observed attributes for this compoment. +/// It supports three optional attributes `name = value` parameters. +/// * `class_name = "ClassName"` - The class name to use for the javascript shim. If not provided uses the structs name instead. +/// * `element_name = "class-name"` - A valid custom element name to use for the element. if not proviced derives it from the class name. +/// * `observed_attrs = "['attr1', attr2']"` - A javascript array with a list of observed attributes for this compoment. Defaults to "[]". /// /// Reference [MDN Web Components Guide](https://developer.mozilla.org/en-US/docs/Web/Web_Components) pub use wasm_web_component_macros::web_component; @@ -206,4 +206,14 @@ mod tests { assert_eq!(AnElement::element_name(), "an-element"); } + + #[wasm_bindgen_test] + fn test_component_no_class_name() { + #[web_component] + pub struct AnotherElement {} + impl WebComponentBinding for AnotherElement {} + + assert_eq!(AnotherElement::class_name(), "AnotherElement"); + assert_eq!(AnotherElement::element_name(), "another-element"); + } }