Skip to content

Commit

Permalink
add skip any generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostiam committed Oct 27, 2023
1 parent e8505c9 commit b213f22
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions protogetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ func flags(opts *Config) flag.FlagSet {
}
return nil
})
fs.BoolVar(&opts.SkipAnyGenerated, "skip-any-generated", false, "skip any generated files")

return *fs
}

type Config struct {
Mode Mode // Zero value is StandaloneMode.
SkipGeneratedBy []string `short:"g" long:"skip-generated-by" description:"Skip files generated with the given prefixes"`
SkipFiles []string `short:"f" long:"skip-files" description:"Skip files with the given glob patterns"`
Mode Mode // Zero value is StandaloneMode.
SkipGeneratedBy []string
SkipFiles []string
SkipAnyGenerated bool
}

func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {
Expand Down Expand Up @@ -104,7 +106,7 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {
// Skip filtered files.
var files []*ast.File
for _, f := range pass.Files {
if skipGeneratedFile(f, skipGeneratedBy) {
if skipGeneratedFile(f, skipGeneratedBy, cfg.SkipAnyGenerated) {
continue
}

Expand Down Expand Up @@ -233,13 +235,19 @@ func (r *Report) ToIssue(fset *token.FileSet) Issue {
}
}

func skipGeneratedFile(f *ast.File, prefixes []string) bool {
func skipGeneratedFile(f *ast.File, prefixes []string, skipAny bool) bool {
if len(f.Comments) == 0 {
return false
}
firstComment := f.Comments[0].Text()

// https://golang.org/s/generatedcode
if skipAny && strings.HasPrefix(firstComment, "Code generated") {
return true
}

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

0 comments on commit b213f22

Please sign in to comment.