Skip to content

feat(transformer/decorators)!: handle class decorators that replace the class (3/4) - #29751

Open
Dunqing wants to merge 1 commit into
standard-decorators-membersfrom
standard-decorators-evaluation
Open

Dunqing wants to merge 1 commit into
standard-decorators-membersfrom
standard-decorators-evaluation

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Stack 3/4 for standard (TC39 2023-11) decorators: class decorators that replace the class, and decorator this receivers. #29750 already calls class decorators and stores the replacement; this PR makes everything that refers to the class see the right value afterwards:

  • Reads of the class name inside the body resolve to the replacement. Writes to the immutable inner binding (K = x, K &&= x, destructuring, for-of) throw a TypeError, keeping evaluation order.
  • In @rep class A extends mk(() => A) {}, () => A returns the replacement, while a direct read is still in the TDZ.
  • export default @dec class Foo {} becomes export { Foo as default }, a live binding.
  • @a.b.c is called with this === a.b.
  • super in static elements resolves through the original class's parent (draft behaviour).

Example

Input:

@register class A {
  static self = A;
  @bound.call method() {}
}

Output with decorator: { lower: true } at es2022 (abridged; babelHelpers.* stands for the @oxc-project/runtime helpers):

_method_dec = [[_bound = bound, _bound.call]];   // [receiver, fn]
let _A = class A { method() {} };
_A2 = _A;
_init = babelHelpers.decoratorStart(null);
babelHelpers.decorateElement(_init, 1, "method", _method_dec, _A);
_A2 = _A = babelHelpers.decorateElement(_init, 0, "A", _A_decorators, _A); // class decorator may replace
babelHelpers.defineProperty(_A, "self", _A2);    // `A` inside the body is the replacement
babelHelpers.runInitializers(_init, 1, _A);
let A = _A;

Both _A and _A2 are updated when the class decorator returns a replacement, so code moved out of the body (static self = A reads _A2) and the outer binding (let A = _A) see it. Static fields are installed on the replacement afterwards. No playground link: the released playground doesn't lower standard decorators.

Class replacement semantics

The proposal is at Stage 2.7 since May 2026 (meeting notes), and Test262 has no semantic tests for it. Oxc follows the draft text in tc39/ecma262#2417 (2024-02-02); Babel and esbuild differ from it for static elements. Here F is the original class and newF the replacement:

field this super method extra initializer this newF.#m()
Draft, Oxc newF F's parent newF throws
Babel 2023-11 newF newF's parent F works
esbuild F or newF varies by case newF works

Only the draft's super works when a decorator wraps the class as class extends C; with Babel, static x = super.who() gets the class's own who(). esbuild's field this is F when only the class is decorated, because it then leaves static fields in the class body. Static private methods are an open question in the draft, which installs them on F but static private fields on newF; Oxc keeps the draft. The full write-up is in the module doc of decorator/standard/mod.rs.

Breaking change

TypeScript parameter decorators on class methods or constructors (m(@d x)) now fail with "Decorators are not valid here." (TS1206) unless legacy decorators are enabled; before, the output kept m(@d x), which is not valid JavaScript. Projects that use them (NestJS, TypeORM, Angular DI) should set decorator: { legacy: true }, matching experimentalDecorators. The parser already reported JavaScript files and other functions' parameters.

The benchmarks and cargo allocs now transform kitchen-sink.tsx, which has parameter decorators, with legacy decorators as its header specifies, so its allocation counts change with the workload (arena 6132 → 6917, system 185 → 238), not from a slowdown.

Transformer tests, clippy and transform conformance with execution pass. Compared with #29750: Babel output 766/1208 unchanged, Oxc output 378 → 383/503, Oxc execution 421 → 453/455, and Babel execution 496 → 495/602: three receiver and outer-binding fixtures now pass, and four replacement-static-{block,property}-super fixtures fail by design, because they resolve super through the replacement class. The other 13 class-replacement failures assert Babel's semantics from the table above, and four more stop on syntax or options Oxc doesn't support: Babel's private destructuring ({ #x: x }) and the ignoreFunctionLength assumption, which object-rest-spread doesn't implement. misc--to-es2015-outer-class-binding-mutated still fails: that copy asserts Babel's missing class TDZ, and the native copy passes.

Depends on #29750. Part of #9170.

AI disclosure: implemented and described with AI assistance; this revision was written with Claude Code (Claude Opus 5.5; reasoning effort not recorded).

@github-actions github-actions Bot added the A-transformer Area - JS/TS transforms and downleveling, oxc_transformer label Nov 17, 2026
@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing standard-decorators-evaluation (e79783e) with standard-decorators-members (aeeaf7e)

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 force-pushed the standard-decorators-evaluation branch from d259f57 to b3c7173 Compare November 17, 2026 10:57
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from b3c7173 to f399480 Compare November 19, 2026 03:28
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch 2 times, most recently from 944a6f1 to 078fca4 Compare November 19, 2026 16:00
@Dunqing Dunqing changed the title feat(transformer): preserve decorator class bindings and receivers feat(transformer): handle class replacement and decorator receivers in standard decorators Nov 19, 2026
@Dunqing Dunqing changed the title feat(transformer): handle class replacement and decorator receivers in standard decorators feat(transformer/decorators): handle class decorators that replace the class (3/4) Nov 19, 2026
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from 078fca4 to aa06560 Compare November 19, 2026 17:48
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch 3 times, most recently from 97d7416 to e79783e Compare November 23, 2026 06:40
@Dunqing
Dunqing added this pull request to stack #30053 November 23, 2026 06:40
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from e79783e to 5327690 Compare November 24, 2026 01:03
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from 9de8de8 to 99029f4 Compare November 25, 2026 07:01
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch 2 times, most recently from aaf7da0 to ee5fd45 Compare November 25, 2026 07:19
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch 2 times, most recently from e860b41 to c7d8269 Compare November 25, 2026 10:58
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch 2 times, most recently from 4e6e100 to 1b94c85 Compare November 25, 2026 12:52
Dunqing added a commit to rolldown/rolldown that referenced this pull request Nov 25, 2026
Patches every oxc crate to oxc-project/oxc@9e2856b2, the head of the
stack that adds standard decorator and auto-accessor lowering
(oxc-project/oxc#29749, oxc-project/oxc#29750, oxc-project/oxc#29751,
oxc-project/oxc#28882). Drop the patch once an oxc release includes it.

The stack sits on a newer oxc main, whose changes update some
snapshots:
- Legacy decorators: computed and assigned keys are passed to
  __decorate instead of null, and decorated static fields keep their
  decorators.
- Source maps: calls whose callee is generated, such as a wrapped
  require(), init_*() or (0, x.f)(), map to their own source call
  instead of a neighbouring token (oxc-project/oxc#30204).
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from 1b94c85 to 5a88f06 Compare November 25, 2026 14:55
@Dunqing Dunqing changed the title feat(transformer/decorators): handle class decorators that replace the class (3/4) feat(transformer/decorators)!: handle class decorators that replace the class (3/4) Nov 25, 2026
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from 61f4ec6 to 594814c Compare November 25, 2026 17:23
@Dunqing
Dunqing removed this pull request from stack #30053 November 25, 2026 17:38
@Dunqing
Dunqing added this pull request to stack #30281 November 25, 2026 17:38
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from 09a9811 to bc69557 Compare November 25, 2026 17:54
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from bc69557 to a75b3ae Compare November 26, 2026 02:03
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from a75b3ae to e2cfe20 Compare November 26, 2026 02:05
@Dunqing
Dunqing force-pushed the standard-decorators-evaluation branch from e2cfe20 to 5a6051e Compare November 26, 2026 02:41
@Dunqing
Dunqing marked this pull request as ready for review November 26, 2026 02:46
…he class

References to a class inside its body, in its `extends` clause and
through `export default` now see the class decorator's replacement.
Writes to the immutable inner binding throw, and `super` in static
elements resolves through the original class's parent, following the
draft spec. A decorator such as `@a.b` is called with `this === a`.

BREAKING CHANGE: TypeScript files with parameter decorators on class
methods or constructors now fail with "Decorators are not valid here."
(TS1206) unless legacy decorators are enabled.

This branch has not been deployed

No deployments
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.

1 participant