Skip to content

fix(transpiler): fold const enum members that reference const variables - #27167

Closed
robobun wants to merge 1 commit into
mainfrom
claude/fix-const-enum-folding-19581
Closed

robobun wants to merge 1 commit into
mainfrom
claude/fix-const-enum-folding-19581

Conversation

@robobun

@robobun robobun commented Feb 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes const enum members not being constant-folded when they reference const variables with constant initializers (e.g., const x = 5)
  • Pre-populates const_values for simple const declarations before the enum preprocessing pass runs, in both the tree-shaking and non-tree-shaking code paths
  • Also checks should_fold_typescript_constant_expressions in handleIdentifier so that const_values are used during enum folding even without the global inlining feature flag

Before: const multiplier = 5; const enum E { A = 1 * multiplier } → kept multiplier as a runtime reference
After: correctly folds to const enum E { A = 5 }, matching TypeScript behavior

Closes #19581

Test plan

  • New regression tests in test/regression/issue/19581.test.ts that verify:
    • Enum members referencing const variables are folded in --no-bundle mode
    • Full folding works with --minify (e.g., console.log(25))
    • Simple const variable references in enum values are folded
  • All tests fail with USE_SYSTEM_BUN=1 (confirming the bug)
  • All tests pass with bun bd test
  • Existing test/bundler/esbuild/ts.test.ts tests pass (57/57)
  • Existing test/bundler/esbuild/default.test.ts tests pass (150/150)

🤖 Generated with Claude Code

When a const enum member expression referenced a `const` variable
with a constant initializer (e.g. `const x = 5`), the value was not
being folded because the enum preprocessing pass runs before const
variable declarations are visited. This pre-populates const_values
for simple const declarations before enum preprocessing, and also
checks should_fold_typescript_constant_expressions in handleIdentifier
so const_values are used during enum folding even without the global
inlining feature flag.

Closes #19581

Co-Authored-By: Claude <noreply@anthropic.com>
@robobun

robobun commented Feb 19, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:54 AM PT - Feb 19th, 2026

❌ Your commit 2d295315 has 2 failures in Build #37596 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 27167

That installs a local version of the PR into your bun-27167 executable, so you can run:

bun-27167 --bun

@claude

claude Bot commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

✅ 2d295 — Looks good!

Reviewed 4 files across src/ast/ and test/regression/issue/: enables const enum members to reference const variables with constant initializers by pre-populating the const_values map before enum preprocessing runs.

@Jarred-Sumner Jarred-Sumner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do not add an extra loop over all the statements. Read over esbuild's implementation and understand what they do.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Constant folding is not working inside const enum

2 participants