Skip to content

Commit

Permalink
golint
Browse files Browse the repository at this point in the history
  • Loading branch information
korfuri committed Jul 9, 2017
1 parent 8786974 commit 717fcf9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
21 changes: 12 additions & 9 deletions packagegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func cleanImportSpec(spec *ast.ImportSpec) string {
// go tool's convention to not try to detect when two packages loaded
// through different paths are the same package.
func candidatePaths(loadpath, parent string) []string {
const kVendor = "vendor"
const vendor = "vendor"
paths := []string{}
for parent != "." && parent != "" {
paths = append(paths, path.Join(parent, kVendor, loadpath))
paths = append(paths, path.Join(parent, vendor, loadpath))
parent = path.Dir(parent)
}
// Some dependencies may be vendored under
Expand All @@ -64,7 +64,7 @@ func candidatePaths(loadpath, parent string) []string {
// that way in the standard library. See
// https://github.com/golang/go/issues/16333 for background on
// that.
paths = append(paths, path.Join(kVendor, loadpath))
paths = append(paths, path.Join(vendor, loadpath))
paths = append(paths, loadpath)
return paths
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (pg *PackageGraph) loadPackage(prog *loader.Program, loadpath string, pi *l
foreignPkg := pg.Packages[pkgLoadPath]
if foreignPkg != nil {
ref := &Ref{
RefType: refTypeForId(prog, id),
RefType: refTypeForIdent(prog, id),
ToIdent: obj.Name(),
ToPackage: foreignPkg,
ToPosition: NewPosition(prog.Fset, obj.Pos(), NoPos),
Expand Down Expand Up @@ -166,7 +166,7 @@ func (pg *PackageGraph) loadPackage(prog *loader.Program, loadpath string, pi *l
// LoadProgram loads recursively packages used from a `main` package.
// It may be called multiple times to load multiple programs'
// package sets in the PackageGraph.
func (p *PackageGraph) LoadProgram(loadpath string, filenames []string) {
func (pg *PackageGraph) LoadProgram(loadpath string, filenames []string) {
conf := loader.Config{}
conf.CreateFromFilenames(loadpath, filenames...)

Expand All @@ -176,14 +176,17 @@ func (p *PackageGraph) LoadProgram(loadpath string, filenames []string) {
}

for k, v := range prog.AllPackages {
p.loadPackage(prog, k.Path(), v)
pg.loadPackage(prog, k.Path(), v)
}
}

func (p *PackageGraph) ComputeInterfaceImplementationMatrix() {
for _, pa := range p.Packages {
// ComputeInterfaceImplementationMatrix processes all loaded types and
// adds cross-package and intra-package Refs for Implementation and
// Extension edges of the graph.
func (pg *PackageGraph) ComputeInterfaceImplementationMatrix() {
for _, pa := range pg.Packages {
for _, iface := range pa.Interfaces {
for _, pb := range p.Packages {
for _, pb := range pg.Packages {
for _, typ := range pb.Impls {
if typ == iface {
continue
Expand Down
9 changes: 7 additions & 2 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Position struct {
}

const (
// NoPos represents a mising position. It has the same
// semantics as token.NoPos. It may be used in place of an
// "end" Pos if the end of an identifier isn't known.
NoPos = token.NoPos
)

Expand All @@ -29,11 +32,13 @@ func (p Position) String() string {
}
if p.EndL >= 0 {
return fmt.Sprintf("%s:%d:%d-%d:%d", p.File, p.PosL, p.PosC, p.EndL, p.EndC)
} else {
return fmt.Sprintf("%s:%d:%d", p.File, p.PosL, p.PosC)
}
return fmt.Sprintf("%s:%d:%d", p.File, p.PosL, p.PosC)
}

// NewPosition creates a Position from a token.FileSet and a pair of
// Pos in that FileSet. It will panic if both Pos are not from the
// same Filename.
func NewPosition(fset *token.FileSet, pos, end token.Pos) Position {
ppos := fset.Position(pos)
if end == token.NoPos {
Expand Down
18 changes: 15 additions & 3 deletions reftype.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ import (
// etc.)
type RefType int

// These are the possible types of edges in a graph.
const (
// Instantiation of a type in another package.
Instantiation = iota

// Call of a function in another pacakge.
Call

// Implementation of an interface by a type.
Implementation

// Extension of an interface by another interface.
Extension

// Reference is the default, used when we can't determine the
// type of reference.
Reference
)

Expand All @@ -31,12 +42,13 @@ func (rt RefType) String() string {
return "Extension"
case Reference:
return "Reference"
default:
panic("Unknown RefType used")
}
panic("Unknown RefType used")
}

func refTypeForId(prog *loader.Program, id *ast.Ident) RefType {
// refTypeForIdent walks the AST from a given Ident and deducts what
// type of Reference it is performing.
func refTypeForIdent(prog *loader.Program, id *ast.Ident) RefType {
_, path, _ := prog.PathEnclosingInterval(id.Pos(), id.End())
// Walk the file's AST to find OutRefs and index those.
for _, n := range path {
Expand Down

0 comments on commit 717fcf9

Please sign in to comment.