@@ -3,6 +3,7 @@ package registry
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "go/ast"
6
7
"go/types"
7
8
"path/filepath"
8
9
"sort"
@@ -16,38 +17,40 @@ import (
16
17
// imports and ensures there are no conflicts in the imported package
17
18
// qualifiers.
18
19
type Registry struct {
19
- srcPkg * packages.Package
20
- moqPkgPath string
21
- aliases map [string ]string
22
- imports map [string ]* Package
20
+ srcPkgName string
21
+ srcPkgTypes * types.Package
22
+ moqPkgPath string
23
+ aliases map [string ]string
24
+ imports map [string ]* Package
23
25
}
24
26
25
27
// New loads the source package info and returns a new instance of
26
28
// Registry.
27
29
func New (srcDir , moqPkg string ) (* Registry , error ) {
28
30
srcPkg , err := pkgInfoFromPath (
29
- srcDir , packages .NeedName | packages .NeedSyntax | packages .NeedTypes | packages . NeedTypesInfo | packages . NeedDeps ,
31
+ srcDir , packages .NeedName | packages .NeedSyntax | packages .NeedTypes ,
30
32
)
31
33
if err != nil {
32
34
return nil , fmt .Errorf ("couldn't load source package: %s" , err )
33
35
}
34
36
35
37
return & Registry {
36
- srcPkg : srcPkg ,
37
- moqPkgPath : findPkgPath (moqPkg , srcPkg ),
38
- aliases : parseImportsAliases (srcPkg ),
39
- imports : make (map [string ]* Package ),
38
+ srcPkgName : srcPkg .Name ,
39
+ srcPkgTypes : srcPkg .Types ,
40
+ moqPkgPath : findPkgPath (moqPkg , srcPkg .PkgPath ),
41
+ aliases : parseImportsAliases (srcPkg .Syntax ),
42
+ imports : make (map [string ]* Package ),
40
43
}, nil
41
44
}
42
45
43
46
// SrcPkg returns the types info for the source package.
44
47
func (r Registry ) SrcPkg () * types.Package {
45
- return r .srcPkg . Types
48
+ return r .srcPkgTypes
46
49
}
47
50
48
51
// SrcPkgName returns the name of the source package.
49
52
func (r Registry ) SrcPkgName () string {
50
- return r .srcPkg . Name
53
+ return r .srcPkgName
51
54
}
52
55
53
56
// LookupInterface returns the underlying interface definition of the
@@ -173,14 +176,14 @@ func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package,
173
176
return pkgs [0 ], nil
174
177
}
175
178
176
- func findPkgPath (pkgInputVal string , srcPkg * packages. Package ) string {
179
+ func findPkgPath (pkgInputVal string , srcPkgPath string ) string {
177
180
if pkgInputVal == "" {
178
- return srcPkg . PkgPath
181
+ return srcPkgPath
179
182
}
180
- if pkgInDir (srcPkg . PkgPath , pkgInputVal ) {
181
- return srcPkg . PkgPath
183
+ if pkgInDir (srcPkgPath , pkgInputVal ) {
184
+ return srcPkgPath
182
185
}
183
- subdirectoryPath := filepath .Join (srcPkg . PkgPath , pkgInputVal )
186
+ subdirectoryPath := filepath .Join (srcPkgPath , pkgInputVal )
184
187
if pkgInDir (subdirectoryPath , pkgInputVal ) {
185
188
return subdirectoryPath
186
189
}
@@ -195,9 +198,9 @@ func pkgInDir(pkgName, dir string) bool {
195
198
return currentPkg .Name == pkgName || currentPkg .Name + "_test" == pkgName
196
199
}
197
200
198
- func parseImportsAliases (pkg * packages. Package ) map [string ]string {
201
+ func parseImportsAliases (syntaxTree [] * ast. File ) map [string ]string {
199
202
aliases := make (map [string ]string )
200
- for _ , syntax := range pkg . Syntax {
203
+ for _ , syntax := range syntaxTree {
201
204
for _ , imprt := range syntax .Imports {
202
205
if imprt .Name != nil && imprt .Name .Name != "." && imprt .Name .Name != "_" {
203
206
aliases [strings .Trim (imprt .Path .Value , `"` )] = imprt .Name .Name
0 commit comments