Skip to content

Fix ts enum negative and block merge - #18734

Merged
nicolo-ribaudo merged 2 commits into
babel:mainfrom
joellemd:fix-ts-enum-negative-and-block-merge
Nov 26, 2026
Merged

nicolo-ribaudo merged 2 commits into
babel:mainfrom
joellemd:fix-ts-enum-negative-and-block-merge

Conversation

@joellemd

@joellemd joellemd commented Nov 25, 2026 •

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues?
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

Follow-up for #18719. It fixes the two regressions found during review, plus the older bugs underneath them.

Negative auto-incremented values. If a member without an initializer comes after a negative value, we build it with t.numericLiteral, and that throws for negative numbers:

enum A { D = -4, B }            // threw on main
enum G { D = -4, B = G.D, C }   // started throwing in #18719, since G.D now folds

I switched it to t.valueToNode, so both give -3.

function parse() {
  {
    enum Flags { None = 0, Read = 1 << 0 }
    enum Flags { Write = 1 << 1, ReadWrite = Read | Write }
    return Flags;
  }
}

Block-scoping turns the first enum's let Flags into a function-level var _Flags before the second enum is transformed. When the second enum then asks the block for a binding, there isn't one, so it redeclares the variable.

Now we also accept a binding that was declared in the same block and then moved out. The second enum assigns to _Flags and reuses the first enum's members. An outer enum with the same name still doesn't count as a merge.

I added a fixture for each case, both with exec tests. The block-scoping one needs @babel/plugin-transform-block-scoping as a test-only dependency, so I added it to devDependencies.

@joellemd
joellemd force-pushed the fix-ts-enum-negative-and-block-merge branch from 9975b22 to ac45262 Compare November 25, 2026 22:29
@babel-bot

babel-bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63868

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 3d90872

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: typescript 7.x: needs backport labels Nov 25, 2026
@JLHwung
JLHwung force-pushed the fix-ts-enum-negative-and-block-merge branch from 362b231 to 3d90872 Compare November 26, 2026 14:45
@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 26, 2026
Merged via the queue into babel:main with commit 3af5d0b Nov 26, 2026
41 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18751.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: typescript PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants