Inline keysOfMap and valuesOfMap.

This commit is contained in:
Andy Hanson 2016-12-28 09:05:52 -08:00
parent f510897dbd
commit 39c19a74ea
6 changed files with 14 additions and 26 deletions

View file

@ -1,4 +1,4 @@
/// <reference path="sys.ts"/>
/// <reference path="sys.ts"/>
/// <reference path="types.ts"/>
/// <reference path="core.ts"/>
/// <reference path="diagnosticInformationMap.generated.ts"/>
@ -543,7 +543,7 @@ namespace ts {
/* @internal */
export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType): Diagnostic {
const namesOfType = keysOfMap(opt.type).map(key => `'${key}'`).join(", ");
const namesOfType = arrayFrom(opt.type.keys()).map(key => `'${key}'`).join(", ");
return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType);
}
@ -1255,8 +1255,8 @@ namespace ts {
}
}
const literalFiles = valuesOfMap(literalFileMap);
const wildcardFiles = valuesOfMap(wildcardFileMap);
const literalFiles = arrayFrom(literalFileMap.values());
const wildcardFiles = arrayFrom(wildcardFileMap.values());
wildcardFiles.sort(host.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive);
return {
fileNames: literalFiles.concat(wildcardFiles),

View file

@ -156,7 +156,7 @@ namespace ts {
}
function getKeys() {
return keysOfMap(files) as Path[];
return arrayFrom(files.keys()) as Path[];
}
// path should already be well-formed so it does not need to be normalized
@ -867,7 +867,8 @@ namespace ts {
return keys;
}
function arrayFrom<T>(iterator: Iterator<T>): T[] {
/** Shims `Array.from`. */
export function arrayFrom<T>(iterator: Iterator<T>): T[] {
const result: T[] = [];
for (let { value, done } = iterator.next(); !done; { value, done } = iterator.next()) {
result.push(value);
@ -875,19 +876,6 @@ namespace ts {
return result;
}
/**
* Array of every key in a map.
* May not actually return string[] if numbers were put into the map.
*/
export function keysOfMap(map: Map<{}>): string[] {
return arrayFrom(map.keys());
}
/** Array of every value in a map. */
export function valuesOfMap<T>(map: Map<T>): T[] {
return arrayFrom(map.values());
}
/**
* Calls `callback` for each entry in the map, returning the first defined result.
* Use `map.forEach` instead for normal iteration.

View file

@ -680,7 +680,7 @@ namespace ts {
description = getDiagnosticText(option.description);
const element = (<CommandLineOptionOfListType>option).element;
const typeMap = <Map<number | string>>element.type;
optionsDescriptionMap.set(description, keysOfMap(typeMap).map(key => `'${key}'`));
optionsDescriptionMap.set(description, arrayFrom(typeMap.keys()).map(key => `'${key}'`));
}
else {
description = getDiagnosticText(option.description);

View file

@ -246,7 +246,7 @@ namespace ts.projectSystem {
export function checkMapKeys(caption: string, map: Map<any>, expectedKeys: string[]) {
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)}`);
assert.isTrue(map.has(name), `${caption} is expected to contain ${name}, actual keys: ${arrayFrom(map.keys())}`);
}
}

View file

@ -1,4 +1,4 @@
/// <reference path="..\services\services.ts" />
/// <reference path="..\services\services.ts" />
/// <reference path="utilities.ts"/>
/// <reference path="scriptInfo.ts"/>
/// <reference path="lsHost.ts"/>
@ -617,7 +617,7 @@ namespace ts.server {
const added: string[] = [];
const removed: string[] = [];
const updated: string[] = keysOfMap(updatedFileNames);
const updated: string[] = arrayFrom(updatedFileNames.keys());
forEachKeyInMap(currentFiles, id => {
if (!lastReportedFileNames.has(id)) {
@ -691,7 +691,7 @@ namespace ts.server {
})
}
const allFileNames = keysOfMap(referencedFiles) as Path[];
const allFileNames = arrayFrom(referencedFiles.keys()) as Path[];
return filter(allFileNames, file => this.projectService.host.fileExists(file));
}

View file

@ -1,4 +1,4 @@
namespace ts {
namespace ts {
/**
* The document registry represents a store of SourceFile objects that can be shared between
* multiple LanguageService instances. A LanguageService instance holds on the SourceFile (AST)
@ -121,7 +121,7 @@ namespace ts {
}
function reportStats() {
const bucketInfoArray = keysOfMap(buckets).filter(name => name && name.charAt(0) === "_").map(name => {
const bucketInfoArray = arrayFrom(buckets.keys()).filter(name => name && name.charAt(0) === "_").map(name => {
const entries = buckets.get(name);
const sourceFiles: { name: string; refCount: number; references: string[]; }[] = [];
entries.forEachValue((key, entry) => {