-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getters.go
144 lines (135 loc) · 3.5 KB
/
getters.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
package getters
import (
"fmt"
"go/ast"
"go/token"
"slices"
"strings"
"github.com/gostaticanalysis/comment/passes/commentmap"
"github.com/k1LoW/gostyle/config"
"github.com/k1LoW/gostyle/detector"
"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 = "getters"
doc = "Analyzer based on https://google.github.io/styleguide/go/decisions#getters"
msg = "Function and method names should not use a Get or get prefix, unless the underlying concept uses the word “get” (e.g. an HTTP GET). Prefer starting the name with the noun directly, for example use Counts over GetCounts. (ref: https://google.github.io/styleguide/go/decisions#getters)"
)
var (
disable bool
includeGenerated bool
exclude string
)
// Analyzer based on https://google.github.io/styleguide/go/decisions#getters
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)
words = c.AnalyzersSettings.Getters.Exclude
includeGenerated = c.AnalyzersSettings.Getters.IncludeGenerated
}
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.InterfaceType)(nil),
(*ast.FuncDecl)(nil),
(*ast.AssignStmt)(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
}
i.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.ValueSpec:
for i, v := range n.Values {
_, ok := v.(*ast.FuncLit)
if !ok {
continue
}
id := n.Names[i]
if slices.Contains(words, id.Name) {
continue
}
if detector.HasGetPrefix(id.Name) {
r.Append(id.Pos(), fmt.Sprintf("%s: %s", msg, id.Name))
}
}
case *ast.InterfaceType:
if n.Methods == nil {
return
}
for _, field := range n.Methods.List {
for _, id := range field.Names {
if slices.Contains(words, id.Name) {
continue
}
if detector.HasGetPrefix(id.Name) {
r.Append(id.Pos(), fmt.Sprintf("%s: %s", msg, id.Name))
}
}
}
case *ast.FuncDecl:
if slices.Contains(words, n.Name.Name) {
return
}
if detector.HasGetPrefix(n.Name.Name) {
r.Append(n.Pos(), fmt.Sprintf("%s: %s", msg, n.Name.Name))
}
case *ast.AssignStmt:
if n.Tok != token.DEFINE {
return
}
for i, e := range n.Rhs {
_, ok := e.(*ast.FuncLit)
if !ok {
continue
}
id, ok := n.Lhs[i].(*ast.Ident)
if !ok {
continue
}
if slices.Contains(words, id.Name) {
continue
}
if detector.HasGetPrefix(id.Name) {
r.Append(id.Pos(), fmt.Sprintf("%s: %s", msg, id.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)")
}