Skip to content

Clean up host related code #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/tsgo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func runAPI(args []string) int {
Out: os.Stdout,
Err: os.Stderr,
Cwd: *cwd,
NewLine: "\n",
DefaultLibraryPath: defaultLibraryPath,
})

Expand Down
8 changes: 0 additions & 8 deletions cmd/tsgo/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"fmt"
"io"
"os"
"runtime"
"time"

"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/execute"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand All @@ -19,7 +17,6 @@ type osSys struct {
writer io.Writer
fs vfs.FS
defaultLibraryPath string
newLine string
cwd string
start time.Time
}
Expand All @@ -44,10 +41,6 @@ func (s *osSys) GetCurrentDirectory() string {
return s.cwd
}

func (s *osSys) NewLine() string {
return s.newLine
}

func (s *osSys) Writer() io.Writer {
return s.writer
}
Expand All @@ -69,7 +62,6 @@ func newSystem() *osSys {
fs: bundled.WrapFS(osvfs.FS()),
defaultLibraryPath: bundled.LibPath(),
writer: os.Stdout,
newLine: core.IfElse(runtime.GOOS == "windows", "\r\n", "\n"),
start: time.Now(),
}
}
5 changes: 0 additions & 5 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ func (api *API) Trace(s string) {
api.options.Logger.Info(s)
}

// NewLine implements ProjectHost.
func (api *API) NewLine() string {
return api.host.NewLine()
}

// PositionEncoding implements ProjectHost.
func (api *API) PositionEncoding() lsproto.PositionEncodingKind {
return lsproto.PositionEncodingKindUTF8
Expand Down
1 change: 0 additions & 1 deletion internal/api/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ type APIHost interface {
FS() vfs.FS
DefaultLibraryPath() string
GetCurrentDirectory() string
NewLine() string
}
7 changes: 0 additions & 7 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type ServerOptions struct {
Out io.Writer
Err io.Writer
Cwd string
NewLine string
DefaultLibraryPath string
}

Expand Down Expand Up @@ -98,7 +97,6 @@ func NewServer(options *ServerOptions) *Server {
w: bufio.NewWriter(options.Out),
stderr: options.Err,
cwd: options.Cwd,
newLine: options.NewLine,
fs: bundled.WrapFS(osvfs.FS()),
defaultLibraryPath: options.DefaultLibraryPath,
}
Expand Down Expand Up @@ -126,11 +124,6 @@ func (s *Server) GetCurrentDirectory() string {
return s.cwd
}

// NewLine implements APIHost.
func (s *Server) NewLine() string {
return s.newLine
}

func (s *Server) Run() error {
for {
messageType, method, payload, err := s.readRequest("")
Expand Down
6 changes: 3 additions & 3 deletions internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ foo.bar;`
fs = bundled.WrapFS(fs)

cd := "/"
host := compiler.NewCompilerHost(nil, cd, fs, bundled.LibPath(), nil)
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath())

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestCheckSrcCompiler(t *testing.T) {

rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")

host := compiler.NewCompilerHost(nil, rootPath, fs, bundled.LibPath(), nil)
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath())
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
p := compiler.NewProgram(compiler.ProgramOptions{
Expand All @@ -87,7 +87,7 @@ func BenchmarkNewChecker(b *testing.B) {

rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")

host := compiler.NewCompilerHost(nil, rootPath, fs, bundled.LibPath(), nil)
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath())
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
p := compiler.NewProgram(compiler.ProgramOptions{
Expand Down
40 changes: 7 additions & 33 deletions internal/compiler/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compiler

import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/tsoptions"
Expand All @@ -15,50 +14,36 @@ type CompilerHost interface {
FS() vfs.FS
DefaultLibraryPath() string
GetCurrentDirectory() string
NewLine() string
Trace(msg string)
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
}

type FileInfo struct {
Name string
Size int64
}

var _ CompilerHost = (*compilerHost)(nil)

type compilerHost struct {
options *core.CompilerOptions
currentDirectory string
fs vfs.FS
defaultLibraryPath string
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]
currentDirectory string
fs vfs.FS
defaultLibraryPath string
}

func NewCachedFSCompilerHost(
options *core.CompilerOptions,
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
) CompilerHost {
return NewCompilerHost(options, currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache)
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath)
}

func NewCompilerHost(
options *core.CompilerOptions,
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry],
) CompilerHost {
return &compilerHost{
options: options,
currentDirectory: currentDirectory,
fs: fs,
defaultLibraryPath: defaultLibraryPath,
extendedConfigCache: extendedConfigCache,
currentDirectory: currentDirectory,
fs: fs,
defaultLibraryPath: defaultLibraryPath,
}
}

Expand All @@ -70,21 +55,10 @@ func (h *compilerHost) DefaultLibraryPath() string {
return h.defaultLibraryPath
}

func (h *compilerHost) SetOptions(options *core.CompilerOptions) {
h.options = options
}

func (h *compilerHost) GetCurrentDirectory() string {
return h.currentDirectory
}

func (h *compilerHost) NewLine() string {
if h.options == nil {
return "\n"
}
return h.options.NewLine.GetNewLineCharacter()
}

func (h *compilerHost) Trace(msg string) {
//!!! TODO: implement
}
Expand Down
6 changes: 3 additions & 3 deletions internal/compiler/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestProgram(t *testing.T) {
CompilerOptions: &opts,
},
},
Host: NewCompilerHost(&opts, "c:/dev/src", fs, bundled.LibPath(), nil),
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath()),
})

actualFiles := []string{}
Expand Down Expand Up @@ -277,7 +277,7 @@ func BenchmarkNewProgram(b *testing.B) {
CompilerOptions: &opts,
},
},
Host: NewCompilerHost(&opts, "c:/dev/src", fs, bundled.LibPath(), nil),
Host: NewCompilerHost("c:/dev/src", fs, bundled.LibPath()),
}

for b.Loop() {
Expand All @@ -294,7 +294,7 @@ func BenchmarkNewProgram(b *testing.B) {
fs := osvfs.FS()
fs = bundled.WrapFS(fs)

host := NewCompilerHost(nil, rootPath, fs, bundled.LibPath(), nil)
host := NewCompilerHost(rootPath, fs, bundled.LibPath())

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), nil, host, nil)
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
Expand Down
34 changes: 17 additions & 17 deletions internal/execute/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func reportStatistics(sys System, program *compiler.Program, result compileAndEm
}

func printVersion(sys System) {
fmt.Fprint(sys.Writer(), diagnostics.Version_0.Format(core.Version())+sys.NewLine())
fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Format(core.Version()))
sys.EndWrite()
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func getHeader(sys System, message string) []string {
// header.push("".padStart(leftAlign) + tsIconSecondLine + sys.newLine);
// }
// else {
header = append(header, message+sys.NewLine(), sys.NewLine())
header = append(header, message+"\n", "\n")
// }
return header
}
Expand All @@ -156,15 +156,15 @@ func printEasyHelp(sys System, simpleOptions []*tsoptions.CommandLineOption) {
for _, example := range examples {
// !!! colors
// output.push(" " + colors.blue(example) + sys.newLine);
output = append(output, " ", example, sys.NewLine())
output = append(output, " ", example, "\n")
}
output = append(output, " ", desc.Format(), sys.NewLine(), sys.NewLine())
output = append(output, " ", desc.Format(), "\n", "\n")
}

msg := diagnostics.X_tsc_Colon_The_TypeScript_Compiler.Format() + " - " + diagnostics.Version_0.Format(core.Version())
output = append(output, getHeader(sys, msg)...)

output = append(output /*colors.bold(*/, diagnostics.COMMON_COMMANDS.Format() /*)*/, sys.NewLine(), sys.NewLine())
output = append(output /*colors.bold(*/, diagnostics.COMMON_COMMANDS.Format() /*)*/, "\n", "\n")

example([]string{"tsc"}, diagnostics.Compiles_the_current_project_tsconfig_json_in_the_working_directory)
example([]string{"tsc app.ts util.ts"}, diagnostics.Ignoring_tsconfig_json_compiles_the_specified_files_with_default_compiler_options)
Expand Down Expand Up @@ -206,15 +206,15 @@ func generateSectionOptionsOutput(
afterOptionsDescription *string,
) (output []string) {
// !!! color
output = append(output /*createColors(sys).bold(*/, sectionName /*)*/, sys.NewLine(), sys.NewLine())
output = append(output /*createColors(sys).bold(*/, sectionName /*)*/, "\n", "\n")

if beforeOptionsDescription != nil {
output = append(output, *beforeOptionsDescription, sys.NewLine(), sys.NewLine())
output = append(output, *beforeOptionsDescription, "\n", "\n")
}
if !subCategory {
output = append(output, generateGroupOptionOutput(sys, options)...)
if afterOptionsDescription != nil {
output = append(output, *afterOptionsDescription, sys.NewLine(), sys.NewLine())
output = append(output, *afterOptionsDescription, "\n", "\n")
}
return output
}
Expand All @@ -227,11 +227,11 @@ func generateSectionOptionsOutput(
categoryMap[curCategory] = append(categoryMap[curCategory], option)
}
for key, value := range categoryMap {
output = append(output, "### ", key, sys.NewLine(), sys.NewLine())
output = append(output, "### ", key, "\n", "\n")
output = append(output, generateGroupOptionOutput(sys, value)...)
}
if afterOptionsDescription != nil {
output = append(output, *afterOptionsDescription, sys.NewLine(), sys.NewLine())
output = append(output, *afterOptionsDescription, "\n", "\n")
}

return output
Expand All @@ -258,8 +258,8 @@ func generateGroupOptionOutput(sys System, optionsList []*tsoptions.CommandLineO
}

// make sure always a blank line in the end.
if len(lines) < 2 || lines[len(lines)-2] != sys.NewLine() {
lines = append(lines, sys.NewLine())
if len(lines) < 2 || lines[len(lines)-2] != "\n" {
lines = append(lines, "\n")
}

return lines
Expand Down Expand Up @@ -312,25 +312,25 @@ func generateOptionOutput(
// !!! }
// !!! text.push(sys.newLine);
} else {
text = append(text /* !!! colors.blue(name) */, name, sys.NewLine())
text = append(text /* !!! colors.blue(name) */, name, "\n")
if option.Description != nil {
text = append(text, option.Description.Format())
}
text = append(text, sys.NewLine())
text = append(text, "\n")
if showAdditionalInfoOutput(valueCandidates, option) {
if valueCandidates != nil {
text = append(text, valueCandidates.valueType, " ", valueCandidates.possibleValues)
}
if defaultValueDescription != "" {
if valueCandidates != nil {
text = append(text, sys.NewLine())
text = append(text, "\n")
}
text = append(text, diagnostics.X_default_Colon.Format(), " ", defaultValueDescription)
}

text = append(text, sys.NewLine())
text = append(text, "\n")
}
text = append(text, sys.NewLine())
text = append(text, "\n")
}

return text
Expand Down
1 change: 0 additions & 1 deletion internal/execute/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type System interface {
FS() vfs.FS
DefaultLibraryPath() string
GetCurrentDirectory() string
NewLine() string // #241 eventually we want to use "\n"

Now() time.Time
SinceStart() time.Duration
Expand Down
4 changes: 0 additions & 4 deletions internal/execute/testsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func (s *testSys) GetCurrentDirectory() string {
return s.cwd
}

func (s *testSys) NewLine() string {
return "\n"
}

func (s *testSys) Writer() io.Writer {
return s.currentWrite
}
Expand Down
Loading