Skip to content

Commit

Permalink
Add skip support for newtype enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
johanholmerin committed Aug 10, 2024
1 parent 29abef8 commit 285f720
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
69 changes: 68 additions & 1 deletion tests/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ fn test_skip() {
C,
/// Comment for D
D,
/// Comment for Struct
Struct {
#[serde(skip)]
field_a: bool,
field_b: u8,
field_c: String,
},
/// Comment for Tuple
Tuple(#[serde(skip)] bool, u8, String),
/// Comment for NewType
NewType(#[serde(skip)] bool),
}

let expected = indoc! {r#"
Expand All @@ -77,13 +88,69 @@ fn test_skip() {
* Comment for D
*/
export type D = "D";
/**
* Comment for Struct
*/
export type Struct = { Struct: { field_b: number; field_c: string } };
/**
* Comment for Tuple
*/
export type Tuple = { Tuple: [number, string] };
/**
* Comment for NewType
*/
export type NewType = { NewType: null };
}
/**
* Comment for Enum
*/
export type Enum = "D";"#
export type Enum = "D" | { Struct: { field_b: number; field_c: string } } | { Tuple: [number, string] } | { NewType: null };"#
};

assert_eq!(Enum::DECL, expected);

/// Comment for InternalTagEnum
#[derive(Tsify)]
#[serde(tag = "type")]
#[tsify(namespace)]
enum InternalTagEnum {
/// Comment for Unit
Unit,
/// Comment for Struct
Struct {
#[serde(skip)]
field_a: bool,
field_b: u8,
},
/// Comment for NewType
NewType(#[serde(skip)] bool),
}

let expected = indoc! {r#"
/**
* Comment for InternalTagEnum
*/
declare namespace InternalTagEnum {
/**
* Comment for Unit
*/
export type Unit = { type: "Unit" };
/**
* Comment for Struct
*/
export type Struct = { type: "Struct"; field_b: number };
/**
* Comment for NewType
*/
export type NewType = { type: "NewType" };
}
/**
* Comment for InternalTagEnum
*/
export type InternalTagEnum = { type: "Unit" } | { type: "Struct"; field_b: number } | { type: "NewType" };"#
};

assert_eq!(InternalTagEnum::DECL, expected);
}
13 changes: 12 additions & 1 deletion tsify-next-macros/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ impl<'a> Parser<'a> {
fn parse_fields(&self, style: Style, fields: &[Field]) -> ParsedFields {
let style = match style {
Style::Struct => FieldsStyle::Named,
Style::Newtype => return ParsedFields::Transparent(self.parse_field(&fields[0]).0),
Style::Newtype => {
let field = &fields[0];
if field.attrs.skip_serializing()
|| field.attrs.skip_deserializing()
|| is_phantom(field.ty)
{
return ParsedFields::Transparent(TsType::nullish(
&self.container.attrs.ty_config,
));
}
return ParsedFields::Transparent(self.parse_field(field).0);
}
Style::Tuple => FieldsStyle::Unnamed,
Style::Unit => {
return ParsedFields::Transparent(TsType::nullish(&self.container.attrs.ty_config))
Expand Down

0 comments on commit 285f720

Please sign in to comment.