-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repetition.go
230 lines (217 loc) · 6.75 KB
/
repetition.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
package repetition
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"slices"
"strings"
"github.com/fatih/camelcase"
"github.com/gostaticanalysis/comment/passes/commentmap"
"github.com/k1LoW/gostyle/config"
"github.com/k1LoW/gostyle/reporter"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const (
name = "repetition"
doc = "Analyzer based on https://google.github.io/styleguide/go/decisions#repetition"
msgp = "when naming exported symbols, the name of the package is always visible outside your package, so redundant information between the two should be reduced or eliminated. (ref: https://google.github.io/styleguide/go/decisions#package-vs-exported-symbol-name)"
msgt = "the compiler always knows the type of a variable, and in most cases it is also clear to the reader what type a variable is by how it is used. It is only necessary to clarify the type of a variable if its value appears twice in the same scope. (ref: https://google.github.io/styleguide/go/decisions#variable-name-vs-type)"
)
var (
disable bool
includeGenerated bool
exclude string
)
// Analyzer based on https://google.github.io/styleguide/go/decisions#repetition
var Analyzer = &analysis.Analyzer{
Name: name,
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
config.Loader,
inspect.Analyzer,
commentmap.Analyzer,
},
}
func run(pass *analysis.Pass) (any, error) {
c, err := config.Load(pass)
if err != nil {
return nil, err
}
words := strings.Split(exclude, ",")
if c != nil {
disable = c.IsDisabled(name)
includeGenerated = c.AnalyzersSettings.Recvnames.IncludeGenerated
words = c.AnalyzersSettings.Repetition.Exclude
}
if disable {
return nil, nil
}
i, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, fmt.Errorf("unexpected result type from inspect: %T", pass.ResultOf[inspect.Analyzer])
}
nodeFilter := []ast.Node{
(*ast.ValueSpec)(nil),
(*ast.FuncDecl)(nil),
}
var opts []reporter.Option
if includeGenerated {
opts = append(opts, reporter.IncludeGenerated())
}
r, err := reporter.New(name, pass, opts...)
if err != nil {
return nil, err
}
conf, err := config.NewTypesConfig(pass)
if err != nil {
return nil, err
}
pkg, err := conf.Check("varnames", pass.Fset, pass.Files, nil)
if err != nil {
return nil, err
}
tr := &typeVarReporter{
pkg: pkg,
r: r,
pass: pass,
}
pkgn := pass.Pkg.Name()
i.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.ValueSpec:
if n.Names == nil {
return
}
// Package vs. exported symbol name
for _, id := range n.Names {
tr.report(id.Pos(), id.Name)
if !id.IsExported() {
continue
}
splitted := camelcase.Split(id.Name)
for _, s := range splitted {
if len(s) == 1 {
continue
}
if slices.Contains(words, s) {
continue
}
if strings.Contains(pkgn, strings.ToLower(s)) {
r.Append(n.Pos(), fmt.Sprintf("%s: %s<-[%s]->%s", msgp, pkgn, s, id.Name))
}
}
}
case *ast.AssignStmt:
if n.Tok != token.DEFINE {
return
}
for _, e := range n.Lhs {
id, ok := e.(*ast.Ident)
if !ok {
continue
}
if slices.Contains(words, id.Name) {
continue
}
tr.report(id.Pos(), id.Name)
}
case *ast.RangeStmt:
idk, ok := n.Key.(*ast.Ident)
if ok {
if !slices.Contains(words, idk.Name) {
tr.report(idk.Pos(), idk.Name)
}
}
idv, ok := n.Value.(*ast.Ident)
if ok {
if !slices.Contains(words, idv.Name) {
tr.report(idv.Pos(), idv.Name)
}
}
case *ast.FuncDecl:
if n.Recv != nil {
return
}
if !n.Name.IsExported() {
return
}
if slices.Contains(words, n.Name.Name) {
return
}
// Package vs. exported symbol name
splitted := camelcase.Split(n.Name.Name)
for _, s := range splitted {
if len(s) == 1 {
continue
}
if strings.Contains(pkgn, strings.ToLower(s)) {
r.Append(n.Pos(), fmt.Sprintf("%s: %s<-[%s]->%s", msgp, pkgn, s, n.Name.Name))
}
}
}
})
r.Report()
return nil, nil
}
func init() {
Analyzer.Flags.BoolVar(&disable, "disable", false, "disable "+name+" analyzer")
Analyzer.Flags.BoolVar(&includeGenerated, "include-generated", false, "include generated codes")
Analyzer.Flags.StringVar(&exclude, "exclude", "", "exclude words (comma separated)")
}
type typeVarReporter struct {
pkg *types.Package
r *reporter.Reporter
pass *analysis.Pass
}
func (tr *typeVarReporter) report(pos token.Pos, varname string) {
// Variable name vs. type.
s := tr.pkg.Scope().Innermost(pos)
o := s.Lookup(varname)
if o == nil {
s, o = s.LookupParent(varname, pos)
if s == nil || o == nil {
return
}
}
switch o.(type) {
case *types.Var, *types.Const:
switch o.Type() {
case types.Typ[types.Int], types.Typ[types.Int8], types.Typ[types.Int16], types.Typ[types.Int32], types.Typ[types.Int64]:
if strings.Contains(strings.ToLower(varname), "int") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "int", o.Type().String()))
} else if strings.Contains(strings.ToLower(varname), "num") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "num", o.Type().String()))
}
case types.Typ[types.Uint], types.Typ[types.Uint8], types.Typ[types.Uint16], types.Typ[types.Uint32], types.Typ[types.Uint64]:
if strings.Contains(strings.ToLower(varname), "uint") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "uint", o.Type().String()))
} else if strings.Contains(strings.ToLower(varname), "num") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "num", o.Type().String()))
}
case types.Typ[types.Float32], types.Typ[types.Float64]:
if strings.Contains(strings.ToLower(varname), "float") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "float", o.Type().String()))
} else if strings.Contains(strings.ToLower(varname), "num") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "num", o.Type().String()))
}
case types.Typ[types.String]:
if strings.Contains(strings.ToLower(varname), "string") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "string", o.Type().String()))
} else if strings.Contains(strings.ToLower(varname), "str") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "str", o.Type().String()))
}
case types.Typ[types.Bool]:
if strings.Contains(strings.ToLower(varname), "bool") {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, varname, "bool", o.Type().String()))
}
default:
if strings.Contains(strings.ToLower(varname), strings.ToLower(o.Type().String())) {
tr.r.Append(pos, fmt.Sprintf("%s: %s<-[%s]->%s", msgt, tr.pkg.Name(), o.Type().String(), varname))
}
}
}
}