diff --git a/src/compiler/core.ts b/src/compiler/core.ts index eb17740277119..092fa597e327d 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 f02cf2667444b..90cc06fa4be9e 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -6851,7 +6851,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 118186bf52a51..f380401c3b83b 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 c51b7e874f772..bd35923022087 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1649,8 +1649,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 6814305e6ca12..a5648d999704f 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -198,9 +198,8 @@ namespace ts { watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher { const path = this.toPath(directoryName); - const callbacks = this.watchedDirectories[path] || (this.watchedDirectories[path] = []); const cbWithRecursive = { cb: callback, recursive }; - callbacks.push(cbWithRecursive); + const callbacks = multiMapAdd(this.watchedDirectories, path, cbWithRecursive); return { referenceCount: 0, directoryName, @@ -235,8 +234,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: () => { unorderedRemoveItem(callbacks, callback); diff --git a/src/services/services.ts b/src/services/services.ts index c97455c324406..f38fa8eb3a3bc 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -987,8 +987,7 @@ namespace ts { function addDeclaration(declaration: Declaration) { const name = getDeclarationName(declaration); if (name) { - const declarations = getDeclarations(name); - declarations.push(declaration); + multiMapAdd(result, name, declaration); } }