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

Fix Affixed Type Aliases #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions tests/affixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn test_prefix() {

#[test]
fn test_suffix() {
#[tsify::declare(type_suffix = "Special")]
type MyType = u32;

#[derive(Tsify)]
Expand Down
14 changes: 14 additions & 0 deletions tests/expand/type_alias.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ const _: () = {
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = "export type TypeAlias<T, U> = Foo<T, number, U>;";
};
type SuffixedTypeAlias = u32;
#[automatically_derived]
const _: () = {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = "export type SuffixedTypeAliasSpecial = number;";
};
type SuffixedTypeAlias<AndThis> = ThisShouldBeSuffixedToo<AndThis>;
#[automatically_derived]
const _: () = {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = "export type SuffixedTypeAliasSpecial<AndThisSpecial> = ThisShouldBeSuffixedTooSpecial<AndThisSpecial>;";
};
4 changes: 4 additions & 0 deletions tests/expand/type_alias.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#[tsify::declare]
type TypeAlias<T, U> = Foo<T, i32, U>;
#[tsify::declare(type_suffix = "Special")]
type SuffixedTypeAlias = u32;
#[tsify::declare(type_suffix = "Special")]
type SuffixedTypeAlias<AndThis> = ThisShouldBeSuffixedToo<AndThis>;
22 changes: 17 additions & 5 deletions tsify-macros/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,29 @@ impl TypeGenerationConfig {
}

impl TsifyContainerAttrs {
pub fn from_derive_input(input: &syn::DeriveInput) -> syn::Result<Self> {
pub fn from_attrs(
attribute_list: &[syn::Attribute],
data: Option<&syn::Data>,
) -> syn::Result<Self> {
let mut attrs = Self {
into_wasm_abi: false,
from_wasm_abi: false,
namespace: false,
ty_config: TypeGenerationConfig::default(),
comments: extract_doc_comments(&input.attrs),
comments: extract_doc_comments(&attribute_list),
};

for attr in &input.attrs {
if !attr.path().is_ident("tsify") {
for attr in attribute_list {
let path_segments: Vec<String> = attr
.path()
.segments
.iter()
.map(|seg| seg.ident.to_string())
.collect();
let is_tsify = attr.path().is_ident("tsify");
let is_declare =
attr.path().is_ident("declare") || path_segments == ["tsify", "declare"];
if !(is_tsify || is_declare) {
continue;
}

Expand All @@ -64,7 +76,7 @@ impl TsifyContainerAttrs {
}

if meta.path.is_ident("namespace") {
if !matches!(input.data, syn::Data::Enum(_)) {
if !matches!(data, Some(syn::Data::Enum(_))) {
return Err(meta.error("#[tsify(namespace)] can only be used on enums"));
}
if attrs.namespace {
Expand Down
2 changes: 1 addition & 1 deletion tsify-macros/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Container<'a> {
impl<'a> Container<'a> {
pub fn new(serde_container: SerdeContainer<'a>) -> Self {
let input = &serde_container.original;
let attrs = TsifyContainerAttrs::from_derive_input(input);
let attrs = TsifyContainerAttrs::from_attrs(&input.attrs, Some(&input.data));
let ctxt = Ctxt::new();

let attrs = match attrs {
Expand Down
2 changes: 1 addition & 1 deletion tsify-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn declare_impl(
item: syn::Item,
) -> syn::Result<proc_macro2::TokenStream> {
match item {
syn::Item::Type(item) => type_alias::expend(item),
syn::Item::Type(item) => type_alias::expand(args, item),
syn::Item::Enum(item) => derive::expand_by_attr(args, item.into()),
syn::Item::Struct(item) => derive::expand_by_attr(args, item.into()),
_ => Err(syn::Error::new_spanned(
Expand Down
18 changes: 13 additions & 5 deletions tsify-macros/src/type_alias.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_quote;

use crate::{
attrs::TypeGenerationConfig, comments::extract_doc_comments, ctxt::Ctxt, decl::TsTypeAliasDecl,
attrs::TsifyContainerAttrs, comments::extract_doc_comments, ctxt::Ctxt, decl::TsTypeAliasDecl,
typescript::TsType,
};

pub fn expend(item: syn::ItemType) -> syn::Result<TokenStream> {
pub fn expand(args: proc_macro2::TokenStream, item: syn::ItemType) -> syn::Result<TokenStream> {
let ctxt = Ctxt::new();

let type_ann = TsType::from_syn_type(&TypeGenerationConfig::default(), item.ty.as_ref());
let attribute = parse_quote!(#[tsify(#args)]);

let attrs = TsifyContainerAttrs::from_attrs(&[attribute], None)?;

let type_ann = TsType::from_syn_type(&attrs.ty_config, item.ty.as_ref());

let decl = TsTypeAliasDecl {
id: item.ident.to_string(),
id: attrs.ty_config.format_name(item.ident.to_string()),
export: true,
type_params: item
.generics
.type_params()
.map(|ty| ty.ident.to_string())
// While strictly speaking we shouldn't need to suffix the type parameters, the
// type parsing code doesn't know if the type its looking at is referencing this
// type's generic or some other type. So we just suffix everything
.map(|ty| attrs.ty_config.format_name(ty.ident.to_string()))
.collect(),
type_ann,
comments: extract_doc_comments(&item.attrs),
Expand Down
Loading