Skip to content

fix(transformer/explicit-resource-management): preserve class names#22306

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/transformer-explicit-resource-management-preserve-class-name
May 11, 2026
Merged

fix(transformer/explicit-resource-management): preserve class names#22306
graphite-app[bot] merged 1 commit into
mainfrom
fix/transformer-explicit-resource-management-preserve-class-name

Conversation

@Dunqing

@Dunqing Dunqing commented May 11, 2026

Copy link
Copy Markdown
Member

Stacked on #22305.

Why

class C { static getSelf() { return C } } lowered by the explicit resource management transform was emitting var C = class {} (anonymous), so when the outer C is reassigned (C = 123), getSelf() returns the reassigned value instead of the original class — the exact behaviour the named class expression's inner binding is supposed to protect against. Babel emits var C = class C {} for the same input.

Fix

  1. Emit var C = class C {} for both class C {} and export default class C {}. Adds a preserve_class_expression_name helper that mirrors the swap in decorator::legacy::transform_class_declaration_with_class_decorators — generate a fresh Class-flagged symbol in the class scope for the inner class id, swap class_decl.id.symbol_id (via Cell::replace) to the new symbol, and repurpose the original symbol as the outer var (flags → FunctionScopedVariable). Returns the outer BoundIdentifier and the original span so the caller can emit the outer declarator with the correct span.

  2. Carry the original span through both sides of the swap. The inner symbol is created with the original id.span (via Scoping::create_symbol(span, ...) + add_binding); the outer var's BindingIdentifier is emitted with the same span (via BoundIdentifier::create_spanned_binding_pattern, added in refactor(traverse): add spanned variants of BoundIdentifier::create_binding_* #22305). This eliminates the Symbol span mismatch diffs in:

    • transform-top-level/hoisting/input.mjs
    • transform-top-level/hoisting-default-class/input.mjs
    • export-class-name/input.js

Comparison with Babel

Babel reuses the original id node for the outer var and clones it for the inner class. This PR does the inverse — reuses the original symbol id for the outer var and generates a new symbol for the inner class. Given oxc's symbol-aware model, the inversion avoids rewiring every outer-scope reference (const K = C, C = 123, …): they already point at the original symbol.

Remaining follow-up

export-class-name/input.js still has Symbol reference IDs mismatch for C — references inside the class body (return C) still resolve to the outer symbol in the transformer's bookkeeping. A fresh semantic rebuild over the printed AST correctly resolves them to the inner class symbol, so tools that re-run semantic (rolldown / oxc_minifier on a fresh build) are fine. Tools that trust the transformer's symbol table directly (in-place minification / mangler) will see the stale binding. Rewiring those references is a separate follow-up (Reference.symbol_id update + delete_resolved_reference / add_resolved_reference).

Conformance impact

  • Babel suite: 692 → 694 passing (+2)
  • transform-top-level/hoisting/input.mjs
  • transform-top-level/hoisting-default-class/input.mjs
  • export-class-name/input.js: 4 span mismatches eliminated; reference-rewiring follow-up remains.

Test plan

  • cargo run -p oxc_transform_conformance
  • cargo run -p oxc_transform_conformance -- --exec
  • cargo test -p oxc_transformer

@github-actions github-actions Bot added the A-transformer Area - Transformer / Transpiler label May 11, 2026

Dunqing commented May 11, 2026

Copy link
Copy Markdown
Member Author

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.

@codspeed-hq

codspeed-hq Bot commented May 11, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 44 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing fix/transformer-explicit-resource-management-preserve-class-name (3abc257) with chore/traverse-spanned-binding-pattern (0104a40)

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 11, 2026 08:23
@Dunqing Dunqing requested a review from overlookmotel as a code owner May 11, 2026 08:23
@Dunqing Dunqing requested a review from camc314 May 11, 2026 08:23

@camc314 camc314 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice!

@camc314 camc314 added the 0-merge Merge with Graphite Merge Queue label May 11, 2026

camc314 commented May 11, 2026

Copy link
Copy Markdown
Contributor

Merge activity

@graphite-app graphite-app Bot changed the base branch from chore/traverse-spanned-binding-pattern to graphite-base/22306 May 11, 2026 08:28
@graphite-app graphite-app Bot changed the base branch from graphite-base/22306 to main May 11, 2026 08:33
…22306)

Stacked on #22305.

## Why

`class C { static getSelf() { return C } }` lowered by the explicit resource management transform was emitting `var C = class {}` (anonymous), so when the outer `C` is reassigned (`C = 123`), `getSelf()` returns the reassigned value instead of the original class — the exact behaviour the named class expression's inner binding is supposed to protect against. Babel emits `var C = class C {}` for the same input.

## Fix

1. **Emit `var C = class C {}`** for both `class C {}` and `export default class C {}`. Adds a `preserve_class_expression_name` helper that mirrors the swap in `decorator::legacy::transform_class_declaration_with_class_decorators` — generate a fresh `Class`-flagged symbol in the class scope for the inner class id, swap `class_decl.id.symbol_id` (via `Cell::replace`) to the new symbol, and repurpose the original symbol as the outer `var` (flags → `FunctionScopedVariable`). Returns the outer `BoundIdentifier` and the original span so the caller can emit the outer declarator with the correct span.

2. **Carry the original span through both sides of the swap.** The inner symbol is created with the original `id.span` (via `Scoping::create_symbol(span, ...)` + `add_binding`); the outer var's `BindingIdentifier` is emitted with the same span (via `BoundIdentifier::create_spanned_binding_pattern`, added in #22305). This eliminates the `Symbol span mismatch` diffs in:
   - `transform-top-level/hoisting/input.mjs`
   - `transform-top-level/hoisting-default-class/input.mjs`
   - `export-class-name/input.js`

## Comparison with Babel

Babel reuses the *original `id` node* for the outer `var` and clones it for the inner class. This PR does the inverse — reuses the original *symbol id* for the outer `var` and generates a new symbol for the inner class. Given oxc's symbol-aware model, the inversion avoids rewiring every outer-scope reference (`const K = C`, `C = 123`, …): they already point at the original symbol.

## Remaining follow-up

`export-class-name/input.js` still has `Symbol reference IDs mismatch` for `C` — references inside the class body (`return C`) still resolve to the outer symbol in the transformer's bookkeeping. A fresh semantic rebuild over the printed AST correctly resolves them to the inner class symbol, so tools that re-run semantic (rolldown / oxc_minifier on a fresh build) are fine. Tools that trust the transformer's symbol table directly (in-place minification / mangler) will see the stale binding. Rewiring those references is a separate follow-up (`Reference.symbol_id` update + `delete_resolved_reference` / `add_resolved_reference`).

## Conformance impact

- Babel suite: **692 → 694 passing** (+2)
- `transform-top-level/hoisting/input.mjs` ✓
- `transform-top-level/hoisting-default-class/input.mjs` ✓
- `export-class-name/input.js`: 4 span mismatches eliminated; reference-rewiring follow-up remains.

## Test plan

- [x] `cargo run -p oxc_transform_conformance`
- [x] `cargo run -p oxc_transform_conformance -- --exec`
- [x] `cargo test -p oxc_transformer`
@graphite-app graphite-app Bot force-pushed the fix/transformer-explicit-resource-management-preserve-class-name branch from 3abc257 to 5da9fda Compare May 11, 2026 08:34
@graphite-app graphite-app Bot merged commit 5da9fda into main May 11, 2026
28 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 11, 2026
@graphite-app graphite-app Bot deleted the fix/transformer-explicit-resource-management-preserve-class-name branch May 11, 2026 08:38
camc314 added a commit that referenced this pull request May 11, 2026
### 🚀 Features

- 66c9b01 transformer/typescript: Debug_assert that `enum_eval` ran in
semantic (#22252) (Dunqing)
- ffe6475 minifier: Fold `Array` constructor with safe spreads (#22215)
(camc314)

### 🐛 Bug Fixes

- d3d0b18 traverse: Handle `ChainElement::TSNonNullExpression` in
`GatherNodeParts` (#22247) (leaysgur)
- 4e880de transformer/object-rest-spread: Declare temp vars for computed
keys (#22284) (camc314)
- a7c3e22 semantic: Clear member write target for computed keys (#22302)
(camc314)
- 6a8852d codegen: Emit newline after legal-comment orphan flush
(#22304) (Dunqing)
- 5da9fda transformer/explicit-resource-management: Preserve class names
(#22306) (Dunqing)
- b5d970f transformer/explicit-resource-management: Preserve class names
(#22290) (camc314)
- bc54fd4 minifier: Keep function / class names if direct eval is
present in the scope (#22241) (sapphi-red)
- 7a810c0 minifier: Refresh direct eval flags after DCE (#21787)
(Dunqing)
- dd88726 transformer/legacy-decorator: Preserve accessor type
annotation for emitDecoratorMetadata (#21966) (Dunqing)
- 29a3cd7 codegen: Swap mapping/indent order for top-level decls
(#22206) (Dunqing)
- 73b4f40 minifier: Preserve catch binding with direct eval (#22221)
(camc314)
- 0e13d17 minifier: Preserve optional chain base side effects (#22219)
(camc314)
- 0c7c01c transformer/typescript: Inline optional-chain enum member
access (#21834) (Dunqing)
- a6aff7e codegen: Emit block/array/object end mapping at close char
(#22200) (Dunqing)
- a099b03 codegen: Emit call end mapping at `)` position, not past it
(#22199) (Dunqing)
- 5753774 minifier: Cap if-return ternary collapse for firefox (#21841)
(Gurupungav Narayanan)
- 2493bdd codegen: Correct sourcemap end mappings for closing delimiters
(#22001) (Mark Dalgleish)
- 3b385e2 minifier: Bail optimizing `Array` with unknown arg count
(#22188) (camc314)
- 9fa2122 parser: Parse array computed class keys (#22159) (camc314)

### 📚 Documentation

- a4a6892 napi/parser: Correct code comment (#22278) (overlookmotel)
- 9305373 oxc: Update README (#22178) (camc314)

Co-authored-by: Cameron <[email protected]>
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.

2 participants