Skip to content

Commit

Permalink
fix #196 - fix Specta with wasm bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Dec 24, 2023
1 parent 9b26be2 commit 1291efd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ once_cell = "1.18.0"
doc-comment = "0.3.3"
serde = { version = "1.0.183", features = ["derive"] }
trybuild = "1.0.82"
wasm-bindgen = "0.2.89"
14 changes: 14 additions & 0 deletions macros/src/specta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token
let function = parse_macro_input::parse::<ItemFn>(item)?;
let wrapper = format_fn_wrapper(&function.sig.ident);

// While using wasm_bindgen and Specta is rare, this should make the DX nicer.
if function.sig.unsafety.is_some()
&& function
.sig
.ident
.to_string()
.starts_with("__wasm_bindgen_generated")
{
return Err(syn::Error::new_spanned(
function.sig.ident,
"specta: You must apply the #[specta] macro before the #[wasm_bindgen] macro",
));
}

let visibility = &function.vis;
let (maybe_macro_export, pub_the_trait) = match &visibility {
Visibility::Public(_) => (quote!(#[macro_export]), Default::default()),
Expand Down
8 changes: 5 additions & 3 deletions macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ pub fn parse_attrs(attrs: &[syn::Attribute]) -> syn::Result<Vec<Attribute>> {
for attr in attrs {
let ident = attr
.path
.get_ident()
.expect("Attribute path must be an ident")
.clone();
.segments
.last()
.expect("Attribute path must have at least one segment")
.clone()
.ident;

// TODO: We should somehow build this up from the macro output automatically -> if not our attribute parser is applied to stuff like `allow` and that's bad.
if !(ident == "specta"
Expand Down
4 changes: 4 additions & 0 deletions tests/macro/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ pub struct InvalidSpectaAttribute1;
#[specta = "todo"]
pub struct InvalidSpectaAttribute2;

#[wasm_bindgen::prelude::wasm_bindgen]
#[specta]
pub fn testing() {}

// TODO: https://docs.rs/trybuild/latest/trybuild/#what-to-test

0 comments on commit 1291efd

Please sign in to comment.