From 310d53fde6f10dd00c80f6085727306cfeeea0c6 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:39:16 -0700 Subject: [PATCH] Cache declaration diagnostics --- internal/compiler/program.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/compiler/program.go b/internal/compiler/program.go index 3fc7bb3a59..eef24c592b 100644 --- a/internal/compiler/program.go +++ b/internal/compiler/program.go @@ -9,6 +9,7 @@ import ( "github.com/microsoft/typescript-go/internal/ast" "github.com/microsoft/typescript-go/internal/binder" "github.com/microsoft/typescript-go/internal/checker" + "github.com/microsoft/typescript-go/internal/collections" "github.com/microsoft/typescript-go/internal/core" "github.com/microsoft/typescript-go/internal/diagnostics" "github.com/microsoft/typescript-go/internal/module" @@ -64,6 +65,8 @@ type Program struct { // List of present unsupported extensions unsupportedExtensions []string + + declarationDiagnosticCache collections.SyncMap[*ast.SourceFile, []*ast.Diagnostic] } // FileExists implements checker.Program. @@ -514,8 +517,15 @@ func (p *Program) getDeclarationDiagnosticsForFile(_ctx context.Context, sourceF if sourceFile.IsDeclarationFile { return []*ast.Diagnostic{} } + + if cached, ok := p.declarationDiagnosticCache.Load(sourceFile); ok { + return cached + } + host := &emitHost{program: p} - return getDeclarationDiagnostics(host, host.GetEmitResolver(sourceFile, true), sourceFile) + diagnostics := getDeclarationDiagnostics(host, host.GetEmitResolver(sourceFile, true), sourceFile) + diagnostics, _ = p.declarationDiagnosticCache.LoadOrStore(sourceFile, diagnostics) + return diagnostics } func (p *Program) getSuggestionDiagnosticsForFile(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {