Repository navigation
fix(transformer/legacy-decorator): keep decorated static fields with setPublicClassFields - #29979
Merged
Dunqing merged 1 commit intoNov 24, 2026
Conversation
Base automatically changed from
fix/transformer-preserved-class-assign-semantics
to
main
November 21, 2026 15:12
Dunqing
force-pushed
the
fix/transformer-legacy-decorator-static-set-public-class-fields
branch
from
November 21, 2026 15:15
d1eda1b to
d2c7904
Compare
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
Dunqing
force-pushed
the
fix/transformer-legacy-decorator-static-set-public-class-fields
branch
3 times, most recently
from
November 21, 2026 16:04
b201d95 to
8c90063
Compare
Dunqing
force-pushed
the
fix/transformer-legacy-decorator-static-set-public-class-fields
branch
2 times, most recently
from
November 24, 2026 07:21
87314df to
f6b76cf
Compare
…`setPublicClassFields`
Dunqing
force-pushed
the
fix/transformer-legacy-decorator-static-set-public-class-fields
branch
from
November 24, 2026 08:14
f6b76cf to
89e6b53
Compare
Dunqing
marked this pull request as ready for review
November 24, 2026 08:22
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).
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.
With legacy decorators (
experimentalDecorators) andsetPublicClassFields: true(TypeScript'suseDefineForClassFields: 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: falsealso turns onremoveClassFieldsWithoutInitializer, so it matches that variant below):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:
After output:
The
ydecoration is now emitted, after the instance member one, as tsc 5.9.3 does (--experimentalDecorators --useDefineForClassFields false --target es2022). WithremoveClassFieldsWithoutInitializer: true, the after output has nostatic y;orz;declarations and keeps bothdecoratecalls. The leftoverz;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
removeClassFieldsWithoutInitializeron, 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 withnull(babelHelpers.decorate([dec], A.prototype, null, void 0)).Known difference from tsc: with
removeClassFieldsWithoutInitializeron, 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 aTODOintransform_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).