Skip to content

refactor(traverse): add rebind_reference helpers - #30057

Merged
Dunqing merged 1 commit into
fix/legacy-decorator-stale-class-referencesfrom
refactor/traverse-rebind-reference
Nov 23, 2026
Merged

Dunqing merged 1 commit into
fix/legacy-decorator-stale-class-referencesfrom
refactor/traverse-rebind-reference

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 23, 2026

Copy link
Copy Markdown
Member

Stacked on #30056.

Several transforms move an existing reference from one symbol to another and write the bookkeeping out by hand: set_symbol_id, remove the reference from the old symbol's resolved references (or from root unresolved references), then add it to the new symbol's. #30056 fixed a bug where one of those steps was missing. This PR adds helpers so callers can't skip a step.

New helpers

  • TraverseScoping::rebind_reference(reference_id, name, symbol_id), with a TraverseCtx shortcut. It rebinds the reference and keeps its ReferenceId and flags. It reads the current binding from the reference itself, so callers don't pass the old symbol. It's built on the existing delete_reference, which already handles both resolved and unresolved references.
  • TraverseCtx::rebind_identifier_reference(&mut IdentifierReference, &BoundIdentifier), which also renames the identifier to the binding's name.

Call sites switched

File Rebinds Now uses
decorator/legacy/mod.rs (ClassReferenceChanger) class symbol → class alias rebind_identifier_reference
es2022/class_properties/static_block_and_prop_init.rs class symbol → temp binding rebind_identifier_reference
es2022/class_static_block.rs (ClassBindingRebinder) outer class symbol → inner class symbol rebind_reference
common/arrow_function_converter.rs unresolved arguments → _arguments binding rebind_reference

These were all the set_symbol_id calls in transformer code. The delete_resolved_reference calls in class_properties/private_field.rs remove references entirely, so they don't use this helper.

What stays identical

Emitted code and scoping are unchanged. Running each of these on this branch left every snapshot file byte-identical to the parent branch:

  • cargo run -p oxc_transform_conformance and -- --exec
  • cargo coverage semantic
  • cargo coverage transformer

Also passing: just lint and just test for oxc_traverse and oxc_transformer, just doc -p oxc_traverse, and just fmt.

AI disclosure

This PR was written with AI assistance (Claude Code). The exact model and reasoning effort level aren't stated here. The contributor is responsible for reviewing the change before it leaves draft.


Generated by Claude Code

Add `TraverseScoping::rebind_reference` (with a `TraverseCtx` shortcut) to move an
existing reference to another symbol, keeping its `ReferenceId` and flags. It removes
the reference from its current symbol's resolved references, or from root unresolved
references, so callers can't forget a step.

Add `TraverseCtx::rebind_identifier_reference`, which also renames the
`IdentifierReference` to the new binding's name.

Use them in the legacy decorator, class-properties, class-static-block and
arrow-function-converter transforms, which each did these steps by hand.
@github-actions github-actions Bot added the A-transformer Area - JS/TS transforms and downleveling, oxc_transformer label Nov 23, 2026
@codspeed

codspeed Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing refactor/traverse-rebind-reference (2a9270c) with fix/legacy-decorator-stale-class-references (3dc4212)

Open in CodSpeed

Footnotes

  1. 29 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 November 23, 2026 08:24
@Dunqing
Dunqing merged commit 355dfb9 into fix/legacy-decorator-stale-class-references Nov 23, 2026
39 checks passed
@Dunqing
Dunqing deleted the refactor/traverse-rebind-reference branch November 23, 2026 08:28
Dunqing added a commit that referenced this pull request Nov 23, 2026
Stacked on #30056.

Several transforms move an existing reference from one symbol to another
and write the bookkeeping out by hand: `set_symbol_id`, remove the
reference from the old symbol's resolved references (or from root
unresolved references), then add it to the new symbol's. #30056 fixed a
bug where one of those steps was missing. This PR adds helpers so
callers can't skip a step.

### New helpers

- `TraverseScoping::rebind_reference(reference_id, name, symbol_id)`,
with a `TraverseCtx` shortcut. It rebinds the reference and keeps its
`ReferenceId` and flags. It reads the current binding from the reference
itself, so callers don't pass the old symbol. It's built on the existing
`delete_reference`, which already handles both resolved and unresolved
references.
- `TraverseCtx::rebind_identifier_reference(&mut IdentifierReference,
&BoundIdentifier)`, which also renames the identifier to the binding's
name.

### Call sites switched

| File | Rebinds | Now uses |
|---|---|---|
| `decorator/legacy/mod.rs` (`ClassReferenceChanger`) | class symbol →
class alias | `rebind_identifier_reference` |
| `es2022/class_properties/static_block_and_prop_init.rs` | class symbol
→ temp binding | `rebind_identifier_reference` |
| `es2022/class_static_block.rs` (`ClassBindingRebinder`) | outer class
symbol → inner class symbol | `rebind_reference` |
| `common/arrow_function_converter.rs` | unresolved `arguments` →
`_arguments` binding | `rebind_reference` |

These were all the `set_symbol_id` calls in transformer code. The
`delete_resolved_reference` calls in `class_properties/private_field.rs`
remove references entirely, so they don't use this helper.

### What stays identical

Emitted code and scoping are unchanged. Running each of these on this
branch left every snapshot file byte-identical to the parent branch:

- `cargo run -p oxc_transform_conformance` and `-- --exec`
- `cargo coverage semantic`
- `cargo coverage transformer`

Also passing: `just lint` and `just test` for `oxc_traverse` and
`oxc_transformer`, `just doc -p oxc_traverse`, and `just fmt`.

### AI disclosure

This PR was written with AI assistance (Claude Code). The exact model
and reasoning effort level aren't stated here. The contributor is
responsible for reviewing the change before it leaves draft.

---
_Generated by [Claude Code](https://claude.ai/code)_

Co-authored-by: Claude <noreply@anthropic.com>
Dunqing added a commit that referenced this pull request Nov 23, 2026
Stacked on #30056.

Several transforms move an existing reference from one symbol to another
and write the bookkeeping out by hand: `set_symbol_id`, remove the
reference from the old symbol's resolved references (or from root
unresolved references), then add it to the new symbol's. #30056 fixed a
bug where one of those steps was missing. This PR adds helpers so
callers can't skip a step.

### New helpers

- `TraverseScoping::rebind_reference(reference_id, name, symbol_id)`,
with a `TraverseCtx` shortcut. It rebinds the reference and keeps its
`ReferenceId` and flags. It reads the current binding from the reference
itself, so callers don't pass the old symbol. It's built on the existing
`delete_reference`, which already handles both resolved and unresolved
references.
- `TraverseCtx::rebind_identifier_reference(&mut IdentifierReference,
&BoundIdentifier)`, which also renames the identifier to the binding's
name.

### Call sites switched

| File | Rebinds | Now uses |
|---|---|---|
| `decorator/legacy/mod.rs` (`ClassReferenceChanger`) | class symbol →
class alias | `rebind_identifier_reference` |
| `es2022/class_properties/static_block_and_prop_init.rs` | class symbol
→ temp binding | `rebind_identifier_reference` |
| `es2022/class_static_block.rs` (`ClassBindingRebinder`) | outer class
symbol → inner class symbol | `rebind_reference` |
| `common/arrow_function_converter.rs` | unresolved `arguments` →
`_arguments` binding | `rebind_reference` |

These were all the `set_symbol_id` calls in transformer code. The
`delete_resolved_reference` calls in `class_properties/private_field.rs`
remove references entirely, so they don't use this helper.

### What stays identical

Emitted code and scoping are unchanged. Running each of these on this
branch left every snapshot file byte-identical to the parent branch:

- `cargo run -p oxc_transform_conformance` and `-- --exec`
- `cargo coverage semantic`
- `cargo coverage transformer`

Also passing: `just lint` and `just test` for `oxc_traverse` and
`oxc_transformer`, `just doc -p oxc_traverse`, and `just fmt`.

### AI disclosure

This PR was written with AI assistance (Claude Code). The exact model
and reasoning effort level aren't stated here. The contributor is
responsible for reviewing the change before it leaves draft.

---
_Generated by [Claude Code](https://claude.ai/code)_

Co-authored-by: Claude <noreply@anthropic.com>
Dunqing added a commit that referenced this pull request Nov 23, 2026
…s symbol (#30056)

When a class with a class decorator references itself inside its body,
the legacy decorator transform rewrites those references to a class
alias (`Foo` → `_Foo`). `ClassReferenceChanger` did this by minting a
fresh read reference on the alias, but never removed the original
reference from the class symbol's resolved references. The transformed
scoping therefore lists one stale reference per rewritten use, which
semantic checks report as `Symbol reference IDs mismatch`.

This PR retargets the existing reference instead, keeping its
`ReferenceId` and flags: it removes the reference from the class
symbol's resolved references and adds it to the alias symbol's.

It also fixes the scope of the injected `static { _Foo = this; }` block.
It was created with `create_child_scope_of_current`, which made it a
child of the program scope without `StrictMode`. It's now created as a
`StrictMode | ClassStaticBlock` child of the class scope.

Emitted code is unchanged; only the scoping data is corrected.

### `rebind_reference` helpers (from #30057)

This bug was one step missing from a three-step update that several
transforms write out by hand. This PR also adds helpers so callers can't
skip a step:

- `TraverseScoping::rebind_reference(reference_id, name, symbol_id)`,
with a `TraverseCtx` shortcut. It rebinds an existing reference to
`symbol_id` and keeps its `ReferenceId` and flags. It reads the current
binding from the reference itself, and removes the reference from that
symbol's resolved references, or from root unresolved references.
- `TraverseCtx::rebind_identifier_reference(&mut IdentifierReference,
&BoundIdentifier)`, which also renames the identifier to the binding's
name.

All four transformer sites that did this by hand now use them: the
legacy decorator fix above,
`class_properties/static_block_and_prop_init.rs`,
`class_static_block.rs` (`ClassBindingRebinder`), and
`arrow_function_converter.rs` (unresolved `arguments`). This part is a
pure refactor: every snapshot was byte-identical with and without it,
including `cargo coverage transformer`.

### Example

Input
(`tasks/transform_conformance/tests/legacy-decorators/test/fixtures/oxc/static-field/input.ts`,
plugin `transform-legacy-decorator`):

```ts
function Bar(): ClassDecorator {
  return (_target) => {
    console.log(Bar.name)
  }
}

@bar()
class Foo {
  static foo = `${Foo.name}`;
}
```

Output (same before and after):

```js
var _Foo;
function Bar() {
  return (_target) => {
    console.log(Bar.name);
  };
}
let Foo = class Foo {
  static {
    _Foo = this;
  }
  static foo = `${_Foo.name}`;
};
Foo = _Foo = babelHelpers.decorate([Bar()], Foo);
```

Before (semantic check from `cargo run -p oxc_transform_conformance`,
parent commit):

```text
* oxc/static-field/input.ts
Scope flags mismatch:
after transform: ScopeId(4): ScopeFlags(ClassStaticBlock)
rebuilt        : ScopeId(4): ScopeFlags(StrictMode | ClassStaticBlock)
Scope parent mismatch:
after transform: ScopeId(4): Some(ScopeId(0))
rebuilt        : ScopeId(4): Some(ScopeId(3))
Symbol reference IDs mismatch for "Foo":
after transform: SymbolId(2): [ReferenceId(4), ReferenceId(6), ReferenceId(8)]
rebuilt        : SymbolId(3): [ReferenceId(4), ReferenceId(8)]
```

After: the fixture passes. The output contains two references to the
outer `Foo` (`Foo = _Foo = decorate(..., Foo)`); the third reference
listed before was the `Foo` in `${Foo.name}`, which had already been
rewritten to `_Foo`.

No playground link: the playground shows emitted code, which this change
doesn't alter, and doesn't run the transformed-vs-rebuilt scoping
comparison.

### Snapshot changes

- `oxc.snap.md`: 357 → 360 of 481 (legacy-decorators 29 → 32 of 114).
`oxc/static-field`, `oxc/static-field-with-class-properties` and
`oxc/static-private-brand-check-with-class-properties` (added in #30054)
now pass.
- `semantic_typescript.snap`: 4165 → 4168.
`decoratedBlockScopedClass1.ts`, `decoratedBlockScopedClass2.ts` and
`decoratedBlockScopedClass3.ts` now pass. Stale-reference entries for
the self-referencing class are also gone from
`legacyDecoratorsEnumAccessSameNameAsClass.ts`,
`staticInitializersAndLegacyClassDecorators.ts`,
`decoratorOnClassConstructorParameter5.ts`,
`decoratedClassExportsCommonJS1.ts`, `decoratorOnClass9.ts`,
`decoratorOnClass{5,6,7}.es6.ts`,
`esDecorators-classDeclaration-{commentPreservation,setFunctionName,setFunctionNameComputedField,sourceMap}.ts`,
and `es6modulekindWithES5Target11.ts` /
`esnextmodulekindWithES5Target11.ts`. Those files still fail for
unrelated reasons.
- `oxc_exec.snap.md`, `semantic_babel.snap`, `semantic_misc.snap`,
`semantic_test262.snap`: unchanged.

### Verification

- `cargo run -p oxc_transform_conformance` and `-- --exec`
- `cargo coverage semantic` and `cargo coverage transformer`
- `just lint` and `just test` for `oxc_traverse` and `oxc_transformer`
- `just doc -p oxc_traverse`
- `just fmt`

### AI disclosure

This PR was written with AI assistance (Claude Code). The exact model
and reasoning effort level aren't stated here. The contributor is
responsible for reviewing the change.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - JS/TS transforms and downleveling, oxc_transformer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants