From 949c517fdd1526eaadc70f906cd98829db97e4e5 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 15 Aug 2016 13:45:33 -0700 Subject: [PATCH] Add `multiMapAdd` helper --- src/compiler/core.ts | 15 +++++++++++++++ src/compiler/emitter.ts | 2 +- src/compiler/sys.ts | 2 +- src/harness/fourslash.ts | 3 +-- src/harness/unittests/tsserverProjectSystem.ts | 6 ++---- src/services/services.ts | 3 +-- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 27b27bc653292..8ac46f9df0df6 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -566,6 +566,21 @@ namespace ts { return result; } + /** + * Adds the value to an array of values associated with the key, and returns the array. + * Creates the array if it does not already exist. + */ + export function multiMapAdd(map: Map, key: string, value: V): V[] { + const values = map[key]; + if (values) { + values.push(value); + return values; + } + else { + return map[key] = [value]; + } + } + /** * Tests whether a value is an array. */ diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 357a15507a457..74553dac20e04 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -6847,7 +6847,7 @@ const _super = (function (geti, seti) { // export { x, y } for (const specifier of (node).exportClause.elements) { const name = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name] || (exportSpecifiers[name] = [])).push(specifier); + multiMapAdd(exportSpecifiers, name, specifier); } } break; diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 350d75429b754..458cbc99a4758 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -267,7 +267,7 @@ namespace ts { } function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void { - (fileWatcherCallbacks[filePath] || (fileWatcherCallbacks[filePath] = [])).push(callback); + multiMapAdd(fileWatcherCallbacks, filePath, callback); } function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile { diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index b5fa53763cb9d..aece7b5c3ec7b 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1635,8 +1635,7 @@ namespace FourSlash { const result = ts.createMap(); for (const range of this.getRanges()) { const text = this.rangeText(range); - const ranges = result[text] || (result[text] = []); - ranges.push(range); + ts.multiMapAdd(result, text, range); } return result; } diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 49f033b9defcb..89def128f9e16 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -198,8 +198,7 @@ namespace ts { watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher { const path = this.toPath(directoryName); - const callbacks = this.watchedDirectories[path] || (this.watchedDirectories[path] = []); - callbacks.push({ cb: callback, recursive }); + const callbacks = multiMapAdd(this.watchedDirectories, path, { cb: callback, recursive }); return { referenceCount: 0, directoryName, @@ -239,8 +238,7 @@ namespace ts { watchFile(fileName: string, callback: FileWatcherCallback) { const path = this.toPath(fileName); - const callbacks = this.watchedFiles[path] || (this.watchedFiles[path] = []); - callbacks.push(callback); + const callbacks = multiMapAdd(this.watchedFiles, path, callback); return { close: () => { const i = callbacks.indexOf(callback); diff --git a/src/services/services.ts b/src/services/services.ts index 9ef14315cb6e9..1c28495dcac40 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -984,8 +984,7 @@ namespace ts { function addDeclaration(declaration: Declaration) { const name = getDeclarationName(declaration); if (name) { - const declarations = getDeclarations(name); - declarations.push(declaration); + multiMapAdd(result, name, declaration); } }