github.com/mvdan/interfacer@v0.0.0-20180901003855-c20040233aed/check/cache.go (about)

     1  // Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
     2  // See LICENSE for licensing information
     3  
     4  package check
     5  
     6  import (
     7  	"go/ast"
     8  	"go/types"
     9  )
    10  
    11  type pkgTypes struct {
    12  	ifaces    map[string]string
    13  	funcSigns map[string]bool
    14  }
    15  
    16  func (p *pkgTypes) getTypes(pkg *types.Package) {
    17  	p.ifaces = make(map[string]string)
    18  	p.funcSigns = make(map[string]bool)
    19  	done := make(map[*types.Package]bool)
    20  	addTypes := func(pkg *types.Package, top bool) {
    21  		if done[pkg] {
    22  			return
    23  		}
    24  		done[pkg] = true
    25  		ifs, funs := fromScope(pkg.Scope())
    26  		fullName := func(name string) string {
    27  			if !top {
    28  				return pkg.Path() + "." + name
    29  			}
    30  			return name
    31  		}
    32  		for iftype, name := range ifs {
    33  			// only suggest exported interfaces
    34  			if ast.IsExported(name) {
    35  				p.ifaces[iftype] = fullName(name)
    36  			}
    37  		}
    38  		for ftype := range funs {
    39  			// ignore non-exported func signatures too
    40  			p.funcSigns[ftype] = true
    41  		}
    42  	}
    43  	for _, imp := range pkg.Imports() {
    44  		addTypes(imp, false)
    45  		for _, imp2 := range imp.Imports() {
    46  			addTypes(imp2, false)
    47  		}
    48  	}
    49  	addTypes(pkg, true)
    50  }