Repository navigation
Conversation
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.
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
Dunqing
marked this pull request as ready for review
November 23, 2026 08:24
Dunqing
merged commit Nov 23, 2026
355dfb9
into
fix/legacy-decorator-stale-class-references
39 checks passed
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aTraverseCtxshortcut. It rebinds the reference and keeps itsReferenceIdand flags. It reads the current binding from the reference itself, so callers don't pass the old symbol. It's built on the existingdelete_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
decorator/legacy/mod.rs(ClassReferenceChanger)rebind_identifier_referencees2022/class_properties/static_block_and_prop_init.rsrebind_identifier_referencees2022/class_static_block.rs(ClassBindingRebinder)rebind_referencecommon/arrow_function_converter.rsarguments→_argumentsbindingrebind_referenceThese were all the
set_symbol_idcalls in transformer code. Thedelete_resolved_referencecalls inclass_properties/private_field.rsremove 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_conformanceand-- --execcargo coverage semanticcargo coverage transformerAlso passing:
just lintandjust testforoxc_traverseandoxc_transformer,just doc -p oxc_traverse, andjust 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