Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #[ts(rename_all_fields = "...")] on enums containing tuple or unit variants #287

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# master

### Breaking

### Features

- Add support for `#[ts(type = "..")]` directly on structs and enums ([#286](https://github.com/Aleph-Alpha/ts-rs/pull/286))

### 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

Expand Down
7 changes: 6 additions & 1 deletion macros/src/attr/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ pub struct SerdeVariantAttr(VariantAttr);

impl VariantAttr {
pub fn new(attrs: &[Attribute], enum_attr: &EnumAttr) -> Result<Self> {
let mut result = Self::from_attrs(attrs)?;
result.rename_all = result.rename_all.or(enum_attr.rename_all_fields);
Ok(result)
}

pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = Self::default();
parse_attrs(attrs)?.for_each(|a| result.merge(a));
result.rename_all = result.rename_all.or(enum_attr.rename_all_fields);
#[cfg(feature = "serde-compat")]
if !result.skip {
crate::utils::parse_serde_attrs::<SerdeVariantAttr>(attrs)
Expand Down
9 changes: 8 additions & 1 deletion macros/src/types/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ fn format_variant(
variant: &Variant,
) -> syn::Result<()> {
let crate_rename = enum_attr.crate_rename();
let variant_attr = VariantAttr::new(&variant.attrs, enum_attr)?;

// 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)?,
};

if variant_attr.skip {
return Ok(());
Expand Down
6 changes: 5 additions & 1 deletion ts-rs/tests/enum_struct_rename_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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""#
)
}
Loading