Skip to content

Commit ea85af3

Browse files
committed
More changes
1 parent 778dc6c commit ea85af3

File tree

5 files changed

+408
-452
lines changed

5 files changed

+408
-452
lines changed

internal/incremental/buildInfo.go

Lines changed: 45 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/microsoft/typescript-go/internal/ast"
87
"github.com/microsoft/typescript-go/internal/collections"
9-
"github.com/microsoft/typescript-go/internal/compiler"
108
"github.com/microsoft/typescript-go/internal/core"
119
"github.com/microsoft/typescript-go/internal/diagnostics"
1210
"github.com/microsoft/typescript-go/internal/tspath"
@@ -188,231 +186,67 @@ func (b *BuildInfoReferenceMapEntry) UnmarshalJSON(data []byte) error {
188186
}
189187

190188
type BuildInfoDiagnostic struct {
191-
// false if diagnostic is not for a file,
192-
// incrementalBuildInfoFileId if it is for a file thats other than its stored for
193-
file any
194-
loc core.TextRange
195-
code int32
196-
category diagnostics.Category
197-
message string
198-
messageChain []*BuildInfoDiagnostic
199-
relatedInformation []*BuildInfoDiagnostic
200-
reportsUnnecessary bool
201-
reportsDeprecated bool
202-
skippedOnNoEmit bool
203-
}
204-
205-
func (b *BuildInfoDiagnostic) toDiagnostic(p *compiler.Program, file *ast.SourceFile) *ast.Diagnostic {
206-
var fileForDiagnostic *ast.SourceFile
207-
if b.file != false {
208-
if b.file == nil {
209-
fileForDiagnostic = file
210-
} else {
211-
fileForDiagnostic = p.GetSourceFileByPath(tspath.Path(b.file.(string)))
212-
}
213-
}
214-
var messageChain []*ast.Diagnostic
215-
for _, msg := range b.messageChain {
216-
messageChain = append(messageChain, msg.toDiagnostic(p, fileForDiagnostic))
217-
}
218-
var relatedInformation []*ast.Diagnostic
219-
for _, info := range b.relatedInformation {
220-
relatedInformation = append(relatedInformation, info.toDiagnostic(p, fileForDiagnostic))
221-
}
222-
return ast.NewDiagnosticWith(
223-
fileForDiagnostic,
224-
b.loc,
225-
b.code,
226-
b.category,
227-
b.message,
228-
messageChain,
229-
relatedInformation,
230-
b.reportsUnnecessary,
231-
b.reportsDeprecated,
232-
b.skippedOnNoEmit,
233-
)
234-
}
235-
236-
func (b *BuildInfoDiagnostic) MarshalJSON() ([]byte, error) {
237-
info := map[string]any{}
238-
if b.file != "" {
239-
info["file"] = b.file
240-
info["pos"] = b.loc.Pos()
241-
info["end"] = b.loc.End()
242-
}
243-
info["code"] = b.code
244-
info["category"] = b.category
245-
info["message"] = b.message
246-
if len(b.messageChain) > 0 {
247-
info["messageChain"] = b.messageChain
248-
}
249-
if len(b.relatedInformation) > 0 {
250-
info["relatedInformation"] = b.relatedInformation
251-
}
252-
if b.reportsUnnecessary {
253-
info["reportsUnnecessary"] = b.reportsUnnecessary
254-
}
255-
if b.reportsDeprecated {
256-
info["reportsDeprecated"] = b.reportsDeprecated
257-
}
258-
if b.skippedOnNoEmit {
259-
info["skippedOnNoEmit"] = b.skippedOnNoEmit
260-
}
261-
return json.Marshal(info)
262-
}
263-
264-
func (b *BuildInfoDiagnostic) UnmarshalJSON(data []byte) error {
265-
var vIncrementalBuildInfoDiagnostic map[string]any
266-
if err := json.Unmarshal(data, &vIncrementalBuildInfoDiagnostic); err != nil {
267-
return fmt.Errorf("invalid incrementalBuildInfoDiagnostic: %s", data)
268-
}
269-
270-
*b = BuildInfoDiagnostic{}
271-
if file, ok := vIncrementalBuildInfoDiagnostic["file"]; ok {
272-
if _, ok := file.(float64); !ok {
273-
if value, ok := file.(bool); !ok || value {
274-
return fmt.Errorf("invalid file in incrementalBuildInfoDiagnostic: expected false or float64, got %T", file)
275-
}
276-
}
277-
b.file = file
278-
var pos float64
279-
posV, ok := vIncrementalBuildInfoDiagnostic["pos"]
280-
if ok {
281-
pos, ok = posV.(float64)
282-
if !ok {
283-
return fmt.Errorf("invalid pos in incrementalBuildInfoDiagnostic: expected float64, got %T", posV)
284-
}
285-
} else {
286-
return fmt.Errorf("missing pos in incrementalBuildInfoDiagnostic")
287-
}
288-
var end float64
289-
endv, ok := vIncrementalBuildInfoDiagnostic["end"]
290-
if ok {
291-
end, ok = endv.(float64)
292-
if !ok {
293-
return fmt.Errorf("invalid end in incrementalBuildInfoDiagnostic: expected float64, got %T", endv)
294-
}
295-
} else {
296-
return fmt.Errorf("missing end in incrementalBuildInfoDiagnostic")
297-
}
298-
b.loc = core.NewTextRange(int(pos), int(end))
299-
}
300-
if codeV, ok := vIncrementalBuildInfoDiagnostic["code"]; ok {
301-
code, ok := codeV.(float64)
302-
if !ok {
303-
return fmt.Errorf("invalid code in incrementalBuildInfoDiagnostic: expected float64, got %T", codeV)
304-
}
305-
b.code = int32(code)
306-
} else {
307-
return fmt.Errorf("missing code in incrementalBuildInfoDiagnostic")
308-
}
309-
if categoryV, ok := vIncrementalBuildInfoDiagnostic["category"]; ok {
310-
category, ok := categoryV.(float64)
311-
if !ok {
312-
return fmt.Errorf("invalid category in incrementalBuildInfoDiagnostic: expected float64, got %T", categoryV)
313-
}
314-
if category < 0 || category > float64(diagnostics.CategoryMessage) {
315-
return fmt.Errorf("invalid category in incrementalBuildInfoDiagnostic: %f is out of range", category)
316-
}
317-
b.category = diagnostics.Category(category)
318-
} else {
319-
return fmt.Errorf("missing category in incrementalBuildInfoDiagnostic")
320-
}
321-
if messageV, ok := vIncrementalBuildInfoDiagnostic["message"]; ok {
322-
if b.message, ok = messageV.(string); !ok {
323-
return fmt.Errorf("invalid message in incrementalBuildInfoDiagnostic: expected string, got %T", messageV)
324-
}
325-
} else {
326-
return fmt.Errorf("missing message in incrementalBuildInfoDiagnostic")
327-
}
328-
if messageChain, ok := vIncrementalBuildInfoDiagnostic["messageChain"]; ok {
329-
if messages, ok := messageChain.([]any); ok {
330-
b.messageChain = make([]*BuildInfoDiagnostic, len(messages))
331-
for _, msg := range messages {
332-
var diagnostic BuildInfoDiagnostic
333-
if err := json.Unmarshal([]byte(msg.(string)), &diagnostic); err != nil {
334-
return fmt.Errorf("invalid messageChain in incrementalBuildInfoDiagnostic: %s", msg)
335-
}
336-
b.messageChain = append(b.messageChain, &diagnostic)
337-
}
338-
}
339-
}
340-
if relatedInformation, ok := vIncrementalBuildInfoDiagnostic["relatedInformation"]; ok {
341-
if infos, ok := relatedInformation.([]any); ok {
342-
b.relatedInformation = make([]*BuildInfoDiagnostic, len(infos))
343-
for _, info := range infos {
344-
var diagnostic BuildInfoDiagnostic
345-
if err := json.Unmarshal([]byte(info.(string)), &diagnostic); err != nil {
346-
return fmt.Errorf("invalid relatedInformation in incrementalBuildInfoDiagnostic: %s", info)
347-
}
348-
b.relatedInformation = append(b.relatedInformation, &diagnostic)
349-
}
350-
}
351-
}
352-
if reportsUnnecessary, ok := vIncrementalBuildInfoDiagnostic["reportsUnnecessary"]; ok {
353-
if b.reportsUnnecessary, ok = reportsUnnecessary.(bool); !ok {
354-
return fmt.Errorf("invalid reportsUnnecessary in incrementalBuildInfoDiagnostic: expected boolean, got %T", reportsUnnecessary)
355-
}
356-
}
357-
if reportsDeprecated, ok := vIncrementalBuildInfoDiagnostic["reportsDeprecated"]; ok {
358-
if b.reportsDeprecated, ok = reportsDeprecated.(bool); !ok {
359-
return fmt.Errorf("invalid reportsDeprecated in incrementalBuildInfoDiagnostic: expected boolean, got %T", reportsDeprecated)
360-
}
361-
}
362-
if skippedOnNoEmit, ok := vIncrementalBuildInfoDiagnostic["skippedOnNoEmit"]; ok {
363-
if b.skippedOnNoEmit, ok = skippedOnNoEmit.(bool); !ok {
364-
return fmt.Errorf("invalid skippedOnNoEmit in incrementalBuildInfoDiagnostic: expected boolean, got %T", skippedOnNoEmit)
365-
}
366-
}
367-
return nil
368-
}
369-
370-
type BuildInfoDiagnosticOfFile struct {
371-
fileId BuildInfoFileId
372-
diagnostics []*BuildInfoDiagnostic
373-
}
374-
375-
func (b *BuildInfoDiagnosticOfFile) MarshalJSON() ([]byte, error) {
189+
// incrementalBuildInfoFileId if it is for a File thats other than its stored for
190+
File BuildInfoFileId `json:"file,omitzero"`
191+
NoFile bool `json:"noFile,omitzero"`
192+
Pos int `json:"pos,omitzero"`
193+
End int `json:"end,omitzero"`
194+
Code int32 `json:"code,omitzero"`
195+
Category diagnostics.Category `json:"category,omitzero"`
196+
Message string `json:"message,omitzero"`
197+
MessageChain []*BuildInfoDiagnostic `json:"messageChain,omitzero"`
198+
RelatedInformation []*BuildInfoDiagnostic `json:"relatedInformation,omitzero"`
199+
ReportsUnnecessary bool `json:"reportsUnnecessary,omitzero"`
200+
ReportsDeprecated bool `json:"reportsDeprecated,omitzero"`
201+
SkippedOnNoEmit bool `json:"skippedOnNoEmit,omitzero"`
202+
}
203+
204+
type BuildInfoDiagnosticsOfFile struct {
205+
FileId BuildInfoFileId
206+
Diagnostics []*BuildInfoDiagnostic
207+
}
208+
209+
func (b *BuildInfoDiagnosticsOfFile) MarshalJSON() ([]byte, error) {
376210
fileIdAndDiagnostics := make([]any, 0, 2)
377-
fileIdAndDiagnostics = append(fileIdAndDiagnostics, b.fileId)
378-
fileIdAndDiagnostics = append(fileIdAndDiagnostics, b.diagnostics)
211+
fileIdAndDiagnostics = append(fileIdAndDiagnostics, b.FileId)
212+
fileIdAndDiagnostics = append(fileIdAndDiagnostics, b.Diagnostics)
379213
return json.Marshal(fileIdAndDiagnostics)
380214
}
381215

382-
func (b *BuildInfoDiagnosticOfFile) UnmarshalJSON(data []byte) error {
216+
func (b *BuildInfoDiagnosticsOfFile) UnmarshalJSON(data []byte) error {
383217
var fileIdAndDiagnostics []any
384218
if err := json.Unmarshal(data, &fileIdAndDiagnostics); err != nil {
385-
return fmt.Errorf("invalid IncrementalBuildInfoDiagnostic: %s", data)
219+
return fmt.Errorf("invalid BuildInfoDiagnosticsOfFile: %s", data)
386220
}
387221
if len(fileIdAndDiagnostics) != 2 {
388-
return fmt.Errorf("invalid IncrementalBuildInfoDiagnostic: expected 2 elements, got %d", len(fileIdAndDiagnostics))
222+
return fmt.Errorf("invalid BuildInfoDiagnosticsOfFile: expected 2 elements, got %d", len(fileIdAndDiagnostics))
389223
}
390224
var fileId BuildInfoFileId
391225
if fileIdV, ok := fileIdAndDiagnostics[0].(float64); !ok {
392-
return fmt.Errorf("invalid fileId in IncrementalBuildInfoDiagnostic: expected float64, got %T", fileIdAndDiagnostics[0])
226+
return fmt.Errorf("invalid fileId in BuildInfoDiagnosticsOfFile: expected float64, got %T", fileIdAndDiagnostics[0])
393227
} else {
394228
fileId = BuildInfoFileId(fileIdV)
395229
}
396230
if diagnostics, ok := fileIdAndDiagnostics[1].([]*BuildInfoDiagnostic); ok {
397-
*b = BuildInfoDiagnosticOfFile{
398-
fileId: fileId,
399-
diagnostics: diagnostics,
231+
*b = BuildInfoDiagnosticsOfFile{
232+
FileId: fileId,
233+
Diagnostics: diagnostics,
400234
}
401235
return nil
402236
}
403-
return fmt.Errorf("invalid diagnostics in IncrementalBuildInfoDiagnostic: expected []*incrementalBuildInfoDiagnostic, got %T", fileIdAndDiagnostics[1])
237+
return fmt.Errorf("invalid diagnostics in BuildInfoDiagnosticsOfFile: expected []*BuildInfoDiagnostic, got %T", fileIdAndDiagnostics[1])
404238
}
405239

406240
type BuildInfoSemanticDiagnostic struct {
407-
FileId BuildInfoFileId // File is not in changedSet and still doesnt have cached diagnostics
408-
Diagnostic BuildInfoDiagnosticOfFile // Diagnostics for file
241+
FileId BuildInfoFileId // File is not in changedSet and still doesnt have cached diagnostics
242+
Diagnostics *BuildInfoDiagnosticsOfFile // Diagnostics for file
409243
}
410244

411245
func (b *BuildInfoSemanticDiagnostic) MarshalJSON() ([]byte, error) {
412246
if b.FileId != 0 {
413247
return json.Marshal(b.FileId)
414248
}
415-
return json.Marshal(b.Diagnostic)
249+
return json.Marshal(b.Diagnostics)
416250
}
417251

418252
func (b *BuildInfoSemanticDiagnostic) UnmarshalJSON(data []byte) error {
@@ -423,14 +257,14 @@ func (b *BuildInfoSemanticDiagnostic) UnmarshalJSON(data []byte) error {
423257
}
424258
return nil
425259
}
426-
var diagnostic BuildInfoDiagnosticOfFile
427-
if err := json.Unmarshal(data, &diagnostic); err == nil {
260+
var diagnostics BuildInfoDiagnosticsOfFile
261+
if err := json.Unmarshal(data, &diagnostics); err == nil {
428262
*b = BuildInfoSemanticDiagnostic{
429-
Diagnostic: diagnostic,
263+
Diagnostics: &diagnostics,
430264
}
431265
return nil
432266
}
433-
return fmt.Errorf("invalid IncrementalBuildInfoDiagnostic: %s", data)
267+
return fmt.Errorf("invalid BuildInfoSemanticDiagnostic: %s", data)
434268
}
435269

436270
/**
@@ -589,20 +423,20 @@ type BuildInfo struct {
589423
// Common between incremental and tsc -b buildinfo for non incremental programs
590424
Errors bool `json:"errors,omitzero"`
591425
CheckPending bool `json:"checkPending,omitzero"`
592-
// Root []BuildInfoRoot `json:"root,omitempty,omitzero"`
426+
// Root []BuildInfoRoot `json:"root,omitzero"`
593427

594428
// IncrementalProgram info
595429
FileNames []string `json:"fileNames,omitzero"`
596430
FileInfos []*BuildInfoFileInfo `json:"fileInfos,omitzero"`
597431
FileIdsList [][]BuildInfoFileId `json:"fileIdsList,omitzero"`
598-
Options *collections.OrderedMap[string, any] `json:"options,omitempty"`
599-
ReferencedMap []BuildInfoReferenceMapEntry `json:"referencedMap,omitzero"`
600-
SemanticDiagnosticsPerFile []BuildInfoSemanticDiagnostic `json:"semanticDiagnosticsPerFile,omitzero"`
601-
EmitDiagnosticsPerFile []BuildInfoDiagnosticOfFile `json:"emitDiagnosticsPerFile,omitzero"`
432+
Options *collections.OrderedMap[string, any] `json:"options,omitzero"`
433+
ReferencedMap []*BuildInfoReferenceMapEntry `json:"referencedMap,omitzero"`
434+
SemanticDiagnosticsPerFile []*BuildInfoSemanticDiagnostic `json:"semanticDiagnosticsPerFile,omitzero"`
435+
EmitDiagnosticsPerFile []*BuildInfoDiagnosticsOfFile `json:"emitDiagnosticsPerFile,omitzero"`
602436
ChangeFileSet []BuildInfoFileId `json:"changeFileSet,omitzero"`
603-
AffectedFilesPendingEmit []BuildInfoFilePendingEmit `json:"affectedFilesPendingEmit,omitzero"`
437+
AffectedFilesPendingEmit []*BuildInfoFilePendingEmit `json:"affectedFilesPendingEmit,omitzero"`
604438
LatestChangedDtsFile string `json:"latestChangedDtsFile,omitzero"` // Because this is only output file in the program, we dont need fileId to deduplicate name
605-
EmitSignatures []BuildInfoEmitSignature `json:"emitSignatures,omitzero"`
439+
EmitSignatures []*BuildInfoEmitSignature `json:"emitSignatures,omitzero"`
606440
// resolvedRoot: readonly IncrementalBuildInfoResolvedRoot[] | undefined;
607441
}
608442

0 commit comments

Comments
 (0)