Skip to content

Commit de6707e

Browse files
author
Andy Hanson
committed
Use removal helpers in more places
1 parent 5ad7729 commit de6707e

5 files changed

Lines changed: 25 additions & 24 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5336,7 +5336,7 @@ namespace ts {
53365336
while (i > 0) {
53375337
i--;
53385338
if (isSubtypeOfAny(types[i], types)) {
5339-
types.splice(i, 1);
5339+
removeItemAt(types, i);
53405340
}
53415341
}
53425342
}

src/compiler/core.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,15 +1394,25 @@ namespace ts {
13941394
}
13951395
}
13961396

1397+
/** Remove an item from an array, moving everything to its right one space left. */
1398+
export function removeItemAt<T>(array: T[], index: number): void {
1399+
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
1400+
for (let i = index; i < array.length - 1; i++) {
1401+
array[i] = array[i + 1];
1402+
}
1403+
array.pop();
1404+
}
1405+
1406+
/** Remove the *first* occurrence of `item` from the array. */
13971407
export function removeItem<T>(item: T, array: T[]): void {
1408+
removeFirstItemWhere(array, element => element === item);
1409+
}
1410+
1411+
/** Remove the *first* element satisfying `predicate`. */
1412+
export function removeFirstItemWhere<T>(array: T[], predicate: (element: T) => boolean): void {
13981413
for (let i = 0; i < array.length; i++) {
1399-
if (array[i] === item) {
1400-
// Move everything over to the left.
1401-
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
1402-
for (; i < array.length - 1; i++) {
1403-
array[i] = array[i + 1];
1404-
}
1405-
array.pop();
1414+
if (predicate(array[i])) {
1415+
removeItemAt(array, i);
14061416
break;
14071417
}
14081418
}

src/compiler/tsc.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,7 @@ namespace ts {
490490
sourceFile.fileWatcher.close();
491491
sourceFile.fileWatcher = undefined;
492492
if (removed) {
493-
const index = rootFileNames.indexOf(sourceFile.fileName);
494-
if (index >= 0) {
495-
rootFileNames.splice(index, 1);
496-
}
493+
removeItem(sourceFile.fileName, rootFileNames);
497494
}
498495
startTimerForRecompilation();
499496
}

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ namespace Harness {
15581558
tsConfig.options.configFilePath = data.name;
15591559

15601560
// delete entry from the list
1561-
testUnitData.splice(i, 1);
1561+
ts.removeItemAt(testUnitData, i);
15621562

15631563
break;
15641564
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,7 @@ namespace ts {
214214
referenceCount: 0,
215215
directoryName,
216216
close: () => {
217-
for (let i = 0; i < callbacks.length; i++) {
218-
if (callbacks[i].cb === callback) {
219-
callbacks.splice(i, 1);
220-
break;
221-
}
222-
}
217+
removeFirstItemWhere(callbacks, cb => cb.cb === callback);
223218
if (!callbacks.length) {
224219
delete this.watchedDirectories[path];
225220
}
@@ -253,8 +248,7 @@ namespace ts {
253248
callbacks.push(callback);
254249
return {
255250
close: () => {
256-
const i = callbacks.indexOf(callback);
257-
callbacks.splice(i, 1);
251+
removeItem(callback, callbacks);
258252
if (!callbacks.length) {
259253
delete this.watchedFiles[path];
260254
}
@@ -269,7 +263,7 @@ namespace ts {
269263
};
270264
readonly clearTimeout = (timeoutId: any): void => {
271265
if (typeof timeoutId === "number") {
272-
this.callbackQueue.splice(timeoutId, 1);
266+
removeItemAt(this.callbackQueue, timeoutId);
273267
}
274268
};
275269

@@ -594,7 +588,7 @@ namespace ts {
594588
content: `{
595589
"compilerOptions": {
596590
"target": "es6"
597-
},
591+
},
598592
"files": [ "main.ts" ]
599593
}`
600594
};
@@ -621,7 +615,7 @@ namespace ts {
621615
content: `{
622616
"compilerOptions": {
623617
"target": "es6"
624-
},
618+
},
625619
"files": [ "main.ts" ]
626620
}`
627621
};

0 commit comments

Comments
 (0)