Repository navigation
fix: honour useDefineForClassFields instead of inverting it - #742
Conversation
The fixture forced a helper with `useDefineForClassFields: false`, which is `[[Define]]` under the current mapping but `[[Set]]` once oxc-project#742 corrects it — and `[[Set]]` needs no helper at all, so the fixture would stop testing anything and the assertion would fail. `target: ES2022` with the option left unset is `[[Define]]` both ways: it is TypeScript's own default for that target, and it is what `unwrap_or_default()` produces today. Verified against both builds.
…lass-field polarity The spec asserted the transformed dependency reports `setterCalled: true`, which holds under the current `useDefineForClassFields` mapping and flips once oxc-project#742 corrects it. It now runs the dependency under both values of the option and asserts they disagree when it was transformed and agree when it was not, which is true under either mapping.
|
Cross-PR note, found by merging all three of these together and running the suites rather than assuming they compose. They do not conflict textually — git merges all three cleanly — but #742 changes the class-field semantics that fixtures in the other two relied on, so whichever landed second would have had red CI:
Both fixtures are now independent of it:
With those in place, all three merged together: 73/73 on Node.js 22.22.3, 24.8.0, 24.20.0, 26.2.0 and 26.8.1, 71 passed + 2 skipped on 22.18.0 and 24.7.0 (the two specs that need the synchronous hooks), and the No product code changed for this — only the two fixtures. |
9edd4d6 to
d7d9bf6
Compare
|
Maintainer pass over the branch (rebased onto current
The spec grew from 13 to 19 cases: the two corrected defaults (no target, no tsconfig at all), plus new pins for field elision, decorator-on-elided-field, and |
`useDefineForClassFields` selects `[[Define]]` semantics for public class fields — the field is installed with `Object.defineProperty`, so an inherited setter is not called. Oxc's `setPublicClassFields` assumption selects the opposite, `[[Set]]` semantics, so it has to be the negation of the tsconfig option. It was assigned it directly, which inverted every explicit setting: | `target` | `useDefineForClassFields` | tsc | before | | -------- | ------------------------- | ------------ | ------------ | | ES2022 | unset | `[[Define]]` | `[[Define]]` | | ES2022 | `true` | `[[Define]]` | `[[Set]]` | | ES2022 | `false` | `[[Set]]` | `[[Define]]` | | ES2017 | unset | `[[Set]]` | `[[Define]]` | | ES2017 | `true` | `[[Define]]` | `[[Set]]` | | ES2017 | `false` | `[[Set]]` | `[[Define]]` | `useDefineForClassFields: false` is what NestJS, MobX, TypeORM and every other decorator-based framework asks for, and it was doing the exact opposite. The default was wrong too. It came from `unwrap_or_default()`, i.e. always `false`, while TypeScript defaults the option to `true` from `target: ES2022` — the first target with native class fields. An unset `target` also means `true`: since TypeScript 6, `tsc` defaults the target to the stable ECMAScript version preceding `ESNext` (before TypeScript 6 it defaulted to `ES5`, which meant `false`; `target: es5` itself was removed in TypeScript 7). Two companion behaviours of `[[Set]]` mode come along for fidelity: - `tsc` drops class fields without an initializer instead of assigning `undefined` through the prototype chain, which would fire an inherited setter. oxc only does that under `removeClassFieldsWithoutInitializer`, whose docs say to pair it with `setPublicClassFields` — so the two are now set together. - The class-properties `loose` option stays `false`: besides public fields it also lowers `#private` fields to string-keyed properties, leaking their names and freezing them with `Object.freeze` — something `tsc` never does. The assumption alone selects `[[Set]]` for public fields (the transformer ORs the two), so `loose` was never needed for the fix. Verified against `tsc` from the repository's own devDependencies: every combination of `target` in ES2022/ESNext/ES2017/ES5 and the option unset, `true` and `false` agrees, plus the no-target and no-tsconfig cases. `packages/integrate-vitest/__tests__/class-fields.spec.ts` pins each of them by observing whether the inherited setter runs, and pins the field-elision, legacy-decorator and `#private`-field behaviour that comes with them. This changes the emitted semantics for any project that set the option explicitly, or that targets below ES2022 — in every case to what `tsc` produces for the same tsconfig.
d7d9bf6 to
0a0a617
Compare
The fixture forced a helper with `useDefineForClassFields: false`, which is `[[Define]]` under the current mapping but `[[Set]]` once oxc-project#742 corrects it — and `[[Set]]` needs no helper at all, so the fixture would stop testing anything and the assertion would fail. `target: ES2022` with the option left unset is `[[Define]]` both ways: it is TypeScript's own default for that target, and it is what `unwrap_or_default()` produces today. Verified against both builds.
## Summary
When a transform needs a runtime helper, oxc injects an import of it and
picks `import` or `require()` based on the module kind of the source.
For `.js`, `.jsx`, `.ts` and `.tsx` that module kind is
`ModuleKind::Unambiguous`, so oxc infers it from the presence of
`import`/`export` syntax — and a file that happens to have none is
treated as a script, which gets `require()`.
Node.js decides from the nearest `package.json` instead. A file with no
module syntax inside a `"type": "module"` package is therefore executed
as an ES module with a `require()` call in it:
```jsonc
// package.json
{ "type": "module" }
```
```ts
// a.ts — no import or export anywhere in the file
class Base {
set field(value: unknown) {
(this as any).setterCalled = true;
}
}
class Derived extends Base {
field = 1;
}
console.log(new Derived().field);
```
```
$ node --import @oxc-node/core/register ./a.ts
var _defineProperty = require("@oxc-node/core/helpers/defineProperty");
^
ReferenceError: require is not defined in ES module scope, you can use import instead
```
Any transform that needs a helper hits this — lowered class fields,
decorators, `using`, object rest/spread, and so on — as long as the file
itself has no `import` or `export`.
## Fix
The `load` hook already knows which it is, because Node.js reports the
format, so it passes that down and the parser is told the module kind
outright instead of guessing from the syntax:
```rust
let source_type = SourceType::from_path(src_path).unwrap_or_default().with_module(is_es_module);
```
The `pirates` hook and the public `transform()` API keep inferring it:
both target CommonJS, where `require()` is the correct output.
Only files that have no module syntax at all change — anything with an
`import` or `export` was already resolved to an ES module by the parser,
and `with_module(false)` is a no-op.
## Verification
`packages/integrate-vitest/__tests__/helper-imports.spec.ts` covers both
module kinds, with and without module syntax:
| package `type` | entry | module syntax | before | after |
| --- | --- | --- | --- | --- |
| `module` | `.ts` | none | `ReferenceError` | ok |
| `module` | `.mts` | none | ok | ok |
| `module` | `.ts` | has `export` | ok | ok |
| `commonjs` | `.ts` | none | ok | ok |
| `commonjs` | `.cts` | none | ok | ok |
plus an imported (rather than entry-point) module with no module syntax.
`integrate-module` (21/21), `integrate-module-bundler` (22/22) and
`integrate-vitest` pass on Node.js 22.23.2 and 24.20.0 with
`OXC_TRANSFORM_ALL` both set and unset. `cargo clippy --all-targets
--all-features -- -D warnings` and `pnpm lint` are clean. No napi
surface change, so the generated bindings are untouched.
## Note
#742 touches `oxc_transform` a few lines away. The two are logically
independent; whichever lands second needs a trivial rebase.
---------
Co-authored-by: LongYinan <[email protected]>
Summary
useDefineForClassFieldsselects[[Define]]semantics for public class fields: the field is installed withObject.defineProperty, so a setter inherited from a base class is not called. Oxc'ssetPublicClassFieldsassumption selects the opposite —[[Set]]semantics, a plain assignment — so it has to be the negation of the tsconfig option.It was assigned it directly, which inverts every explicit setting:
useDefineForClassFields: falseis what NestJS, MobX, TypeORM and every other decorator/DI based framework asks for, and oxc-node was doing the exact opposite of it.The default was wrong too. It came from
unwrap_or_default(), i.e. alwaysfalse, ignoringtarget. TypeScript defaults the option totruefromtarget: ES2022— the first target with native class fields — and tofalsebelow that. An unsettargetalso meanstrue: since TypeScript 6,tscdefaults the target to the stable ECMAScript version precedingESNext(before TypeScript 6 it defaulted toES5, which meantfalse;target: es5itself was removed in TypeScript 7).Two companion behaviours of
[[Set]]mode come along for fidelity:tscdrops class fields without an initializer instead of assigningundefinedthrough the prototype chain (which would fire an inherited setter). Oxc only does that underremoveClassFieldsWithoutInitializer, whose own documentation says to pair it withsetPublicClassFields— so the two are now set together.looseoption staysfalse: besides public fields it also lowers#privatefields to string-keyed properties, leaking their names and freezing them withObject.freeze— somethingtscnever does. The assumption alone selects[[Set]]for public fields (the transformer ORs the two), soloosewas never needed for the fix.Verification
Ground truth is
tscfrom this repository's own devDependencies (7.0.2 at the time of writing), compilingtargetuseDefineForClassFieldstsc[[Define]][[Define]][[Define]]true[[Define]][[Set]][[Define]]false[[Set]][[Define]][[Set]][[Define]][[Define]][[Define]]true[[Define]][[Set]][[Define]]false[[Set]][[Define]][[Set]][[Set]][[Define]][[Set]]true[[Define]][[Set]][[Define]]false[[Set]][[Define]][[Set]][[Set]](TS≤5 config;tsc7 rejectses5outright)[[Define]][[Set]]true[[Define]](ditto)[[Set]][[Define]]false[[Set]](ditto)[[Define]][[Set]][[Define]](TS≥6 default target)[[Define]][[Define]]All thirteen combinations now agree with
tsc. Fields without an initializer were checked too:tscelides them under[[Set]]and defines them asundefinedunder[[Define]], legacy decorators on elided fields still run, and#privatefields stay private and mutation survivesObject.freezeunder both — all pinned by the new spec.packages/integrate-vitest/__tests__/class-fields.spec.tspins each row by observing whether the inherited setter runs, so the direction is asserted rather than just "the two settings differ".integrate-module(21/21),integrate-module-bundler(22/22 — it setsuseDefineForClassFields: falseand exercises NestJS decorators and DI) andintegrate-vitest(55/55, 19 of them the new class-fields specs) pass withOXC_TRANSFORM_ALLboth set and unset.cargo clippy --all-targets --all-features -- -D warnings,cargo fmt --checkandvp checkare clean. No napi surface change, so the generated bindings are untouched.Compatibility
This changes the emitted semantics for any project that set
useDefineForClassFieldsexplicitly, or that targets below ES2022 — in every case to whattscproduces for the same tsconfig, which is the point.Note
This branch is rebased on current
main(past #569's per-file tsconfig discovery, which changed theoxc_transformsignature this code touches). #743 touchesoxc_transforma few lines away and still applies on its own; itshelper-importsfixture usestarget: ES2022with the option unset, which is[[Define]]both before and after this change, so the two remain independent of merge order.