Skip to content

Commit 522aedd

Browse files
committed
pass source line in diagnostic
1 parent c94e8d9 commit 522aedd

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

cmd/tsgo/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import (
44
"os"
5-
6-
"github.com/microsoft/typescript-go/internal/execute"
75
)
86

97
func main() {
@@ -14,12 +12,9 @@ func runMain() int {
1412
args := os.Args[1:]
1513
if len(args) > 0 {
1614
switch args[0] {
17-
case "--lsp":
18-
return runLSP(args[1:])
1915
case "--api":
2016
return runAPI(args[1:])
2117
}
2218
}
23-
result := execute.CommandLine(newSystem(), args, nil)
24-
return int(result.Status)
19+
return 1
2520
}

internal/ls/api.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type Diagnostic struct {
7575
ReportsUnnecessary bool `json:"reportsUnnecessary"`
7676
ReportsDeprecated bool `json:"reportsDeprecated"`
7777
SkippedOnNoEmit bool `json:"skippedOnNoEmit"`
78+
SourceLine string `json:"sourceLine"`
7879
}
7980

8081
type diagnosticMaps struct {
@@ -88,13 +89,26 @@ func (d *diagnosticMaps) addDiagnostic(diagnostic *ast.Diagnostic, ls *LanguageS
8889
}
8990
id := DiagnosticId(len(d.diagnosticMapById) + 1)
9091

92+
startPos := diagnostic.Loc().Pos()
93+
startPosLineCol := getPosition(diagnostic.File(), startPos, ls)
94+
lineMap := ls.converters.getLineMap(diagnostic.File().FileName())
95+
lineStartPos := lineMap.LineStarts[startPosLineCol.Line]
96+
var lineEndPos int
97+
if int(startPosLineCol.Line+1) >= len(lineMap.LineStarts) {
98+
lineEndPos = len(diagnostic.File().Text())
99+
} else {
100+
lineEndPos = int(lineMap.LineStarts[startPosLineCol.Line+1]) - 1
101+
}
102+
sourceLine := diagnostic.File().Text()[lineStartPos:lineEndPos]
103+
91104
diag := Diagnostic{
92105
Id: id,
93106
FileName: diagnostic.File().FileName(),
94-
Start: getPosition(diagnostic.File(), diagnostic.Loc().Pos(), ls),
107+
Start: startPosLineCol,
95108
End: getPosition(diagnostic.File(), diagnostic.Loc().End(), ls),
96-
StartPos: diagnostic.Loc().Pos(),
109+
StartPos: startPos,
97110
EndPos: diagnostic.Loc().End(),
111+
SourceLine: sourceLine,
98112
Code: diagnostic.Code(),
99113
Category: diagnostic.Category().Name(),
100114
Message: diagnostic.Message(),

0 commit comments

Comments
 (0)