-
-
Notifications
You must be signed in to change notification settings - Fork 966
Description
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:
unocss/packages-engine/core/test/utils/countable-set.test.ts
Lines 47 to 52 in 8f8a215
| 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:
unocss/packages-engine/core/src/utils/countable-set.ts
Lines 1 to 13 in 8f8a215
| 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
System Info
(stackblitz)
Validations
- Read the Contributing Guidelines.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.