Make all the attribute args optional

This commit is contained in:
Jeremy Wall 2022-10-06 23:59:30 -04:00
parent 7e257ba7bc
commit c311de79b1
2 changed files with 24 additions and 11 deletions

View File

@ -33,7 +33,10 @@ fn expand_crate_ref(name: &str, path: Path) -> syn::Path {
}
}
fn get_class_and_element_names(args: Vec<NestedMeta>) -> (Literal, Literal, Literal) {
fn get_class_and_element_names(
args: Vec<NestedMeta>,
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<NestedMeta>) -> (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)
}

View File

@ -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");
}
}