Skip to content

transformer: ??= on a static #private field drops the RHS value #21874

@sapphi-red

Description

@sapphi-red
class Singleton {
  static #shared;

  static shared() {
    this.#shared ??= 1;
    return this.#shared;
  }
}

is transformed to

import _assertClassBrand from "@oxc-project/runtime/helpers/assertClassBrand";
class Singleton {
	static shared() {
		_assertClassBrand(Singleton, this, _shared)._ ?? (_shared._ = _assertClassBrand(Singleton, this, null));
		return _assertClassBrand(Singleton, this, _shared)._;
	}
}
var _shared = { _: void 0 };
console.log(Singleton.shared());

but it should be

import _assertClassBrand from "@oxc-project/runtime/helpers/assertClassBrand";
class Singleton {
	static shared() {
		_assertClassBrand(Singleton, this, _shared)._ ?? (_shared._ = _assertClassBrand(Singleton, this, 1));
		return _assertClassBrand(Singleton, this, _shared)._;
	}
}
var _shared = { _: void 0 };
console.log(Singleton.shared());

.
playground

Originally reported at vitejs/vite#22338

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / Transpiler

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions