Skip to content

Commit f635d1a

Browse files
authored
Merge pull request #59 from nametake/refactor
Refactor
2 parents cb03c97 + 4debbeb commit f635d1a

14 files changed

+135
-30
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/nametake/golangci-lint-langserver
22

33
go 1.23.4
44

5-
require github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2
5+
require github.com/sourcegraph/jsonrpc2 v0.2.0
66

77
require github.com/google/go-cmp v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
22
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
44
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
5-
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2 h1:5VGNYxMxzZ8Jb2bARgVl1DNg8vpcd9S8b4MbbjWQ8/w=
6-
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
5+
github.com/sourcegraph/jsonrpc2 v0.2.0 h1:KjN/dC4fP6aN9030MZCJs9WQbTOjWHhrtKVpzzSrr/U=
6+
github.com/sourcegraph/jsonrpc2 v0.2.0/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=

golangci-lint.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package main
33
import "strings"
44

55
type Issue struct {
6-
FromLinter string `json:"FromLinter"`
7-
Text string `json:"Text"`
8-
Severity string `json:"Severity"`
9-
SourceLines []string `json:"SourceLines"`
10-
Replacement interface{} `json:"Replacement"`
6+
FromLinter string `json:"FromLinter"`
7+
Text string `json:"Text"`
8+
Severity string `json:"Severity"`
9+
SourceLines []string `json:"SourceLines"`
10+
Replacement any `json:"Replacement"`
1111
Pos struct {
1212
Filename string `json:"Filename"`
1313
Offset int `json:"Offset"`
@@ -42,7 +42,6 @@ func (i Issue) DiagSeverity() DiagnosticSeverity {
4242
}
4343
}
4444

45-
//nolint:unused,deadcode
4645
type GolangCILintResult struct {
4746
Issues []Issue `json:"Issues"`
4847
Report struct {
@@ -51,5 +50,6 @@ type GolangCILintResult struct {
5150
Enabled bool `json:"Enabled"`
5251
EnabledByDefault bool `json:"EnabledByDefault,omitempty"`
5352
} `json:"Linters"`
53+
Error string `json:"Error"`
5454
} `json:"Report"`
5555
}

handler.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
5656
args := make([]string, 0, len(h.command))
5757
args = append(args, h.command[1:]...)
5858
args = append(args, dir)
59-
//nolint:gosec
6059
cmd := exec.Command(h.command[0], args...)
6160
if strings.HasPrefix(path, h.rootDir) {
6261
cmd.Dir = h.rootDir
6362
file = path[len(h.rootDir)+1:]
6463
} else {
6564
cmd.Dir = dir
6665
}
67-
h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd", cmd)
66+
67+
h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd:", cmd.Args)
6868

6969
b, err := cmd.Output()
7070
if err == nil {
@@ -134,7 +134,7 @@ func (h *langHandler) linter() {
134134

135135
diagnostics, err := h.lint(uri)
136136
if err != nil {
137-
h.logger.Printf("%s", err)
137+
h.logger.Printf("%s\n", err)
138138

139139
continue
140140
}
@@ -146,12 +146,12 @@ func (h *langHandler) linter() {
146146
URI: uri,
147147
Diagnostics: diagnostics,
148148
}); err != nil {
149-
h.logger.Printf("%s", err)
149+
h.logger.Printf("%s\n", err)
150150
}
151151
}
152152
}
153153

154-
func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
154+
func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
155155
h.logger.DebugJSON("golangci-lint-langserver: request:", req)
156156

157157
switch req.Method {
@@ -176,7 +176,7 @@ func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *json
176176
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}
177177
}
178178

179-
func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
179+
func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
180180
var params InitializeParams
181181
if err := json.Unmarshal(*req.Params, &params); err != nil {
182182
return nil, err
@@ -198,13 +198,13 @@ func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, r
198198
}, nil
199199
}
200200

201-
func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
201+
func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
202202
close(h.request)
203203

204204
return nil, nil
205205
}
206206

207-
func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
207+
func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
208208
var params DidOpenTextDocumentParams
209209
if err := json.Unmarshal(*req.Params, &params); err != nil {
210210
return nil, err
@@ -215,15 +215,15 @@ func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.C
215215
return nil, nil
216216
}
217217

218-
func (h *langHandler) handleTextDocumentDidClose(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
218+
func (h *langHandler) handleTextDocumentDidClose(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
219219
return nil, nil
220220
}
221221

222-
func (h *langHandler) handleTextDocumentDidChange(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
222+
func (h *langHandler) handleTextDocumentDidChange(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
223223
return nil, nil
224224
}
225225

226-
func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
226+
func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
227227
var params DidSaveTextDocumentParams
228228
if err := json.Unmarshal(*req.Params, &params); err != nil {
229229
return nil, err
@@ -234,6 +234,6 @@ func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.C
234234
return nil, nil
235235
}
236236

237-
func (h *langHandler) handlerWorkspaceDidChangeConfiguration(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
237+
func (h *langHandler) handlerWorkspaceDidChangeConfiguration(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
238238
return nil, nil
239239
}

handler_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,79 @@ func TestLangHandler_lint_Integration(t *testing.T) {
166166
},
167167
},
168168
},
169+
{
170+
name: "monorepo with multiple go.mod and .golangci.yaml files (foo module)",
171+
h: &langHandler{
172+
logger: newStdLogger(false),
173+
command: command,
174+
rootDir: filepath.Dir("./testdata/monorepo"),
175+
},
176+
filePath: "./testdata/monorepo/foo/main.go",
177+
want: []Diagnostic{
178+
{
179+
Range: Range{
180+
Start: Position{
181+
Line: 8,
182+
Character: 0,
183+
},
184+
End: Position{
185+
Line: 8,
186+
Character: 0,
187+
},
188+
},
189+
Severity: DSWarning,
190+
Code: nil,
191+
Source: pt("wsl"),
192+
Message: "wsl: block should not end with a whitespace (or comment)",
193+
RelatedInformation: nil,
194+
},
195+
},
196+
},
197+
{
198+
name: "monorepo with multiple go.mod and .golangci.yaml files (bar module)",
199+
h: &langHandler{
200+
logger: newStdLogger(false),
201+
command: command,
202+
rootDir: filepath.Dir("./testdata/monorepo"),
203+
},
204+
filePath: "./testdata/monorepo/bar/main.go",
205+
want: []Diagnostic{
206+
{
207+
Range: Range{
208+
Start: Position{
209+
Line: 3,
210+
Character: 4,
211+
},
212+
End: Position{
213+
Line: 3,
214+
Character: 4,
215+
},
216+
},
217+
Severity: DSWarning,
218+
Code: nil,
219+
Source: pt("unused"),
220+
Message: "unused: var foo is unused",
221+
RelatedInformation: nil,
222+
},
223+
{
224+
Range: Range{
225+
Start: Position{
226+
Line: 8,
227+
Character: 0,
228+
},
229+
End: Position{
230+
Line: 8,
231+
Character: 0,
232+
},
233+
},
234+
Severity: DSWarning,
235+
Code: nil,
236+
Source: pt("wsl"),
237+
Message: "wsl: block should not end with a whitespace (or comment)",
238+
RelatedInformation: nil,
239+
},
240+
},
241+
},
169242
}
170243

171244
for _, tt := range tests {

logger.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package main
33
import (
44
"encoding/json"
55
"log"
6-
"os"
76
)
87

98
var _ logger = (*stdLogger)(nil)
109

1110
type logger interface {
12-
Printf(format string, args ...interface{})
13-
DebugJSON(label string, arg interface{})
11+
Printf(format string, args ...any)
12+
DebugJSON(label string, arg any)
1413
}
1514

1615
type stdLogger struct {
@@ -21,22 +20,23 @@ type stdLogger struct {
2120
func newStdLogger(debug bool) *stdLogger {
2221
return &stdLogger{
2322
debug: debug,
24-
stderr: log.New(os.Stderr, "", 0),
23+
stderr: log.New(log.Writer(), "", log.LstdFlags),
2524
}
2625
}
2726

28-
func (l *stdLogger) Printf(format string, args ...interface{}) {
27+
func (l *stdLogger) Printf(format string, args ...any) {
2928
l.stderr.Printf(format, args...)
3029
}
3130

32-
func (l *stdLogger) DebugJSON(label string, arg interface{}) {
31+
func (l *stdLogger) DebugJSON(label string, arg any) {
3332
if !l.debug {
3433
return
3534
}
3635

3736
b, err := json.Marshal(arg)
3837
if err != nil {
3938
l.stderr.Println(err)
39+
return
4040
}
4141

4242
l.stderr.Println(label, string(b))

lsp.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type InitializeResult struct {
1717

1818
type TextDocumentSyncKind int
1919

20-
//nolint:unused,deadcode
2120
const (
2221
TDSKNone TextDocumentSyncKind = iota
2322
TDSKFull
@@ -89,7 +88,6 @@ type DiagnosticRelatedInformation struct {
8988

9089
type DiagnosticSeverity int
9190

92-
//nolint:unused,deadcode
9391
const (
9492
DSError DiagnosticSeverity = iota + 1
9593
DSWarning

testdata/monorepo/bar/.golangci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
linters:
3+
enable:
4+
- unused
5+
- wsl

testdata/monorepo/bar/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module foo
2+
3+
go 1.22.10

testdata/monorepo/bar/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
var bar = "bar"
4+
var foo = "foo"
5+
6+
func Bar() {
7+
_ = bar
8+
9+
}

testdata/monorepo/foo/.golangci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
linters:
3+
enable:
4+
- wsl
5+
disable:
6+
- unused

testdata/monorepo/foo/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module foo
2+
3+
go 1.22.10

testdata/monorepo/foo/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
var bar = "bar"
4+
var foo = "foo"
5+
6+
func Bar() {
7+
_ = bar
8+
9+
}

uri.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func uriToPath(uri string) string {
2727
}
2828

2929
func isWindowsDriveURIPath(uri string) bool {
30-
//nolint:gomnd
3130
if len(uri) < 4 {
3231
return false
3332
}

0 commit comments

Comments
 (0)