refactor(traverse): add spanned variants of BoundIdentifier::create_binding_*#22305
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
enum_eval ran in semantic (#22252)
Merging this PR will not alter performance
Comparing Footnotes
|
Merge activity
|
…inding_* (#22305) ## Summary Add `create_spanned_binding_identifier(span, ctx)` and `create_spanned_binding_pattern(span, ctx)` on `BoundIdentifier`, mirroring the existing `create_spanned_*` pattern used for references / expressions / assignment targets. The existing `create_binding_identifier` / `create_binding_pattern` now delegate to the spanned variants with `SPAN`. ## Why Needed by transforms that lower a class/function declaration to a `var` declarator and want the synthesized `BindingIdentifier` to keep the original `id.span`, so semantic re-analysis of the printed AST agrees with the transformer's symbol-table bookkeeping (`Symbol span mismatch` lines in `transform_conformance` snapshots). First user lands in a follow-up PR for the explicit-resource-management transform. ## Test plan - [x] `cargo build -p oxc_traverse` - [x] `cargo clippy -p oxc_traverse`
0104a40 to
7e75f08
Compare
…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`

Summary
Add
create_spanned_binding_identifier(span, ctx)andcreate_spanned_binding_pattern(span, ctx)onBoundIdentifier, mirroring the existingcreate_spanned_*pattern used for references / expressions / assignment targets.The existing
create_binding_identifier/create_binding_patternnow delegate to the spanned variants withSPAN.Why
Needed by transforms that lower a class/function declaration to a
vardeclarator and want the synthesizedBindingIdentifierto keep the originalid.span, so semantic re-analysis of the printed AST agrees with the transformer's symbol-table bookkeeping (Symbol span mismatchlines intransform_conformancesnapshots).First user lands in a follow-up PR for the explicit-resource-management transform.
Test plan
cargo build -p oxc_traversecargo clippy -p oxc_traverse