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

Ensure type names ignore serde rename when exporting #44

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
36 changes: 36 additions & 0 deletions tests/reference_rename.rs
Original file line number Diff line number Diff line change
@@ -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;
}"
}
);
}
4 changes: 4 additions & 0 deletions tsify-macros/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tsify-macros/src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +27,7 @@ impl Display for TsTypeAliasDecl {
}
}

#[derive(Debug)]
pub struct TsInterfaceDecl {
pub id: String,
pub type_params: Vec<String>,
Expand Down Expand Up @@ -69,6 +70,7 @@ impl Display for TsInterfaceDecl {
}
}

#[derive(Debug)]
pub struct TsEnumDecl {
pub id: String,
pub type_params: Vec<String>,
Expand Down
6 changes: 3 additions & 3 deletions tsify-macros/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down