Deduplicate unresolvedImports (#17248)

* Deduplicate unresolvedImports

* Add `isNonDuplicateInSortedArray` helper
This commit is contained in:
Andy
2017-08-08 11:02:10 -07:00
committed by GitHub
parent f69ce5c0c8
commit 5141ce751d
4 changed files with 16 additions and 7 deletions

View File

@@ -361,11 +361,11 @@ namespace ts {
return false;
}
export function filterMutate<T>(array: T[], f: (x: T) => boolean): void {
export function filterMutate<T>(array: T[], f: (x: T, i: number, array: T[]) => boolean): void {
let outIndex = 0;
for (const item of array) {
if (f(item)) {
array[outIndex] = item;
for (let i = 0; i < array.length; i++) {
if (f(array[i], i, array)) {
array[outIndex] = array[i];
outIndex++;
}
}

View File

@@ -495,7 +495,7 @@ namespace ts.server {
this.projectStateVersion++;
}
private extractUnresolvedImportsFromSourceFile(file: SourceFile, result: string[]) {
private extractUnresolvedImportsFromSourceFile(file: SourceFile, result: Push<string>) {
const cached = this.cachedUnresolvedImportsPerFile.get(file.path);
if (cached) {
// found cached result - use it and return
@@ -555,7 +555,7 @@ namespace ts.server {
for (const sourceFile of this.program.getSourceFiles()) {
this.extractUnresolvedImportsFromSourceFile(sourceFile, result);
}
this.lastCachedUnresolvedImportsList = toSortedArray(result);
this.lastCachedUnresolvedImportsList = toDeduplicatedSortedArray(result);
}
unresolvedImports = this.lastCachedUnresolvedImportsList;

View File

@@ -262,6 +262,15 @@ namespace ts.server {
return arr as SortedArray<T>;
}
export function toDeduplicatedSortedArray(arr: string[]): SortedArray<string> {
arr.sort();
filterMutate(arr, isNonDuplicateInSortedArray);
return arr as SortedArray<string>;
}
function isNonDuplicateInSortedArray<T>(value: T, index: number, array: T[]) {
return index === 0 || value !== array[index - 1];
}
export function enumerateInsertsAndDeletes<T>(newItems: SortedReadonlyArray<T>, oldItems: SortedReadonlyArray<T>, inserted: (newItem: T) => void, deleted: (oldItem: T) => void, compare?: Comparer<T>) {
compare = compare || compareValues;
let newIndex = 0;

View File

@@ -35,7 +35,7 @@ namespace ts.JsTyping {
"crypto", "stream", "util", "assert", "tty", "domain",
"constants", "process", "v8", "timers", "console"];
const nodeCoreModules = arrayToMap(<string[]>nodeCoreModuleList, x => x);
const nodeCoreModules = arrayToSet(nodeCoreModuleList);
/**
* A map of loose file names to library names that we are confident require typings