diff --git a/tests/skip.rs b/tests/skip.rs index 618a90a..ac6676a 100644 --- a/tests/skip.rs +++ b/tests/skip.rs @@ -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#" @@ -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"; } /** * Comment for Enum */ - export type Enum = "D";"# + export type Enum = "D" | { Struct: { field_b: number; field_c: string } } | { Tuple: [number, string] } | "NewType";"# }; 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); } diff --git a/tsify-next-macros/src/parser.rs b/tsify-next-macros/src/parser.rs index c10d490..4c0a42e 100644 --- a/tsify-next-macros/src/parser.rs +++ b/tsify-next-macros/src/parser.rs @@ -288,7 +288,16 @@ impl<'a> Parser<'a> { fn parse_variant(&self, variant: &Variant) -> TsType { let tag_type = self.container.serde_attrs().tag(); let name = variant.attrs.name().serialize_name().to_owned(); - let style = variant.style; + // Checks for Newtype with a skip attribute and treats it as a Unit + let style = if matches!(variant.style, Style::Newtype) + && (variant.fields[0].attrs.skip_serializing() + || variant.fields[0].attrs.skip_deserializing() + || is_phantom(variant.fields[0].ty)) + { + Style::Unit + } else { + variant.style + }; let type_ann: TsType = self.parse_fields(style, &variant.fields).into(); type_ann.with_tag_type(&self.container.attrs.ty_config, name, style, tag_type) }