Skip to content

Commit 011f524

Browse files
authored
Eliminate ProgramOptions.ConfigFileName (#976)
1 parent 360255e commit 011f524

File tree

8 files changed

+63
-103
lines changed

8 files changed

+63
-103
lines changed

internal/checker/checker_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"github.com/microsoft/typescript-go/internal/bundled"
88
"github.com/microsoft/typescript-go/internal/checker"
99
"github.com/microsoft/typescript-go/internal/compiler"
10+
"github.com/microsoft/typescript-go/internal/core"
1011
"github.com/microsoft/typescript-go/internal/repo"
12+
"github.com/microsoft/typescript-go/internal/tsoptions"
1113
"github.com/microsoft/typescript-go/internal/tspath"
1214
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
1315
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
16+
"gotest.tools/v3/assert"
1417
)
1518

1619
func TestGetSymbolAtLocation(t *testing.T) {
@@ -25,19 +28,20 @@ foo.bar;`
2528
"/foo.ts": content,
2629
"/tsconfig.json": `
2730
{
28-
"compilerOptions": {}
31+
"compilerOptions": {},
32+
"files": ["foo.ts"]
2933
}
3034
`,
3135
}, false /*useCaseSensitiveFileNames*/)
3236
fs = bundled.WrapFS(fs)
3337

3438
cd := "/"
3539
host := compiler.NewCompilerHost(nil, cd, fs, bundled.LibPath())
36-
opts := compiler.ProgramOptions{
37-
Host: host,
38-
ConfigFileName: "/tsconfig.json",
39-
}
40-
p := compiler.NewProgram(opts)
40+
41+
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
42+
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
43+
44+
p := compiler.NewProgramFromParsedCommandLine(parsed, host)
4145
p.BindSourceFiles()
4246
c, done := p.GetTypeChecker(t.Context())
4347
defer done()
@@ -64,11 +68,9 @@ func TestCheckSrcCompiler(t *testing.T) {
6468
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
6569

6670
host := compiler.NewCompilerHost(nil, rootPath, fs, bundled.LibPath())
67-
opts := compiler.ProgramOptions{
68-
Host: host,
69-
ConfigFileName: tspath.CombinePaths(rootPath, "tsconfig.json"),
70-
}
71-
p := compiler.NewProgram(opts)
71+
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
72+
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
73+
p := compiler.NewProgramFromParsedCommandLine(parsed, host)
7274
p.CheckSourceFiles(t.Context())
7375
}
7476

@@ -80,11 +82,9 @@ func BenchmarkNewChecker(b *testing.B) {
8082
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
8183

8284
host := compiler.NewCompilerHost(nil, rootPath, fs, bundled.LibPath())
83-
opts := compiler.ProgramOptions{
84-
Host: host,
85-
ConfigFileName: tspath.CombinePaths(rootPath, "tsconfig.json"),
86-
}
87-
p := compiler.NewProgram(opts)
85+
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
86+
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
87+
p := compiler.NewProgramFromParsedCommandLine(parsed, host)
8888

8989
b.ReportAllocs()
9090

internal/compiler/program.go

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/microsoft/typescript-go/internal/diagnostics"
1414
"github.com/microsoft/typescript-go/internal/module"
1515
"github.com/microsoft/typescript-go/internal/modulespecifiers"
16-
"github.com/microsoft/typescript-go/internal/parser"
1716
"github.com/microsoft/typescript-go/internal/printer"
1817
"github.com/microsoft/typescript-go/internal/scanner"
1918
"github.com/microsoft/typescript-go/internal/sourcemap"
@@ -22,7 +21,6 @@ import (
2221
)
2322

2423
type ProgramOptions struct {
25-
ConfigFileName string
2624
RootFiles []string
2725
Host CompilerHost
2826
Options *core.CompilerOptions
@@ -177,7 +175,11 @@ func NewProgram(options ProgramOptions) *Program {
177175
p.compilerOptions = options.Options
178176
p.configFileParsingDiagnostics = slices.Clip(options.ConfigFileParsingDiagnostics)
179177
if p.compilerOptions == nil {
180-
p.compilerOptions = &core.CompilerOptions{}
178+
panic("compiler options required")
179+
}
180+
p.host = options.Host
181+
if p.host == nil {
182+
panic("host required")
181183
}
182184
p.initCheckerPool()
183185

@@ -187,55 +189,6 @@ func NewProgram(options ProgramOptions) *Program {
187189
// tracing?.push(tracing.Phase.Program, "createProgram", { configFilePath: options.configFilePath, rootDir: options.rootDir }, /*separateBeginAndEnd*/ true);
188190
// performance.mark("beforeProgram");
189191

190-
p.host = options.Host
191-
if p.host == nil {
192-
panic("host required")
193-
}
194-
195-
rootFiles := options.RootFiles
196-
197-
p.configFileName = options.ConfigFileName
198-
if p.configFileName != "" {
199-
// !!! delete this code, require options
200-
jsonText, ok := p.host.FS().ReadFile(p.configFileName)
201-
if !ok {
202-
panic("config file not found")
203-
}
204-
configFilePath := tspath.ToPath(p.configFileName, p.host.GetCurrentDirectory(), p.host.FS().UseCaseSensitiveFileNames())
205-
parsedConfig := parser.ParseJSONText(p.configFileName, configFilePath, jsonText)
206-
if len(parsedConfig.Diagnostics()) > 0 {
207-
p.configFileParsingDiagnostics = append(p.configFileParsingDiagnostics, parsedConfig.Diagnostics()...)
208-
return p
209-
}
210-
211-
tsConfigSourceFile := &tsoptions.TsConfigSourceFile{
212-
SourceFile: parsedConfig,
213-
}
214-
215-
parseConfigFileContent := tsoptions.ParseJsonSourceFileConfigFileContent(
216-
tsConfigSourceFile,
217-
p.host,
218-
p.host.GetCurrentDirectory(),
219-
options.Options,
220-
p.configFileName,
221-
/*resolutionStack*/ nil,
222-
/*extraFileExtensions*/ nil,
223-
/*extendedConfigCache*/ nil,
224-
)
225-
226-
p.compilerOptions = parseConfigFileContent.CompilerOptions()
227-
228-
if len(parseConfigFileContent.Errors) > 0 {
229-
p.configFileParsingDiagnostics = append(p.configFileParsingDiagnostics, parseConfigFileContent.Errors...)
230-
return p
231-
}
232-
233-
if rootFiles == nil {
234-
// !!! merge? override? this?
235-
rootFiles = parseConfigFileContent.FileNames()
236-
}
237-
}
238-
239192
p.resolver = module.NewResolver(p.host, p.compilerOptions)
240193

241194
var libs []string
@@ -255,7 +208,7 @@ func NewProgram(options ProgramOptions) *Program {
255208
}
256209
}
257210

258-
p.processedFiles = processAllProgramFiles(p.host, p.programOptions, p.compilerOptions, p.resolver, rootFiles, libs, p.singleThreaded())
211+
p.processedFiles = processAllProgramFiles(p.host, p.programOptions, p.compilerOptions, p.resolver, options.RootFiles, libs, p.singleThreaded())
259212
p.filesByPath = make(map[tspath.Path]*ast.SourceFile, len(p.files))
260213
for _, file := range p.files {
261214
p.filesByPath[file.Path()] = file

internal/compiler/program_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/microsoft/typescript-go/internal/bundled"
1010
"github.com/microsoft/typescript-go/internal/core"
1111
"github.com/microsoft/typescript-go/internal/repo"
12+
"github.com/microsoft/typescript-go/internal/tsoptions"
1213
"github.com/microsoft/typescript-go/internal/tspath"
1314
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
1415
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
@@ -237,7 +238,7 @@ func TestProgram(t *testing.T) {
237238
})
238239

239240
actualFiles := []string{}
240-
for _, file := range program.files {
241+
for _, file := range program.GetSourceFiles() {
241242
actualFiles = append(actualFiles, strings.TrimPrefix(file.FileName(), libPrefix))
242243
}
243244

@@ -278,14 +279,19 @@ func BenchmarkNewProgram(b *testing.B) {
278279
b.Run("compiler", func(b *testing.B) {
279280
repo.SkipIfNoTypeScriptSubmodule(b)
280281

281-
compilerDir := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
282+
rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
282283

283284
fs := osvfs.FS()
284285
fs = bundled.WrapFS(fs)
285286

287+
host := NewCompilerHost(nil, rootPath, fs, bundled.LibPath())
288+
289+
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
290+
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
291+
286292
opts := ProgramOptions{
287-
ConfigFileName: tspath.CombinePaths(compilerDir, "tsconfig.json"),
288-
Host: NewCompilerHost(nil, compilerDir, fs, bundled.LibPath()),
293+
Host: host,
294+
Options: parsed.CompilerOptions(),
289295
}
290296

291297
for b.Loop() {

internal/execute/tsc.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
115115

116116
if configFileName != "" {
117117
extendedConfigCache := map[tspath.Path]*tsoptions.ExtendedConfigCacheEntry{}
118-
configParseResult, errors := getParsedCommandLineOfConfigFile(configFileName, compilerOptionsFromCommandLine, sys, extendedConfigCache)
118+
configParseResult, errors := tsoptions.GetParsedCommandLineOfConfigFile(configFileName, compilerOptionsFromCommandLine, sys, extendedConfigCache)
119119
if len(errors) != 0 {
120120
// these are unrecoverable errors--exit to report them as diagnostics
121121
for _, e := range errors {
@@ -172,31 +172,6 @@ func findConfigFile(searchPath string, fileExists func(string) bool, configName
172172
return result
173173
}
174174

175-
// Reads the config file and reports errors. Exits if the config file cannot be found
176-
func getParsedCommandLineOfConfigFile(configFileName string, options *core.CompilerOptions, sys System, extendedConfigCache map[tspath.Path]*tsoptions.ExtendedConfigCacheEntry) (*tsoptions.ParsedCommandLine, []*ast.Diagnostic) {
177-
errors := []*ast.Diagnostic{}
178-
configFileText, errors := tsoptions.TryReadFile(configFileName, sys.FS().ReadFile, errors)
179-
if len(errors) > 0 {
180-
// these are unrecoverable errors--exit to report them as diagnostics
181-
return nil, errors
182-
}
183-
184-
cwd := sys.GetCurrentDirectory()
185-
tsConfigSourceFile := tsoptions.NewTsconfigSourceFileFromFilePath(configFileName, tspath.ToPath(configFileName, cwd, sys.FS().UseCaseSensitiveFileNames()), configFileText)
186-
// tsConfigSourceFile.resolvedPath = tsConfigSourceFile.FileName()
187-
// tsConfigSourceFile.originalFileName = tsConfigSourceFile.FileName()
188-
return tsoptions.ParseJsonSourceFileConfigFileContent(
189-
tsConfigSourceFile,
190-
sys,
191-
tspath.GetNormalizedAbsolutePath(tspath.GetDirectoryPath(configFileName), cwd),
192-
options,
193-
tspath.GetNormalizedAbsolutePath(configFileName, cwd),
194-
nil,
195-
nil,
196-
extendedConfigCache,
197-
), nil
198-
}
199-
200175
func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLine, reportDiagnostic diagnosticReporter) ExitStatus {
201176
host := compiler.NewCachedFSCompilerHost(config.CompilerOptions(), sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath())
202177
// todo: cache, statistics, tracing

internal/execute/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (w *watcher) hasErrorsInTsConfig() bool {
4646
if w.configFileName != "" {
4747
extendedConfigCache := map[tspath.Path]*tsoptions.ExtendedConfigCacheEntry{}
4848
// !!! need to check that this merges compileroptions correctly. This differs from non-watch, since we allow overriding of previous options
49-
configParseResult, errors := getParsedCommandLineOfConfigFile(w.configFileName, &core.CompilerOptions{}, w.sys, extendedConfigCache)
49+
configParseResult, errors := tsoptions.GetParsedCommandLineOfConfigFile(w.configFileName, &core.CompilerOptions{}, w.sys, extendedConfigCache)
5050
if len(errors) > 0 {
5151
for _, e := range errors {
5252
w.reportDiagnostic(e)

internal/tsoptions/commandlineparser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func getInputOptionName(input string) string {
120120
}
121121

122122
func (p *commandLineParser) parseResponseFile(fileName string) {
123-
fileContents, errors := TryReadFile(fileName, func(fileName string) (string, bool) {
123+
fileContents, errors := tryReadFile(fileName, func(fileName string) (string, bool) {
124124
if p.fs == nil {
125125
return "", false
126126
}
@@ -166,7 +166,7 @@ func (p *commandLineParser) parseResponseFile(fileName string) {
166166
p.parseStrings(args)
167167
}
168168

169-
func TryReadFile(fileName string, readFile func(string) (string, bool), errors []*ast.Diagnostic) (string, []*ast.Diagnostic) {
169+
func tryReadFile(fileName string, readFile func(string) (string, bool), errors []*ast.Diagnostic) (string, []*ast.Diagnostic) {
170170
// this function adds a compiler diagnostic if the file cannot be read
171171
text, e := readFile(fileName)
172172

internal/tsoptions/tsconfigparsing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ func parseOwnConfigOfJson(
864864
}
865865

866866
func readJsonConfigFile(fileName string, path tspath.Path, readFile func(fileName string) (string, bool)) (*TsConfigSourceFile, []*ast.Diagnostic) {
867-
text, diagnostic := TryReadFile(fileName, readFile, []*ast.Diagnostic{})
867+
text, diagnostic := tryReadFile(fileName, readFile, []*ast.Diagnostic{})
868868
if text != "" {
869869
return &TsConfigSourceFile{
870870
SourceFile: parser.ParseJSONText(fileName, path, text),

internal/tsoptions/utilities.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99

1010
"github.com/dlclark/regexp2"
11+
"github.com/microsoft/typescript-go/internal/ast"
1112
"github.com/microsoft/typescript-go/internal/core"
1213
"github.com/microsoft/typescript-go/internal/stringutil"
1314
"github.com/microsoft/typescript-go/internal/tspath"
@@ -468,3 +469,28 @@ func matchFiles(path string, extensions []string, excludes []string, includes []
468469
func readDirectory(host vfs.FS, currentDir string, path string, extensions []string, excludes []string, includes []string, depth *int) []string {
469470
return matchFiles(path, extensions, excludes, includes, host.UseCaseSensitiveFileNames(), currentDir, depth, host)
470471
}
472+
473+
// Reads the config file and reports errors.
474+
func GetParsedCommandLineOfConfigFile(configFileName string, options *core.CompilerOptions, sys ParseConfigHost, extendedConfigCache map[tspath.Path]*ExtendedConfigCacheEntry) (*ParsedCommandLine, []*ast.Diagnostic) {
475+
errors := []*ast.Diagnostic{}
476+
configFileText, errors := tryReadFile(configFileName, sys.FS().ReadFile, errors)
477+
if len(errors) > 0 {
478+
// these are unrecoverable errors--exit to report them as diagnostics
479+
return nil, errors
480+
}
481+
482+
cwd := sys.GetCurrentDirectory()
483+
tsConfigSourceFile := NewTsconfigSourceFileFromFilePath(configFileName, tspath.ToPath(configFileName, cwd, sys.FS().UseCaseSensitiveFileNames()), configFileText)
484+
// tsConfigSourceFile.resolvedPath = tsConfigSourceFile.FileName()
485+
// tsConfigSourceFile.originalFileName = tsConfigSourceFile.FileName()
486+
return ParseJsonSourceFileConfigFileContent(
487+
tsConfigSourceFile,
488+
sys,
489+
tspath.GetNormalizedAbsolutePath(tspath.GetDirectoryPath(configFileName), cwd),
490+
options,
491+
tspath.GetNormalizedAbsolutePath(configFileName, cwd),
492+
nil,
493+
nil,
494+
extendedConfigCache,
495+
), nil
496+
}

0 commit comments

Comments
 (0)