Skip to content
This repository has been archived by the owner on Sep 1, 2018. It is now read-only.

Commit

Permalink
Stop generating code for std types
Browse files Browse the repository at this point in the history
Instead, suggest only those types available in the package itself and
its direct dependencies. This makes the tool simpler and it will also
suggest closer types.

This also means that, for example, that if "io" or "fmt" aren't
imported, their interfaces won't be suggested. But this can be a good
thing - most of the time, these suggestions wouldn't be particularly
useful.
  • Loading branch information
mvdan committed Mar 27, 2017
1 parent 17c838a commit 366d889
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 416 deletions.
43 changes: 7 additions & 36 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ import (
"golang.org/x/tools/go/loader"
)

//go:generate sh -c "go list std | go run generate/std/main.go -o std.go"
//go:generate gofmt -w -s std.go

type cache struct {
loader.Config

cur pkgCache

grabbed map[string]pkgCache
}

type pkgCache struct {
Expand All @@ -31,39 +26,22 @@ type typeSet struct {
}

func (c *cache) isFuncType(t string) bool {
if stdFuncs[t] {
return true
}
if s := c.cur.exp.funcs[t]; s != "" {
return true
}
return c.cur.unexp.funcs[t] != ""
}

func (c *cache) ifaceOf(t string) string {
if s := stdIfaces[t]; s != "" {
return s
}
if s := c.cur.exp.ifaces[t]; s != "" {
return s
}
return c.cur.unexp.ifaces[t]
}

func (c *cache) grabNames(pkg *types.Package) {
c.fillCache(pkg)
c.cur = c.grabbed[pkg.Path()]
}

func (c *cache) fillCache(pkg *types.Package) {
path := pkg.Path()
if _, e := c.grabbed[path]; e {
return
}
for _, imp := range pkg.Imports() {
c.fillCache(imp)
}
cur := pkgCache{
c.cur = pkgCache{
exp: typeSet{
ifaces: make(map[string]string),
funcs: make(map[string]string),
Expand All @@ -81,29 +59,22 @@ func (c *cache) fillCache(pkg *types.Package) {
return name
}
for iftype, name := range ifs {
if _, e := stdIfaces[iftype]; e {
continue
}
if ast.IsExported(name) {
cur.exp.ifaces[iftype] = fullName(name)
c.cur.exp.ifaces[iftype] = fullName(name)
}
}
for ftype, name := range funs {
if stdFuncs[ftype] {
continue
}
if ast.IsExported(name) {
cur.exp.funcs[ftype] = fullName(name)
c.cur.exp.funcs[ftype] = fullName(name)
} else {
cur.unexp.funcs[ftype] = fullName(name)
c.cur.unexp.funcs[ftype] = fullName(name)
}
}
}
for _, imp := range pkg.Imports() {
pc := c.grabbed[imp.Path()]
addTypes(imp.Path(), pc.exp.ifaces, pc.exp.funcs, false)
ifs, funs := fromScope(imp.Scope())
addTypes(imp.Path(), ifs, funs, false)
}
ifs, funs := FromScope(pkg.Scope())
ifs, funs := fromScope(pkg.Scope())
addTypes(path, ifs, funs, true)
c.grabbed[path] = cur
}
4 changes: 2 additions & 2 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ func (*Checker) Check(lprog *loader.Program, prog *ssa.Program) ([]lint.Issue, e
if err != nil {
return nil, err
}
c := &cache{grabbed: make(map[string]pkgCache)}
c := &cache{}
v := &visitor{
cache: c,
fset: lprog.Fset,
}
var total []lint.Issue
for _, pkg := range pkgs {
c.grabNames(pkg)
c.fillCache(pkg)
total = append(total, v.checkPkg(lprog.AllPackages[pkg])...)
}
return total, nil
Expand Down
177 changes: 0 additions & 177 deletions generate/std/main.go

This file was deleted.

Loading

0 comments on commit 366d889

Please sign in to comment.