Skip to content

Commit 80b19a0

Browse files
author
Andy
authored
Introduce a ReadonlyMap interface and use it in core.ts (microsoft#17161)
1 parent 0a8ddca commit 80b19a0

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

src/compiler/core.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ namespace ts {
585585
return result;
586586
}
587587

588-
export function mapEntries<T, U>(map: Map<T>, f: (key: string, value: T) => [string, U]): Map<U> {
588+
export function mapEntries<T, U>(map: ReadonlyMap<T>, f: (key: string, value: T) => [string, U]): Map<U> {
589589
if (!map) {
590590
return undefined;
591591
}
@@ -998,9 +998,9 @@ namespace ts {
998998
* Calls `callback` for each entry in the map, returning the first truthy result.
999999
* Use `map.forEach` instead for normal iteration.
10001000
*/
1001-
export function forEachEntry<T, U>(map: UnderscoreEscapedMap<T>, callback: (value: T, key: __String) => U | undefined): U | undefined;
1002-
export function forEachEntry<T, U>(map: Map<T>, callback: (value: T, key: string) => U | undefined): U | undefined;
1003-
export function forEachEntry<T, U>(map: UnderscoreEscapedMap<T> | Map<T>, callback: (value: T, key: (string & __String)) => U | undefined): U | undefined {
1001+
export function forEachEntry<T, U>(map: ReadonlyUnderscoreEscapedMap<T>, callback: (value: T, key: __String) => U | undefined): U | undefined;
1002+
export function forEachEntry<T, U>(map: ReadonlyMap<T>, callback: (value: T, key: string) => U | undefined): U | undefined;
1003+
export function forEachEntry<T, U>(map: ReadonlyUnderscoreEscapedMap<T> | ReadonlyMap<T>, callback: (value: T, key: (string & __String)) => U | undefined): U | undefined {
10041004
const iterator = map.entries();
10051005
for (let { value: pair, done } = iterator.next(); !done; { value: pair, done } = iterator.next()) {
10061006
const [key, value] = pair;
@@ -1013,9 +1013,9 @@ namespace ts {
10131013
}
10141014

10151015
/** `forEachEntry` for just keys. */
1016-
export function forEachKey<T>(map: UnderscoreEscapedMap<{}>, callback: (key: __String) => T | undefined): T | undefined;
1017-
export function forEachKey<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined;
1018-
export function forEachKey<T>(map: UnderscoreEscapedMap<{}> | Map<{}>, callback: (key: string & __String) => T | undefined): T | undefined {
1016+
export function forEachKey<T>(map: ReadonlyUnderscoreEscapedMap<{}>, callback: (key: __String) => T | undefined): T | undefined;
1017+
export function forEachKey<T>(map: ReadonlyMap<{}>, callback: (key: string) => T | undefined): T | undefined;
1018+
export function forEachKey<T>(map: ReadonlyUnderscoreEscapedMap<{}> | ReadonlyMap<{}>, callback: (key: string & __String) => T | undefined): T | undefined {
10191019
const iterator = map.keys();
10201020
for (let { value: key, done } = iterator.next(); !done; { value: key, done } = iterator.next()) {
10211021
const result = callback(key as string & __String);
@@ -1027,8 +1027,8 @@ namespace ts {
10271027
}
10281028

10291029
/** Copy entries from `source` to `target`. */
1030-
export function copyEntries<T>(source: UnderscoreEscapedMap<T>, target: UnderscoreEscapedMap<T>): void;
1031-
export function copyEntries<T>(source: Map<T>, target: Map<T>): void;
1030+
export function copyEntries<T>(source: ReadonlyUnderscoreEscapedMap<T>, target: UnderscoreEscapedMap<T>): void;
1031+
export function copyEntries<T>(source: ReadonlyMap<T>, target: Map<T>): void;
10321032
export function copyEntries<T, U extends UnderscoreEscapedMap<T> | Map<T>>(source: U, target: U): void {
10331033
(source as Map<T>).forEach((value, key) => {
10341034
(target as Map<T>).set(key, value);
@@ -1106,8 +1106,8 @@ namespace ts {
11061106
}
11071107

11081108
export function cloneMap(map: SymbolTable): SymbolTable;
1109-
export function cloneMap<T>(map: Map<T>): Map<T>;
1110-
export function cloneMap<T>(map: Map<T> | SymbolTable): Map<T> | SymbolTable {
1109+
export function cloneMap<T>(map: ReadonlyMap<T>): Map<T>;
1110+
export function cloneMap<T>(map: ReadonlyMap<T> | SymbolTable): Map<T> | SymbolTable {
11111111
const clone = createMap<T>();
11121112
copyEntries(map as Map<T>, clone);
11131113
return clone;

src/compiler/types.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ namespace ts {
88
[index: string]: T;
99
}
1010

11-
/** ES6 Map interface. */
12-
export interface Map<T> {
11+
/** ES6 Map interface, only read methods included. */
12+
export interface ReadonlyMap<T> {
1313
get(key: string): T | undefined;
1414
has(key: string): boolean;
15-
set(key: string, value: T): this;
16-
delete(key: string): boolean;
17-
clear(): void;
1815
forEach(action: (value: T, key: string) => void): void;
1916
readonly size: number;
2017
keys(): Iterator<string>;
2118
values(): Iterator<T>;
2219
entries(): Iterator<[string, T]>;
2320
}
2421

22+
/** ES6 Map interface. */
23+
export interface Map<T> extends ReadonlyMap<T> {
24+
set(key: string, value: T): this;
25+
delete(key: string): boolean;
26+
clear(): void;
27+
}
28+
2529
/** ES6 Iterator type. */
2630
export interface Iterator<T> {
2731
next(): { value: T, done: false } | { value: never, done: true };
@@ -2983,20 +2987,24 @@ namespace ts {
29832987
*/
29842988
export type __String = (string & { __escapedIdentifier: void }) | (void & { __escapedIdentifier: void }) | InternalSymbolName;
29852989

2986-
/** EscapedStringMap based on ES6 Map interface. */
2987-
export interface UnderscoreEscapedMap<T> {
2990+
/** ReadonlyMap where keys are `__String`s. */
2991+
export interface ReadonlyUnderscoreEscapedMap<T> {
29882992
get(key: __String): T | undefined;
29892993
has(key: __String): boolean;
2990-
set(key: __String, value: T): this;
2991-
delete(key: __String): boolean;
2992-
clear(): void;
29932994
forEach(action: (value: T, key: __String) => void): void;
29942995
readonly size: number;
29952996
keys(): Iterator<__String>;
29962997
values(): Iterator<T>;
29972998
entries(): Iterator<[__String, T]>;
29982999
}
29993000

3001+
/** Map where keys are `__String`s. */
3002+
export interface UnderscoreEscapedMap<T> extends ReadonlyUnderscoreEscapedMap<T> {
3003+
set(key: __String, value: T): this;
3004+
delete(key: __String): boolean;
3005+
clear(): void;
3006+
}
3007+
30003008
/** SymbolTable based on ES6 Map interface. */
30013009
export type SymbolTable = UnderscoreEscapedMap<Symbol>;
30023010

0 commit comments

Comments
 (0)