Skip to content

assumptions.setPublicClassFields doesn't fully simulate tsconfig useDefineForClassFields #9192

@hi-ogawa

Description

@hi-ogawa

reproduction: https://github.com/hi-ogawa/reproductions/tree/main/unocss-countable-set

I found a somewhat odd code on unocss while testing it with rolldown-vite https://github.com/unocss/unocss/blob/0a32090ddf452ef78ad5bedb6d5e888a161fefc9/packages-engine/core/src/utils/countable-set.ts. A simplified version is the following:

class CountableSet<K> extends Set<K> {
  _map: Map<K, number>

  constructor(values?: Iterable<K>) {
    super(values)  // `super` calls `add` which initialize `this._map`
    this._map ??= new Map()
  }

  add(key: K) {
    this._map ??= new Map()
    this._map.set(key, (this._map.get(key) ?? 0) + 1)
    return super.add(key)
  }
}

console.log(new CountableSet(["foo"]))
// expected to output
// CountableSet(1) [Set] { 'foo', _map: Map(1) { 'foo' => 1 } }

This only works when typescript useDefineForClassFields: false, which entirely strips class field parts typescript playground

class CountableSet extends Set {
    constructor(values) {
        super(values);
        this._map ?? (this._map = new Map());
    }
   ...

On rolldown-vite, I wasn't able to find a way to achieve this and it's likely because Oxc's setPublicClassFields: true resets _map after super calls.

class CountableSet extends Set {
        constructor(values) {
                super(values);
                this._map = void 0; 
                this._map ?? (this._map = new Map());
        }
...
console.log(new CountableSet(["foo"]))
// rolldown-vite output
// CountableSet(1) [Set] { 'foo', _map: Map(0) {} }

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / TranspilerC-bugCategory - BugP-highPriority - High

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions