Skip to content

Commit d5bc859

Browse files
authored
Support *ast.SelectorExpr aliases (#881)
* support interface alias
1 parent bb1e69e commit d5bc859

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

pkg/fixtures/iface_new_type/iface_new_type_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestParsing(t *testing.T) {
1414
require.NoError(t, parser.ParsePackages(ctx, []string{"github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type"}))
1515
require.NoError(t, parser.Load(ctx))
1616

17-
for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3"} {
17+
for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3", "Interface4"} {
1818
iface, err := parser.Find(ifaceName)
1919
require.NoError(t, err)
2020
require.NotNil(t, iface)
@@ -33,4 +33,8 @@ func TestUsage(t *testing.T) {
3333
interface3 := NewMockInterface3(t)
3434
interface3.EXPECT().Method1().Return()
3535
interface3.Method1()
36+
37+
interface4 := NewMockInterface4(t)
38+
interface4.EXPECT().Method1().Return()
39+
interface4.Method1()
3640
}

pkg/fixtures/iface_new_type/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Interface1 interface {
99
type (
1010
Interface2 Interface1
1111
Interface3 subpkg.SubPkgInterface
12+
Interface4 = subpkg.SubPkgInterface
1213
)

pkg/fixtures/iface_new_type/mock_interface_4_test.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/parse.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,24 @@ func (p *Parser) packageInterfaces(
247247
continue
248248
}
249249

250-
typ, ok := obj.Type().(*types.Named)
250+
var typ *types.Named
251+
var name string
252+
253+
ttyp := obj.Type()
254+
255+
if talias, ok := obj.Type().(*types.Alias); ok {
256+
name = talias.Obj().Name()
257+
ttyp = types.Unalias(obj.Type())
258+
}
259+
260+
typ, ok := ttyp.(*types.Named)
251261
if !ok {
252262
continue
253263
}
254264

255-
name = typ.Obj().Name()
265+
if name == "" {
266+
name = typ.Obj().Name()
267+
}
256268

257269
if typ.Obj().Pkg() == nil {
258270
continue

0 commit comments

Comments
 (0)