@@ -709,89 +709,25 @@ func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) co
709
709
710
710
func (p * Program ) CommonSourceDirectory () string {
711
711
p .commonSourceDirectoryOnce .Do (func () {
712
- var files []string
713
- host := & emitHost {program : p }
714
- for _ , file := range p .files {
715
- if sourceFileMayBeEmitted (file , host , false /*forceDtsEmit*/ ) {
716
- files = append (files , file .FileName ())
717
- }
718
- }
719
- p .commonSourceDirectory = getCommonSourceDirectory (
712
+ p .commonSourceDirectory = outputpaths .GetCommonSourceDirectory (
720
713
p .Options (),
721
- files ,
714
+ func () []string {
715
+ var files []string
716
+ host := & emitHost {program : p }
717
+ for _ , file := range p .files {
718
+ if sourceFileMayBeEmitted (file , host , false /*forceDtsEmit*/ ) {
719
+ files = append (files , file .FileName ())
720
+ }
721
+ }
722
+ return files
723
+ },
722
724
p .GetCurrentDirectory (),
723
725
p .UseCaseSensitiveFileNames (),
724
726
)
725
727
})
726
728
return p .commonSourceDirectory
727
729
}
728
730
729
- func computeCommonSourceDirectoryOfFilenames (fileNames []string , currentDirectory string , useCaseSensitiveFileNames bool ) string {
730
- var commonPathComponents []string
731
- for _ , sourceFile := range fileNames {
732
- // Each file contributes into common source file path
733
- sourcePathComponents := tspath .GetNormalizedPathComponents (sourceFile , currentDirectory )
734
-
735
- // The base file name is not part of the common directory path
736
- sourcePathComponents = sourcePathComponents [:len (sourcePathComponents )- 1 ]
737
-
738
- if commonPathComponents == nil {
739
- // first file
740
- commonPathComponents = sourcePathComponents
741
- continue
742
- }
743
-
744
- n := min (len (commonPathComponents ), len (sourcePathComponents ))
745
- for i := range n {
746
- if tspath .GetCanonicalFileName (commonPathComponents [i ], useCaseSensitiveFileNames ) != tspath .GetCanonicalFileName (sourcePathComponents [i ], useCaseSensitiveFileNames ) {
747
- if i == 0 {
748
- // Failed to find any common path component
749
- return ""
750
- }
751
-
752
- // New common path found that is 0 -> i-1
753
- commonPathComponents = commonPathComponents [:i ]
754
- break
755
- }
756
- }
757
-
758
- // If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents
759
- if len (sourcePathComponents ) < len (commonPathComponents ) {
760
- commonPathComponents = commonPathComponents [:len (sourcePathComponents )]
761
- }
762
- }
763
-
764
- if len (commonPathComponents ) == 0 {
765
- // Can happen when all input files are .d.ts files
766
- return currentDirectory
767
- }
768
-
769
- return tspath .GetPathFromPathComponents (commonPathComponents )
770
- }
771
-
772
- func getCommonSourceDirectory (options * core.CompilerOptions , files []string , currentDirectory string , useCaseSensitiveFileNames bool ) string {
773
- var commonSourceDirectory string
774
- if options .RootDir != "" {
775
- // If a rootDir is specified use it as the commonSourceDirectory
776
- commonSourceDirectory = options .RootDir
777
- } else if options .Composite .IsTrue () && options .ConfigFilePath != "" {
778
- // If the rootDir is not specified, but the project is composite, then the common source directory
779
- // is the directory of the config file.
780
- commonSourceDirectory = tspath .GetDirectoryPath (options .ConfigFilePath )
781
- } else {
782
- commonSourceDirectory = computeCommonSourceDirectoryOfFilenames (files , currentDirectory , useCaseSensitiveFileNames )
783
- }
784
-
785
- if len (commonSourceDirectory ) > 0 {
786
- // Make sure directory path ends with directory separator so this string can directly
787
- // used to replace with "" to get the relative path of the source file and the relative path doesn't
788
- // start with / making it rooted path
789
- commonSourceDirectory = tspath .EnsureTrailingDirectorySeparator (commonSourceDirectory )
790
- }
791
-
792
- return commonSourceDirectory
793
- }
794
-
795
731
type EmitOptions struct {
796
732
TargetSourceFile * ast.SourceFile // Single file to emit. If `nil`, emits all files
797
733
forceDtsEmit bool
0 commit comments