Skip to content

fix(transformer/typescript): preserve computed key context - #30148

Merged
Dunqing merged 1 commit into
mainfrom
fix/typescript-computed-key-static-block
Nov 24, 2026
Merged

Dunqing merged 1 commit into
mainfrom
fix/typescript-computed-key-static-block

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Computed class keys that depend on the surrounding context can be moved into a static block when setPublicClassFields is enabled. For example, moving yield makes the output invalid; moving this can change the key. Oxc #29930 exposed this path for classes with preserved decorators or auto-accessors.

Example

Input (playground for this example; the playground uses the released Oxc version):

function* create() { return class { static [yield 'name'] = 1; accessor x = 0 } }
const it=create(); console.log(it.next().value, it.next('field').value.field);

With target ES2021 and setPublicClassFields: true, the Rolldown bundle using Oxc before this change contains:

function* create() {
  let _yield$name;
  return class {
    static {
      _yield$name = yield "name";
    }
    static {
      this[_yield$name] = 1;
    }
    accessor x = 0;
  };
}

The fixed Oxc transform produces (captured from the transformer with target ES2021, set_public_class_fields and remove_class_fields_without_initializer enabled):

var _computedKey = Symbol();
function* create() {
  let _yield$name;
  return class {
    static {
      delete this[_computedKey];
    }
    static {
      this[_yield$name] = 1;
    }
    accessor x = 0;
    static [(_yield$name = yield "name", _computedKey)]() {}
  };
}

A moved key already runs in place when a later computed key stays in the class, because it is prepended to that key. Only the keys after the last one went into the leading static block. When one of those keys uses this, super, arguments, new.target, yield, await or direct eval, they now go into the key of one temporary static method at the end of the class, and the static block deletes that method before any other static initializer runs. The method is keyed by a symbol created once at the top of the file. Field writes and their order are unchanged.

Decorators kept for a later transform can still see the temporary method before it is deleted.

Transformer tests, clippy, formatting, and transform conformance passed. Both new fixtures and the exec fixture pass.

AI disclosure: prepared with AI assistance (models group/smart-split[1m] and Claude Opus 5.5 claude-opus-5-5; the reasoning effort level was not exposed).

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

codspeed Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing fix/typescript-computed-key-static-block (a81c69d) with main (dae2f74)

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 marked this pull request as ready for review November 24, 2026 07:30
@Dunqing
Dunqing force-pushed the fix/typescript-computed-key-static-block branch 2 times, most recently from 7f51967 to 524ffba Compare November 24, 2026 09:55
@Dunqing
Dunqing force-pushed the fix/typescript-computed-key-static-block branch from 524ffba to a81c69d Compare November 24, 2026 15:28
@Dunqing
Dunqing added this pull request to stack #30185 November 24, 2026 16:08
@Dunqing
Dunqing added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit 9353d2c Nov 24, 2026
40 checks passed
@Dunqing
Dunqing deleted the fix/typescript-computed-key-static-block branch November 24, 2026 16:48
hoanle0126 pushed a commit to hoanle0126/oxc that referenced this pull request Nov 24, 2026
oxc-project#30184)

A standard element decorator can inspect the class before its static
blocks run. With `setPublicClassFields` enabled, it can see the
symbol-keyed method that oxc-project#30148 generates to evaluate a computed key.
The source class has no such property.

### Example

```ts
const observed = [];
function inspect(getClass) {
  observed.push(Object.getOwnPropertySymbols(getClass()).filter(key => key !== Symbol.metadata));
}
function* make() {
  return class C {
    @inspect static getClass() { return C; }
    static [yield "key"] = 1;
  };
}
const it = make();
expect(it.next().value).toBe("key");
const C = it.next("x").value;
expect(C.x).toBe(1);
expect(observed).toEqual([[]]);
```

The Oxc playground cannot run the downstream decorator pass that exposes
the extra symbol. To print the JavaScript locally, save the input as
`/tmp/repro.ts` and run the transformer example with the fixture
options.

```sh
fixture=tasks/transform_conformance/tests/babel-plugin-transform-typescript/test/fixtures/oxc/preserved-class-context-dependent-computed-keys
cargo run -p oxc_transformer --example transformer -- /tmp/repro.ts --babel-options "$fixture/options.json"
```

Before, the generated method and cleanup were visible in the output.

```js
static {
  delete this[_computedKey];
}
// …
static [(_yield$key = yield "key", _computedKey)]() {}
```

After, the key runs in a decorator expression on a private method.

```js
@(_yield$key = yield "key", () => {}) static #_computedKey() {}
```

The generator still yields `"key"`, and the static field receives `1`.
With Babel 2023-11 and esbuild, the inspecting decorator saw one extra
symbol before and zero after. The runtime fixture also exercises a
user-defined symbol and enclosing private names accessed directly and
through `eval`.

```sh
cargo run -p oxc_transform_conformance -- --filter /downstream-decorators/ --exec
```

The focused runtime result is `Passed: 1 of 1`. Transformer tests and
lint passed. Transformer coverage snapshots matched, and full transform
conformance recorded one additional passing Oxc exec fixture.

AI disclosure: prepared with AI assistance. The exact model version and
reasoning effort were not recorded.
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.
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