internal/compiler/host.go (77 lines of code) (raw):
package compiler
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
"github.com/microsoft/typescript-go/internal/vfs/cachedvfs"
)
type CompilerHost interface {
FS() vfs.FS
DefaultLibraryPath() string
GetCurrentDirectory() string
Trace(msg *diagnostics.Message, args ...any)
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
}
var _ CompilerHost = (*compilerHost)(nil)
type compilerHost struct {
currentDirectory string
fs vfs.FS
defaultLibraryPath string
extendedConfigCache tsoptions.ExtendedConfigCache
trace func(msg *diagnostics.Message, args ...any)
}
func NewCachedFSCompilerHost(
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache tsoptions.ExtendedConfigCache,
trace func(msg *diagnostics.Message, args ...any),
) CompilerHost {
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, trace)
}
func NewCompilerHost(
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache tsoptions.ExtendedConfigCache,
trace func(msg *diagnostics.Message, args ...any),
) CompilerHost {
if trace == nil {
trace = func(msg *diagnostics.Message, args ...any) {}
}
return &compilerHost{
currentDirectory: currentDirectory,
fs: fs,
defaultLibraryPath: defaultLibraryPath,
extendedConfigCache: extendedConfigCache,
trace: trace,
}
}
func (h *compilerHost) FS() vfs.FS {
return h.fs
}
func (h *compilerHost) DefaultLibraryPath() string {
return h.defaultLibraryPath
}
func (h *compilerHost) GetCurrentDirectory() string {
return h.currentDirectory
}
func (h *compilerHost) Trace(msg *diagnostics.Message, args ...any) {
h.trace(msg, args...)
}
func (h *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
text, ok := h.FS().ReadFile(opts.FileName)
if !ok {
return nil
}
return parser.ParseSourceFile(opts, text, core.GetScriptKindFromFileName(opts.FileName))
}
func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
commandLine, _ := tsoptions.GetParsedCommandLineOfConfigFilePath(fileName, path, nil, nil /*optionsRaw*/, h, h.extendedConfigCache)
return commandLine
}