Skip to content

feat(transformer/decorators): lower @dec on class members and classes (2/4) - #29750

Open
Dunqing wants to merge 1 commit into
standard-auto-accessorsfrom
standard-decorators-members
Open

Dunqing wants to merge 1 commit into
standard-auto-accessorsfrom
standard-decorators-members

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Stack 2/4 for standard (TC39 2023-11) decorators, and the main implementation. This PR lowers @dec on class members and on the class itself, with decorator context objects, addInitializer, Symbol.metadata, and the spec's order for decorators, keys, initializers and extra initializers. A decorated class is lowered whole, as in esbuild, because decorators can observe and replace its elements.

Class replacement, decorator this receivers and parameter decorators come in #29751; lowering is opt-in with decorator: { lower: true } until #28882.

Example

Input:

class A {
  @log method() {}
  @tracked count = 0;
}

Output with decorator: { lower: true } at es2022, in external helper mode (runtime mode imports the same helpers from @oxc-project/runtime, added in #29447):

var _method_dec, _count_dec;
let _init;
_method_dec = [[void 0, log]];
_count_dec = [[void 0, tracked]];
class A {
  constructor() {
    babelHelpers.runInitializers(_init, 5, this);    // instance method extra initializers
    babelHelpers.defineProperty(this, "count", babelHelpers.runInitializers(_init, 8, this, 0));
    babelHelpers.runInitializers(_init, 11, this);   // field extra initializers
  }
  method() {}
}
_init = babelHelpers.decoratorStart(null);
babelHelpers.decorateElement(_init, 1, "method", _method_dec, A);
babelHelpers.decorateElement(_init, 5, "count", _count_dec, A);
babelHelpers.decoratorMetadata(_init, A);

Decorator expressions are evaluated into _x_dec arrays before the class; after it, the runtime helpers apply the decorators in spec order. No playground link: the released playground doesn't lower standard decorators.

Points to review

Start at es2022/class_properties/decorators.rs. The runtime protocol (context array slots and flags) is documented in decorator/standard/mod.rs and npm/runtime/scripts/helpers/. Cases that are easy to get wrong, each covered by fixtures:

  • Named evaluation through TypeScript wrappers: { x: @dec class {} as any } gives name === "x", as do satisfies, ! and <T>.
  • Field keys of a decorated class are converted once, at class definition, in element order.
  • A decorated class in a brace-less loop or a parameter initializer gets fresh decorator state on each evaluation.
  • Each decorateElement call maps to its decorated element in source maps, so a stack trace from a throwing decorator points at that decorator (codegen support from fix(codegen): map a call with a generated callee at the call's span #30204).
  • A computed key that could equal a decorated method's name gives that side's public methods temporary keys, so each decorator gets its own method even if a later member overwrites it. A well-known symbol such as [Symbol.iterator] can't collide with a string name and keeps every key; the temporary keys keep their original key's source position.

The External helper mode's docs (Rust and napi, including Helpers.mode) now warn that lowered decorators call helpers Babel doesn't define, such as decoratorStart and the legacy decorateParam, so the Runtime mode is needed when decorators are lowered.

Transformer tests, clippy and transform conformance with execution pass. Compared with #29749: Babel output 759 → 766/1208, Babel execution 350 → 496/602, Oxc output 375 → 378/498, Oxc execution 90 → 421/429. Of the 19 remaining Babel decorator execution failures, 13 test class replacement and 4 test decorator this or the outer class binding, both handled in #29751, which explains the ones that still fail there. The two duplicated-keys--to-es2015 fixtures expect Babel's 'Decorating two elements with the same name' error, which the spec doesn't require.

Depends on #29749. 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-members (aeeaf7e) with standard-auto-accessors (3cb1bfe)2

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. ↩

  2. No successful run was found on standard-auto-accessors (40433a8) during the generation of this report, so dbff9fd was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@Dunqing
Dunqing force-pushed the standard-decorators-members branch from e066485 to 5bf3975 Compare November 17, 2026 10:57
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 5bf3975 to 0a14f00 Compare November 19, 2026 03:28
@github-actions github-actions Bot added the A-napi Area - Node.js bindings and WASM builds of the napi/ packages, oxc_napi label Nov 19, 2026
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 0a14f00 to d4d72b4 Compare November 19, 2026 11:52
@Dunqing Dunqing changed the title feat(transformer): lower standard member and class decorators feat(transformer): lower standard element and class decorators Nov 19, 2026
@Dunqing Dunqing changed the title feat(transformer): lower standard element and class decorators feat(transformer/decorators): lower @dec on class members and classes (2/4) Nov 19, 2026
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from d4d72b4 to 7f475dc Compare November 19, 2026 17:48
graphite-app Bot pushed a commit that referenced this pull request Nov 19, 2026
Standard decorator lowering needs helpers to apply decorators, share metadata, preserve class names, and run initializers. These helpers are not yet available in `@oxc-project/runtime` or the Test262 runtime harness.

Add `decorateElement`, `decoratorStart`, `decoratorKey`, `runInitializers`, `decoratorMetadata`, and `setClassName` as Oxc-owned helpers. Shared source generates their CommonJS, ESM, and coverage runtime copies. Babel runtime sync preserves these helpers and rejects upstream name collisions before writing.

`decoratorStart` can capture per-element public descriptors stored under temporary symbol keys on the original class. `decorateElement` accepts these explicit descriptors and can replay undecorated overrides without consuming initializer slots. This preserves each original method value, getter/setter pairing, method names, and home objects when public names collide. Existing callers keep the original protocol.

Metadata is installed as an own data property with Object.defineProperty, including when a class decorator returns a Proxy. This invokes the Proxy defineProperty trap and cannot be swallowed by an unrelated set trap.

### Example

Input:

```ts
const key = Symbol.metadata || Symbol.for("Symbol.metadata");
const events = [];
const replacement = new Proxy(class {}, {
 set() { events.push("set"); return true; },
 defineProperty(target, key, descriptor) { events.push("defineProperty"); return Reflect.defineProperty(target, key, descriptor); }
});
@(() => replacement)
class C {}
console.log(JSON.stringify({events, own: Object.hasOwn(C, key)}));
```

Before output:

```text
{"events":["set"],"own":false}
```

After output:

```text
{"events":["defineProperty"],"own":true}
```

Metadata must be an own data property even when the replacement Proxy swallows assignment.

These runtime results were captured from the original published decorator stack (`563f5dd5ff627c206b8910b00e7cf19bcde739f6`) and the corrected stack, using `proposal-decorators` version `2023-11`, with no target override. They show the correction folded into this layer; the full standard-decorator examples require the implementation layers above runtime helpers and the intermediate feature prerequisites. The playground does not expose the explicit standard-decorator lowering option used by these intermediate layers, so it cannot represent this configuration. Exact Babel options:

```json
{"plugins": [["proposal-decorators", {"version": "2023-11"}]]}
```

To reproduce the local transform, save the input as `example.ts`, save the matching Babel options as `example-options/options.json`, and run:

```bash
cargo run -p oxc_transformer --example transformer -- example.ts --babel-options ./example-options
```

Execute the emitted `Transformed` section in Node with `babelHelpers` resolving to this checkout’s `npm/runtime/src/helpers`.

Part of #9170.

## Review follow-up

Two commits added:

- `fix(runtime): keep source order of captured public class elements`. `decoratorStart` removed every captured public element and immediately redefined only the auto-accessors, so they moved ahead of other elements. For `class A { m() {} accessor x; @d [k]() {} }`, `Object.getOwnPropertyNames(A.prototype)` gave `constructor,x,m,n` instead of `constructor,m,x,n`. The first element of each name is now defined in source order, and decorated definitions replace it in place. As a result, a captured element is visible on the prototype while its decorators run, as non-captured decorated methods already were. The runtime test was updated for that. The order is also covered by the transformer exec fixture `oxc/public-element-order[--chrome94]` in #29750.
- `fix(runtime): ship esbuild's license notice with the standard decorator helpers`. The helpers are adapted from esbuild's runtime (MIT), but the package only shipped Babel's `LICENSE`, which `scripts/sync.mjs` overwrites on every sync. esbuild's notice is now shipped as `LICENSE-esbuild` and listed in `files` (checked with `npm pack --dry-run`), and a test keeps it there.

Checks at this layer: all 47 runtime/sync tests, `generate-decorator-helpers.mjs --check`, transformer tests, full transform conformance with execution. Conformance reports: Babel output 754/1190, Babel execution 342/592, Oxc output 337/462, Oxc execution 68/364.

Implemented with AI assistance.

Prepared with AI assistance (model ID: `mythos-magpie-4133180673`). The review follow-up was implemented with Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code; its reasoning effort level was not available to the agent.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_018ogydaCgdkgWgzV7R21DrC
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 6766f5f to f7f43e6 Compare November 20, 2026 00:09
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 090a767 to aeeaf7e 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-members branch from aeeaf7e to a8be1ca Compare November 24, 2026 01:03
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from e204e47 to 160007b Compare November 24, 2026 19:27
@Dunqing
Dunqing force-pushed the standard-decorators-members branch 2 times, most recently from 6d7f06f to 918fe9b Compare November 25, 2026 03:26
jwh3times pushed a commit to jwh3times/oxc that referenced this pull request Nov 25, 2026
…xc-project#30204)

A stack trace reports a call at its callee's position. When a transform
emits a call whose callee it generated, such as a runtime helper
reference, the callee has no span, so the call gets no source mapping of
its own. A source-mapped stack trace through that call then points at
whatever was mapped earlier, often the end of an enclosing block. This
PR maps such a call at the call's own span, when the call has one.

No transform on main gives a helper call a span yet. The standard
decorator lowering in oxc-project#29750 is the first: it maps each
`decorateElement`, `decoratorStart` and `decoratorMetadata` call to its
decorators, so a throwing decorator's stack trace points at that
decorator.

### Example

Input: `foo();\nbar(1);`, parsed and then given a generated callee by
clearing the span of `bar`, as the new
`call_with_generated_callee_maps_to_call_span` test does. There is no
playground link: the playground can't produce a callee without a span,
because that needs a transform that only exists in the decorator stack,
or an AST edit.

Before output (`origin/main`), the code and the source map tokens of
line 2:

```text
foo();
bar(1);
generated 2:4 -> source 2:4
generated 2:5 -> source 2:5
```

After output:

```text
foo();
bar(1);
generated 2:0 -> source 2:0
generated 2:4 -> source 2:4
generated 2:5 -> source 2:5
```

The call on line 2 gains a mapping at its start, where a stack trace
through it reports its position. Before, the first mapping on that line
was the argument list.

[Source map viewer: main (A) versus this PR
(B)](https://source-map-viewer.void.app/compare?a=eJxNjjELwjAQhf-KnEuF0CJuKQ5HpoougpPnEO0pLfQSktal9L9Lbalu97jvPb4eXiwcbMulcSWDhqdzySYnuduQbMeDJMvWq-i68OCT9b6S1-V83C-9tI5pYz0JKFioQ3QCGnqCN4dYOSHQO0UgtuFIoK83RTP9jQSV-K5N60jw9zJOWpZ2ImaznxrJBDeT1ThEgIioCsQiL9CgMoiGYIDhA7YdTcU&b=eJxNjj0LwjAQhv-KnEuF0CJuKQ5HphZdBCfPIdooLfQSktSl9L9LP6hu7_E-9_L08DZsvI6mUrYyIOFlbbLLiR_aJ_sxEGfZdhNs55_mrJ2r-X29nI7rX9qEtNWOGASsVBksg4Se4GN8qC0TyIMgYN2aQCBvd0ELPZ0ENbsupk0g-KuU5Wg4zsRi9lMjnuF2thqHCBARRYFY5IiqnJJQiIpggOELPRRPBg&tab=table&ignore-source-name=0)

Calls whose callee comes from the source are unchanged, and so are
generated calls without a span: `add_source_mapping` skips empty spans.
The new test fails without the change. `oxc_codegen`, `oxc_transformer`,
`oxc_transformer_plugins`, `oxc_minifier` and `oxc` tests pass.

AI disclosure: written with Claude Code (Claude Opus 5.5; reasoning
effort not recorded).
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 918fe9b to 9b7c80c Compare November 25, 2026 04:01
@Dunqing
Dunqing force-pushed the standard-decorators-members branch 2 times, most recently from ba39387 to 5366b0f Compare November 25, 2026 07:01
@Dunqing
Dunqing marked this pull request as ready for review November 25, 2026 08:56
@Dunqing
Dunqing force-pushed the standard-decorators-members branch 2 times, most recently from 5c41a52 to 67a72d3 Compare November 25, 2026 10:58
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-members branch from 4e49fa7 to e6d179f 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-members branch 2 times, most recently from 15b7ae9 to 059e426 Compare November 26, 2026 02:03
@Dunqing
Dunqing force-pushed the standard-decorators-members branch 2 times, most recently from df47aac to 7e5b774 Compare November 26, 2026 02:41
@Dunqing
Dunqing force-pushed the standard-decorators-members branch 3 times, most recently from eee650c to 271a8a1 Compare November 26, 2026 05:08
Lower TC39 2023-11 decorators on methods, getters, setters, fields,
auto-accessors and classes, with decorator context objects,
`addInitializer`, `Symbol.metadata` and the spec's evaluation order. A
decorated class is lowered as a whole, and runtime helpers from
`@oxc-project/runtime` apply the decorators after the class.

Document on the External helper mode that lowered decorators call
helpers Babel's `babelHelpers` does not provide.
@Dunqing
Dunqing force-pushed the standard-decorators-members branch from 271a8a1 to 8665662 Compare November 26, 2026 09:02

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-codegen Area - JS/TS code printing and source maps, oxc_codegen A-napi Area - Node.js bindings and WASM builds of the napi/ packages, oxc_napi 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