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: Include borrowed types' type arguments in typescript #181

Merged
merged 1 commit into from
Dec 12, 2023
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
7 changes: 5 additions & 2 deletions macros/src/types/generics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
GenericArgument, GenericParam, Generics, ItemStruct, PathArguments, Type, TypeGroup, TypeTuple,
GenericArgument, GenericParam, Generics, ItemStruct, PathArguments, Type, TypeGroup,
TypeReference, TypeTuple,
};

use crate::{attr::StructAttr, deps::Dependencies};
Expand Down Expand Up @@ -109,7 +110,9 @@ pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generi

fn extract_type_args(ty: &Type) -> Option<Vec<&Type>> {
let last_segment = match ty {
Type::Group(TypeGroup { elem, .. }) => return extract_type_args(elem),
Type::Group(TypeGroup { elem, .. }) | Type::Reference(TypeReference { elem, .. }) => {
return extract_type_args(elem)
}
Type::Path(type_path) => type_path.path.segments.last(),
_ => None,
}?;
Expand Down
22 changes: 22 additions & 0 deletions ts-rs/tests/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,25 @@ fn contains_borrow() {

assert_eq!(S::decl(), "interface S { s: string, }")
}

#[test]
fn contains_borrow_type_args() {
#[derive(TS)]
#[allow(dead_code)]
struct B<'a, T: 'a> {
a: &'a T,
}

#[derive(TS)]
#[allow(dead_code)]
struct A<'a> {
a: &'a &'a &'a Vec<u32>, //Multiple References
b: &'a Vec<B<'a, u32>>, //Nesting
c: &'a std::collections::HashMap<String, bool>, //Multiple type args
}

assert_eq!(
A::decl(),
"interface A { a: Array<number>, b: Array<B<number>>, c: Record<string, boolean>, }"
);
}
Loading