Skip to content

Do not lower class fields, private members, static blocks, and using on runtimes that run them natively #808

Closed

Description

@fengmk2

The transformer lowers class fields, private members, static blocks, and using unconditionally (class_properties: Some(..), class_static_block: true, explicit_resource_management: true, src/lib.rs#L800-L810). Every Node.js release that has module.register() runs class fields, private members, and static blocks natively, and using is native from Node.js 24.

Reproduction

// main.ts
class Counter {
  count = 1;
  #step = 1;
  static created = 0;
  static {
    Counter.created += 1;
  }
  bump() {
    return (this.count += this.#step);
  }
}
{
  using res = { [Symbol.dispose]() { console.log('disposed'); } };
}
console.log(new Counter().bump(), Counter.toString().includes('count = 1'));

@oxc-node/core 0.1.5 output (the same on Node.js 22.18, 24.21, and 26.5):

"use strict";
var _defineProperty = require("@oxc-node/core/helpers/defineProperty");
var _classPrivateFieldInitSpec = require("@oxc-node/core/helpers/classPrivateFieldInitSpec");
var _classPrivateFieldGet = require("@oxc-node/core/helpers/classPrivateFieldGet2");
var _usingCtx2 = require("@oxc-node/core/helpers/usingCtx");
var _Counter;
var _step = /* @__PURE__ */ new WeakMap();
class Counter {
	constructor() {
		_defineProperty(this, "count", 1);
		_classPrivateFieldInitSpec(this, _step, 1);
	}
	bump() {
		return this.count += _classPrivateFieldGet(_step, this);
	}
}
_Counter = Counter;
_defineProperty(Counter, "created", 0);
_Counter.created += 1;
try {
	var _usingCtx = _usingCtx2();
	// ...

node --import @oxc-node/core/register main.ts prints disposed and 2 false: the class no longer contains count = 1.

Why it matters

Suggestion

  • Lower class features only when native semantics differ: [[Set]] fields (useDefineForClassFields: false, already computed as set_public_class_fields) and legacy decorators (experimentalDecorators), which are applied to the lowered class.
  • Lower using only when the running Node.js lacks it, for example from napi's Env::get_node_version() (major < 24).
let legacy_decorators = compiler_options.and_then(|c| c.experimental_decorators).unwrap_or(false);
let lower_class_features = set_public_class_fields || legacy_decorators;
// ...
class_static_block: lower_class_features,
class_properties: lower_class_features.then(ClassPropertiesOptions::default),
// ...
es2026: ES2026Options { explicit_resource_management: !native_using },

Vite+ carries this change in its vendored copy and runs it on Node.js 22, 24, and 26.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions