44 "errors"
55 "flag"
66 "fmt"
7- "go/ast"
87 "go/types"
98 "strings"
109
1817
1918type packageModeParser struct {
2019 pkgName string
21-
22- // Mapping from underlying types to aliases used within the package source.
23- //
24- // We prefer to use aliases used in the source rather than underlying type names
25- // as those may be unexported or internal.
26- // TODO(joaks): Once mock is Go1.23+ only, we can remove this
27- // as the casing for types.Alias will automatically handle this
28- // in all cases.
29- aliasReplacements map [types.Type ]aliasReplacement
30- }
31-
32- type aliasReplacement struct {
33- name string
34- pkg string
3520}
3621
3722func (p * packageModeParser ) parsePackage (packageName string , ifaces []string ) (* model.Package , error ) {
@@ -42,8 +27,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
4227 return nil , fmt .Errorf ("load package: %w" , err )
4328 }
4429
45- p .buildAliasReplacements (pkg )
46-
4730 interfaces , err := p .extractInterfacesFromPackage (pkg , ifaces )
4831 if err != nil {
4932 return nil , fmt .Errorf ("extract interfaces from package: %w" , err )
@@ -56,90 +39,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
5639 }, nil
5740}
5841
59- // buildAliasReplacements finds and records any references to aliases
60- // within the given package's source.
61- // These aliases will be preferred when parsing types
62- // over the underlying name counterparts, as those may be unexported / internal.
63- //
64- // If a type has more than one alias within the source package,
65- // the latest one to be inspected will be the one used for mapping.
66- // This is fine, since all aliases and their underlying types are interchangeable
67- // from a type-checking standpoint.
68- func (p * packageModeParser ) buildAliasReplacements (pkg * packages.Package ) {
69- p .aliasReplacements = make (map [types.Type ]aliasReplacement )
70-
71- // checkIdent checks if the given identifier exists
72- // in the given package as an alias, and adds it to
73- // the alias replacements map if so.
74- checkIdent := func (pkg * types.Package , ident string ) bool {
75- scope := pkg .Scope ()
76- if scope == nil {
77- return true
78- }
79- obj := scope .Lookup (ident )
80- if obj == nil {
81- return true
82- }
83- objTypeName , ok := obj .(* types.TypeName )
84- if ! ok {
85- return true
86- }
87- if ! objTypeName .IsAlias () {
88- return true
89- }
90- typ := objTypeName .Type ()
91- if typ == nil {
92- return true
93- }
94- p .aliasReplacements [typ ] = aliasReplacement {
95- name : objTypeName .Name (),
96- pkg : pkg .Path (),
97- }
98- return false
99-
100- }
101-
102- for _ , f := range pkg .Syntax {
103- fileScope , ok := pkg .TypesInfo .Scopes [f ]
104- if ! ok {
105- continue
106- }
107- ast .Inspect (f , func (node ast.Node ) bool {
108-
109- // Simple identifiers: check if it is an alias
110- // from the source package.
111- if ident , ok := node .(* ast.Ident ); ok {
112- return checkIdent (pkg .Types , ident .String ())
113- }
114-
115- // Selector expressions: check if it is an alias
116- // from the package represented by the qualifier.
117- selExpr , ok := node .(* ast.SelectorExpr )
118- if ! ok {
119- return true
120- }
121-
122- x , sel := selExpr .X , selExpr .Sel
123- xident , ok := x .(* ast.Ident )
124- if ! ok {
125- return true
126- }
127-
128- xObj := fileScope .Lookup (xident .String ())
129- pkgName , ok := xObj .(* types.PkgName )
130- if ! ok {
131- return true
132- }
133-
134- xPkg := pkgName .Imported ()
135- if xPkg == nil {
136- return true
137- }
138- return checkIdent (xPkg , sel .String ())
139- })
140- }
141- }
142-
14342func (p * packageModeParser ) loadPackage (packageName string ) (* packages.Package , error ) {
14443 var buildFlagsSet []string
14544 if * buildFlags != "" {
@@ -293,21 +192,15 @@ func (p *packageModeParser) parseType(t types.Type) (model.Type, error) {
293192
294193 return sig , nil
295194 case * types.Named , * types.Alias :
195+ fmt .Printf ("(%T): %v\n " , t , t )
296196 object := t .(interface { Obj () * types.TypeName })
297197 name := object .Obj ().Name ()
198+ fmt .Println (object .Obj ())
298199 var pkg string
299200 if object .Obj ().Pkg () != nil {
300201 pkg = object .Obj ().Pkg ().Path ()
301202 }
302203
303- // If there was an alias to this type used somewhere in the source,
304- // use that alias instead of the underlying type,
305- // since the underlying type might be unexported.
306- if alias , ok := p .aliasReplacements [t ]; ok {
307- name = alias .name
308- pkg = alias .pkg
309- }
310-
311204 // TypeArgs method not available for aliases in go1.22
312205 genericType , ok := t .(interface { TypeArgs () * types.TypeList })
313206 if ! ok || genericType .TypeArgs () == nil {
0 commit comments