Clean up helpers

This commit is contained in:
Andy Hanson
2016-12-08 08:00:10 -08:00
parent b15ffda7f6
commit 863e4d6fa0
4 changed files with 25 additions and 23 deletions

View File

@@ -66,7 +66,6 @@ namespace ts {
/** Methods on native maps but not on shim maps. Only used in this file. */
interface ES6Map<T> extends Map<T> {
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<U>(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<T>(left: Map<T>, right: Map<T>, 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.
*

View File

@@ -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

View File

@@ -193,6 +193,20 @@ namespace ts {
}
}
/** True if the maps have the same keys and values. */
function mapsAreEqual<T>(left: Map<T>, right: Map<T>, 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<ResolvedModule>): void {
checkCache("resolved modules", program, fileName, expectedContent, f => f.resolvedModules, checkResolvedModule);
}

View File

@@ -243,18 +243,12 @@ namespace ts.projectSystem {
}
export function checkMapKeys(caption: string, map: Map<any>, 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<T>(map: Map<T>): 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() {