Skip to content

Commit e1d4742

Browse files
committed
Add skip support for newtype enum variants
1 parent 29abef8 commit e1d4742

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

tests/skip.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ fn test_skip() {
6666
C,
6767
/// Comment for D
6868
D,
69+
/// Comment for Struct
70+
Struct {
71+
#[serde(skip)]
72+
field_a: bool,
73+
field_b: u8,
74+
field_c: String,
75+
},
76+
/// Comment for Tuple
77+
Tuple(#[serde(skip)] bool, u8, String),
78+
/// Comment for NewType
79+
NewType(#[serde(skip)] bool),
6980
}
7081

7182
let expected = indoc! {r#"
@@ -77,13 +88,69 @@ fn test_skip() {
7788
* Comment for D
7889
*/
7990
export type D = "D";
91+
/**
92+
* Comment for Struct
93+
*/
94+
export type Struct = { Struct: { field_b: number; field_c: string } };
95+
/**
96+
* Comment for Tuple
97+
*/
98+
export type Tuple = { Tuple: [number, string] };
99+
/**
100+
* Comment for NewType
101+
*/
102+
export type NewType = "NewType";
80103
}
81104
82105
/**
83106
* Comment for Enum
84107
*/
85-
export type Enum = "D";"#
108+
export type Enum = "D" | { Struct: { field_b: number; field_c: string } } | { Tuple: [number, string] } | "NewType";"#
86109
};
87110

88111
assert_eq!(Enum::DECL, expected);
112+
113+
/// Comment for InternalTagEnum
114+
#[derive(Tsify)]
115+
#[serde(tag = "type")]
116+
#[tsify(namespace)]
117+
enum InternalTagEnum {
118+
/// Comment for Unit
119+
Unit,
120+
/// Comment for Struct
121+
Struct {
122+
#[serde(skip)]
123+
field_a: bool,
124+
field_b: u8,
125+
},
126+
/// Comment for NewType
127+
NewType(#[serde(skip)] bool),
128+
}
129+
130+
let expected = indoc! {r#"
131+
/**
132+
* Comment for InternalTagEnum
133+
*/
134+
declare namespace InternalTagEnum {
135+
/**
136+
* Comment for Unit
137+
*/
138+
export type Unit = { type: "Unit" };
139+
/**
140+
* Comment for Struct
141+
*/
142+
export type Struct = { type: "Struct"; field_b: number };
143+
/**
144+
* Comment for NewType
145+
*/
146+
export type NewType = { type: "NewType" };
147+
}
148+
149+
/**
150+
* Comment for InternalTagEnum
151+
*/
152+
export type InternalTagEnum = { type: "Unit" } | { type: "Struct"; field_b: number } | { type: "NewType" };"#
153+
};
154+
155+
assert_eq!(InternalTagEnum::DECL, expected);
89156
}

tsify-next-macros/src/parser.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,16 @@ impl<'a> Parser<'a> {
288288
fn parse_variant(&self, variant: &Variant) -> TsType {
289289
let tag_type = self.container.serde_attrs().tag();
290290
let name = variant.attrs.name().serialize_name().to_owned();
291-
let style = variant.style;
291+
// Checks for Newtype with a skip attribute and treats it as a Unit
292+
let style = if matches!(variant.style, Style::Newtype)
293+
&& (variant.fields[0].attrs.skip_serializing()
294+
|| variant.fields[0].attrs.skip_deserializing()
295+
|| is_phantom(variant.fields[0].ty))
296+
{
297+
Style::Unit
298+
} else {
299+
variant.style
300+
};
292301
let type_ann: TsType = self.parse_fields(style, &variant.fields).into();
293302
type_ann.with_tag_type(&self.container.attrs.ty_config, name, style, tag_type)
294303
}

0 commit comments

Comments
 (0)