Skip to content

Commit 863e4d6

Browse files
author
Andy Hanson
committed
Clean up helpers
1 parent b15ffda commit 863e4d6

4 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/compiler/core.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ namespace ts {
6666

6767
/** Methods on native maps but not on shim maps. Only used in this file. */
6868
interface ES6Map<T> extends Map<T> {
69-
readonly size: number;
7069
entries(): Iterator<[string, T]>;
7170
}
7271

@@ -130,6 +129,14 @@ namespace ts {
130129
return !this.some(() => true);
131130
}
132131

132+
get size(): number {
133+
let size = 0;
134+
for (const _ in this.data) {
135+
size++;
136+
}
137+
return size;
138+
}
139+
133140
forEachInMap<U>(callback: (value: T, key: string) => U | undefined): U | undefined {
134141
for (const key in this.data) {
135142
const result = callback(this.data[key], key);
@@ -988,20 +995,6 @@ namespace ts {
988995
return true;
989996
}
990997

991-
/** True if the maps have the same keys and values. */
992-
export function mapsAreEqual<T>(left: Map<T>, right: Map<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
993-
if (left === right) return true;
994-
if (!left || !right) return false;
995-
const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => {
996-
if (!right.has(leftKey)) return true;
997-
const rightValue = right.get(leftKey);
998-
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
999-
});
1000-
if (someInLeftHasNoMatch) return false;
1001-
const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey));
1002-
return !someInRightHasNoMatch;
1003-
}
1004-
1005998
/**
1006999
* Creates a map from the elements of an array.
10071000
*

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace ts {
2020
clear(): void;
2121
/** `key` may *not* be a string if it was set with a number and we are not using the shim. */
2222
forEach(action: (value: T, key: string) => void): void;
23+
readonly size: number;
2324
}
2425

2526
// branded string type used to store absolute, normalized and canonicalized paths

src/harness/unittests/reuseProgramStructure.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ namespace ts {
193193
}
194194
}
195195

196+
/** True if the maps have the same keys and values. */
197+
function mapsAreEqual<T>(left: Map<T>, right: Map<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
198+
if (left === right) return true;
199+
if (!left || !right) return false;
200+
const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => {
201+
if (!right.has(leftKey)) return true;
202+
const rightValue = right.get(leftKey);
203+
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
204+
});
205+
if (someInLeftHasNoMatch) return false;
206+
const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey));
207+
return !someInRightHasNoMatch;
208+
}
209+
196210
function checkResolvedModulesCache(program: Program, fileName: string, expectedContent: Map<ResolvedModule>): void {
197211
checkCache("resolved modules", program, fileName, expectedContent, f => f.resolvedModules, checkResolvedModule);
198212
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,12 @@ namespace ts.projectSystem {
243243
}
244244

245245
export function checkMapKeys(caption: string, map: Map<any>, expectedKeys: string[]) {
246-
assert.equal(mapSize(map), expectedKeys.length, `${caption}: incorrect size of map`);
246+
assert.equal(map.size, expectedKeys.length, `${caption}: incorrect size of map`);
247247
for (const name of expectedKeys) {
248248
assert.isTrue(map.has(name), `${caption} is expected to contain ${name}, actual keys: ${keysOfMap(map)}`);
249249
}
250250
}
251251

252-
function mapSize<T>(map: Map<T>): number {
253-
let size = 0;
254-
map.forEach(() => { size++; });
255-
return size;
256-
}
257-
258252
export function checkFileNames(caption: string, actualFileNames: string[], expectedFileNames: string[]) {
259253
assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${JSON.stringify(expectedFileNames)}, got ${actualFileNames}`);
260254
for (const f of expectedFileNames) {
@@ -313,7 +307,7 @@ namespace ts.projectSystem {
313307
}
314308

315309
count() {
316-
return mapSize(this.map);
310+
return this.map.size;
317311
}
318312

319313
invoke() {

0 commit comments

Comments
 (0)