Skip to content

Commit 3cf8b1d

Browse files
committed
Docstrings and other attributes in component! macro
1 parent b610a82 commit 3cf8b1d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
* Hush excessive logging when no Content-Type or cookie provided
1717

18+
### Fixed
19+
20+
* Docstrings and other attributes in `component!` macro
21+
1822
## 0.6.0 - 2024-08-02
1923

2024
### Added

crates/vertigo-macro/src/component.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro::{Span, TokenStream};
22
use quote::{quote, ToTokens};
3-
use syn::Visibility;
3+
use syn::{FnArg, Visibility};
44

55
pub(crate) fn component_inner(input: TokenStream) -> TokenStream {
66
let ast = syn::parse_macro_input!(input as syn::ItemFn);
@@ -25,10 +25,16 @@ pub(crate) fn component_inner(input: TokenStream) -> TokenStream {
2525

2626
let mut struct_fields = Vec::new();
2727

28-
for field in ast.sig.inputs.iter() {
29-
struct_fields.push(quote! {
30-
pub #field
31-
})
28+
for field in ast.sig.inputs.clone().into_iter() {
29+
match field {
30+
FnArg::Receiver(_) => unreachable!(),
31+
FnArg::Typed(mut pat_type) => {
32+
let attrs = pat_type.attrs.drain(..);
33+
struct_fields.push(quote! {
34+
#(#attrs)* pub #pat_type
35+
})
36+
}
37+
}
3238
}
3339

3440
let body = ast.block;

crates/vertigo/src/tests/dom/component.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,24 @@ fn test_namespaces() {
7272
_ => panic!("Expected DomNode::Node"),
7373
}
7474
}
75+
76+
#[test]
77+
fn test_docstrings() {
78+
use crate::{self as vertigo, component, dom, DomNode};
79+
80+
#[component]
81+
fn Hello<'a>(
82+
/// Name of the person you want to greet
83+
name: &'a str,
84+
) {
85+
dom! {
86+
<span>"Hello " {name}</span>
87+
}
88+
}
89+
90+
let ret = dom! {
91+
<p><Hello name={"world"} /></p>
92+
};
93+
94+
assert!(matches!(ret, DomNode::Node { node: _ }));
95+
}

0 commit comments

Comments
 (0)