Skip to content

Commit 69fa71e

Browse files
authored
refactor formatopts in main (microsoft#253)
1 parent 38fad5b commit 69fa71e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

cmd/tsgo/main.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ func main() {
177177
var bindTime, checkTime time.Duration
178178

179179
diagnostics := program.GetOptionsDiagnostics()
180+
formatOpts := getFormatOpts(host)
180181
if len(diagnostics) != 0 {
181-
printDiagnostics(diagnostics, host, compilerOptions)
182+
printDiagnostics(diagnostics, formatOpts, compilerOptions)
182183
os.Exit(1)
183184
}
184185

@@ -216,7 +217,7 @@ func main() {
216217
runtime.ReadMemStats(&memStats)
217218

218219
if !opts.devel.quiet && len(diagnostics) != 0 {
219-
printDiagnostics(diagnostics, host, compilerOptions)
220+
printDiagnostics(diagnostics, formatOpts, compilerOptions)
220221
}
221222

222223
if compilerOptions.ListFiles.IsTrue() {
@@ -290,22 +291,24 @@ func listFiles(p *ts.Program) {
290291
}
291292
}
292293

293-
func printDiagnostics(diagnostics []*ast.Diagnostic, host ts.CompilerHost, compilerOptions *core.CompilerOptions) {
294-
comparePathOptions := tspath.ComparePathsOptions{
295-
CurrentDirectory: host.GetCurrentDirectory(),
296-
UseCaseSensitiveFileNames: host.FS().UseCaseSensitiveFileNames(),
294+
func getFormatOpts(host ts.CompilerHost) *diagnosticwriter.FormattingOptions {
295+
return &diagnosticwriter.FormattingOptions{
296+
NewLine: host.NewLine(),
297+
ComparePathsOptions: tspath.ComparePathsOptions{
298+
CurrentDirectory: host.GetCurrentDirectory(),
299+
UseCaseSensitiveFileNames: host.FS().UseCaseSensitiveFileNames(),
300+
},
297301
}
302+
}
303+
304+
func printDiagnostics(diagnostics []*ast.Diagnostic, formatOpts *diagnosticwriter.FormattingOptions, compilerOptions *core.CompilerOptions) {
298305
if compilerOptions.Pretty.IsTrueOrUnknown() {
299-
formatOpts := diagnosticwriter.FormattingOptions{
300-
NewLine: "\n",
301-
ComparePathsOptions: comparePathOptions,
302-
}
303-
diagnosticwriter.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, &formatOpts)
306+
diagnosticwriter.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, formatOpts)
304307
fmt.Fprintln(os.Stdout)
305-
diagnosticwriter.WriteErrorSummaryText(os.Stdout, diagnostics, &formatOpts)
308+
diagnosticwriter.WriteErrorSummaryText(os.Stdout, diagnostics, formatOpts)
306309
} else {
307310
for _, diagnostic := range diagnostics {
308-
printDiagnostic(diagnostic, 0, comparePathOptions)
311+
printDiagnostic(diagnostic, 0, formatOpts.ComparePathsOptions)
309312
}
310313
}
311314
}

internal/compiler/host.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type CompilerHost interface {
99
FS() vfs.FS
1010
GetCurrentDirectory() string
11+
NewLine() string
1112
Trace(msg string)
1213
}
1314

@@ -40,6 +41,13 @@ func (h *compilerHost) GetCurrentDirectory() string {
4041
return h.currentDirectory
4142
}
4243

43-
func (d *compilerHost) Trace(msg string) {
44+
func (h *compilerHost) NewLine() string {
45+
if h.options == nil {
46+
return "\n"
47+
}
48+
return h.options.NewLine.GetNewLineCharacter()
49+
}
50+
51+
func (h *compilerHost) Trace(msg string) {
4452
//!!! TODO: implement
4553
}

0 commit comments

Comments
 (0)