From 3e81856908e8149a856082d194a55ecb402ff7d8 Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Thu, 11 Jan 2024 17:23:56 -0500 Subject: [PATCH 1/2] Ensure type names ignore serde rename when exporting --- tsify-macros/src/container.rs | 4 ++++ tsify-macros/src/decl.rs | 4 +++- tsify-macros/src/parser.rs | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tsify-macros/src/container.rs b/tsify-macros/src/container.rs index fb44844..b8395f0 100644 --- a/tsify-macros/src/container.rs +++ b/tsify-macros/src/container.rs @@ -47,6 +47,10 @@ impl<'a> Container<'a> { &self.serde_container.ident } + pub fn ident_str(&self) -> String { + self.ident().to_string() + } + #[inline] pub fn serde_attrs(&self) -> &attr::Container { &self.serde_container.attrs diff --git a/tsify-macros/src/decl.rs b/tsify-macros/src/decl.rs index 9be3a9e..4bd5f49 100644 --- a/tsify-macros/src/decl.rs +++ b/tsify-macros/src/decl.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use crate::typescript::{TsType, TsTypeElement, TsTypeLit}; -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct TsTypeAliasDecl { pub id: String, pub export: bool, @@ -27,6 +27,7 @@ impl Display for TsTypeAliasDecl { } } +#[derive(Debug)] pub struct TsInterfaceDecl { pub id: String, pub type_params: Vec, @@ -69,6 +70,7 @@ impl Display for TsInterfaceDecl { } } +#[derive(Debug)] pub struct TsEnumDecl { pub id: String, pub type_params: Vec, diff --git a/tsify-macros/src/parser.rs b/tsify-macros/src/parser.rs index 66c6c2d..08567ee 100644 --- a/tsify-macros/src/parser.rs +++ b/tsify-macros/src/parser.rs @@ -70,7 +70,7 @@ impl<'a> Parser<'a> { fn create_type_alias_decl(&self, type_ann: TsType) -> Decl { Decl::TsTypeAlias(TsTypeAliasDecl { - id: self.container.name(), + id: self.container.ident_str(), export: true, type_params: self.create_relevant_type_params(type_ann.type_ref_names()), type_ann, @@ -91,7 +91,7 @@ impl<'a> Parser<'a> { let type_params = self.create_relevant_type_params(type_ref_names); Decl::TsInterface(TsInterfaceDecl { - id: self.container.name(), + id: self.container.ident_str(), type_params, extends, body: members, @@ -264,7 +264,7 @@ impl<'a> Parser<'a> { let relevant_type_params = self.create_relevant_type_params(type_ref_names); Decl::TsEnum(TsEnumDecl { - id: self.container.name(), + id: self.container.ident_str(), type_params: relevant_type_params, members, namespace: self.container.attrs.namespace, From 36f42bef747bb918c4ec913df479ad548ec26276 Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Sat, 13 Jan 2024 19:39:26 -0500 Subject: [PATCH 2/2] Added tests --- tests/reference_rename.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/reference_rename.rs diff --git a/tests/reference_rename.rs b/tests/reference_rename.rs new file mode 100644 index 0000000..0f641f0 --- /dev/null +++ b/tests/reference_rename.rs @@ -0,0 +1,36 @@ +#![allow(dead_code)] + +use indoc::indoc; +use pretty_assertions::assert_eq; +use tsify::Tsify; + +#[test] +fn test_reference_rename() { + #[derive(Tsify)] + #[serde(rename = "foo")] + pub struct Foo { + x: i32, + } + + #[derive(Tsify)] + pub struct Bar { + foo: Foo, + } + + assert_eq!( + Bar::DECL, + indoc! {" + export interface Bar { + foo: Foo; + }" + } + ); + assert_eq!( + Foo::DECL, + indoc! {" + export interface Foo { + x: number; + }" + } + ); +}