Skip to content

feat(transformer/decorators): lower accessor fields without decorators - #29749

Open
Dunqing wants to merge 1 commit into
mainfrom
standard-auto-accessors
Open

Dunqing wants to merge 1 commit into
mainfrom
standard-auto-accessors

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Stack 1/4 for standard (TC39 2023-11) decorators. This PR lowers the accessor keyword in classes without decorators; classes with decorators pass through unchanged until #29750. Lowering is opt-in with decorator: { lower: true } (Rust, napi, or Babel's proposal-decorators plugin with version "2023-11") until #28882 makes it the default at concrete targets.

Example

Input:

class A {
  accessor x = 1;
  static accessor #y = 2;
}

Output with decorator: { lower: true } at es2022:

class A {
  #_x_accessor_storage = 1;
  get x() { return this.#_x_accessor_storage; }
  set x(value) { this.#_x_accessor_storage = value; }
  static #_y_accessor_storage = 2;
  static get #y() { return A.#_y_accessor_storage; }
  static set #y(value) { A.#_y_accessor_storage = value; }
}

Each auto-accessor becomes a private storage field and a getter/setter pair, built with the helper shared with legacy decorators (#29449). At targets that also lower class fields, class-properties lowers this result further. No playground link: the released playground doesn't lower auto-accessors.

Points to review

  • Static storage is read through the defining class (A.#_y_accessor_storage), as in Babel, so a subclass that inherits the accessor passes the private brand check. The spec text reads through this instead; transformer/decorators: follow-ups from the standard-decorators stack review #30265 tracks the difference.
  • A computed key is converted once with toPropertyKey and shared by the getter and setter; converting twice would define them on different keys when the key's toString has side effects.
  • Brace-less loop bodies are wrapped in a block, so each loop iteration gets its own accessor state.
  • Babel options with a proposal-decorators version other than "2023-11" are rejected.
  • An anonymous class assigned to an auto-accessor keeps its inferred name at es2022, but loses it when class fields are also lowered, as plain fields do on main (tracked in transformer/decorators: follow-ups from the standard-decorators stack review #30265).

Transformer tests, clippy, napi tests and transform conformance with execution pass. Compared with main: Babel output 753 → 759/1208, Babel execution 345 → 350/602, Oxc output 374 → 375/495, Oxc execution 88 → 90/386. #28882 has the performance measurements for the whole stack.

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

Warning

oxc-project has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing standard-auto-accessors (c9b2669) with main (ab59476)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 main (1622c5f) during the generation of this report, so ab59476 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-auto-accessors branch from f37dd00 to 1af8649 Compare November 17, 2026 10:57
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch from 1af8649 to 806cdb0 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-auto-accessors branch from 806cdb0 to 6ee2b83 Compare November 19, 2026 11:52
@Dunqing Dunqing added 0-merge Merge with Graphite Merge Queue and removed 0-merge Merge with Graphite Merge Queue labels Nov 19, 2026 — with Claude
@Dunqing Dunqing changed the title feat(transformer): lower standard auto-accessors feat(transformer): lower undecorated standard auto-accessors Nov 19, 2026
@Dunqing Dunqing changed the title feat(transformer): lower undecorated standard auto-accessors feat(transformer/decorators): lower accessor fields without decorators (1/4) Nov 19, 2026
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch 2 times, most recently from 2c70bad to 3cb1bfe Compare November 20, 2026 00:09
@cursor
cursor Bot changed the base branch from transformer-shared-auto-accessor to graphite-base/29749 November 20, 2026 08:29
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch from 3cb1bfe to 40433a8 Compare November 23, 2026 06:40
@Dunqing
Dunqing changed the base branch from graphite-base/29749 to transformer-shared-auto-accessor November 23, 2026 06:40
@Dunqing
Dunqing added this pull request to stack #30053 November 23, 2026 06:40
Base automatically changed from transformer-shared-auto-accessor to main November 23, 2026 10:25
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch 2 times, most recently from af4ee93 to bdc58ec Compare November 24, 2026 01:25
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch 2 times, most recently from db16329 to 965fd9d Compare November 25, 2026 04:01
@Dunqing
Dunqing marked this pull request as ready for review November 25, 2026 08:56
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch from 022c4d8 to e95c4e5 Compare November 25, 2026 10:35
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 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-auto-accessors branch 2 times, most recently from a2c5e7f to 72ce19e Compare November 26, 2026 02:41
Lower auto-accessors (`accessor x = 1`) in classes without decorators to
a private storage field plus a getter and setter, when standard
decorator lowering is enabled (`decorator: { lower: true }`). Classes
with decorators pass through unchanged.
philbooth pushed a commit to philbooth/oxc-space-before-function-paren that referenced this pull request Nov 26, 2026
…zers to their source (oxc-project#30326)

The storage field, getter and setter that lowering generates for an
`accessor` field have no source positions. Stepping into the getter or
setter in a debugger shows no original location, and the
`_defineProperty(...)` / `_classPrivateFieldInitSpec(...)` calls that
class-properties emits for field initializers map only their arguments.

### Example

Input ([playground for this
example](https://playground.oxc.rs/?t=codegen&options=%7B%22run%22%3A%7B%22transform%22%3Atrue%7D%2C%22parser%22%3A%7B%22extension%22%3A%22ts%22%7D%2C%22transformer%22%3A%7B%22target%22%3A%22es2021%22%2C%22experimentalDecorators%22%3Atrue%7D%7D&code=class+C+%7B%0A++%40dec+accessor+x+%3D+1%3B%0A%7D%0Aclass+D+%7B%0A++y+%3D+1%3B%0A++static+z+%3D+2%3B%0A%7D%0A)):

```ts
class C {
  @dec accessor x = 1;
}
class D {
  y = 1;
  static z = 2;
}
```

Options: `target: "es2021"`, legacy decorators, source map on. Each line
lists its decoded mappings as `generated`→`line:column original`.

Before output:

```js
_classPrivateFieldInitSpec(this, _x_accessor_storage, 1);  // `1`→2:20 `1`
get x() {                                                  // `x`→2:16 `x`
  return _classPrivateFieldGet(_x_accessor_storage, this); // (none)
set x(value) {                                             // (none)
  _classPrivateFieldSet(_x_accessor_storage, this, value); // (none)
_defineProperty(this, "y", 1);                             // `"`→5:2 `y`, `1`→5:6 `1`
_defineProperty(D, "z", 2);                                // `"`→6:9 `z`, `2`→6:13 `2`
```

After output:

```js
_classPrivateFieldInitSpec(this, _x_accessor_storage, 1);  // `_classPrivateFieldInitSpec`→2:2 `@dec`, `1`→2:20 `1`, `)`→2:21 `;`
get x() {                                                  // `get`→2:2 `@dec`, `x`→2:16 `x`, `{`→2:2 `@dec`
  return _classPrivateFieldGet(_x_accessor_storage, this); // `return`→2:16 `x`
set x(value) {                                             // `set`→2:2 `@dec`, `x`→2:16 `x`, `{`→2:2 `@dec`
  _classPrivateFieldSet(_x_accessor_storage, this, value); // `_classPrivateFieldSet`→2:16 `x`
_defineProperty(this, "y", 1);                             // `_defineProperty`→5:2 `y`, `1`→5:6 `1`, `)`→5:7 `;`
_defineProperty(D, "z", 2);                                // `_defineProperty`→6:2 `static`, `"`→6:9 `z`, `2`→6:13 `2`, `)`→6:14 `;`
```

[Before and after in the source map
viewer](https://source-map-viewer.void.app/compare?a=eJylkm9P4kAQxr_KZO8NmpUG7x0NCUvhPPUwRmLMxTHNZhmhXtnt7W4RNHx3s2DlT5Dcxb5o0t1nfs8z03llI9JkpadhYobEmmwqLaQql85d22wqPf3IKB-e68wPClLQAkt_y8xSDVnbzNRJYc0TKR_ZUvtsQtGY8oKsiz5FIDuKUe-3OSP_BYcz8qcb9CEpEzr7d2JVcSji4EsRB7sRHzNN19YUZP38f4Ju1m0AZ6lUipwzNnXeWDkK3UfH0E7T69ubXprCcQSanuGO5J--LGqhcpkSEnhFjV4Z7bwtlTe2drQ6Qv_5QtT8OHN8nzGHRoCjX4TXiDzM1kBLvrR6_wbU9sKCzwbPLXlTmZd0IOXgEI3DqryCLlB_7EztfkjqgUNSL6zxxs8L4oBshoyDLvN8PbbuobFt_6f3WSGbB0xjx3hL2Q2ylyA7DbIo-gbOlFZRXxZFpke3N79aVVcnE5np-pOrT2SBmnH2obxwRrMme0U2Jesyo5E1v3Nkj1lOyJrIdhHIODItJ-SQNe8fOL6zlp9red07ZBu3idGetF-J1tuEGqA9JAVVHcygBY0YQ8ub8wvCeXUF4Lz0mYIXaMFpJV75TVbdu2X4ePkIIQTvC9HnPSF6cfzcFcnvOL4U4qISJONOfCe6Z7EQvZ9b2o5IrvilEOdB1ruKJx3RHSwPkC3Y4g1Okc8I&b=eJylklFP2zAQx7_KyXspyDSie2tUiWuaMca6VVSIB4yiyLmWQGpnttO1oH73yS2hoSvVJvJgKb7__f5353tmU1JkUkdZpDNiXTZPDSSySK0dmXyeOvqSU5FdqNyNS5LQA0O_qtxQS7AzvZAnpdEPJF1gKuXyGQX3VJRkbPAuQrCjUKj9NufkPuBwTq7ToGckte_s34l1xqESxx8qcbxb4iRXNDK6JOOW_1NoM68BXCSplGStNol12qRT331wDGdJMrq-ipMEjgNQ9BtuKH0cpmXLZ66rhAiehRJOamWdqaTTpnW0uRLu_YVoufvc8n3GHE49XLiVP6bkYLEFGnKVUfs3oLUX5n0aPLvmzdOiogNVjg_ROGzSa-hKqNedad1mJO84RO3SaKfdsiQOgi0E46CqotiObXBobG_f6WVWgi095nTH-I1y4GVPXtbxsiD4BFZXRtIwLctcTa-vvvfqrk4m-aL9YNuztBSKcfYq_Ga1Yl32LNicjM21Eqz7mQs2yQsSrCvYDkEwLphKZ2QF697ecfGCWv9u1W1nBWtEI60cKbcRbXdJKICzjCTUebCAHpyGwjfcnJ4XLusQgHWpyyU8QQ86tXjjN9v0bte1h-sPEZEPEYc8RozDMMboB7cDxMc-jxCjMIwQVZ9fIEp_ZGGMKP3lZTMyfhtBjEw_vMHBeYgYf_3LoNNHvHjhI8ajEHEQ81kf8Se_rEOCrdjqD82N4CU&tab=table&ignore-source-name=0)

The storage, getter, setter and their bodies now take the accessor's
span, which starts at its first decorator like other class elements. The
statements in the bodies map to the accessor's key instead: a source map
drops a mapping whose original position repeats the previous one, so
giving them the accessor's span would leave those lines unmapped. On
main only legacy decorators use this builder; the standard-decorator
stack (oxc-project#29749) reuses it and gets the same mappings once rebased. The
field initializer calls map to their fields, as the assignments in loose
mode already did.

`var _x = new WeakMap()` declarations for lowered private fields remain
unmapped; that is unchanged here.

Transformer tests, transform conformance (`--exec`, no snapshot changes)
and the `oxc-transform` napi tests pass. The new `source_map`
integration tests fail without the change.

AI disclosure: written with Claude Code (Claude Opus 5.5; reasoning
effort not recorded).
@Dunqing
Dunqing force-pushed the standard-auto-accessors branch from 72ce19e to c9b2669 Compare November 26, 2026 09:02
@camc314 camc314 changed the title feat(transformer/decorators): lower accessor fields without decorators (1/4) feat(transformer/decorators): lower accessor fields without decorators Nov 26, 2026

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

2 participants