Skip to content

Commit

Permalink
Support *ast.SelectorExpr aliases (#881)
Browse files Browse the repository at this point in the history
* support interface alias
  • Loading branch information
RangelReale authored Jan 3, 2025
1 parent bb1e69e commit d5bc859
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/fixtures/iface_new_type/iface_new_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestParsing(t *testing.T) {
require.NoError(t, parser.ParsePackages(ctx, []string{"github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type"}))
require.NoError(t, parser.Load(ctx))

for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3"} {
for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3", "Interface4"} {
iface, err := parser.Find(ifaceName)
require.NoError(t, err)
require.NotNil(t, iface)
Expand All @@ -33,4 +33,8 @@ func TestUsage(t *testing.T) {
interface3 := NewMockInterface3(t)
interface3.EXPECT().Method1().Return()
interface3.Method1()

interface4 := NewMockInterface4(t)
interface4.EXPECT().Method1().Return()
interface4.Method1()
}
1 change: 1 addition & 0 deletions pkg/fixtures/iface_new_type/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Interface1 interface {
type (
Interface2 Interface1
Interface3 subpkg.SubPkgInterface
Interface4 = subpkg.SubPkgInterface
)
64 changes: 64 additions & 0 deletions pkg/fixtures/iface_new_type/mock_interface_4_test.go

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

16 changes: 14 additions & 2 deletions pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,24 @@ func (p *Parser) packageInterfaces(
continue
}

typ, ok := obj.Type().(*types.Named)
var typ *types.Named
var name string

ttyp := obj.Type()

if talias, ok := obj.Type().(*types.Alias); ok {
name = talias.Obj().Name()
ttyp = types.Unalias(obj.Type())
}

typ, ok := ttyp.(*types.Named)
if !ok {
continue
}

name = typ.Obj().Name()
if name == "" {
name = typ.Obj().Name()
}

if typ.Obj().Pkg() == nil {
continue
Expand Down

0 comments on commit d5bc859

Please sign in to comment.