Repository navigation
fix(transformer): keep original keys on leftover fields with setPublicClassFields - #29906
oxc-captain[bot] wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
|
Closing in favor of #29911, which includes this change (credited as co-author) and fixes two problems it still had:
Generated by Claude Code |
…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

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 theNullLiteraldummy:Instances got a
nullown property instead ofx, and the leftover member could shadow a class member namednull. Identifier keys were unaffected because their branch never takes the key.convert_property_definitionwas moving the key out of thePropertyDefinitionwithtake_in, leaving theNullLiteraldummy in an element that stays in the class body wheneverremove_class_fields_without_initializeris false (the default, sinceallowDeclareFieldsdefaults to true). The key is now left in place — cloned into thethis[...]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:Closes #29904
Automated triagebot fix (AI-assisted).