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

Conversation

escritorio-gustavo
Copy link
Contributor

Goal

Using #[ts(rename_all_fields = "...")] on an enum containing tuple or unit variants would cause the error "rename_all is not applicable to newtype/tuple/unit structs"

Changes

Only add rename_all from rename_all_fields if variant.fields is Fields::Named(_)

Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@escritorio-gustavo escritorio-gustavo added the bug Something isn't working label Mar 28, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Mar 28, 2024

To summarize my understanding:
The problem is that when creating VariantAttrs in VariantAttr::new, we use the rename_all_fields of the enum as value for rename_all.
These VariantAttrs are then converted into StructAttrs, and when they reach newtype, we get the error.

There are a couple of thinks that stood out to me.
This basically duplicates VariantAttr::new, though we could fix that by re-writing VariantAttr::new to use VariantAttr::from_attrs internally.
Also, the match, deciding between VariantAttr::new and VariantAttr::from_attrs really doesn't feel great.

An alternative way of getting rid of the error would be to remove the check from newtype. It probably wouldn't be a big deal to just ignore it if it's there, to be honest.

If we wanted to keep the error, we could alternatively move these sanity checks, which we have in a lot of places, into the attribute parsing logic. But again, we'd then need to know what we're parsing (a unit struct?). So that'd probably be a larger refactor, and I'm not sure if it'd end up being nice.

So yeah, I'm not super satisfied with this solution, but I kinda don't see a better way of doing it right now.
If we're going with this, I think we should definetely add a comment to the match, and re-write VariantAttr::new to use VariantAttr::from_attrs to not have that logic duplicated.

@escritorio-gustavo
Copy link
Contributor Author

This basically duplicates VariantAttr::new, though we could fix that by re-writing VariantAttr::new to use VariantAttr::from_attrs internally.

Agreed! I'll push a commit to do that

Copy link
Collaborator

@NyxCode NyxCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, nice!
I'd be happy with merging this as-is. Maybe we'll find some nice way to clean all the "... is not compatible with ..." errors up at some point in the future.

@escritorio-gustavo escritorio-gustavo merged commit cf450ac into main Apr 1, 2024
14 checks passed
@escritorio-gustavo escritorio-gustavo deleted the fix_rename_all_fields branch April 1, 2024 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants