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 bug in enum flattening #282

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Conversation

escritorio-gustavo
Copy link
Contributor

@escritorio-gustavo escritorio-gustavo commented Mar 22, 2024

Goal

When a struct containing a flattened enum was itself flattened, the type generated would be missing the enum
That is, the following code

#[derive(TS)]
struct FooExternally {
    qux: i32,
    #[ts(flatten)]
    baz: BarExternally,
    biz: Option<String>,
}

#[derive(TS)]
enum BarExternally {
    Baz { a: i32, a2: String },
    Biz { b: bool },
    Buz { c: String, d: Option<i32> },
}

#[derive(TS)]
#[ts(export, export_to = "enum_flattening/externally_tagged/")]
struct NestedExternally {
    #[ts(flatten)]
    a: FooExternally,
    u: u32,
}

Would generate the following TS:

export type NestedExternally = { u: number, qux: number, biz: string | null, };

When it should generate

export type NestedExternally = { u: number, qux: number, biz: string | null, } & (
    | { "Baz": { a: number, a2: string, } }
    | { "Biz": { b: boolean, } }
    | { "Buz": { c: string, d: number | null, } }
);

Changes

Updated the generation of inline_flattened in macros/src/types/named.rs to be similar to inline, except for not removing parenthesis around lonely flattened fields, as that caused issues when flattening a struct that contained only a flattened enum. (The replace(" } & { ", " ") would merge the fields with the first enum variant)

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.

Comment on lines +10 to +14
struct FooExternally {
#[cfg_attr(feature = "serde-compat", serde(flatten))]
#[cfg_attr(not(feature = "serde-compat"), ts(flatten))]
baz: BarExternally,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test file checks what happens when the flattened enum is the only field in the flattened struct

@NyxCode NyxCode mentioned this pull request Mar 22, 2024
3 tasks
@NyxCode
Copy link
Collaborator

NyxCode commented Mar 22, 2024

Nice, thanks!
I never realized that inline_flattened would just drop flattened fields. Amazing that the test suite didnt catch that.

@escritorio-gustavo
Copy link
Contributor Author

Nice, thanks! I never realized that inline_flattened would just drop flattened fields. Amazing that the test suite didnt catch that.

Me neither! I only realized because I was looking at some old PRs and when I got to #206 I went "wtf was I thinking" lol

@escritorio-gustavo escritorio-gustavo merged commit 6f7311d into main Mar 22, 2024
14 checks passed
@escritorio-gustavo escritorio-gustavo deleted the fix-nested-flattened-enum branch March 22, 2024 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants