Skip to content

Commit 3bcfb6b

Browse files
author
Andy
authored
Merge pull request #10434 from Microsoft/multi_map_add
Add `multiMapAdd` helper
2 parents 8038eb9 + 13b63c5 commit 3bcfb6b

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

src/compiler/core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,21 @@ namespace ts {
566566
return result;
567567
}
568568

569+
/**
570+
* Adds the value to an array of values associated with the key, and returns the array.
571+
* Creates the array if it does not already exist.
572+
*/
573+
export function multiMapAdd<V>(map: Map<V[]>, key: string, value: V): V[] {
574+
const values = map[key];
575+
if (values) {
576+
values.push(value);
577+
return values;
578+
}
579+
else {
580+
return map[key] = [value];
581+
}
582+
}
583+
569584
/**
570585
* Tests whether a value is an array.
571586
*/

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6851,7 +6851,7 @@ const _super = (function (geti, seti) {
68516851
// export { x, y }
68526852
for (const specifier of (<ExportDeclaration>node).exportClause.elements) {
68536853
const name = (specifier.propertyName || specifier.name).text;
6854-
(exportSpecifiers[name] || (exportSpecifiers[name] = [])).push(specifier);
6854+
multiMapAdd(exportSpecifiers, name, specifier);
68556855
}
68566856
}
68576857
break;

src/compiler/sys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace ts {
267267
}
268268

269269
function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void {
270-
(fileWatcherCallbacks[filePath] || (fileWatcherCallbacks[filePath] = [])).push(callback);
270+
multiMapAdd(fileWatcherCallbacks, filePath, callback);
271271
}
272272

273273
function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile {

src/harness/fourslash.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,8 +1649,7 @@ namespace FourSlash {
16491649
const result = ts.createMap<Range[]>();
16501650
for (const range of this.getRanges()) {
16511651
const text = this.rangeText(range);
1652-
const ranges = result[text] || (result[text] = []);
1653-
ranges.push(range);
1652+
ts.multiMapAdd(result, text, range);
16541653
}
16551654
return result;
16561655
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ namespace ts {
198198

199199
watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher {
200200
const path = this.toPath(directoryName);
201-
const callbacks = this.watchedDirectories[path] || (this.watchedDirectories[path] = []);
202201
const cbWithRecursive = { cb: callback, recursive };
203-
callbacks.push(cbWithRecursive);
202+
const callbacks = multiMapAdd(this.watchedDirectories, path, cbWithRecursive);
204203
return {
205204
referenceCount: 0,
206205
directoryName,
@@ -235,8 +234,7 @@ namespace ts {
235234

236235
watchFile(fileName: string, callback: FileWatcherCallback) {
237236
const path = this.toPath(fileName);
238-
const callbacks = this.watchedFiles[path] || (this.watchedFiles[path] = []);
239-
callbacks.push(callback);
237+
const callbacks = multiMapAdd(this.watchedFiles, path, callback);
240238
return {
241239
close: () => {
242240
unorderedRemoveItem(callbacks, callback);

src/services/services.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,7 @@ namespace ts {
987987
function addDeclaration(declaration: Declaration) {
988988
const name = getDeclarationName(declaration);
989989
if (name) {
990-
const declarations = getDeclarations(name);
991-
declarations.push(declaration);
990+
multiMapAdd(result, name, declaration);
992991
}
993992
}
994993

0 commit comments

Comments
 (0)