Skip to content

Commit f7c1c37

Browse files
authored
Remove unnecessary compilerOptions and use directly from config (#1049)
1 parent 6f5e2b9 commit f7c1c37

File tree

7 files changed

+84
-93
lines changed

7 files changed

+84
-93
lines changed

internal/compiler/emitHost.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ func (host *emitHost) GetSourceFileFromReference(origin *ast.SourceFile, ref *as
9191

9292
func (host *emitHost) Options() *core.CompilerOptions { return host.program.Options() }
9393
func (host *emitHost) SourceFiles() []*ast.SourceFile { return host.program.SourceFiles() }
94-
func (host *emitHost) GetCurrentDirectory() string { return host.program.host.GetCurrentDirectory() }
94+
func (host *emitHost) GetCurrentDirectory() string { return host.program.GetCurrentDirectory() }
9595
func (host *emitHost) CommonSourceDirectory() string { return host.program.CommonSourceDirectory() }
9696
func (host *emitHost) UseCaseSensitiveFileNames() bool {
97-
return host.program.host.FS().UseCaseSensitiveFileNames()
97+
return host.program.UseCaseSensitiveFileNames()
9898
}
9999

100100
func (host *emitHost) IsEmitBlocked(file string) bool {
@@ -103,7 +103,7 @@ func (host *emitHost) IsEmitBlocked(file string) bool {
103103
}
104104

105105
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *printer.WriteFileData) error {
106-
return host.program.host.FS().WriteFile(fileName, text, writeByteOrderMark)
106+
return host.program.Host().FS().WriteFile(fileName, text, writeByteOrderMark)
107107
}
108108

109109
func (host *emitHost) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) printer.EmitResolver {

internal/compiler/fileloader.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import (
1818
)
1919

2020
type fileLoader struct {
21-
host CompilerHost
22-
programOptions ProgramOptions
23-
compilerOptions *core.CompilerOptions
21+
opts ProgramOptions
2422
resolver *module.Resolver
2523
defaultLibraryPath string
2624
comparePathsOptions tspath.ComparePathsOptions
@@ -52,24 +50,21 @@ type jsxRuntimeImportSpecifier struct {
5250
}
5351

5452
func processAllProgramFiles(
55-
host CompilerHost,
56-
programOptions ProgramOptions,
57-
compilerOptions *core.CompilerOptions,
53+
opts ProgramOptions,
5854
resolver *module.Resolver,
59-
rootFiles []string,
6055
libs []string,
6156
singleThreaded bool,
6257
) processedFiles {
58+
compilerOptions := opts.Config.CompilerOptions()
59+
rootFiles := opts.Config.FileNames()
6360
supportedExtensions := tsoptions.GetSupportedExtensions(compilerOptions, nil /*extraFileExtensions*/)
6461
loader := fileLoader{
65-
host: host,
66-
programOptions: programOptions,
67-
compilerOptions: compilerOptions,
62+
opts: opts,
6863
resolver: resolver,
69-
defaultLibraryPath: tspath.GetNormalizedAbsolutePath(host.DefaultLibraryPath(), host.GetCurrentDirectory()),
64+
defaultLibraryPath: tspath.GetNormalizedAbsolutePath(opts.Host.DefaultLibraryPath(), opts.Host.GetCurrentDirectory()),
7065
comparePathsOptions: tspath.ComparePathsOptions{
71-
UseCaseSensitiveFileNames: host.FS().UseCaseSensitiveFileNames(),
72-
CurrentDirectory: host.GetCurrentDirectory(),
66+
UseCaseSensitiveFileNames: opts.Host.FS().UseCaseSensitiveFileNames(),
67+
CurrentDirectory: opts.Host.GetCurrentDirectory(),
7368
},
7469
wg: core.NewWorkGroup(singleThreaded),
7570
rootTasks: make([]*parseTask, 0, len(rootFiles)+len(libs)),
@@ -138,23 +133,24 @@ func processAllProgramFiles(
138133

139134
func (p *fileLoader) addRootTasks(files []string, isLib bool) {
140135
for _, fileName := range files {
141-
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.host.GetCurrentDirectory())
142-
if core.Tristate.IsTrue(p.compilerOptions.AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
136+
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
137+
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
143138
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: absPath, isLib: isLib})
144139
}
145140
}
146141
}
147142

148143
func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
149144
var containingDirectory string
150-
if p.compilerOptions.ConfigFilePath != "" {
151-
containingDirectory = tspath.GetDirectoryPath(p.compilerOptions.ConfigFilePath)
145+
compilerOptions := p.opts.Config.CompilerOptions()
146+
if compilerOptions.ConfigFilePath != "" {
147+
containingDirectory = tspath.GetDirectoryPath(compilerOptions.ConfigFilePath)
152148
} else {
153-
containingDirectory = p.host.GetCurrentDirectory()
149+
containingDirectory = p.opts.Host.GetCurrentDirectory()
154150
}
155151
containingFileName := tspath.CombinePaths(containingDirectory, module.InferredTypesContainingFile)
156152

157-
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(p.compilerOptions, p.host)
153+
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(compilerOptions, p.opts.Host)
158154
for _, name := range automaticTypeDirectiveNames {
159155
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, core.ModuleKindNodeNext, nil)
160156
if resolved.IsResolved() {
@@ -265,15 +261,16 @@ func (t *parseTask) start(loader *fileLoader) {
265261
t.addSubTask(resolvedPath, false)
266262
}
267263

264+
compilerOptions := loader.opts.Config.CompilerOptions()
268265
for _, ref := range file.TypeReferenceDirectives {
269-
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, t.metadata, loader.compilerOptions)
266+
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, t.metadata, compilerOptions)
270267
resolved := loader.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, nil)
271268
if resolved.IsResolved() {
272269
t.addSubTask(resolved.ResolvedFileName, false)
273270
}
274271
}
275272

276-
if loader.compilerOptions.NoLib != core.TSTrue {
273+
if compilerOptions.NoLib != core.TSTrue {
277274
for _, lib := range file.LibReferenceDirectives {
278275
name, ok := tsoptions.GetLibFileName(lib.FileName)
279276
if !ok {
@@ -314,8 +311,8 @@ func (p *fileLoader) loadSourceFileMetaData(fileName string) *ast.SourceFileMeta
314311
}
315312

316313
func (p *fileLoader) parseSourceFile(fileName string) *ast.SourceFile {
317-
path := tspath.ToPath(fileName, p.host.GetCurrentDirectory(), p.host.FS().UseCaseSensitiveFileNames())
318-
sourceFile := p.host.GetSourceFile(fileName, path, p.compilerOptions.GetEmitScriptTarget())
314+
path := tspath.ToPath(fileName, p.opts.Host.GetCurrentDirectory(), p.opts.Host.FS().UseCaseSensitiveFileNames())
315+
sourceFile := p.opts.Host.GetSourceFile(fileName, path, p.opts.Config.CompilerOptions().GetEmitScriptTarget())
319316
return sourceFile
320317
}
321318

@@ -354,14 +351,15 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile,
354351
isJavaScriptFile := ast.IsSourceFileJS(file)
355352
isExternalModuleFile := ast.IsExternalModule(file)
356353

357-
if isJavaScriptFile || (!file.IsDeclarationFile && (p.compilerOptions.GetIsolatedModules() || isExternalModuleFile)) {
358-
if p.compilerOptions.ImportHelpers.IsTrue() {
354+
compilerOptions := p.opts.Config.CompilerOptions()
355+
if isJavaScriptFile || (!file.IsDeclarationFile && (compilerOptions.GetIsolatedModules() || isExternalModuleFile)) {
356+
if compilerOptions.ImportHelpers.IsTrue() {
359357
specifier := p.createSyntheticImport(externalHelpersModuleNameText, file)
360358
moduleNames = append(moduleNames, specifier)
361359
importHelpersImportSpecifier = specifier
362360
}
363361

364-
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(p.compilerOptions, file), p.compilerOptions)
362+
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(compilerOptions, file), compilerOptions)
365363
if jsxImport != "" {
366364
specifier := p.createSyntheticImport(jsxImport, file)
367365
moduleNames = append(moduleNames, specifier)
@@ -398,9 +396,9 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile,
398396
// Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs')
399397
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
400398
hasAllowedExtension := false
401-
if p.compilerOptions.ResolveJsonModule.IsTrue() {
399+
if compilerOptions.ResolveJsonModule.IsTrue() {
402400
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsWithJsonFlat)
403-
} else if p.compilerOptions.AllowJs.IsTrue() {
401+
} else if compilerOptions.AllowJs.IsTrue() {
404402
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedJSExtensionsFlat) || tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsFlat)
405403
} else {
406404
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsFlat)
@@ -430,7 +428,7 @@ func (p *fileLoader) resolveModuleNames(entries []*ast.Node, file *ast.SourceFil
430428
if moduleName == "" {
431429
continue
432430
}
433-
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), getModeForUsageLocation(file, meta, entry, p.compilerOptions), nil)
431+
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), getModeForUsageLocation(file, meta, entry, p.opts.Config.CompilerOptions()), nil)
434432
resolvedModules = append(resolvedModules, &resolution{node: entry, resolvedModule: resolvedModule})
435433
}
436434

@@ -457,7 +455,7 @@ type resolution struct {
457455

458456
func (p *fileLoader) getCompilerOptionsForFile(file *ast.SourceFile) *core.CompilerOptions {
459457
// !!! return getRedirectReferenceForResolution(file)?.commandLine.options || options;
460-
return p.compilerOptions
458+
return p.opts.Config.CompilerOptions()
461459
}
462460

463461
func getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, file *ast.SourceFile, meta *ast.SourceFileMetaData, options *core.CompilerOptions) core.ResolutionMode {

0 commit comments

Comments
 (0)