From 39c19a74eae41dc83f44885bfa740d50e7b17c81 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 28 Dec 2016 09:05:52 -0800 Subject: [PATCH] Inline `keysOfMap` and `valuesOfMap`. --- src/compiler/commandLineParser.ts | 8 ++++---- src/compiler/core.ts | 18 +++--------------- src/compiler/tsc.ts | 2 +- src/harness/unittests/tsserverProjectSystem.ts | 2 +- src/server/project.ts | 6 +++--- src/services/documentRegistry.ts | 4 ++-- 6 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index a32a5137bad..7bf50f675aa 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1,4 +1,4 @@ -/// +/// /// /// /// @@ -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), diff --git a/src/compiler/core.ts b/src/compiler/core.ts index e9dbdb8786a..9f8f1ba3f55 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -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(iterator: Iterator): T[] { + /** Shims `Array.from`. */ + export function arrayFrom(iterator: Iterator): 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(map: Map): 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. diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index ba5a0d8faea..a0e7d5ba323 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -680,7 +680,7 @@ namespace ts { description = getDiagnosticText(option.description); const element = (option).element; const typeMap = >element.type; - optionsDescriptionMap.set(description, keysOfMap(typeMap).map(key => `'${key}'`)); + optionsDescriptionMap.set(description, arrayFrom(typeMap.keys()).map(key => `'${key}'`)); } else { description = getDiagnosticText(option.description); diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 4ae2dc7a389..caa51cd5566 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -246,7 +246,7 @@ namespace ts.projectSystem { export function checkMapKeys(caption: string, map: Map, 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())}`); } } diff --git a/src/server/project.ts b/src/server/project.ts index 505eb2f63fb..054b484392e 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1,4 +1,4 @@ -/// +/// /// /// /// @@ -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)); } diff --git a/src/services/documentRegistry.ts b/src/services/documentRegistry.ts index 4291abf6c1f..80b0133ccfe 100644 --- a/src/services/documentRegistry.ts +++ b/src/services/documentRegistry.ts @@ -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) => {