Skip to content

Commit 949c517

Browse files
author
Andy Hanson
committed
Add multiMapAdd helper
1 parent d2d5d42 commit 949c517

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
@@ -6847,7 +6847,7 @@ const _super = (function (geti, seti) {
68476847
// export { x, y }
68486848
for (const specifier of (<ExportDeclaration>node).exportClause.elements) {
68496849
const name = (specifier.propertyName || specifier.name).text;
6850-
(exportSpecifiers[name] || (exportSpecifiers[name] = [])).push(specifier);
6850+
multiMapAdd(exportSpecifiers, name, specifier);
68516851
}
68526852
}
68536853
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
@@ -1635,8 +1635,7 @@ namespace FourSlash {
16351635
const result = ts.createMap<Range[]>();
16361636
for (const range of this.getRanges()) {
16371637
const text = this.rangeText(range);
1638-
const ranges = result[text] || (result[text] = []);
1639-
ranges.push(range);
1638+
ts.multiMapAdd(result, text, range);
16401639
}
16411640
return result;
16421641
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ 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] = []);
202-
callbacks.push({ cb: callback, recursive });
201+
const callbacks = multiMapAdd(this.watchedDirectories, path, { cb: callback, recursive });
203202
return {
204203
referenceCount: 0,
205204
directoryName,
@@ -239,8 +238,7 @@ namespace ts {
239238

240239
watchFile(fileName: string, callback: FileWatcherCallback) {
241240
const path = this.toPath(fileName);
242-
const callbacks = this.watchedFiles[path] || (this.watchedFiles[path] = []);
243-
callbacks.push(callback);
241+
const callbacks = multiMapAdd(this.watchedFiles, path, callback);
244242
return {
245243
close: () => {
246244
const i = callbacks.indexOf(callback);

src/services/services.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,7 @@ namespace ts {
984984
function addDeclaration(declaration: Declaration) {
985985
const name = getDeclarationName(declaration);
986986
if (name) {
987-
const declarations = getDeclarations(name);
988-
declarations.push(declaration);
987+
multiMapAdd(result, name, declaration);
989988
}
990989
}
991990

0 commit comments

Comments
 (0)