44 "context"
55 "errors"
66 "fmt"
7- "slices"
87
98 "github.com/microsoft/typescript-go/internal/ast"
109 "github.com/microsoft/typescript-go/internal/astnav"
@@ -61,34 +60,27 @@ func getPosition(file *ast.SourceFile, position int, ls *LanguageService) Positi
6160type DiagnosticId uint32
6261
6362type Diagnostic struct {
64- Id DiagnosticId `json:"id"`
65- FileName string `json:"fileName"`
66- Start Position `json:"start"`
67- End Position `json:"end"`
68- StartPos int `json:"startPos"`
69- EndPos int `json:"endPos"`
70- Code int32 `json:"code"`
71- Category string `json:"category"`
72- Message string `json:"message"`
73- MessageChain []DiagnosticId `json:"messageChain"`
74- RelatedInformation []DiagnosticId `json:"relatedInformation"`
75- ReportsUnnecessary bool `json:"reportsUnnecessary"`
76- ReportsDeprecated bool `json:"reportsDeprecated"`
77- SkippedOnNoEmit bool `json:"skippedOnNoEmit"`
78- SourceLine string `json:"sourceLine"`
63+ FileName string `json:"fileName"`
64+ Start Position `json:"start"`
65+ End Position `json:"end"`
66+ StartPos int `json:"startPos"`
67+ EndPos int `json:"endPos"`
68+ Code int32 `json:"code"`
69+ Category string `json:"category"`
70+ Message string `json:"message"`
71+ MessageChain []* Diagnostic `json:"messageChain"`
72+ RelatedInformation []* Diagnostic `json:"relatedInformation"`
73+ ReportsUnnecessary bool `json:"reportsUnnecessary"`
74+ ReportsDeprecated bool `json:"reportsDeprecated"`
75+ SkippedOnNoEmit bool `json:"skippedOnNoEmit"`
76+ SourceLine string `json:"sourceLine"`
7977}
8078
81- type diagnosticMaps struct {
82- diagnosticMapById map [DiagnosticId ]Diagnostic
83- diagnosticReverseMap map [* ast.Diagnostic ]DiagnosticId
79+ type diagnosticList struct {
80+ diagnostics []* Diagnostic
8481}
8582
86- func (d * diagnosticMaps ) addDiagnostic (diagnostic * ast.Diagnostic , ls * LanguageService ) DiagnosticId {
87- if i , ok := d .diagnosticReverseMap [diagnostic ]; ok {
88- return i
89- }
90- id := DiagnosticId (len (d .diagnosticMapById ) + 1 )
91-
83+ func (d * diagnosticList ) addDiagnostic (diagnostic * ast.Diagnostic , ls * LanguageService , shouldAdd bool ) * Diagnostic {
9284 startPos := diagnostic .Loc ().Pos ()
9385 startPosLineCol := getPosition (diagnostic .File (), startPos , ls )
9486 lineMap := ls .converters .getLineMap (diagnostic .File ().FileName ())
@@ -101,8 +93,7 @@ func (d *diagnosticMaps) addDiagnostic(diagnostic *ast.Diagnostic, ls *LanguageS
10193 }
10294 sourceLine := diagnostic .File ().Text ()[lineStartPos :lineEndPos ]
10395
104- diag := Diagnostic {
105- Id : id ,
96+ diag := & Diagnostic {
10697 FileName : diagnostic .File ().FileName (),
10798 Start : startPosLineCol ,
10899 End : getPosition (diagnostic .File (), diagnostic .Loc ().End (), ls ),
@@ -112,42 +103,32 @@ func (d *diagnosticMaps) addDiagnostic(diagnostic *ast.Diagnostic, ls *LanguageS
112103 Code : diagnostic .Code (),
113104 Category : diagnostic .Category ().Name (),
114105 Message : diagnostic .Message (),
115- MessageChain : make ([]DiagnosticId , 0 , len (diagnostic .MessageChain ())),
116- RelatedInformation : make ([]DiagnosticId , 0 , len (diagnostic .RelatedInformation ())),
106+ MessageChain : make ([]* Diagnostic , 0 , len (diagnostic .MessageChain ())),
107+ RelatedInformation : make ([]* Diagnostic , 0 , len (diagnostic .RelatedInformation ())),
117108 }
118109
119- d .diagnosticReverseMap [diagnostic ] = id
120-
121110 for _ , messageChain := range diagnostic .MessageChain () {
122- diag .MessageChain = append (diag .MessageChain , d .addDiagnostic (messageChain , ls ))
111+ diag .MessageChain = append (diag .MessageChain , d .addDiagnostic (messageChain , ls , false ))
123112 }
124-
125113 for _ , relatedInformation := range diagnostic .RelatedInformation () {
126- diag .RelatedInformation = append (diag .RelatedInformation , d .addDiagnostic (relatedInformation , ls ))
114+ diag .RelatedInformation = append (diag .RelatedInformation , d .addDiagnostic (relatedInformation , ls , false ))
127115 }
128116
129- d .diagnosticMapById [id ] = diag
130- return id
131- }
132-
133- func (d * diagnosticMaps ) getDiagnostics () []Diagnostic {
134- diagnostics := make ([]Diagnostic , 0 , len (d .diagnosticMapById ))
135- for _ , diagnostic := range d .diagnosticMapById {
136- diagnostics = append (diagnostics , diagnostic )
117+ if shouldAdd {
118+ d .diagnostics = append (d .diagnostics , diag )
137119 }
120+ return diag
121+ }
138122
139- slices .SortFunc (diagnostics , func (a , b Diagnostic ) int {
140- return int (int64 (a .Id ) - int64 (b .Id ))
141- })
142- return diagnostics
123+ func (d * diagnosticList ) getDiagnostics () []* Diagnostic {
124+ return d .diagnostics
143125}
144126
145- func (l * LanguageService ) GetDiagnostics (ctx context.Context ) []Diagnostic {
127+ func (l * LanguageService ) GetDiagnostics (ctx context.Context ) []* Diagnostic {
146128 program := l .GetProgram ()
147129 sourceFiles := program .GetSourceFiles ()
148- diagnosticMaps := & diagnosticMaps {
149- diagnosticMapById : make (map [DiagnosticId ]Diagnostic ),
150- diagnosticReverseMap : make (map [* ast.Diagnostic ]DiagnosticId ),
130+ diagnosticList := & diagnosticList {
131+ diagnostics : make ([]* Diagnostic , 0 ),
151132 }
152133 diagnostics := make ([]* ast.Diagnostic , 0 , len (sourceFiles ))
153134 for _ , sourceFile := range sourceFiles {
@@ -156,7 +137,7 @@ func (l *LanguageService) GetDiagnostics(ctx context.Context) []Diagnostic {
156137 }
157138 diagnostics = compiler .SortAndDeduplicateDiagnostics (diagnostics )
158139 for _ , diagnostic := range diagnostics {
159- diagnosticMaps .addDiagnostic (diagnostic , l )
140+ diagnosticList .addDiagnostic (diagnostic , l , true )
160141 }
161- return diagnosticMaps .getDiagnostics ()
142+ return diagnosticList .getDiagnostics ()
162143}
0 commit comments