Skip to content

CountableSet with constructor arguments doesn't work #4436

@hi-ogawa

Description

@hi-ogawa

UnoCSS version

65.5.0

Describe the bug

Example code:

import { CountableSet } from '@unocss/core';

const set = new CountableSet(['foo']);
console.log('[set]', set);
console.log('[count]', set.getCount('foo'));

Output:

[set] CountableSet(1) [Set] { 'foo', _map: Map(0) {} }
[count] 0

Even though the intended behavior is confirmed by the following test:

it('getCount', () => {
const s = new CountableSet(['bar1', 'bar2', 'bar2'])
expect(s.getCount('bar1')).toBe(1)
expect(s.getCount('bar2')).toBe(2)
})

this actually depends on how class field _map: Map<K, number> is transformed. On Vitest, it's transformed with typescript useDefineForClassFields: false semantics and it works, but the published version is transpiled with standard class field semantics and it breaks current implementation of CountableSet constructor:

export class CountableSet<K> extends Set<K> {
_map: Map<K, number>
constructor(values?: Iterable<K>) {
super(values)
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)
}

Internally, new CountableSet() is always used without constructor arguments, so the issue haven't probably manifested anywhere.

Side note: I was checking the test failure when using rolldown-vite and the code is broken even on Vitest due to oxc transform oxc-project/oxc#9192. Then I just noticed the published @unocss/core is also broken, so I ended up reporting it here.

Reproduction

https://stackblitz.com/edit/vitejs-vite-wgp96mef?file=repro.js

System Info

(stackblitz)

Validations

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions