From 863e4d6fa06b7d705592167285f17dd12981d8e8 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 8 Dec 2016 08:00:10 -0800 Subject: [PATCH] Clean up helpers --- src/compiler/core.ts | 23 +++++++------------ src/compiler/types.ts | 1 + .../unittests/reuseProgramStructure.ts | 14 +++++++++++ .../unittests/tsserverProjectSystem.ts | 10 ++------ 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 3fe3b386701..3c370f7b154 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -66,7 +66,6 @@ namespace ts { /** Methods on native maps but not on shim maps. Only used in this file. */ interface ES6Map extends Map { - readonly size: number; entries(): Iterator<[string, T]>; } @@ -130,6 +129,14 @@ namespace ts { return !this.some(() => true); } + get size(): number { + let size = 0; + for (const _ in this.data) { + size++; + } + return size; + } + forEachInMap(callback: (value: T, key: string) => U | undefined): U | undefined { for (const key in this.data) { const result = callback(this.data[key], key); @@ -988,20 +995,6 @@ namespace ts { return true; } - /** True if the maps have the same keys and values. */ - export function mapsAreEqual(left: Map, right: Map, valuesAreEqual?: (left: T, right: T) => boolean): boolean { - if (left === right) return true; - if (!left || !right) return false; - const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => { - if (!right.has(leftKey)) return true; - const rightValue = right.get(leftKey); - return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue); - }); - if (someInLeftHasNoMatch) return false; - const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey)); - return !someInRightHasNoMatch; - } - /** * Creates a map from the elements of an array. * diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 055049999ec..75e1e82e7f6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -20,6 +20,7 @@ namespace ts { clear(): void; /** `key` may *not* be a string if it was set with a number and we are not using the shim. */ forEach(action: (value: T, key: string) => void): void; + readonly size: number; } // branded string type used to store absolute, normalized and canonicalized paths diff --git a/src/harness/unittests/reuseProgramStructure.ts b/src/harness/unittests/reuseProgramStructure.ts index 54c05a9c383..eb4395154e4 100644 --- a/src/harness/unittests/reuseProgramStructure.ts +++ b/src/harness/unittests/reuseProgramStructure.ts @@ -193,6 +193,20 @@ namespace ts { } } + /** True if the maps have the same keys and values. */ + function mapsAreEqual(left: Map, right: Map, valuesAreEqual?: (left: T, right: T) => boolean): boolean { + if (left === right) return true; + if (!left || !right) return false; + const someInLeftHasNoMatch = someInMap(left, (leftValue, leftKey) => { + if (!right.has(leftKey)) return true; + const rightValue = right.get(leftKey); + return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue); + }); + if (someInLeftHasNoMatch) return false; + const someInRightHasNoMatch = someKeyInMap(right, rightKey => !left.has(rightKey)); + return !someInRightHasNoMatch; + } + function checkResolvedModulesCache(program: Program, fileName: string, expectedContent: Map): void { checkCache("resolved modules", program, fileName, expectedContent, f => f.resolvedModules, checkResolvedModule); } diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index bec3a700879..9beefe480df 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -243,18 +243,12 @@ namespace ts.projectSystem { } export function checkMapKeys(caption: string, map: Map, expectedKeys: string[]) { - assert.equal(mapSize(map), expectedKeys.length, `${caption}: incorrect size of map`); + assert.equal(map.size, expectedKeys.length, `${caption}: incorrect size of map`); for (const name of expectedKeys) { assert.isTrue(map.has(name), `${caption} is expected to contain ${name}, actual keys: ${keysOfMap(map)}`); } } - function mapSize(map: Map): number { - let size = 0; - map.forEach(() => { size++; }); - return size; - } - export function checkFileNames(caption: string, actualFileNames: string[], expectedFileNames: string[]) { assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${JSON.stringify(expectedFileNames)}, got ${actualFileNames}`); for (const f of expectedFileNames) { @@ -313,7 +307,7 @@ namespace ts.projectSystem { } count() { - return mapSize(this.map); + return this.map.size; } invoke() {