From e7e1fa72ec198f63d794abb296c4befb8b823dbb Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 16 Oct 2015 12:00:31 -0700 Subject: [PATCH] Add sortBeforeComparison option back to arrayIsEqualTo --- src/compiler/tsc.ts | 3 ++- src/compiler/utilities.ts | 16 ++++++++++------ src/server/editorServices.ts | 3 ++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 5420caabe7c..b4774b9a842 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -360,7 +360,8 @@ namespace ts { let newFileNames = ts.map(parsedCommandLine.fileNames, compilerHost.getCanonicalFileName); let canonicalRootFileNames = ts.map(rootFileNames, compilerHost.getCanonicalFileName); - if (!arrayIsEqualTo(newFileNames.sort(), canonicalRootFileNames.sort())) { + // We check if the project file list has changed. If so, we just throw away the old program and start fresh. + if (!arrayIsEqualTo(newFileNames, canonicalRootFileNames, /*equaler*/ undefined, /*sortBeforeComparison*/ true)) { setCachedProgram(undefined); startTimerForRecompilation(); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2a16c2c71fb..b56c63013aa 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -82,17 +82,21 @@ namespace ts { return node.end - node.pos; } - export function arrayIsEqualTo(arr1: T[], arr2: T[], comparer?: (a: T, b: T) => boolean): boolean { - if (!arr1 || !arr2) { - return arr1 === arr2; + export function arrayIsEqualTo(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean, + sortBeforeComparison?: boolean, comparer?: (a: T, b: T) => number): boolean { + if (!array1 || !array2) { + return array1 === array2; } - if (arr1.length !== arr2.length) { + if (array1.length !== array2.length) { return false; } - for (let i = 0; i < arr1.length; ++i) { - let equals = comparer ? comparer(arr1[i], arr2[i]) : arr1[i] === arr2[i]; + let newArray1 = sortBeforeComparison ? array1.slice().sort(comparer) : array1; + let newArray2 = sortBeforeComparison ? array2.slice().sort(comparer) : array2; + + for (let i = 0; i < array1.length; ++i) { + let equals = equaler ? equaler(newArray1[i], newArray2[i]) : newArray1[i] === newArray2[i]; if (!equals) { return false; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index b98129f99e9..7cb047bb74d 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -576,7 +576,8 @@ namespace ts.server { let newRootFiles = projectOptions.files.map((f => this.getCanonicalFileName(f))); let currentRootFiles = project.getRootFiles().map((f => this.getCanonicalFileName(f))); - if (!arrayIsEqualTo(currentRootFiles.sort(), newRootFiles.sort())) { + // We check if the project file list has changed. If so, we update the project. + if (!arrayIsEqualTo(currentRootFiles, newRootFiles, /*equaler*/ undefined, /*sortBeforeComparison*/ true)) { // For configured projects, the change is made outside the tsconfig file, and // it is not likely to affect the project for other files opened by the client. We can // just update the current project.