Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,10 @@ export const getScriptTargetFeatures: () => ScriptTargetFeatures = /* @__PURE__
"keys",
"values",
],
esnext: [
"getOrInsert",
"getOrInsertComputed",
],
})),
MapConstructor: new Map(Object.entries({
es2024: [
Expand Down Expand Up @@ -1635,6 +1639,10 @@ export const getScriptTargetFeatures: () => ScriptTargetFeatures = /* @__PURE__
"keys",
"values",
],
esnext: [
"getOrInsert",
"getOrInsertComputed",
],
})),
WeakSet: new Map(Object.entries({
es2015: [
Expand Down
30 changes: 30 additions & 0 deletions src/lib/esnext.collection.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
/// <reference lib="es2024.collection" />

interface Map<K, V> {
/**
* Returns a specified element from the Map object.
* If no element is associated with the specified key, a new element with the value `defaultValue` will be inserted into the Map and returned.
* @returns The element associated with the specified key, which will be `defaultValue` if no element previously existed.
*/
getOrInsert(key: K, defaultValue: V): V;
/**
* Returns a specified element from the Map object.
* If no element is associated with the specified key, the result of passing the specified key to the `callback` function will be inserted into the Map and returned.
* @returns The element associated with the specific key, which will be the newly computed value if no element previously existed.
*/
getOrInsertComputed(key: K, callback: (key: K) => V): V;
}

interface WeakMap<K extends WeakKey, V> {
/**
* Returns a specified element from the WeakMap object.
* If no element is associated with the specified key, a new element with the value `defaultValue` will be inserted into the WeakMap and returned.
* @returns The element associated with the specified key, which will be `defaultValue` if no element previously existed.
*/
getOrInsert(key: K, defaultValue: V): V;
/**
* Returns a specified element from the WeakMap object.
* If no element is associated with the specified key, the result of passing the specified key to the `callback` function will be inserted into the WeakMap and returned.
* @returns The element associated with the specific key, which will be the newly computed value if no element previously existed.
*/
getOrInsertComputed(key: K, callback: (key: K) => V): V;
}

interface ReadonlySetLike<T> {
/**
* Despite its name, returns an iterator of the values in the set-like.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/acceptSymbolAsWeakType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ws.delete(s);

const wm = new WeakMap([[s, false]]);
>wm : Symbol(wm, Decl(acceptSymbolAsWeakType.ts, 7, 5))
>WeakMap : Symbol(WeakMap, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>WeakMap : Symbol(WeakMap, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>s : Symbol(s, Decl(acceptSymbolAsWeakType.ts, 0, 5))

wm.set(s, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function f() {
await using it5 = new Map<string, string>().entries();
>it5 : Symbol(it5, Decl(awaitUsingDeclarationsWithIteratorObject.ts, 15, 15))
>new Map<string, string>().entries : Symbol(Map.entries, Decl(lib.es2015.iterable.d.ts, --, --))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>entries : Symbol(Map.entries, Decl(lib.es2015.iterable.d.ts, --, --))

await using it6 = new Set<string>().keys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare const array: number[];

declare const map: Map<string, number>;
>map : Symbol(map, Decl(builtinIteratorReturn.ts, 1, 13))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))

declare const set: Set<number>;
>set : Symbol(set, Decl(builtinIteratorReturn.ts, 2, 13))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare const array: number[];

declare const map: Map<string, number>;
>map : Symbol(map, Decl(builtinIteratorReturn.ts, 1, 13))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))

declare const set: Set<number>;
>set : Symbol(set, Decl(builtinIteratorReturn.ts, 2, 13))
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/esNextWeakRefs_IterableWeakMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ export class IterableWeakMap<K extends object, V> implements WeakMap<K, V> {
return this.#weakMap.get(key)?.value;
}

getOrInsert(key: K, defaultValue: V): V {
if (!this.has(key)) {
this.set(key, defaultValue);
return defaultValue;
}
return this.get(key)!;
}

getOrInsertComputed(key: K, callback: (key: K) => V): V {
if (!this.has(key)) {
const value = callback(key);
this.set(key, value);
return value;
}
return this.get(key)!;
}

delete(key: K): boolean {
const entry = this.#weakMap.get(key);
if (entry === undefined) {
Expand Down Expand Up @@ -144,6 +161,21 @@ export class IterableWeakMap {
get(key) {
return this.#weakMap.get(key)?.value;
}
getOrInsert(key, defaultValue) {
if (!this.has(key)) {
this.set(key, defaultValue);
return defaultValue;
}
return this.get(key);
}
getOrInsertComputed(key, callback) {
if (!this.has(key)) {
const value = callback(key);
this.set(key, value);
return value;
}
return this.get(key);
}
delete(key) {
const entry = this.#weakMap.get(key);
if (entry === undefined) {
Expand Down
Loading