Skip to content

fix(transformer/typescript): inline const enum members through type-cast wrappers#22500

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/transformer-const-enum-type-cast
May 18, 2026
Merged

fix(transformer/typescript): inline const enum members through type-cast wrappers#22500
graphite-app[bot] merged 1 commit into
mainfrom
fix/transformer-const-enum-type-cast

Conversation

@Dunqing
Copy link
Copy Markdown
Member

@Dunqing Dunqing commented May 17, 2026

Closes #22444.

Phase.idle as Phase, Phase.idle satisfies Phase, Phase.idle!, and <Phase>Phase.idle failed to inline under optimizeConstEnums. The const enum declaration was stripped while the member access was left as Phase.idle, producing a runtime ReferenceError.

Cause

The enum inliner's enter_expression matched only bare StaticMemberExpression/ComputedMemberExpression. annotations.rs strips TS-only wrappers in its own enter_expression, but walk_expression only calls enter_expression once per node — after the strip the inner Phase.idle was never revisited.

Fix

Peek through the wrappers via Expression::get_inner_expression_mut() (it already handles TSAsExpression, TSSatisfiesExpression, TSNonNullExpression, TSTypeAssertion, TSInstantiationExpression, ParenthesizedExpression). The outer *expr = … replacement then drops the wrapper along with the inlined value.

Example

const enum Phase { idle, active }

class Foo {
  #foo = Phase.idle as Phase;
}

Before:

class Foo {
  #foo = Phase.idle;  // dangling reference
}

After:

class Foo {
  #foo = 0;
}

Verification

New fixtures cover all wrapper shapes for const + regular enums:

  • babel-plugin-transform-typescript/const-enum-type-cast/
  • babel-plugin-transform-typescript/optimize-enums/type-cast/

@github-actions github-actions Bot added the A-transformer Area - Transformer / Transpiler label May 17, 2026
Copy link
Copy Markdown
Member Author

Dunqing commented May 17, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Dunqing Dunqing changed the title fix(minifier): drop unused-var-init pure IIFEs and preserve annotation for downstream (#22349) fix(transformer): inline const enum members through type-cast wrappers May 17, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 17, 2026

Merging this PR will not alter performance

✅ 44 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing fix/transformer-const-enum-type-cast (62376dd) with main (c9f506e)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing Dunqing marked this pull request as ready for review May 18, 2026 01:16
@Dunqing Dunqing requested a review from overlookmotel as a code owner May 18, 2026 01:16
@Dunqing Dunqing force-pushed the fix/transformer-const-enum-type-cast branch from b7fe1e2 to 62376dd Compare May 18, 2026 01:20
@graphite-app graphite-app Bot added the 0-merge Merge with Graphite Merge Queue label May 18, 2026
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app Bot commented May 18, 2026

Merge activity

#22500)

Closes #22444.

`Phase.idle as Phase`, `Phase.idle satisfies Phase`, `Phase.idle!`, and `<Phase>Phase.idle` failed to inline under `optimizeConstEnums`. The const enum declaration was stripped while the member access was left as `Phase.idle`, producing a runtime `ReferenceError`.

## Cause

The enum inliner's `enter_expression` matched only bare `StaticMemberExpression`/`ComputedMemberExpression`. `annotations.rs` strips TS-only wrappers in its own `enter_expression`, but `walk_expression` only calls `enter_expression` once per node — after the strip the inner `Phase.idle` was never revisited.

## Fix

Peek through the wrappers via `Expression::get_inner_expression_mut()` (it already handles `TSAsExpression`, `TSSatisfiesExpression`, `TSNonNullExpression`, `TSTypeAssertion`, `TSInstantiationExpression`, `ParenthesizedExpression`). The outer `*expr = …` replacement then drops the wrapper along with the inlined value.

## Example

```ts
const enum Phase { idle, active }

class Foo {
  #foo = Phase.idle as Phase;
}
```

Before:
```js
class Foo {
  #foo = Phase.idle;  // dangling reference
}
```

After:
```js
class Foo {
  #foo = 0;
}
```

## Verification

New fixtures cover all wrapper shapes for const + regular enums:
- `babel-plugin-transform-typescript/const-enum-type-cast/`
- `babel-plugin-transform-typescript/optimize-enums/type-cast/`
@graphite-app graphite-app Bot force-pushed the fix/transformer-const-enum-type-cast branch from 62376dd to 931b7d6 Compare May 18, 2026 02:19
@Dunqing Dunqing changed the title fix(transformer): inline const enum members through type-cast wrappers fix(transformer/typescript): inline const enum members through type-cast wrappers May 18, 2026
@graphite-app graphite-app Bot merged commit 931b7d6 into main May 18, 2026
30 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 18, 2026
@graphite-app graphite-app Bot deleted the fix/transformer-const-enum-type-cast branch May 18, 2026 02:23
camc314 pushed a commit that referenced this pull request May 18, 2026
### 🐛 Bug Fixes

- 0f26de6 ecmascript: Resolve identifier value type via tracked
constants (#22234) (Alexander Lichter)
- c27a8cf minifier: Normalize `{ x: x }` shorthand so adjacent-if merge
is idempotent (#22401) (Dunqing)
- e431a0e parser: Break extends clause loop on fatal error (#22517)
(Boshen)
- e9ec7c6 minifier: Fold optional chains by base nullishness (#22236)
(Alexander Lichter)
- e6090e7 transformer: Keep enum IIFE when a non-inlinable value
reference remains (#22501) (Dunqing)
- 931b7d6 transformer: Inline const enum members through type-cast
wrappers (#22500) (Dunqing)
- b9615b2 codegen: Preserve string quotes in require() calls during
minification (#22475) (zennnnnnn11)
- c73c159 transformer/async-to-generator: Reparent parameter initializer
scopes (#22507) (camc314)
- ecfd3ca transformer/async-to-generator: Move only parameter bindings
(#22503) (camc314)
- 3ce3431 transformer/explicit-resource-managment: Preserve shadowed
for-head block (#22451) (camc314)

### ⚡ Performance

- ce92c6c semantic: `#[inline]` `Scoping::get_binding` (#22414)
(Dunqing)
- 98be95c regular_expression: Track regex flags via bitflags (#22427)
(Boshen)
- dbbc059 jsdoc: Skip should_attach_jsdoc when no remaining comments
(#22409) (Boshen)
- 217d7d8 minifier: Index `SymbolValues` by `SymbolId` (#22441)
(Dunqing)
- d782b78 minifier: Use BitSet for LiveUsageCollector live references
(#22425) (Boshen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformer: const enums with as types / satisfies types is not replaced with values

1 participant