-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitfile.go
171 lines (131 loc) · 3.58 KB
/
splitfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package splitfile
import (
"errors"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"github.com/mccurdyc/splitfile/internal/graph"
)
var Analyzer = &analysis.Analyzer{
Name: "splitfile",
Doc: "checks for clean splits of files in packages based on objects and their relationships with other objects.",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
type Poser interface {
Pos() token.Pos
}
func run(pass *analysis.Pass) (interface{}, error) {
g := traverse(pass.TypesInfo.Defs)
nodes := g.Partition() // TODO (Issue#8): right now, this returns every single node in the graph
for _, n := range nodes {
p, ok := n.Object.(Poser)
if !ok {
continue
}
pass.Reportf(p.Pos(), "parition found - %+v", n)
}
return nil, nil
}
// traverse traverses the map of definitions and builds a graph based on the
// relationships.
func traverse(defs map[*ast.Ident]types.Object) graph.Graph {
g := graph.New()
for _, def := range defs {
if skip := filter(def); skip {
continue
}
node := graph.NewNode(Id(def), def)
err := g.AddNode(node)
if err != nil {
continue
}
err = addRelated(g, node)
if err != nil {
continue
}
}
return g
}
// filter returns whether or not a def should be filtered out.
func filter(def types.Object) bool {
if def == nil {
return true
}
return false
}
type Typer interface {
Type() types.Type
}
// addRelated given a graph, g, and root node finds relationships
// with other declarations in the same package and adds them to the graph.
//
// TODO (Issue #15): read value from config or use default
// TODO: check other places for related (e.g., funcs, interfaces, etc.)
func addRelated(g graph.Graph, node *graph.Node) error {
t, ok := node.Object.(Typer)
if !ok {
return errors.New("node does not have a Type() method")
}
m := checkMethods(types.NewMethodSet(t.Type()))
for _, r := range m {
if r.ID == node.ID {
continue
}
err := g.AddNode(r)
if err != nil {
continue
}
node.AddEdge(r, 5.0) // TODO (Issue #15): read value from config or use default
}
// TODO: check other places for related (e.g., funcs, interfaces, etc.)
return nil
}
// checkMethods checks methods' signatures for related types.
func checkMethods(mset *types.MethodSet) []*graph.Node {
rel := make([]*graph.Node, 0)
for i := 0; i < mset.Len(); i++ {
method := mset.At(i)
m := graph.NewNode(Id(method.Obj()), method.Obj())
rel = append(rel, m) // methods themselves are always related
sig, ok := method.Type().(*types.Signature)
if !ok {
continue
}
related := checkSignature(sig)
rel = append(rel, related...)
}
return rel
}
// checkSignature checks a function signature, the receiver (if it is a method this
// will be a non-nil value), the parameters and the return types.
func checkSignature(sig *types.Signature) []*graph.Node {
rel := make([]*graph.Node, 0)
if v := checkVar(sig.Recv()); v != nil {
rel = append(rel, v)
}
rel = append(rel, checkTuple(sig.Params())...)
rel = append(rel, checkTuple(sig.Results())...)
return rel
}
// checkVar validates a variable and if it is valid, it is returned as a valid related.
func checkVar(v *types.Var) *graph.Node {
if v == nil || v.Type() == types.Type(nil) {
return nil
}
return graph.NewNode(Id(v), v)
}
// checkTuple checks a tuple of variables for related nodes.
func checkTuple(vars *types.Tuple) []*graph.Node {
rel := make([]*graph.Node, 0)
for i := 0; i < vars.Len(); i++ {
v := checkVar(vars.At(i))
if v == nil {
continue
}
rel = append(rel, v)
}
return rel
}