Skip to content

fix(transformer): keep original keys on leftover fields with setPublicClassFields - #29906

Closed
oxc-captain[bot] wants to merge 1 commit into
mainfrom
triagebot/fix-29904
Closed

oxc-captain[bot] wants to merge 1 commit into
mainfrom
triagebot/fix-29904

Conversation

@oxc-captain

@oxc-captain oxc-captain Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

With setPublicClassFields: true, the TypeScript transform moves field initializers into the constructor, but for fields with string, numeric, BigInt or computed keys it left the original class element behind with its key replaced by the NullLiteral dummy:

class C {
  constructor() {
    this["x"] = 1;
    this[_y] = 2;
  }
  null;
  [null];
}

Instances got a null own property instead of x, and the leftover member could shadow a class member named null. Identifier keys were unaffected because their branch never takes the key.

convert_property_definition was moving the key out of the PropertyDefinition with take_in, leaving the NullLiteral dummy in an element that stays in the class body whenever remove_class_fields_without_initializer is false (the default, since allowDeclareFields defaults to true). The key is now left in place — cloned into the this[...] assignment for side-effect-free keys, or replaced with a reference to the hoisted temp var for computed keys — so the leftover declaration keeps its original property name, matching the existing identifier-key behavior:

class C {
  constructor() {
    this["x"] = 1;
    this[_y] = 2;
  }
  "x";
  [_y];
}

Closes #29904

Automated triagebot fix (AI-assisted).

@oxc-captain
oxc-captain Bot requested a review from Dunqing as a code owner November 19, 2026 17:03
@oxc-captain oxc-captain Bot added the triage: fix verified The proposed fix was verified label Nov 19, 2026
@github-actions github-actions Bot added the A-transformer Area - JS/TS transforms and downleveling, oxc_transformer label Nov 19, 2026
@codspeed

codspeed Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing triagebot/fix-29904 (28c793e) with main (e385fe8)

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 commented Nov 20, 2026

Copy link
Copy Markdown
Member

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Dunqing commented Nov 20, 2026

Copy link
Copy Markdown
Member

Closing in favor of #29911, which includes this change (credited as co-author) and fixes two problems it still had:

  • With the default allowDeclareFields, a computed key that needs a temp var stayed as [_y] while _y was assigned in a static {} block. Computed keys are evaluated before static blocks run, so instances got an extra "undefined" property. fix(transformer): keep correct keys on leftover fields with setPublicClassFields #29911 assigns the temp var inside the key instead ([_y = y];).
  • With allowDeclareFields: false, the clone_in copy of _y has no ReferenceId, and key_needs_temp_var panics on it.

#29911 also closes #29904.


Generated by Claude Code

@Dunqing Dunqing closed this Nov 20, 2026
graphite-app Bot pushed a commit that referenced this pull request Nov 20, 2026
…ClassFields (#29911)

With `setPublicClassFields: true`, the TypeScript transform moves field initializers into the constructor. With the default `allowDeclareFields`, the field declaration stays in the class body. For string, numeric, BigInt and computed keys, the key was moved out with `take_in`, leaving `null;` / `[null];` behind, so instances got a bogus `null` own property.

Supersedes #29906. That PR kept literal keys, but left computed keys as `[_y]` while assigning `_y` in a `static {}` block. JavaScript evaluates every computed class key during class definition, before any static block runs, so `[_y]` read `undefined`. That PR also panicked with `allowDeclareFields: false`.

Closes #29904

### Example

Input (options: `{ "assumptions": { "setPublicClassFields": true }, "plugins": ["transform-typescript"] }`):

```ts
const getY = () => "yKey";
class C {
  "x" = 1;
  [getY()] = 2;
}
console.log(Object.keys(new C()));
```

No playground link: the playground's `useDefineForClassFields: false` turns on both `setPublicClassFields` and `removeClassFieldsWithoutInitializer`, so it can't select the kept-declaration path.

Before output (`main`, `37ec2e29`):

```js
let _getY;
const getY = () => "yKey";
class C {
  constructor() {
    this["x"] = 1;
    this[_getY] = 2;
  }
  static {
    _getY = getY();
  }
  null;
  [null];
}
console.log(Object.keys(new C()));
// node: [ 'null', 'x', 'yKey' ]
```

After output:

```js
let _getY;
const getY = () => "yKey";
class C {
  constructor() {
    this["x"] = 1;
    this[_getY] = 2;
  }
  "x";
  [_getY = getY()];
}
console.log(Object.keys(new C()));
// node: [ 'x', 'yKey' ]
```

Outputs come from `cargo run -p oxc_transformer --example transformer -- --babel-options <dir with that options.json> ex.ts` at each commit, run with `node`. With #29906's approach, the same input gives `[ 'x', 'undefined', 'yKey' ]`.

### Changes

- **Declaration kept (default):** keep the original key on the leftover declaration. For a computed key that needs a temp var, assign the temp var inside the key (`[_y = y];`), so each key runs exactly once, at class definition and in source order. Key assignments still waiting to be emitted (from static fields turned into static blocks) are folded into the next kept key. That includes kept fields without an initializer: before, `static [k("c")] = 2; [k("d")]: number;` ran `d` before `c`.
- **Declaration removed (`allowDeclareFields: false`):** keep the static-block approach, which matches `tsc` with `useDefineForClassFields: false`.
- Keys with no side effects are copied for `this[...]` with `duplicate_expression`, which creates proper references, rather than `clone_in`.

### Upstream comparison

- **Babel** (7.29, 8.0), TS plugin only: ignores `setPublicClassFields` and keeps native fields (`[y] = 4;`).
- **tsc** (5.9.3, 7.0.2) and **esbuild** (0.28.2) with `useDefineForClassFields: false`: drop every field declaration, and evaluate keys in source order, in a static block (tsc) or before the class (esbuild).

None of them keeps a leftover declaration, so for that mode the target is the runtime requirement: each key evaluated once, in order, with correct property names.

### Tests

- Fixture `oxc/set-public-class-fields-non-identifier-keys` (from #29906, extended): output plus a new `exec.ts`. It checks `Object.keys(new C())` and the values, and that side-effecting keys run once and in order.
- New fixture `oxc/set-public-class-fields-computed-key-order-remove-uninitialized` (`allowDeclareFields: false`), with output and exec.
- `cargo test -p oxc_transformer`, `cargo clippy -p oxc_transformer --all-targets`, and `just fmt` pass. `cargo run -p oxc_transform_conformance -- --exec`, compared with `main` (`f573c939`): oxc 342/467 → 344/469, oxc exec 71/367 → 73/369. The other exec failures are already failing on `main`.

AI disclosure: written with Claude Code, using Claude Opus 5.5 (`claude-opus-5-5`). The reasoning effort level isn't exposed in the session, so I can't state it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_018ogydbUHX7FDns3cQgBB58
@oxc-captain
oxc-captain Bot deleted the triagebot/fix-29904 branch November 20, 2026 12:13
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 triage: fix verified The proposed fix was verified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformer: setPublicClassFields leaves TS class fields with null keys

1 participant