Skip to content

Commit 8c178d3

Browse files
authored
add protogetter linter (#4069)
1 parent 2d5d29f commit 8c178d3

File tree

13 files changed

+732
-0
lines changed

13 files changed

+732
-0
lines changed

Diff for: .golangci.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,7 @@ linters:
22632263
- prealloc
22642264
- predeclared
22652265
- promlinter
2266+
- protogetter
22662267
- reassign
22672268
- revive
22682269
- rowserrcheck
@@ -2378,6 +2379,7 @@ linters:
23782379
- prealloc
23792380
- predeclared
23802381
- promlinter
2382+
- protogetter
23812383
- reassign
23822384
- revive
23832385
- rowserrcheck

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/fatih/color v1.15.0
3535
github.com/firefart/nonamedreturns v1.0.4
3636
github.com/fzipp/gocyclo v0.6.0
37+
github.com/ghostiam/protogetter v0.2.2
3738
github.com/go-critic/go-critic v0.9.0
3839
github.com/go-xmlfmt/xmlfmt v1.1.2
3940
github.com/gofrs/flock v0.8.1

Diff for: go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/golinters/protogetter.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package golinters
2+
3+
import (
4+
"sync"
5+
6+
"github.com/ghostiam/protogetter"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
10+
"github.com/golangci/golangci-lint/pkg/lint/linter"
11+
"github.com/golangci/golangci-lint/pkg/result"
12+
)
13+
14+
func NewProtoGetter() *goanalysis.Linter {
15+
var mu sync.Mutex
16+
var resIssues []goanalysis.Issue
17+
18+
a := protogetter.NewAnalyzer()
19+
a.Run = func(pass *analysis.Pass) (any, error) {
20+
pgIssues := protogetter.Run(pass, protogetter.GolangciLintMode)
21+
22+
issues := make([]goanalysis.Issue, len(pgIssues))
23+
for i, issue := range pgIssues {
24+
report := &result.Issue{
25+
FromLinter: a.Name,
26+
Pos: issue.Pos,
27+
Text: issue.Message,
28+
Replacement: &result.Replacement{
29+
Inline: &result.InlineFix{
30+
StartCol: issue.InlineFix.StartCol,
31+
Length: issue.InlineFix.Length,
32+
NewString: issue.InlineFix.NewString,
33+
},
34+
},
35+
}
36+
37+
issues[i] = goanalysis.NewIssue(report, pass)
38+
}
39+
40+
if len(issues) == 0 {
41+
return nil, nil
42+
}
43+
44+
mu.Lock()
45+
resIssues = append(resIssues, issues...)
46+
mu.Unlock()
47+
48+
return nil, nil
49+
}
50+
51+
return goanalysis.NewLinter(
52+
a.Name,
53+
a.Doc,
54+
[]*analysis.Analyzer{a},
55+
nil,
56+
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
57+
return resIssues
58+
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
59+
}

Diff for: pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
717717
WithPresets(linter.PresetStyle).
718718
WithURL("https://github.com/yeya24/promlinter"),
719719

720+
linter.NewConfig(golinters.NewProtoGetter()).
721+
WithSince("v1.55.0").
722+
WithPresets(linter.PresetBugs).
723+
WithLoadForGoAnalysis().
724+
WithAutoFix().
725+
WithURL("https://github.com/ghostiam/protogetter"),
726+
720727
linter.NewConfig(golinters.NewReassign(reassignCfg)).
721728
WithSince("1.49.0").
722729
WithPresets(linter.PresetBugs).

Diff for: test/linters_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestSourcesFromTestdataSubDir(t *testing.T) {
3232
"loggercheck",
3333
"ginkgolinter",
3434
"zerologlint",
35+
"protogetter",
3536
}
3637

3738
for _, dir := range subDirs {

Diff for: test/testdata/protogetter/go.mod

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module protogetter
2+
3+
go 1.19
4+
5+
require (
6+
google.golang.org/grpc v1.57.0
7+
google.golang.org/protobuf v1.31.0
8+
)
9+
10+
require (
11+
github.com/golang/protobuf v1.5.3 // indirect
12+
golang.org/x/net v0.9.0 // indirect
13+
golang.org/x/sys v0.7.0 // indirect
14+
golang.org/x/text v0.9.0 // indirect
15+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
16+
)

Diff for: test/testdata/protogetter/go.sum

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/testdata/protogetter/proto/test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package proto
2+
3+
func (x *Embedded) CustomMethod() interface{} {
4+
return nil
5+
}
6+
7+
type Other struct {
8+
}
9+
10+
func (x *Other) MyMethod(certs *Test) *Embedded {
11+
return nil
12+
}
13+
14+
func (x *Test) Equal(v *Test) bool {
15+
return false
16+
}
17+
18+
func (x *Embedded) SetS(s string) {
19+
x.S = s
20+
}
21+
22+
func (x *Embedded) SetMap(_ map[string]string) {
23+
}

0 commit comments

Comments
 (0)