From 72239456667645cf5bd9a6fc06a057c42b25ff0d Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 27 Aug 2018 17:03:30 -0700 Subject: [PATCH] Use array helper in computeCommonSourceDirectory and remove two unnecessary tests (#26416) --- src/compiler/program.ts | 25 +++++++++---------------- src/compiler/utilities.ts | 2 -- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index abbc935cf65..670e2c200d8 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2319,27 +2319,20 @@ namespace ts { } function computeCommonSourceDirectory(sourceFiles: SourceFile[]): string { - const fileNames: string[] = []; - for (const file of sourceFiles) { - if (!file.isDeclarationFile) { - fileNames.push(file.fileName); - } - } + const fileNames = mapDefined(sourceFiles, file => file.isDeclarationFile ? undefined : file.fileName); return computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName); } - function checkSourceFilesBelongToPath(sourceFiles: SourceFile[], rootDirectory: string): boolean { + function checkSourceFilesBelongToPath(sourceFiles: ReadonlyArray, rootDirectory: string): boolean { let allFilesBelongToPath = true; - if (sourceFiles) { - const absoluteRootDirectoryPath = host.getCanonicalFileName(getNormalizedAbsolutePath(rootDirectory, currentDirectory)); + const absoluteRootDirectoryPath = host.getCanonicalFileName(getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (const sourceFile of sourceFiles) { - if (!sourceFile.isDeclarationFile) { - const absoluteSourceFilePath = host.getCanonicalFileName(getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); - if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files, sourceFile.fileName, rootDirectory)); - allFilesBelongToPath = false; - } + for (const sourceFile of sourceFiles) { + if (!sourceFile.isDeclarationFile) { + const absoluteSourceFilePath = host.getCanonicalFileName(getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); + if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files, sourceFile.fileName, rootDirectory)); + allFilesBelongToPath = false; } } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6b905ad65ca..39a298329c7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -7302,8 +7302,6 @@ namespace ts { if (pathComponents.length === 0) return ""; const root = pathComponents[0] && ensureTrailingDirectorySeparator(pathComponents[0]); - if (pathComponents.length === 1) return root; - return root + pathComponents.slice(1).join(directorySeparator); }