Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions internal/ast/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"strings"

"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
)
Expand Down Expand Up @@ -122,8 +123,10 @@ func NewCompilerDiagnostic(message *diagnostics.Message, args ...any) *Diagnosti
}

type DiagnosticsCollection struct {
fileDiagnostics map[string][]*Diagnostic
nonFileDiagnostics []*Diagnostic
fileDiagnostics map[string][]*Diagnostic
fileDiagnosticsSorted collections.Set[string]
nonFileDiagnostics []*Diagnostic
nonFileDiagnosticsSorted bool
}

func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) {
Expand All @@ -132,18 +135,20 @@ func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) {
if c.fileDiagnostics == nil {
c.fileDiagnostics = make(map[string][]*Diagnostic)
}
c.fileDiagnostics[fileName] = core.InsertSorted(c.fileDiagnostics[fileName], diagnostic, CompareDiagnostics)
c.fileDiagnostics[fileName] = append(c.fileDiagnostics[fileName], diagnostic)
c.fileDiagnosticsSorted.Delete(fileName)
} else {
c.nonFileDiagnostics = core.InsertSorted(c.nonFileDiagnostics, diagnostic, CompareDiagnostics)
c.nonFileDiagnostics = append(c.nonFileDiagnostics, diagnostic)
c.nonFileDiagnosticsSorted = false
}
}

func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic {
var diagnostics []*Diagnostic
if diagnostic.File() != nil {
diagnostics = c.fileDiagnostics[diagnostic.File().FileName()]
diagnostics = c.GetDiagnosticsForFile(diagnostic.File().FileName())
} else {
diagnostics = c.nonFileDiagnostics
diagnostics = c.GetGlobalDiagnostics()
}
if i, ok := slices.BinarySearchFunc(diagnostics, diagnostic, CompareDiagnostics); ok {
return diagnostics[i]
Expand All @@ -152,10 +157,18 @@ func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic {
}

func (c *DiagnosticsCollection) GetGlobalDiagnostics() []*Diagnostic {
if !c.nonFileDiagnosticsSorted {
slices.SortStableFunc(c.nonFileDiagnostics, CompareDiagnostics)
c.nonFileDiagnosticsSorted = true
}
return c.nonFileDiagnostics
}

func (c *DiagnosticsCollection) GetDiagnosticsForFile(fileName string) []*Diagnostic {
if !c.fileDiagnosticsSorted.Has(fileName) {
slices.SortStableFunc(c.fileDiagnostics[fileName], CompareDiagnostics)
c.fileDiagnosticsSorted.Add(fileName)
}
return c.fileDiagnostics[fileName]
}

Expand Down