forked from alecthomas/gometalinter
/
types.go
170 lines (157 loc) · 3.42 KB
/
types.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
// Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package check
import (
"bytes"
"fmt"
"go/types"
"sort"
"strings"
)
type methoder interface {
NumMethods() int
Method(int) *types.Func
}
func methoderFuncMap(m methoder, skip bool) map[string]string {
ifuncs := make(map[string]string, m.NumMethods())
for i := 0; i < m.NumMethods(); i++ {
f := m.Method(i)
if !f.Exported() {
if skip {
continue
}
return nil
}
sign := f.Type().(*types.Signature)
ifuncs[f.Name()] = signString(sign)
}
return ifuncs
}
func typeFuncMap(t types.Type) map[string]string {
switch x := t.(type) {
case *types.Pointer:
return typeFuncMap(x.Elem())
case *types.Named:
u := x.Underlying()
if types.IsInterface(u) {
return typeFuncMap(u)
}
return methoderFuncMap(x, true)
case *types.Interface:
return methoderFuncMap(x, false)
default:
return nil
}
}
func funcMapString(iface map[string]string) string {
fnames := make([]string, 0, len(iface))
for fname := range iface {
fnames = append(fnames, fname)
}
sort.Strings(fnames)
var b bytes.Buffer
for i, fname := range fnames {
if i > 0 {
fmt.Fprint(&b, "; ")
}
fmt.Fprint(&b, fname, iface[fname])
}
return b.String()
}
func tupleJoin(buf *bytes.Buffer, t *types.Tuple) {
buf.WriteByte('(')
for i := 0; i < t.Len(); i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(t.At(i).Type().String())
}
buf.WriteByte(')')
}
// signString is similar to Signature.String(), but it ignores
// param/result names.
func signString(sign *types.Signature) string {
var buf bytes.Buffer
tupleJoin(&buf, sign.Params())
tupleJoin(&buf, sign.Results())
return buf.String()
}
func interesting(t types.Type) bool {
switch x := t.(type) {
case *types.Interface:
return x.NumMethods() > 1
case *types.Named:
if u := x.Underlying(); types.IsInterface(u) {
return interesting(u)
}
return x.NumMethods() >= 1
case *types.Pointer:
return interesting(x.Elem())
default:
return false
}
}
func anyInteresting(params *types.Tuple) bool {
for i := 0; i < params.Len(); i++ {
t := params.At(i).Type()
if interesting(t) {
return true
}
}
return false
}
func fromScope(scope *types.Scope) (ifaces map[string]string, funcs map[string]bool) {
ifaces = make(map[string]string)
funcs = make(map[string]bool)
for _, name := range scope.Names() {
tn, ok := scope.Lookup(name).(*types.TypeName)
if !ok {
continue
}
switch x := tn.Type().Underlying().(type) {
case *types.Interface:
iface := methoderFuncMap(x, false)
if len(iface) == 0 {
continue
}
for i := 0; i < x.NumMethods(); i++ {
f := x.Method(i)
sign := f.Type().(*types.Signature)
if !anyInteresting(sign.Params()) {
continue
}
funcs[signString(sign)] = true
}
s := funcMapString(iface)
if _, e := ifaces[s]; !e {
ifaces[s] = tn.Name()
}
case *types.Signature:
if !anyInteresting(x.Params()) {
continue
}
funcs[signString(x)] = true
}
}
return ifaces, funcs
}
func mentionsName(fname, name string) bool {
if len(name) < 2 {
return false
}
capit := strings.ToUpper(name[:1]) + name[1:]
lower := strings.ToLower(name)
return strings.Contains(fname, capit) || strings.HasPrefix(fname, lower)
}
func typeNamed(t types.Type) *types.Named {
for {
switch x := t.(type) {
case *types.Named:
return x
case *types.Pointer:
t = x.Elem()
default:
return nil
}
}
}