Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support imported type aliases #225

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/matryer/moq

go 1.22.5
go 1.23

require (
github.com/pmezard/go-difflib v1.0.0
Expand Down
12 changes: 12 additions & 0 deletions internal/registry/method_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ func (m MethodScope) populateImports(t types.Type, imports map[string]*Package)
}
}

case *types.Alias:
if pkg := t.Obj().Pkg(); pkg != nil {
imports[stripVendorPath(pkg.Path())] = m.registry.AddImport(pkg)
}
// The imports of a Type with a TypeList must be added to the imports list
// For example: Foo[otherpackage.Bar] , must have otherpackage imported
if targs := t.TypeArgs(); targs != nil {
for i := 0; i < targs.Len(); i++ {
m.populateImports(targs.At(i), imports)
}
}

case *types.Array:
m.populateImports(t.Elem(), imports)

Expand Down
6 changes: 6 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"Magician"},
goldenFile: filepath.Join("testpackages/rangenum", "rangenum_moq.golden.go"),
},
{
name: "TypeAlias",
cfg: Config{SrcDir: "testpackages/typealias"},
interfaces: []string{"Example"},
goldenFile: filepath.Join("testpackages/typealias", "typealias_moq.golden.go"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/moq/testpackages/typealias/typealias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package typealias

import (
"github.com/matryer/moq/pkg/moq/testpackages/typealiastwo"
)

type Example interface {

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type Example should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type Example should have comment or be unexported
Do(a typealiastwo.AliasType, b typealiastwo.GenericAliasType) error
}
81 changes: 81 additions & 0 deletions pkg/moq/testpackages/typealias/typealias_moq.golden.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package typealiasinternal

// we shouldn't be able to import these types directly, you need to use the alias in the parent package

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)
type MyInternalType struct {
sudo-suhas marked this conversation as resolved.
Show resolved Hide resolved
Foo int
}

type MyGenericType[T any] struct {

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type MyGenericType should have comment or be unexported

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type MyGenericType should have comment or be unexported
A T
}
7 changes: 7 additions & 0 deletions pkg/moq/testpackages/typealiastwo/typealiastwo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package typealiastwo

import "github.com/matryer/moq/pkg/moq/testpackages/typealiastwo/internal/typealiasinternal"

type AliasType = typealiasinternal.MyInternalType

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type AliasType should have comment or be unexported

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type AliasType should have comment or be unexported

type GenericAliasType = typealiasinternal.MyGenericType[int]

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type GenericAliasType should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type GenericAliasType should have comment or be unexported
Loading