Skip to content

Commit

Permalink
Docstrings and other attributes in component! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Dec 18, 2024
1 parent b610a82 commit 3dae491
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

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

### Fixed

* Docstrings and other attributes in `component!` macro

## 0.6.0 - 2024-08-02

### Added
Expand Down
16 changes: 11 additions & 5 deletions crates/vertigo-macro/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::Visibility;
use syn::{FnArg, Visibility};

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

let mut struct_fields = Vec::new();

for field in ast.sig.inputs.iter() {
struct_fields.push(quote! {
pub #field
})
for field in ast.sig.inputs.clone().into_iter() {
match field {
FnArg::Receiver(_) => unreachable!(),
FnArg::Typed(mut pat_type) => {
let attrs = pat_type.attrs.drain(..);
struct_fields.push(quote! {
#(#attrs)* pub #pat_type
})
}
}
}

let body = ast.block;
Expand Down
23 changes: 22 additions & 1 deletion crates/vertigo/src/tests/dom/component.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[test]
fn test_lifetimes() {
fn test_if_lifetimes_allowed() {
use crate::{self as vertigo, component, dom, DomNode};

#[component]
Expand Down Expand Up @@ -72,3 +72,24 @@ fn test_namespaces() {
_ => panic!("Expected DomNode::Node"),
}
}

#[test]
fn test_if_docstrings_allowed() {
use crate::{self as vertigo, component, dom, DomNode};

#[component]
fn Hello<'a>(
/// Name of the person you want to greet
name: &'a str,
) {
dom! {
<span>"Hello " {name}</span>
}
}

let ret = dom! {
<p><Hello name={"world"} /></p>
};

assert!(matches!(ret, DomNode::Node { node: _ }));
}

0 comments on commit 3dae491

Please sign in to comment.