Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docstrings and other attributes in component! macro #309

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: _ }));
}
Loading