Skip to content

Commit 0ad0def

Browse files
committed
Move common source directory computation to output paths
1 parent 494efba commit 0ad0def

File tree

2 files changed

+83
-75
lines changed

2 files changed

+83
-75
lines changed

internal/compiler/program.go

Lines changed: 11 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -709,89 +709,25 @@ func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) co
709709

710710
func (p *Program) CommonSourceDirectory() string {
711711
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(
720713
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+
},
722724
p.GetCurrentDirectory(),
723725
p.UseCaseSensitiveFileNames(),
724726
)
725727
})
726728
return p.commonSourceDirectory
727729
}
728730

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-
795731
type EmitOptions struct {
796732
TargetSourceFile *ast.SourceFile // Single file to emit. If `nil`, emits all files
797733
forceDtsEmit bool
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package outputpaths
2+
3+
import (
4+
"github.com/microsoft/typescript-go/internal/core"
5+
"github.com/microsoft/typescript-go/internal/tspath"
6+
)
7+
8+
func computeCommonSourceDirectoryOfFilenames(fileNames []string, currentDirectory string, useCaseSensitiveFileNames bool) string {
9+
var commonPathComponents []string
10+
for _, sourceFile := range fileNames {
11+
// Each file contributes into common source file path
12+
sourcePathComponents := tspath.GetNormalizedPathComponents(sourceFile, currentDirectory)
13+
14+
// The base file name is not part of the common directory path
15+
sourcePathComponents = sourcePathComponents[:len(sourcePathComponents)-1]
16+
17+
if commonPathComponents == nil {
18+
// first file
19+
commonPathComponents = sourcePathComponents
20+
continue
21+
}
22+
23+
n := min(len(commonPathComponents), len(sourcePathComponents))
24+
for i := range n {
25+
if tspath.GetCanonicalFileName(commonPathComponents[i], useCaseSensitiveFileNames) != tspath.GetCanonicalFileName(sourcePathComponents[i], useCaseSensitiveFileNames) {
26+
if i == 0 {
27+
// Failed to find any common path component
28+
return ""
29+
}
30+
31+
// New common path found that is 0 -> i-1
32+
commonPathComponents = commonPathComponents[:i]
33+
break
34+
}
35+
}
36+
37+
// If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents
38+
if len(sourcePathComponents) < len(commonPathComponents) {
39+
commonPathComponents = commonPathComponents[:len(sourcePathComponents)]
40+
}
41+
}
42+
43+
if len(commonPathComponents) == 0 {
44+
// Can happen when all input files are .d.ts files
45+
return currentDirectory
46+
}
47+
48+
return tspath.GetPathFromPathComponents(commonPathComponents)
49+
}
50+
51+
func GetCommonSourceDirectory(options *core.CompilerOptions, files func() []string, currentDirectory string, useCaseSensitiveFileNames bool) string {
52+
var commonSourceDirectory string
53+
if options.RootDir != "" {
54+
// If a rootDir is specified use it as the commonSourceDirectory
55+
commonSourceDirectory = options.RootDir
56+
} else if options.Composite.IsTrue() && options.ConfigFilePath != "" {
57+
// If the rootDir is not specified, but the project is composite, then the common source directory
58+
// is the directory of the config file.
59+
commonSourceDirectory = tspath.GetDirectoryPath(options.ConfigFilePath)
60+
} else {
61+
commonSourceDirectory = computeCommonSourceDirectoryOfFilenames(files(), currentDirectory, useCaseSensitiveFileNames)
62+
}
63+
64+
if len(commonSourceDirectory) > 0 {
65+
// Make sure directory path ends with directory separator so this string can directly
66+
// used to replace with "" to get the relative path of the source file and the relative path doesn't
67+
// start with / making it rooted path
68+
commonSourceDirectory = tspath.EnsureTrailingDirectorySeparator(commonSourceDirectory)
69+
}
70+
71+
return commonSourceDirectory
72+
}

0 commit comments

Comments
 (0)