Skip to content

Commit

Permalink
add config support + add skip protoc-gen-grpc-gateway generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostiam committed Oct 25, 2023
1 parent d7b06e7 commit a24d332
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmd/protogetter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ import (
)

func main() {
singlechecker.Main(protogetter.NewAnalyzer())
cfg := &protogetter.Config{
Mode: protogetter.StandaloneMode,
}

singlechecker.Main(protogetter.NewAnalyzer(cfg))
}
38 changes: 31 additions & 7 deletions protogetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ const (

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

func NewAnalyzer() *analysis.Analyzer {
func NewAnalyzer(cfg *Config) *analysis.Analyzer {
return &analysis.Analyzer{
Name: "protogetter",
Doc: "Reports direct reads from proto message fields when getters should be used",
Run: func(pass *analysis.Pass) (any, error) {
Run(pass, StandaloneMode)
Run(pass, cfg)
return nil, nil
},
}
}

func Run(pass *analysis.Pass, mode Mode) []Issue {
type Config struct {
Mode Mode
SkipGeneratedBy []string
}

func Run(pass *analysis.Pass, cfg *Config) []Issue {
// Always skip files generated by protoc-gen-go and protoc-gen-grpc-gateway.
skipGeneratedBy := []string{"protoc-gen-go", "protoc-gen-grpc-gateway"}
for _, s := range cfg.SkipGeneratedBy {
if strings.TrimSpace(s) == "" {
continue
}
skipGeneratedBy = append(skipGeneratedBy, s)
}

nodeTypes := []ast.Node{
(*ast.AssignStmt)(nil),
(*ast.CallExpr)(nil),
Expand All @@ -45,7 +59,7 @@ func Run(pass *analysis.Pass, mode Mode) []Issue {
// Skip protoc-generated files.
var files []*ast.File
for _, f := range pass.Files {
if !isProtocGeneratedFile(f) {
if !skipGeneratedFile(f, skipGeneratedBy) {
files = append(files, f)

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

switch mode {
switch cfg.Mode {
case StandaloneMode:
pass.Report(report.ToDiagReport())
case GolangciLintMode:
Expand Down Expand Up @@ -168,8 +182,18 @@ func (r *Report) ToIssue(fset *token.FileSet) Issue {
}
}

func isProtocGeneratedFile(f *ast.File) bool {
return len(f.Comments) > 0 && strings.HasPrefix(f.Comments[0].Text(), "Code generated by protoc-gen-go")
func skipGeneratedFile(f *ast.File, prefixes []string) bool {
if len(f.Comments) == 0 {
return false
}

for _, prefix := range prefixes {
if strings.HasPrefix(f.Comments[0].Text(), "Code generated by "+prefix) {
return true
}
}

return false
}

func formatNode(node ast.Node) string {
Expand Down
6 changes: 4 additions & 2 deletions protogetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
)

func Test(t *testing.T) {
cfg := &protogetter.Config{}

testdata := analysistest.TestData()
analysistest.RunWithSuggestedFixes(t, testdata, protogetter.NewAnalyzer())
analysistest.RunWithSuggestedFixes(t, testdata, protogetter.NewAnalyzer(cfg))

analysistest.Run(t, testdata, protogetter.NewAnalyzer(), "./proto/...")
analysistest.Run(t, testdata, protogetter.NewAnalyzer(cfg), "./proto/...")
}

0 comments on commit a24d332

Please sign in to comment.