-
Notifications
You must be signed in to change notification settings - Fork 0
/
protogetter.go
189 lines (158 loc) · 3.61 KB
/
protogetter.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
package protogetter
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"log"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/inspector"
)
type Mode int
const (
StandaloneMode Mode = iota
GolangciLintMode
)
const msgFormat = "avoid direct access to proto field %q use %q"
func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "protogetter",
Doc: "Reports direct reads from proto message fields when getters should be used",
Run: func(pass *analysis.Pass) (any, error) {
Run(pass, StandaloneMode)
return nil, nil
},
}
}
func Run(pass *analysis.Pass, mode Mode) []Issue {
nodeTypes := []ast.Node{
(*ast.AssignStmt)(nil),
(*ast.CallExpr)(nil),
(*ast.SelectorExpr)(nil),
(*ast.IncDecStmt)(nil),
(*ast.UnaryExpr)(nil),
}
// Skip generated files.
var files []*ast.File
for _, f := range pass.Files {
if !isGeneratedFile(f) {
files = append(files, f)
// ast.Print(pass.Fset, f)
}
}
ins := inspector.New(files)
var issues []Issue
filter := NewPosFilter()
ins.Preorder(nodeTypes, func(node ast.Node) {
report := analyse(pass, filter, node)
if report == nil {
return
}
switch mode {
case StandaloneMode:
pass.Report(report.ToDiagReport())
case GolangciLintMode:
issues = append(issues, report.ToIssue(pass.Fset))
}
})
return issues
}
func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node) *Report {
// fmt.Printf("\n>>> check: %s\n", formatNode(n))
// ast.Print(pass.Fset, n)
if filter.IsFiltered(n.Pos()) {
// fmt.Printf(">>> filtered\n")
return nil
}
result, err := Process(pass.TypesInfo, filter, n)
if err != nil {
pass.Report(analysis.Diagnostic{
Pos: n.Pos(),
End: n.End(),
Message: fmt.Sprintf("error: %v", err),
})
return nil
}
// If existing in filter, skip it.
if filter.IsFiltered(n.Pos()) {
return nil
}
if result.Skipped() {
return nil
}
// If the expression has already been replaced, skip it.
if filter.IsAlreadyReplaced(pass.Fset, n.Pos(), n.End()) {
return nil
}
// Add the expression to the filter.
filter.AddAlreadyReplaced(pass.Fset, n.Pos(), n.End())
return &Report{
node: n,
result: result,
}
}
// Issue is used to integrate with golangci-lint's inline auto fix.
type Issue struct {
Pos token.Position
Message string
InlineFix InlineFix
}
type InlineFix struct {
StartCol int // zero-based
Length int
NewString string
}
type Report struct {
node ast.Node
result *Result
}
func (r *Report) ToDiagReport() analysis.Diagnostic {
msg := fmt.Sprintf(msgFormat, r.result.From, r.result.To)
return analysis.Diagnostic{
Pos: r.node.Pos(),
End: r.node.End(),
Message: msg,
SuggestedFixes: []analysis.SuggestedFix{
{
Message: msg,
TextEdits: []analysis.TextEdit{
{
Pos: r.node.Pos(),
End: r.node.End(),
NewText: []byte(r.result.To),
},
},
},
},
}
}
func (r *Report) ToIssue(fset *token.FileSet) Issue {
msg := fmt.Sprintf(msgFormat, r.result.From, r.result.To)
return Issue{
Pos: fset.Position(r.node.Pos()),
Message: msg,
InlineFix: InlineFix{
StartCol: fset.Position(r.node.Pos()).Column - 1,
Length: len(r.result.From),
NewString: r.result.To,
},
}
}
func isGeneratedFile(f *ast.File) bool {
for _, c := range f.Comments {
if strings.HasPrefix(c.Text(), "Code generated") {
return true
}
}
return false
}
func formatNode(node ast.Node) string {
buf := new(bytes.Buffer)
if err := format.Node(buf, token.NewFileSet(), node); err != nil {
log.Printf("Error formatting expression: %v", err)
return ""
}
return buf.String()
}