Skip to content

Commit a24d332

Browse files
committed
add config support + add skip protoc-gen-grpc-gateway generated files
1 parent d7b06e7 commit a24d332

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

cmd/protogetter/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ import (
77
)
88

99
func main() {
10-
singlechecker.Main(protogetter.NewAnalyzer())
10+
cfg := &protogetter.Config{
11+
Mode: protogetter.StandaloneMode,
12+
}
13+
14+
singlechecker.Main(protogetter.NewAnalyzer(cfg))
1115
}

protogetter.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,32 @@ const (
2222

2323
const msgFormat = "avoid direct access to proto field %s, use %s instead"
2424

25-
func NewAnalyzer() *analysis.Analyzer {
25+
func NewAnalyzer(cfg *Config) *analysis.Analyzer {
2626
return &analysis.Analyzer{
2727
Name: "protogetter",
2828
Doc: "Reports direct reads from proto message fields when getters should be used",
2929
Run: func(pass *analysis.Pass) (any, error) {
30-
Run(pass, StandaloneMode)
30+
Run(pass, cfg)
3131
return nil, nil
3232
},
3333
}
3434
}
3535

36-
func Run(pass *analysis.Pass, mode Mode) []Issue {
36+
type Config struct {
37+
Mode Mode
38+
SkipGeneratedBy []string
39+
}
40+
41+
func Run(pass *analysis.Pass, cfg *Config) []Issue {
42+
// Always skip files generated by protoc-gen-go and protoc-gen-grpc-gateway.
43+
skipGeneratedBy := []string{"protoc-gen-go", "protoc-gen-grpc-gateway"}
44+
for _, s := range cfg.SkipGeneratedBy {
45+
if strings.TrimSpace(s) == "" {
46+
continue
47+
}
48+
skipGeneratedBy = append(skipGeneratedBy, s)
49+
}
50+
3751
nodeTypes := []ast.Node{
3852
(*ast.AssignStmt)(nil),
3953
(*ast.CallExpr)(nil),
@@ -45,7 +59,7 @@ func Run(pass *analysis.Pass, mode Mode) []Issue {
4559
// Skip protoc-generated files.
4660
var files []*ast.File
4761
for _, f := range pass.Files {
48-
if !isProtocGeneratedFile(f) {
62+
if !skipGeneratedFile(f, skipGeneratedBy) {
4963
files = append(files, f)
5064

5165
// ast.Print(pass.Fset, f)
@@ -63,7 +77,7 @@ func Run(pass *analysis.Pass, mode Mode) []Issue {
6377
return
6478
}
6579

66-
switch mode {
80+
switch cfg.Mode {
6781
case StandaloneMode:
6882
pass.Report(report.ToDiagReport())
6983
case GolangciLintMode:
@@ -168,8 +182,18 @@ func (r *Report) ToIssue(fset *token.FileSet) Issue {
168182
}
169183
}
170184

171-
func isProtocGeneratedFile(f *ast.File) bool {
172-
return len(f.Comments) > 0 && strings.HasPrefix(f.Comments[0].Text(), "Code generated by protoc-gen-go")
185+
func skipGeneratedFile(f *ast.File, prefixes []string) bool {
186+
if len(f.Comments) == 0 {
187+
return false
188+
}
189+
190+
for _, prefix := range prefixes {
191+
if strings.HasPrefix(f.Comments[0].Text(), "Code generated by "+prefix) {
192+
return true
193+
}
194+
}
195+
196+
return false
173197
}
174198

175199
func formatNode(node ast.Node) string {

protogetter_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
)
1010

1111
func Test(t *testing.T) {
12+
cfg := &protogetter.Config{}
13+
1214
testdata := analysistest.TestData()
13-
analysistest.RunWithSuggestedFixes(t, testdata, protogetter.NewAnalyzer())
15+
analysistest.RunWithSuggestedFixes(t, testdata, protogetter.NewAnalyzer(cfg))
1416

15-
analysistest.Run(t, testdata, protogetter.NewAnalyzer(), "./proto/...")
17+
analysistest.Run(t, testdata, protogetter.NewAnalyzer(cfg), "./proto/...")
1618
}

0 commit comments

Comments
 (0)