From e1d474216cc0e3873a9fc1f5a8b78d33a42671b6 Mon Sep 17 00:00:00 2001 From: Johan Holmerin Date: Mon, 12 Aug 2024 19:47:42 +0200 Subject: [PATCH] Add skip support for newtype enum variants --- tests/skip.rs | 69 ++++++++++++++++++++++++++++++++- tsify-next-macros/src/parser.rs | 11 +++++- 2 files changed, 78 insertions(+), 2 deletions(-) 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) }