Skip to content

Commit

Permalink
support import shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasy1217 committed Jul 11, 2024
1 parent 2c0a7fd commit a9a24c1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mockgen/internal/tests/import_shadowing/foo/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package foo

type MyInt int
11 changes: 11 additions & 0 deletions mockgen/internal/tests/import_shadowing/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package import_shadowing

import (
"go.uber.org/mock/mockgen/internal/tests/import_shadowing/foo"
)

//go:generate mockgen -destination mock.go -source import.go -package import_shadowing . Bar

type Bar interface {
Hoge(foo string) foo.MyInt
}
59 changes: 59 additions & 0 deletions mockgen/internal/tests/import_shadowing/mock.go

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

17 changes: 17 additions & 0 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
i++
}

isImportShadowing := func() bool {
for _, intf := range pkg.Interfaces {
for _, m := range intf.Methods {
for _, i := range m.In {
if i.Name == pkgName {
return true
}
}
}
}
return false
}

if isImportShadowing() {
pkgName = base + "Pkg"
}

// Avoid importing package if source pkg == output pkg
if pth == pkg.PkgPath && outputPackagePath == pkg.PkgPath {
continue
Expand Down

0 comments on commit a9a24c1

Please sign in to comment.