Use removal helpers in more places

This commit is contained in:
Andy Hanson 2016-08-15 14:01:52 -07:00
parent 5ad7729357
commit de6707e1d5
5 changed files with 25 additions and 24 deletions

View file

@ -5336,7 +5336,7 @@ namespace ts {
while (i > 0) {
i--;
if (isSubtypeOfAny(types[i], types)) {
types.splice(i, 1);
removeItemAt(types, i);
}
}
}

View file

@ -1394,15 +1394,25 @@ namespace ts {
}
}
/** Remove an item from an array, moving everything to its right one space left. */
export function removeItemAt<T>(array: T[], index: number): void {
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
for (let i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.pop();
}
/** Remove the *first* occurrence of `item` from the array. */
export function removeItem<T>(item: T, array: T[]): void {
removeFirstItemWhere(array, element => element === item);
}
/** Remove the *first* element satisfying `predicate`. */
export function removeFirstItemWhere<T>(array: T[], predicate: (element: T) => boolean): void {
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
// Move everything over to the left.
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
for (; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.pop();
if (predicate(array[i])) {
removeItemAt(array, i);
break;
}
}

View file

@ -490,10 +490,7 @@ namespace ts {
sourceFile.fileWatcher.close();
sourceFile.fileWatcher = undefined;
if (removed) {
const index = rootFileNames.indexOf(sourceFile.fileName);
if (index >= 0) {
rootFileNames.splice(index, 1);
}
removeItem(sourceFile.fileName, rootFileNames);
}
startTimerForRecompilation();
}

View file

@ -1558,7 +1558,7 @@ namespace Harness {
tsConfig.options.configFilePath = data.name;
// delete entry from the list
testUnitData.splice(i, 1);
ts.removeItemAt(testUnitData, i);
break;
}

View file

@ -214,12 +214,7 @@ namespace ts {
referenceCount: 0,
directoryName,
close: () => {
for (let i = 0; i < callbacks.length; i++) {
if (callbacks[i].cb === callback) {
callbacks.splice(i, 1);
break;
}
}
removeFirstItemWhere(callbacks, cb => cb.cb === callback);
if (!callbacks.length) {
delete this.watchedDirectories[path];
}
@ -253,8 +248,7 @@ namespace ts {
callbacks.push(callback);
return {
close: () => {
const i = callbacks.indexOf(callback);
callbacks.splice(i, 1);
removeItem(callback, callbacks);
if (!callbacks.length) {
delete this.watchedFiles[path];
}
@ -269,7 +263,7 @@ namespace ts {
};
readonly clearTimeout = (timeoutId: any): void => {
if (typeof timeoutId === "number") {
this.callbackQueue.splice(timeoutId, 1);
removeItemAt(this.callbackQueue, timeoutId);
}
};
@ -594,7 +588,7 @@ namespace ts {
content: `{
"compilerOptions": {
"target": "es6"
},
},
"files": [ "main.ts" ]
}`
};
@ -621,7 +615,7 @@ namespace ts {
content: `{
"compilerOptions": {
"target": "es6"
},
},
"files": [ "main.ts" ]
}`
};