/
package.go
319 lines (272 loc) · 8.44 KB
/
package.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"fmt"
"go/ast"
"go/build"
"go/token"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types"
)
const (
internal = "internal"
)
type identifier struct {
pkg string
parent string
name string
pos token.Position
}
// String prints out the identifier in gorename-compatible format.
func (i *identifier) string() string {
if i.parent != "" {
return fmt.Sprintf("\"%s\".%s.%s", i.pkg, i.parent, i.name)
}
return fmt.Sprintf("\"%s\".%s", i.pkg, i.name)
}
func (i *identifier) isExported() bool {
return ast.IsExported(i.name)
}
func (i *identifier) unexportedName() string {
if len(i.name) == 0 {
return ""
}
return strings.ToLower(string(i.name[0])) + i.name[1:]
}
// NameEquals returns true if name part of identifiers are equal.
// pos is ignored.
func (i *identifier) nameEquals(other *identifier) bool {
return i.name == other.name && i.parent == other.parent && i.pkg == other.pkg
}
// searchByPos tries to find a identifier that resides in provided position.
// Uses "comma ok"-idiom.
func searchByPos(idarr []identifier, pos token.Position) (identifier, bool) {
var ret identifier
for _, id := range idarr {
if id.pos == pos {
return id, true
}
}
return ret, false
}
// isInternal tells whether a package is internal (ie. if package itself
// is internal or it has internal ancestors).
func isInternal(pkgName string) bool {
cleanedPath := filepath.Clean(pkgName)
explodedPath := strings.Split(cleanedPath, string(os.PathSeparator))
for _, pathPart := range explodedPath {
if pathPart == internal {
return true
}
}
return false
}
// getInternalParentPrefix returns package name prefix until the last
// internal package.
// example: "/a/b/internal/a/c/internal/x/y/z" -> "/a/b/internal/a/c"
func getInternalParentPrefix(pkgName string) string {
cleanedPath := filepath.Clean(pkgName)
explodedPath := strings.Split(cleanedPath, string(os.PathSeparator))
i := len(explodedPath) - 1
for ; i > 0; i-- {
if explodedPath[i] == internal {
break
}
}
return filepath.Join(explodedPath[0:i]...)
}
// packagesToSearch returns a list of package names, that should be
// searched for usages of identifiers usages from unexported package.
// pkgPrefix can narrow down the search for a certain package and it's
// subpackages.
// By default (empty pkgPrefix) all packages under build.Default.SrcDirs()
// are included in the search.
func packagesToSearch(unexportPkg string, pkgPrefix string) []string {
var searchPackages []string
var allPackages []string
var internalPrefix string
for _, dir := range build.Default.SrcDirs() {
allPackages = append(allPackages, getSubDirectories(dir)...)
}
// If package is internal, the search can be narrowed down:
// Only immediate parent (and their children) can import internal
// packages.
if isInternal(unexportPkg) {
internalPrefix = getInternalParentPrefix(unexportPkg)
}
// Filter with prefixes.
for _, p := range allPackages {
if p == unexportPkg || !strings.HasPrefix(p, internalPrefix) || !strings.HasPrefix(p, pkgPrefix) {
continue
}
searchPackages = append(searchPackages, p)
}
return searchPackages
}
// createProgram returns a program containing packages specified.
func createProgram(packages ...string) (*loader.Program, error) {
var conf loader.Config
for _, name := range packages {
conf.CreateFromFilenames(name, getFileNames(name)...)
}
return conf.Load()
}
// findIdentifiers returns a list of all identifiers (both exported and local)
// found in provided package.
func findIdentifiers(pkgName string) []identifier {
var ret []identifier
addID := func(newID identifier) {
for _, id := range ret {
// Compare names only, because if type embedding is used
// the same symbols is added multiple times.
if id.nameEquals(&newID) {
return
}
}
ret = append(ret, newID)
}
prog, err := createProgram(pkgName)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ret
}
// Traverse definitions and convert them to identifiers.
for dident, dobj := range prog.Package(pkgName).Defs {
if dident.Obj == nil {
// This branch handles receivers.
var parent string
if t, ok := dobj.(*types.Func); ok {
if t2, ok := t.Type().(*types.Signature); ok && t2.Recv() != nil {
switch t3 := t2.Recv().Type().(type) {
case *types.Pointer:
if t4, ok := t3.Elem().(*types.Named); ok {
parent = t4.Obj().Name()
}
case *types.Named:
parent = t3.Obj().Name()
}
}
}
addID(identifier{pkgName, parent, dident.Name, prog.Fset.Position(dident.Pos())})
} else if typeSpec, ok := dident.Obj.Decl.(*ast.TypeSpec); ok {
/// This branch handles structs and interfaces and their fields.
addID(identifier{pkgName, "", dident.Name, prog.Fset.Position(dident.Pos())})
var fl *ast.FieldList
switch t := typeSpec.Type.(type) {
case *ast.StructType:
fl = t.Fields
case *ast.InterfaceType:
fl = t.Methods
}
// Handle field names.
if fl != nil {
for _, v := range fl.List {
for _, n := range v.Names {
addID(identifier{pkgName, dident.Name, n.Name, prog.Fset.Position(n.Pos())})
}
}
}
} else if _, ok := dident.Obj.Decl.(*ast.Field); ok {
// This branch handles Fields.
// Fields get added when handling structs, interfaces and FieldLists (previous branch),
// so no need to export them here.
continue
} else {
// This branch handles all other top level declarations (vars, consts)
addID(identifier{pkgName, "", dident.Name, prog.Fset.Position(dident.Pos())})
}
}
return ret
}
// findExports returns a list of exported identifiers found in provided package.
func findExports(pkgName string) []identifier {
var ret []identifier
ids := findIdentifiers(pkgName)
for _, id := range ids {
if id.isExported() {
ret = append(ret, id)
}
}
return ret
}
// usageCounts returns a list of usages of "pkgName"-package in "searchUsageFrom"-package
// per exported identifier. Each usage-element contains call-site's exact position in a file.
// targetExports is a list of exported identifiers in "pkgName"-package.
func usageCounts(pkgName string, targetExports []identifier, searchUsageFrom string) map[identifier][]string {
ret := make(map[identifier][]string)
prog, err := createProgram(pkgName, searchUsageFrom)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ret
}
targetInfo := prog.Package(pkgName)
fromInfo := prog.Package(searchUsageFrom)
for uident, uobj := range fromInfo.Uses {
if uobj.Pkg() != nil && uobj.Pkg().Path() == pkgName {
pos := prog.Fset.Position(uident.Pos())
lineCol := fmt.Sprintf("%s:%d:%d", pos.Filename, pos.Line, pos.Column)
for dident, dobj := range targetInfo.Defs {
if uobj == dobj {
if id, ok := searchByPos(targetExports, prog.Fset.Position(dident.Pos())); ok {
ret[id] = append(ret[id], lineCol)
}
}
}
}
}
return ret
}
// unusedExports returns a list of unused package-exports for pkgName.
// Export usage is searched from provided "searchPackages"-packages.
func unusedExports(pkgName string, searchPackages []string) []identifier {
var ret []identifier
used := make(map[identifier]bool)
exports := findExports(pkgName)
for _, s := range searchPackages {
fmt.Fprintln(os.Stdout, "searching usages from:", s)
u := usageCounts(pkgName, exports, s)
for id := range u {
used[id] = true
}
}
for _, id := range exports {
if !used[id] {
ret = append(ret, id)
}
}
return ret
}
// nameCollisions returns a list of identifiers, which will cause a naming collision
// if exported.
func nameCollisions(pkgName string, candidates []identifier) []identifier {
var collisions []identifier
unexportCollision := func(a *identifier, b *identifier) bool {
return a.pkg == b.pkg && a.parent == b.parent && a.unexportedName() == b.name
}
allIds := findIdentifiers(pkgName)
for _, cid := range candidates {
for _, id := range allIds {
if unexportCollision(&cid, &id) {
collisions = append(collisions, cid)
break
}
}
}
return collisions
}
// usageReport returns a list of users (file+location)
// for each exported identifier in provided package.
func usageReport(pkgName string, searchFrom string) map[identifier][]string {
ret := make(map[identifier][]string)
exports := findExports(pkgName)
searchPackages := packagesToSearch(pkgName, searchFrom)
for _, s := range searchPackages {
newuc := usageCounts(pkgName, exports, s)
for id, str := range newuc {
ret[id] = append(ret[id], str...)
}
}
return ret
}