Skip to content

fix(transformer/legacy-decorator): keep decorated static fields with setPublicClassFields - #29979

Merged
Dunqing merged 1 commit into
mainfrom
fix/transformer-legacy-decorator-static-set-public-class-fields
Nov 24, 2026
Merged

Dunqing merged 1 commit into
mainfrom
fix/transformer-legacy-decorator-static-set-public-class-fields

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

With legacy decorators (experimentalDecorators) and setPublicClassFields: true (TypeScript's useDefineForClassFields: false), a decorator on a static field was dropped.

Fixes #29977

On class entry, the TypeScript transform moves field values out of the class. An instance field keeps its declaration and decorators, so legacy decorators still lower it on class exit, but a static field was replaced wholesale by static { this.y = 1; }, so the decorated field was gone before legacy decorators ran.

Example

Input (playground for this example; its useDefineForClassFields: false also turns on removeClassFieldsWithoutInitializer, so it matches that variant below):

class A {
  @dec static y = 1;
  @dec z = 3;
}

Options: { "assumptions": { "setPublicClassFields": true }, "plugins": ["transform-legacy-decorator", "transform-typescript"] }

Command: cargo run -p oxc_transformer --example transformer -- input.ts --babel-options <dir with options.json>

Before output:

class A {
  constructor() {
    this.z = 3;
  }
  static {
    this.y = 1;
  }
  z;
}
babelHelpers.decorate([dec], A.prototype, "z", void 0);

After output:

class A {
  constructor() {
    this.z = 3;
  }
  static y;
  static {
    this.y = 1;
  }
  z;
}
babelHelpers.decorate([dec], A.prototype, "z", void 0);
babelHelpers.decorate([dec], A, "y", void 0);

The y decoration is now emitted, after the instance member one, as tsc 5.9.3 does (--experimentalDecorators --useDefineForClassFields false --target es2022). With removeClassFieldsWithoutInitializer: true, the after output has no static y; or z; declarations and keeps both decorate calls. The leftover z; comes from that option being off by default, which this PR doesn't change.

Fix

A decorated static field keeps its declaration, without its value, just before the new static block, so legacy decorators lower it like an instance field. With removeClassFieldsWithoutInitializer on, TypeScript removes the declaration on class exit, after legacy decorators have taken its decorators; when it stays, it comes before the static block and can't overwrite the value. Decorated fields also keep their computed keys in the class body, so they are no longer decorated with null (babelHelpers.decorate([dec], A.prototype, null, void 0)).

Known difference from tsc: with removeClassFieldsWithoutInitializer on, a decorated field whose computed key needs a temp var keeps an empty declaration (e.g. [_c2 = _c = c()];), which hides an accessor the decorator installs on the prototype. Removing it needs legacy decorators and TypeScript to share the key temp var; there is a TODO in transform_class_fields.

New exec tests check that keys are evaluated once in source order, static initializers run before decorators, decorators run in tsc's order, accessor descriptors replace decorated static fields, and class decorators run last. They pass on tsc 5.9.3 output, and fail without this fix because the static decorator calls are missing. Transformer, conformance, exec, coverage, allocation and napi tests pass with no snapshot changes beyond the new fixtures.

AI disclosure: 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 21, 2026
Base automatically changed from fix/transformer-preserved-class-assign-semantics to main November 21, 2026 15:12
@Dunqing
Dunqing force-pushed the fix/transformer-legacy-decorator-static-set-public-class-fields branch from d1eda1b to d2c7904 Compare November 21, 2026 15:15
@codspeed

codspeed Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing fix/transformer-legacy-decorator-static-set-public-class-fields (89e6b53) with main (4675362)

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 fix/transformer-legacy-decorator-static-set-public-class-fields branch 3 times, most recently from b201d95 to 8c90063 Compare November 21, 2026 16:04
@Dunqing
Dunqing force-pushed the fix/transformer-legacy-decorator-static-set-public-class-fields branch 2 times, most recently from 87314df to f6b76cf Compare November 24, 2026 07:21
@Dunqing
Dunqing force-pushed the fix/transformer-legacy-decorator-static-set-public-class-fields branch from f6b76cf to 89e6b53 Compare November 24, 2026 08:14
@Dunqing
Dunqing marked this pull request as ready for review November 24, 2026 08:22
@Dunqing
Dunqing merged commit f2f83f8 into main Nov 24, 2026
39 checks passed
@Dunqing
Dunqing deleted the fix/transformer-legacy-decorator-static-set-public-class-fields branch November 24, 2026 08:23
Dunqing added a commit to rolldown/rolldown that referenced this pull request Nov 24, 2026
Pins the restacked decorators-lowering head, now on oxc main 1abb96db.

Snapshot changes:
- Legacy decorator fixtures pick up main's fixes: computed and assigned
  keys are passed to __decorate instead of null, and decorated static
  fields keep their decorators (oxc-project/oxc#29979, #30148, #30184).
- Generated calls whose callee has no source position now map to the
  call itself, so wrapped require(), init_*() and (0, x.f)() calls point
  at their source call rather than a neighbouring token.
- _decoratorStart and _decorateElement now map to the class and to each
  decorated element, so a throwing decorator's frame lands on it.
Dunqing added a commit to rolldown/rolldown that referenced this pull request Nov 24, 2026
Pins the restacked decorators-lowering head, now on oxc main 1abb96db.

Snapshot changes:
- Legacy decorator fixtures pick up main's fixes: computed and assigned
  keys are passed to __decorate instead of null, and decorated static
  fields keep their decorators (oxc-project/oxc#29979, oxc-project/oxc#30148,
  oxc-project/oxc#30184).
- Generated calls whose callee has no source position now map to the
  call itself, so wrapped require(), init_*() and (0, x.f)() calls point
  at their source call rather than a neighbouring token.
- _decoratorStart and _decorateElement now map to the class and to each
  decorated element, so a throwing decorator's frame lands on it.
princejain756 pushed a commit to princejain756/oxc that referenced this pull request Nov 26, 2026
…d `declare` fields (oxc-project#30264)

With legacy decorators (`experimentalDecorators`), a decorated `declare`
field whose computed key has side effects, such as `@dec declare
[key()]: any`, got `undefined` as its decorator key, and `key()` never
ran: legacy decorators put `_key = key()` in the field's key, but
TypeScript removes `declare` fields with their keys. Define mode was
already broken; oxc-project#29979 broke `useDefineForClassFields: false`, which
used to call `key()` (with a `null` key). An identifier key, `@dec
declare [k]: any`, also got an unassigned temp var, and an imported `k`
was removed as type-only.

### 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%22useDefineForClassFields%22%3Afalse%2C%22experimentalDecorators%22%3Atrue%7D%7D&code=const+log%3A+string%5B%5D+%3D+%5B%5D%3B%0Aconst+key+%3D+%28%29+%3D%3E+%28log.push%28%22key%22%29%2C+%22k%22%29%3B%0Aconst+dec+%3D+%28_%3A+object%2C+name%3A+string%29+%3D%3E+log.push%28%60dec%3A%24%7Bname%7D%60%29%3B%0A%0Aclass+Foo+%7B%0A++%40dec+declare+%5Bkey%28%29%5D%3A+any%3B%0A%7D%0Aconsole.log%28log%29%3B%0A)):

```ts
const log: string[] = [];
const key = () => (log.push("key"), "k");
const dec = (_: object, name: string) => log.push(`dec:${name}`);

class Foo {
  @dec declare [key()]: any;
}
console.log(log);
```

Options: `{ "assumptions": { "setPublicClassFields": true }, "plugins":
["transform-legacy-decorator", ["transform-typescript", {
"removeClassFieldsWithoutInitializer": true }]] }`

Command: `cargo run -p oxc_transformer --example transformer -- input.ts
--babel-options <dir with options.json>`

Before output:

```js
var _key;
const log = [];
const key = () => (log.push("key"), "k");
const dec = (_, name) => log.push(`dec:${name}`);
class Foo {}
babelHelpers.decorate([dec], Foo.prototype, _key, void 0);
console.log(log);
```

After output:

```js
let _key;
const log = [];
const key = () => (log.push("key"), "k");
const dec = (_, name) => log.push(`dec:${name}`);
class Foo {
  static {
    _key = key();
  }
}
babelHelpers.decorate([dec], Foo.prototype, _key, void 0);
console.log(log);
```

Run with a minimal `babelHelpers.decorate`, the before output logs `[
'dec:undefined' ]` and the after output logs `[ 'key', 'dec:k' ]`. tsc
passes `key()` to `__decorate` directly; esbuild evaluates it in order
with the other class keys, which this follows.

### Fix

The TypeScript transform now replaces such a key with a temp var whose
assignment joins the class's other computed keys, in source order: it is
prepended to the next key the class evaluates, or put in a static block
when none follows. This covers define and assign semantics and
class-properties lowering. It also marks the key's references, which
semantic resolves as types in a `declare` field, as reads. Legacy
decorators then pass identifier and literal keys of `declare` fields to
`_decorate` as is. Other decorator modes are unchanged: TypeScript still
erases decorated `declare` fields together with their decorators, as tsc
does (it reports TS1206 for them).

New fixtures cover instance and static fields in all three field modes,
with an exec test of the evaluation order, and that keys stay
unevaluated without legacy decorators; the legacy ones fail before this
change. Transformer tests, conformance with `--exec`, clippy, and
transformer/semantic coverage pass. The one coverage change,
`decoratorInAmbientContext.ts`, now passes `b` instead of an unassigned
`_b`, as tsc's baseline does.

AI disclosure: written with Claude Code (Claude Opus 5.5; reasoning
effort not recorded).
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.

transformer: legacy decorators on static fields are dropped with setPublicClassFields

1 participant