Skip to content

Commit c8b4dca

Browse files
authored
Cache CompilerOptions.SourceFileAffecting (#1156)
1 parent 1318a34 commit c8b4dca

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

internal/compiler/fileloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (p *fileLoader) loadSourceFileMetaData(fileName string) *ast.SourceFileMeta
278278

279279
func (p *fileLoader) parseSourceFile(t *parseTask) *ast.SourceFile {
280280
path := p.toPath(t.normalizedFilePath)
281-
sourceFile := p.opts.Host.GetSourceFile(t.normalizedFilePath, path, p.projectReferenceFileMapper.getCompilerOptionsForFile(t).SourceFileAffecting(), t.metadata) // TODO(jakebailey): cache :(
281+
sourceFile := p.opts.Host.GetSourceFile(t.normalizedFilePath, path, p.projectReferenceFileMapper.getCompilerOptionsForFile(t).SourceFileAffecting(), t.metadata)
282282
return sourceFile
283283
}
284284

internal/compiler/program.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ type Program struct {
4141
nodeModules map[string]*ast.SourceFile
4242
checkerPool CheckerPool
4343

44-
sourceAffectingCompilerOptionsOnce sync.Once
45-
sourceAffectingCompilerOptions *core.SourceFileAffectingCompilerOptions
46-
4744
comparePathsOptions tspath.ComparePathsOptions
4845

4946
processedFiles
@@ -302,19 +299,12 @@ func (p *Program) singleThreaded() bool {
302299
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
303300
}
304301

305-
func (p *Program) getSourceAffectingCompilerOptions() *core.SourceFileAffectingCompilerOptions {
306-
p.sourceAffectingCompilerOptionsOnce.Do(func() {
307-
p.sourceAffectingCompilerOptions = p.Options().SourceFileAffecting()
308-
})
309-
return p.sourceAffectingCompilerOptions
310-
}
311-
312302
func (p *Program) BindSourceFiles() {
313303
wg := core.NewWorkGroup(p.singleThreaded())
314304
for _, file := range p.files {
315305
if !file.IsBound() {
316306
wg.Queue(func() {
317-
binder.BindSourceFile(file, p.getSourceAffectingCompilerOptions())
307+
binder.BindSourceFile(file, p.projectReferenceFileMapper.getCompilerOptionsForFile(file).SourceFileAffecting())
318308
})
319309
}
320310
}
@@ -605,7 +595,7 @@ func compactAndMergeRelatedInfos(diagnostics []*ast.Diagnostic) []*ast.Diagnosti
605595
func (p *Program) getDiagnosticsHelper(ctx context.Context, sourceFile *ast.SourceFile, ensureBound bool, ensureChecked bool, getDiagnostics func(context.Context, *ast.SourceFile) []*ast.Diagnostic) []*ast.Diagnostic {
606596
if sourceFile != nil {
607597
if ensureBound {
608-
binder.BindSourceFile(sourceFile, p.getSourceAffectingCompilerOptions())
598+
binder.BindSourceFile(sourceFile, p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile).SourceFileAffecting())
609599
}
610600
return SortAndDeduplicateDiagnostics(getDiagnostics(ctx, sourceFile))
611601
}

internal/core/compileroptions.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"reflect"
55
"strings"
6+
"sync"
67

78
"github.com/microsoft/typescript-go/internal/collections"
89
"github.com/microsoft/typescript-go/internal/tspath"
@@ -146,6 +147,9 @@ type CompilerOptions struct {
146147
PprofDir string `json:"pprofDir,omitzero"`
147148
SingleThreaded Tristate `json:"singleThreaded,omitzero"`
148149
Quiet Tristate `json:"quiet,omitzero"`
150+
151+
sourceFileAffectingCompilerOptionsOnce sync.Once
152+
sourceFileAffectingCompilerOptions *SourceFileAffectingCompilerOptions
149153
}
150154

151155
// noCopy may be embedded into structs which must not be copied
@@ -360,17 +364,20 @@ type SourceFileAffectingCompilerOptions struct {
360364
}
361365

362366
func (options *CompilerOptions) SourceFileAffecting() *SourceFileAffectingCompilerOptions {
363-
return &SourceFileAffectingCompilerOptions{
364-
AllowUnreachableCode: options.AllowUnreachableCode,
365-
AllowUnusedLabels: options.AllowUnusedLabels,
366-
BindInStrictMode: options.AlwaysStrict.IsTrue() || options.Strict.IsTrue(),
367-
EmitModuleDetectionKind: options.GetEmitModuleDetectionKind(),
368-
EmitModuleKind: options.GetEmitModuleKind(),
369-
EmitScriptTarget: options.GetEmitScriptTarget(),
370-
JsxEmit: options.Jsx,
371-
NoFallthroughCasesInSwitch: options.NoFallthroughCasesInSwitch,
372-
ShouldPreserveConstEnums: options.ShouldPreserveConstEnums(),
373-
}
367+
options.sourceFileAffectingCompilerOptionsOnce.Do(func() {
368+
options.sourceFileAffectingCompilerOptions = &SourceFileAffectingCompilerOptions{
369+
AllowUnreachableCode: options.AllowUnreachableCode,
370+
AllowUnusedLabels: options.AllowUnusedLabels,
371+
BindInStrictMode: options.AlwaysStrict.IsTrue() || options.Strict.IsTrue(),
372+
EmitModuleDetectionKind: options.GetEmitModuleDetectionKind(),
373+
EmitModuleKind: options.GetEmitModuleKind(),
374+
EmitScriptTarget: options.GetEmitScriptTarget(),
375+
JsxEmit: options.Jsx,
376+
NoFallthroughCasesInSwitch: options.NoFallthroughCasesInSwitch,
377+
ShouldPreserveConstEnums: options.ShouldPreserveConstEnums(),
378+
}
379+
})
380+
return options.sourceFileAffectingCompilerOptions
374381
}
375382

376383
type ModuleDetectionKind int32

internal/tsoptions/commandlineparser_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/google/go-cmp/cmp/cmpopts"
1011
"github.com/microsoft/typescript-go/internal/core"
1112
"github.com/microsoft/typescript-go/internal/diagnostics"
1213
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
@@ -173,7 +174,7 @@ func (f commandLineSubScenario) assertParseResult(t *testing.T) {
173174
newParsedCompilerOptions := &core.CompilerOptions{}
174175
e := json.Unmarshal(o, newParsedCompilerOptions)
175176
assert.NilError(t, e)
176-
assert.DeepEqual(t, tsBaseline.options, newParsedCompilerOptions)
177+
assert.DeepEqual(t, tsBaseline.options, newParsedCompilerOptions, cmpopts.IgnoreUnexported(core.CompilerOptions{}))
177178

178179
newParsedWatchOptions := core.WatchOptions{}
179180
e = json.Unmarshal(o, &newParsedWatchOptions)

internal/tsoptions/tsconfigparsing_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/google/go-cmp/cmp/cmpopts"
1314
"github.com/microsoft/typescript-go/internal/core"
1415
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
1516
"github.com/microsoft/typescript-go/internal/parser"
@@ -863,7 +864,7 @@ func TestParseSrcCompiler(t *testing.T) {
863864
SourceMap: core.TSTrue,
864865
UseUnknownInCatchVariables: core.TSFalse,
865866
Pretty: core.TSTrue,
866-
})
867+
}, cmpopts.IgnoreUnexported(core.CompilerOptions{}))
867868

868869
fileNames := parseConfigFileContent.ParsedConfig.FileNames
869870
relativePaths := make([]string, 0, len(fileNames))

0 commit comments

Comments
 (0)