From 5141ce751d9887a8b402a34d66c63581c17aed00 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 8 Aug 2017 11:02:10 -0700 Subject: [PATCH] Deduplicate unresolvedImports (#17248) * Deduplicate unresolvedImports * Add `isNonDuplicateInSortedArray` helper --- src/compiler/core.ts | 8 ++++---- src/server/project.ts | 4 ++-- src/server/utilities.ts | 9 +++++++++ src/services/jsTyping.ts | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index bc131b201fd..d5c8396d06d 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -361,11 +361,11 @@ namespace ts { return false; } - export function filterMutate(array: T[], f: (x: T) => boolean): void { + export function filterMutate(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++; } } diff --git a/src/server/project.ts b/src/server/project.ts index 844ded761d8..97c8eb706f3 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -495,7 +495,7 @@ namespace ts.server { this.projectStateVersion++; } - private extractUnresolvedImportsFromSourceFile(file: SourceFile, result: string[]) { + private extractUnresolvedImportsFromSourceFile(file: SourceFile, result: Push) { 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; diff --git a/src/server/utilities.ts b/src/server/utilities.ts index fc88f11408a..5efb20d074f 100644 --- a/src/server/utilities.ts +++ b/src/server/utilities.ts @@ -262,6 +262,15 @@ namespace ts.server { return arr as SortedArray; } + export function toDeduplicatedSortedArray(arr: string[]): SortedArray { + arr.sort(); + filterMutate(arr, isNonDuplicateInSortedArray); + return arr as SortedArray; + } + function isNonDuplicateInSortedArray(value: T, index: number, array: T[]) { + return index === 0 || value !== array[index - 1]; + } + export function enumerateInsertsAndDeletes(newItems: SortedReadonlyArray, oldItems: SortedReadonlyArray, inserted: (newItem: T) => void, deleted: (oldItem: T) => void, compare?: Comparer) { compare = compare || compareValues; let newIndex = 0; diff --git a/src/services/jsTyping.ts b/src/services/jsTyping.ts index 4de7bb3191d..5e7b7d424f8 100644 --- a/src/services/jsTyping.ts +++ b/src/services/jsTyping.ts @@ -35,7 +35,7 @@ namespace ts.JsTyping { "crypto", "stream", "util", "assert", "tty", "domain", "constants", "process", "v8", "timers", "console"]; - const nodeCoreModules = arrayToMap(nodeCoreModuleList, x => x); + const nodeCoreModules = arrayToSet(nodeCoreModuleList); /** * A map of loose file names to library names that we are confident require typings