@@ -394,9 +394,7 @@ namespace ts {
394394 getTypeChecker,
395395 getClassifiableNames,
396396 getDiagnosticsProducingTypeChecker,
397- getCommonSourceDirectory : ( ) => {
398- return typeof commonSourceDirectory === "undefined" ? ( commonSourceDirectory = computeCommonSourceDirectory ( files ) ) : commonSourceDirectory ;
399- } ,
397+ getCommonSourceDirectory,
400398 emit,
401399 getCurrentDirectory : ( ) => currentDirectory ,
402400 getNodeCount : ( ) => getDiagnosticsProducingTypeChecker ( ) . getNodeCount ( ) ,
@@ -407,6 +405,25 @@ namespace ts {
407405 } ;
408406 return program ;
409407
408+ function getCommonSourceDirectory ( ) {
409+ if ( typeof commonSourceDirectory === "undefined" ) {
410+ if ( options . rootDir && checkSourceFilesBelongToPath ( files , options . rootDir ) ) {
411+ // If a rootDir is specified and is valid use it as the commonSourceDirectory
412+ commonSourceDirectory = getNormalizedAbsolutePath ( options . rootDir , currentDirectory ) ;
413+ }
414+ else {
415+ commonSourceDirectory = computeCommonSourceDirectory ( files ) ;
416+ }
417+ if ( commonSourceDirectory && commonSourceDirectory [ commonSourceDirectory . length - 1 ] !== directorySeparator ) {
418+ // Make sure directory path ends with directory separator so this string can directly
419+ // used to replace with "" to get the relative path of the source file and the relative path doesn't
420+ // start with / making it rooted path
421+ commonSourceDirectory += directorySeparator ;
422+ }
423+ }
424+ return commonSourceDirectory ;
425+ }
426+
410427 function getClassifiableNames ( ) {
411428 if ( ! classifiableNames ) {
412429 // Initialize a checker so that all our files are bound.
@@ -1059,24 +1076,12 @@ namespace ts {
10591076 options . sourceRoot || // there is --sourceRoot specified
10601077 options . mapRoot ) { // there is --mapRoot specified
10611078
1062- if ( options . rootDir && checkSourceFilesBelongToPath ( files , options . rootDir ) ) {
1063- // If a rootDir is specified and is valid use it as the commonSourceDirectory
1064- commonSourceDirectory = getNormalizedAbsolutePath ( options . rootDir , currentDirectory ) ;
1065- }
1066- else {
1067- // Compute the commonSourceDirectory from the input files
1068- commonSourceDirectory = computeCommonSourceDirectory ( files ) ;
1069- // If we failed to find a good common directory, but outDir is specified and at least one of our files is on a windows drive/URL/other resource, add a failure
1070- if ( options . outDir && commonSourceDirectory === "" && forEach ( files , file => getRootLength ( file . fileName ) > 1 ) ) {
1071- programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . Cannot_find_the_common_subdirectory_path_for_the_input_files ) ) ;
1072- }
1073- }
1079+ // Precalculate and cache the common source directory
1080+ const dir = getCommonSourceDirectory ( ) ;
10741081
1075- if ( commonSourceDirectory && commonSourceDirectory [ commonSourceDirectory . length - 1 ] !== directorySeparator ) {
1076- // Make sure directory path ends with directory separator so this string can directly
1077- // used to replace with "" to get the relative path of the source file and the relative path doesn't
1078- // start with / making it rooted path
1079- commonSourceDirectory += directorySeparator ;
1082+ // If we failed to find a good common directory, but outDir is specified and at least one of our files is on a windows drive/URL/other resource, add a failure
1083+ if ( options . outDir && dir === "" && forEach ( files , file => getRootLength ( file . fileName ) > 1 ) ) {
1084+ programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . Cannot_find_the_common_subdirectory_path_for_the_input_files ) ) ;
10801085 }
10811086 }
10821087
0 commit comments