Skip to content

Commit

Permalink
fix(type): Make iterators compatible with TS 5.6 --target ESNext (#…
Browse files Browse the repository at this point in the history
…8279)

fix(type): Make iterators compatible with TS 5.6 --target ESNext

Use ReturnType<> to avoid explicitly naming the types of builtin
iterators that have changed in 5.6, and convert manually implemented
iterators to generators so they are compatible with builtin iterators
under --target ESNext.

Fixes part of #8277.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored Oct 31, 2024
1 parent b3ee626 commit dfde1fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 65 deletions.
45 changes: 9 additions & 36 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
*/
get namedChunkGroups() {
return createReadonlyMap<ChunkGroup>({
keys: (): MapIterator<string> => {
keys: (): ReturnType<string[]["values"]> => {
const names = this.#inner.getNamedChunkGroupKeys();
return names[Symbol.iterator]();
},
Expand Down Expand Up @@ -450,7 +450,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
*/
get namedChunks() {
return createReadonlyMap<Chunk>({
keys: (): IterableIterator<string> => {
keys: (): ReturnType<string[]["values"]> => {
const names = this.#inner.getNamedChunkKeys();
return names[Symbol.iterator]();
},
Expand Down Expand Up @@ -1240,22 +1240,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si

export type EntryData = binding.JsEntryData;

/**
* Copied from `lib.es2015.iterable.d.ts` in TS 5.6 for compatibility
* 1. In 5.6 and after, `IterableIterator` cannot be assigned to 'MapIterator'
* 2. Before 5.6, Cannot find name 'MapIterator'
* @see https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/#iterator-helper-methods
*/
interface IteratorObject<T, TReturn = unknown, TNext = unknown>
extends Iterator<T, TReturn, TNext> {
[Symbol.iterator](): IteratorObject<T, TReturn, TNext>;
}
type BuiltinIteratorReturn = any;
interface MapIterator<T>
extends IteratorObject<T, BuiltinIteratorReturn, unknown> {
[Symbol.iterator](): MapIterator<T>;
}

export class Entries implements Map<string, EntryData> {
#data: binding.JsEntries;

Expand Down Expand Up @@ -1284,28 +1268,17 @@ export class Entries implements Map<string, EntryData> {
return this.#data.size;
}

entries(): MapIterator<[string, binding.JsEntryData]> {
const self = this;
const keys = this.keys();
return {
[Symbol.iterator]() {
return this;
},
next() {
const { done, value } = keys.next();
return {
done,
value: done ? (undefined as any) : [value, self.get(value)!]
};
}
};
*entries(): ReturnType<Map<string, EntryData>["entries"]> {
for (const key of this.keys()) {
yield [key, this.get(key)!];
}
}

values(): MapIterator<binding.JsEntryData> {
values(): ReturnType<Map<string, EntryData>["values"]> {
return this.#data.values()[Symbol.iterator]();
}

[Symbol.iterator](): MapIterator<[string, binding.JsEntryData]> {
[Symbol.iterator](): ReturnType<Map<string, EntryData>["entries"]> {
return this.entries();
}

Expand All @@ -1330,7 +1303,7 @@ export class Entries implements Map<string, EntryData> {
return this.#data.get(key);
}

keys(): MapIterator<string> {
keys(): ReturnType<Map<string, EntryData>["keys"]> {
return this.#data.keys()[Symbol.iterator]();
}
}
34 changes: 5 additions & 29 deletions packages/rspack/src/util/ArrayQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,11 @@ class ArrayQueue<T> {
}
}

[Symbol.iterator]() {
let i = -1;
let reversed = false;
return {
next: () => {
if (!reversed) {
i++;
if (i < this._list.length) {
return {
done: false,
value: this._list[i]
};
}
reversed = true;
i = this._listReversed.length;
}
i--;
if (i < 0) {
return {
done: true,
value: undefined
};
}
return {
done: false,
value: this._listReversed[i]
};
}
};
*[Symbol.iterator]() {
yield* this._list;
for (let i = this._listReversed.length - 1; i >= 0; i--) {
yield this._listReversed[i];
}
}
}

Expand Down

2 comments on commit dfde1fd

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-10-31 02f0c85) Current Change
10000_big_production-mode + exec 49.3 s ± 1.56 s 47.7 s ± 2.4 s -3.32 %
10000_development-mode + exec 2.12 s ± 33 ms 2.09 s ± 15 ms -1.55 %
10000_development-mode_hmr + exec 638 ms ± 4.3 ms 640 ms ± 4 ms +0.29 %
10000_production-mode + exec 2.69 s ± 41 ms 2.63 s ± 28 ms -2.20 %
arco-pro_development-mode + exec 1.79 s ± 73 ms 1.76 s ± 103 ms -1.65 %
arco-pro_development-mode_hmr + exec 428 ms ± 1.1 ms 428 ms ± 1.3 ms +0.05 %
arco-pro_production-mode + exec 3.21 s ± 130 ms 3.21 s ± 60 ms -0.12 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.25 s ± 68 ms 3.28 s ± 80 ms +0.67 %
threejs_development-mode_10x + exec 1.62 s ± 20 ms 1.61 s ± 18 ms -0.58 %
threejs_development-mode_10x_hmr + exec 770 ms ± 13 ms 772 ms ± 7.4 ms +0.29 %
threejs_production-mode_10x + exec 5.01 s ± 27 ms 5.02 s ± 29 ms +0.18 %

Please sign in to comment.