@@ -22,18 +22,32 @@ const (
22
22
23
23
const msgFormat = "avoid direct access to proto field %s, use %s instead"
24
24
25
- func NewAnalyzer () * analysis.Analyzer {
25
+ func NewAnalyzer (cfg * Config ) * analysis.Analyzer {
26
26
return & analysis.Analyzer {
27
27
Name : "protogetter" ,
28
28
Doc : "Reports direct reads from proto message fields when getters should be used" ,
29
29
Run : func (pass * analysis.Pass ) (any , error ) {
30
- Run (pass , StandaloneMode )
30
+ Run (pass , cfg )
31
31
return nil , nil
32
32
},
33
33
}
34
34
}
35
35
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
+
37
51
nodeTypes := []ast.Node {
38
52
(* ast .AssignStmt )(nil ),
39
53
(* ast .CallExpr )(nil ),
@@ -45,7 +59,7 @@ func Run(pass *analysis.Pass, mode Mode) []Issue {
45
59
// Skip protoc-generated files.
46
60
var files []* ast.File
47
61
for _ , f := range pass .Files {
48
- if ! isProtocGeneratedFile ( f ) {
62
+ if ! skipGeneratedFile ( f , skipGeneratedBy ) {
49
63
files = append (files , f )
50
64
51
65
// ast.Print(pass.Fset, f)
@@ -63,7 +77,7 @@ func Run(pass *analysis.Pass, mode Mode) []Issue {
63
77
return
64
78
}
65
79
66
- switch mode {
80
+ switch cfg . Mode {
67
81
case StandaloneMode :
68
82
pass .Report (report .ToDiagReport ())
69
83
case GolangciLintMode :
@@ -168,8 +182,18 @@ func (r *Report) ToIssue(fset *token.FileSet) Issue {
168
182
}
169
183
}
170
184
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
173
197
}
174
198
175
199
func formatNode (node ast.Node ) string {
0 commit comments