Skip to content

fix(transformer/legacy-decorator): evaluate computed keys of decorated declare fields - #30264

Merged
Dunqing merged 1 commit into
mainfrom
fix/legacy-decorator-declare-computed-key
Nov 26, 2026
Merged

Dunqing merged 1 commit into
mainfrom
fix/legacy-decorator-declare-computed-key

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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; #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):

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:

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:

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

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

codspeed Bot commented Nov 25, 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 fix/legacy-decorator-declare-computed-key (9ccd228) with main (697a09b)

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/legacy-decorator-declare-computed-key branch 4 times, most recently from 114c81b to b525e65 Compare November 26, 2026 01:04
…d `declare` fields

A legacy-decorated `declare` field whose computed key has side effects,
such as `@dec declare [key()]: any`, got an unassigned temp var as its
decorator key, and `key()` was never called: TypeScript removes the field
along with the `_key = key()` assignment that legacy decorators put in
its key. An identifier key, `@dec declare [k]: any`, also got an
unassigned temp var, and its import was removed as type-only.

TypeScript now replaces such a key with a temp var, assigned in order with
the other class keys (in the next computed key the class evaluates, or a
static block), and marks the key's references as reads. Legacy decorators
pass identifier and literal keys of `declare` fields to `_decorate` as is.

This only applies with legacy decorators. Without them, TypeScript keeps
erasing decorated `declare` fields with their decorators and keys, as tsc
does (it reports TS1206 for such decorators).
@Dunqing
Dunqing force-pushed the fix/legacy-decorator-declare-computed-key branch from b525e65 to 9ccd228 Compare November 26, 2026 01:20
@Dunqing
Dunqing added this pull request to the merge queue Nov 26, 2026
Merged via the queue into main with commit 56b8f4f Nov 26, 2026
40 checks passed
@Dunqing
Dunqing deleted the fix/legacy-decorator-declare-computed-key branch November 26, 2026 01:35
Dunqing added a commit to rolldown/rolldown that referenced this pull request Nov 26, 2026
Pins the decorators-lowering head restacked onto oxc main 746ad93f.

Snapshot changes from oxc main:
- ts_experimental_decorators: computed keys of decorated `declare`
  fields are evaluated and passed to __decorate again
  (oxc-project/oxc#30264).
- ts_declare_class_fields: keys of `declare` fields are no longer
  evaluated, as tsc and esbuild do (oxc-project/oxc#30272).
- invalid_define_config/value: the error names the key and value
  (oxc-project/oxc#30280).
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