Add multiMapAdd helper

This commit is contained in:
Andy Hanson 2016-08-15 13:45:33 -07:00
parent d2d5d42359
commit 949c517fdd
6 changed files with 21 additions and 10 deletions

View file

@ -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<V>(map: Map<V[]>, 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.
*/

View file

@ -6847,7 +6847,7 @@ const _super = (function (geti, seti) {
// export { x, y }
for (const specifier of (<ExportDeclaration>node).exportClause.elements) {
const name = (specifier.propertyName || specifier.name).text;
(exportSpecifiers[name] || (exportSpecifiers[name] = [])).push(specifier);
multiMapAdd(exportSpecifiers, name, specifier);
}
}
break;

View file

@ -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 {

View file

@ -1635,8 +1635,7 @@ namespace FourSlash {
const result = ts.createMap<Range[]>();
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;
}

View file

@ -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);

View file

@ -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);
}
}