Skip to content

Commit

Permalink
added ignoring interfaces if they have a comment "gomock:ignore" (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
tulzke committed Aug 30, 2023
1 parent 665220d commit 01264d0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
21 changes: 21 additions & 0 deletions mockgen/internal/tests/ignore/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ignore

//go:generate mockgen -source=interfaces.go -destination=mock.go -package=ignore

//gomock:ignore
type IgnoreMe interface{}

//gomock:ignore
type (
IgnoreMe2 interface{}
)

type (
//gomock:ignore
IgnoreMe3 interface{}
)

// GenerateMockForMe some interface
type GenerateMockForMe interface {
B() bool
}
52 changes: 52 additions & 0 deletions mockgen/internal/tests/ignore/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 72 additions & 1 deletion mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func sourceMode(source string) (*model.Package, error) {
}

fs := token.NewFileSet()
file, err := parser.ParseFile(fs, source, nil, 0)
file, err := parser.ParseFile(fs, source, nil, parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("failed parsing source file %v: %v", source, err)
}
Expand Down Expand Up @@ -221,8 +221,14 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag
}
}

ic := newIgnoreChecker(p.fileSet, file)

var is []*model.Interface
for ni := range iterInterfaces(file) {
if ic.isIgnore(ni.name.String()) {
continue
}

i, err := p.parseInterface(ni.name.String(), importPath, ni)
if err != nil {
return nil, err
Expand Down Expand Up @@ -257,7 +263,13 @@ func (p *fileParser) parsePackage(path string) (*fileParser, error) {

for _, pkg := range pkgs {
file := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates)
ic := newIgnoreChecker(p.fileSet, file)

for ni := range iterInterfaces(file) {
if ic.isIgnore(ni.name.String()) {
continue
}

newP.importedInterfaces.Set(path, ni.name.Name, ni)
}
imports, _ := importsOfFile(file)
Expand Down Expand Up @@ -660,6 +672,52 @@ func (p *fileParser) parseArrayLength(expr ast.Expr) (string, error) {
}
}

const (
ignoreComment = "gomock:ignore"
)

type ignoreChecker struct {
ignoreNames map[string]struct{}
}

func newIgnoreChecker(fileSet *token.FileSet, file *ast.File) ignoreChecker {
ignoreTypeNames := make(map[string]struct{}, len(file.Decls))
commentsMap := ast.NewCommentMap(fileSet, file, file.Comments)

for _, decl := range file.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}

ignoreAllSpecs := hasIgnoreComments(commentsMap[decl])
for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}

if ignoreAllSpecs {
ignoreTypeNames[ts.Name.String()] = struct{}{}
continue
}

if hasIgnoreComments(commentsMap[spec]) {
ignoreTypeNames[ts.Name.Name] = struct{}{}
}
}
}

return ignoreChecker{ignoreNames: ignoreTypeNames}
}

// isIgnore returns true if such an interface needs to be ignored
func (l ignoreChecker) isIgnore(name string) bool {
_, ok := l.ignoreNames[name]

return ok
}

// importsOfFile returns a map of package name to import path
// of the imports in file.
func importsOfFile(file *ast.File) (normalImports map[string]importedPackage, dotImports []string) {
Expand Down Expand Up @@ -766,6 +824,19 @@ func isVariadic(f *ast.FuncType) bool {
return ok
}

// hasIgnoreComments returns true if the comment contains ignoreComment
func hasIgnoreComments(commentsGroups []*ast.CommentGroup) bool {
for _, commentGroup := range commentsGroups {
for _, comment := range commentGroup.List {
if strings.Contains(comment.Text, ignoreComment) {
return true
}
}
}

return false
}

// packageNameOfDir get package import path via dir
func packageNameOfDir(srcDir string) (string, error) {
files, err := os.ReadDir(srcDir)
Expand Down

0 comments on commit 01264d0

Please sign in to comment.