Skip to content

refactor(traverse): add spanned variants of BoundIdentifier::create_binding_*#22305

Merged
graphite-app[bot] merged 1 commit into
mainfrom
chore/traverse-spanned-binding-pattern
May 11, 2026
Merged

refactor(traverse): add spanned variants of BoundIdentifier::create_binding_*#22305
graphite-app[bot] merged 1 commit into
mainfrom
chore/traverse-spanned-binding-pattern

Conversation

@Dunqing

@Dunqing Dunqing commented May 11, 2026

Copy link
Copy Markdown
Member

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

  • cargo build -p oxc_traverse
  • cargo clippy -p oxc_traverse

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.

@Dunqing Dunqing changed the title feat(transformer/typescript): debug_assert that enum_eval ran in semantic (#22252) refactor(traverse): add spanned variants of BoundIdentifier::create_binding_* May 11, 2026
@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 chore/traverse-spanned-binding-pattern (0104a40) with main (b5d970f)2

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.

  2. No successful run was found on main (6b50f23) during the generation of this report, so b5d970f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@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 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

…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`
@graphite-app graphite-app Bot force-pushed the chore/traverse-spanned-binding-pattern branch from 0104a40 to 7e75f08 Compare May 11, 2026 08:28
@graphite-app graphite-app Bot merged commit 7e75f08 into main May 11, 2026
29 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 chore/traverse-spanned-binding-pattern branch May 11, 2026 08:32
graphite-app Bot pushed a commit that referenced this pull request May 11, 2026
…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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants