From a54a369c8c09e0fe0a6312f05b503f8598fdd57b Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 28 Mar 2024 10:59:29 -0300 Subject: [PATCH 1/4] Fix rename_all_fields on enums containing unnamed fields --- macros/src/attr/variant.rs | 11 +++++++++++ macros/src/types/enum.rs | 5 ++++- ts-rs/tests/enum_struct_rename_all.rs | 6 +++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/macros/src/attr/variant.rs b/macros/src/attr/variant.rs index c6db337c2..27450e6f2 100644 --- a/macros/src/attr/variant.rs +++ b/macros/src/attr/variant.rs @@ -32,6 +32,17 @@ impl VariantAttr { Ok(result) } + pub fn from_attrs(attrs: &[Attribute]) -> Result { + let mut result = Self::default(); + parse_attrs(attrs)?.for_each(|a| result.merge(a)); + #[cfg(feature = "serde-compat")] + if !result.skip { + crate::utils::parse_serde_attrs::(attrs) + .for_each(|a| result.merge(a.0)); + } + Ok(result) + } + fn merge( &mut self, VariantAttr { diff --git a/macros/src/types/enum.rs b/macros/src/types/enum.rs index 130b4aaba..aea19c18e 100644 --- a/macros/src/types/enum.rs +++ b/macros/src/types/enum.rs @@ -71,7 +71,10 @@ fn format_variant( variant: &Variant, ) -> syn::Result<()> { let crate_rename = enum_attr.crate_rename(); - let variant_attr = VariantAttr::new(&variant.attrs, enum_attr)?; + let variant_attr = match variant.fields { + Fields::Unit | Fields::Unnamed(_) => VariantAttr::from_attrs(&variant.attrs)?, + Fields::Named(_) => VariantAttr::new(&variant.attrs, enum_attr)?, + }; if variant_attr.skip { return Ok(()); diff --git a/ts-rs/tests/enum_struct_rename_all.rs b/ts-rs/tests/enum_struct_rename_all.rs index bbc798fd0..b43edcbc7 100644 --- a/ts-rs/tests/enum_struct_rename_all.rs +++ b/ts-rs/tests/enum_struct_rename_all.rs @@ -44,12 +44,16 @@ pub enum TaskStatus2 { stdout: String, stderr: String, }, + + A(i32), + B(i32, i32), + C, } #[test] pub fn enum_struct_rename_all_fields() { assert_eq!( TaskStatus2::inline(), - r#"{ "Running": { "started-time": string, } } | { "Terminated": { status: number, stdout: string, stderr: string, } }"# + r#"{ "Running": { "started-time": string, } } | { "Terminated": { status: number, stdout: string, stderr: string, } } | { "A": number } | { "B": [number, number] } | "C""# ) } From de81057d0a19173f16fbba75490a397c040ed958 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 28 Mar 2024 11:04:18 -0300 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e5b462d5..57353af60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # master + ### Breaking + ### Features + ### Fixes +Fix `#[ts(rename_all_fields = "...")]` on enums containing tuple or unit variants ([#287](https://github.com/Aleph-Alpha/ts-rs/pull/287)) # 8.1.0 From c0404b05d741cdb87e33d043aa50133959888c45 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 28 Mar 2024 16:42:28 -0300 Subject: [PATCH 3/4] Remove duplication between new and from_attrs --- macros/src/attr/variant.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/macros/src/attr/variant.rs b/macros/src/attr/variant.rs index 27450e6f2..cca953268 100644 --- a/macros/src/attr/variant.rs +++ b/macros/src/attr/variant.rs @@ -21,14 +21,8 @@ pub struct SerdeVariantAttr(VariantAttr); impl VariantAttr { pub fn new(attrs: &[Attribute], enum_attr: &EnumAttr) -> Result { - let mut result = Self::default(); - parse_attrs(attrs)?.for_each(|a| result.merge(a)); + let mut result = Self::from_attrs(attrs)?; result.rename_all = result.rename_all.or(enum_attr.rename_all_fields); - #[cfg(feature = "serde-compat")] - if !result.skip { - crate::utils::parse_serde_attrs::(attrs) - .for_each(|a| result.merge(a.0)); - } Ok(result) } From 2d71c86bde6bc923c13cc7894e0c2f7de2cc978a Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 28 Mar 2024 16:46:12 -0300 Subject: [PATCH 4/4] Explain match expression --- macros/src/types/enum.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/macros/src/types/enum.rs b/macros/src/types/enum.rs index aea19c18e..75077a65a 100644 --- a/macros/src/types/enum.rs +++ b/macros/src/types/enum.rs @@ -71,6 +71,10 @@ fn format_variant( variant: &Variant, ) -> syn::Result<()> { let crate_rename = enum_attr.crate_rename(); + + // If `variant.fields` is not a `Fields::Named(_)` the `rename_all_fields` + // attribute must be ignored to prevent a `rename_all` from getting to + // the newtype, tuple or unit formatting, which would cause an error let variant_attr = match variant.fields { Fields::Unit | Fields::Unnamed(_) => VariantAttr::from_attrs(&variant.attrs)?, Fields::Named(_) => VariantAttr::new(&variant.attrs, enum_attr)?,