Skip to content

Commit e7895c5

Browse files
committed
Move commonSourceDirectory computation to its own function
1 parent db492f1 commit e7895c5

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

src/compiler/program.ts

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,51 @@ module ts {
453453
}
454454
}
455455

456+
function computecommonSourceDirectory(sourceFiles: SourceFile[]): string {
457+
let commonPathComponents: string[];
458+
forEach(files, sourceFile => {
459+
// Each file contributes into common source file path
460+
if (!(sourceFile.flags & NodeFlags.DeclarationFile)
461+
&& !fileExtensionIs(sourceFile.fileName, ".js")) {
462+
let sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, host.getCurrentDirectory());
463+
sourcePathComponents.pop(); // FileName is not part of directory
464+
if (commonPathComponents) {
465+
for (let i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) {
466+
if (commonPathComponents[i] !== sourcePathComponents[i]) {
467+
if (i === 0) {
468+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files));
469+
return;
470+
}
471+
472+
// New common path found that is 0 -> i-1
473+
commonPathComponents.length = i;
474+
break;
475+
}
476+
}
477+
478+
// If the fileComponent path completely matched and less than already found update the length
479+
if (sourcePathComponents.length < commonPathComponents.length) {
480+
commonPathComponents.length = sourcePathComponents.length;
481+
}
482+
}
483+
else {
484+
// first file
485+
commonPathComponents = sourcePathComponents;
486+
}
487+
}
488+
});
489+
490+
let commonSourceDirectory = getNormalizedPathFromPathComponents(commonPathComponents);
491+
if (commonSourceDirectory) {
492+
// Make sure directory path ends with directory separator so this string can directly
493+
// used to replace with "" to get the relative path of the source file and the relative path doesn't
494+
// start with / making it rooted path
495+
commonSourceDirectory += directorySeparator;
496+
}
497+
498+
return commonSourceDirectory;
499+
}
500+
456501
function verifyCompilerOptions() {
457502
if (options.separateCompilation) {
458503
if (options.sourceMap) {
@@ -515,46 +560,7 @@ module ts {
515560
(options.mapRoot && // there is --mapRoot specified and there would be multiple js files generated
516561
(!options.out || firstExternalModuleSourceFile !== undefined))) {
517562

518-
let commonPathComponents: string[];
519-
forEach(files, sourceFile => {
520-
// Each file contributes into common source file path
521-
if (!(sourceFile.flags & NodeFlags.DeclarationFile)
522-
&& !fileExtensionIs(sourceFile.fileName, ".js")) {
523-
let sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, host.getCurrentDirectory());
524-
sourcePathComponents.pop(); // FileName is not part of directory
525-
if (commonPathComponents) {
526-
for (let i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) {
527-
if (commonPathComponents[i] !== sourcePathComponents[i]) {
528-
if (i === 0) {
529-
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files));
530-
return;
531-
}
532-
533-
// New common path found that is 0 -> i-1
534-
commonPathComponents.length = i;
535-
break;
536-
}
537-
}
538-
539-
// If the fileComponent path completely matched and less than already found update the length
540-
if (sourcePathComponents.length < commonPathComponents.length) {
541-
commonPathComponents.length = sourcePathComponents.length;
542-
}
543-
}
544-
else {
545-
// first file
546-
commonPathComponents = sourcePathComponents;
547-
}
548-
}
549-
});
550-
551-
commonSourceDirectory = getNormalizedPathFromPathComponents(commonPathComponents);
552-
if (commonSourceDirectory) {
553-
// Make sure directory path ends with directory separator so this string can directly
554-
// used to replace with "" to get the relative path of the source file and the relative path doesn't
555-
// start with / making it rooted path
556-
commonSourceDirectory += directorySeparator;
557-
}
563+
commonSourceDirectory = computecommonSourceDirectory(files);
558564
}
559565

560566
if (options.noEmit) {

0 commit comments

Comments
 (0)