This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrlint.go
179 lines (149 loc) · 4.4 KB
/
logrlint.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
package logrlint
import (
"flag"
"fmt"
"go/ast"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
)
const Doc = `Checks key valur pairs for common logger libraries (logr,klog,zap).`
func NewAnalyzer() *analysis.Analyzer {
l := &logrlint{
enable: loggerCheckersFlag{
newStringSet(defaultEnabledCheckers...),
},
}
a := &analysis.Analyzer{
Name: "logrlint",
Doc: Doc,
Run: l.run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
checkerKeys := strings.Join(loggerCheckersByName.Keys(), ",")
a.Flags.Init("logrlint", flag.ExitOnError)
a.Flags.BoolVar(&l.disableAll, "disableall", false, "disable all logger checkers")
a.Flags.Var(&l.disable, "disable", fmt.Sprintf("comma-separated list of disabled logger checker (%s)", checkerKeys))
a.Flags.Var(&l.enable, "enable", fmt.Sprintf("comma-separated list of enabled logger checker (%s)", checkerKeys))
return a
}
type logrlint struct {
disableAll bool // flag -disableall
disable loggerCheckersFlag // flag -disable
enable loggerCheckersFlag // flag -enable
}
func (l *logrlint) isCheckerDisabled(name string) bool {
if l.disableAll {
return !l.enable.Has(name)
}
if l.disable.Has(name) {
return true
}
return !l.enable.Has(name)
}
func (l *logrlint) getLoggerFuncs(pkgPath string) stringSet {
for name, entry := range loggerCheckersByName {
if l.isCheckerDisabled(name) {
// Skip ignored logger checker.
continue
}
if entry.packageImport == pkgPath {
return entry.funcs
}
if strings.HasSuffix(pkgPath, "/vendor/"+entry.packageImport) {
return decorateVendoredFuncs(entry.funcs, pkgPath, entry.packageImport)
}
}
return nil
}
func decorateVendoredFuncs(entryFuncs stringSet, currentPkgImport, packageImport string) stringSet {
funcs := make(stringSet, len(entryFuncs))
for fn := range entryFuncs {
lastDot := strings.LastIndex(fn, ".")
if lastDot == -1 {
continue // invalid pattern
}
importOrReceiver := fn[:lastDot]
fnName := fn[lastDot+1:]
if strings.HasPrefix(importOrReceiver, "(") { // is receiver
if !strings.HasSuffix(importOrReceiver, ")") {
continue // invalid pattern
}
var pointerIndicator string
if strings.HasPrefix(importOrReceiver[1:], "*") { // pointer type
pointerIndicator = "*"
}
leftOver := strings.TrimPrefix(importOrReceiver, "("+pointerIndicator+packageImport+".")
importOrReceiver = fmt.Sprintf("(%s%s.%s", pointerIndicator, currentPkgImport, leftOver)
} else { // is import
importOrReceiver = currentPkgImport
}
fn = fmt.Sprintf("%s.%s", importOrReceiver, fnName)
funcs.Insert(fn)
}
return funcs
}
func (l *logrlint) isValidLoggerFunc(fn *types.Func) bool {
pkg := fn.Pkg()
if pkg == nil {
return false
}
funcs := l.getLoggerFuncs(pkg.Path())
return funcs.Has(fn.FullName())
}
func (l *logrlint) checkLoggerArguments(pass *analysis.Pass, call *ast.CallExpr) {
fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func)
if fn == nil {
return // function pointer is not supported
}
sig, ok := fn.Type().(*types.Signature)
if !ok || !sig.Variadic() {
return // not variadic
}
if !l.isValidLoggerFunc(fn) {
return
}
// ellipsis args is hard, just skip
if call.Ellipsis.IsValid() {
return
}
params := sig.Params()
nparams := params.Len() // variadic => nonzero
args := params.At(nparams - 1)
iface, ok := args.Type().(*types.Slice).Elem().(*types.Interface)
if !ok || !iface.Empty() {
return // final (args) param is not ...interface{}
}
startIndex := nparams - 1
nargs := len(call.Args)
variadicLen := nargs - startIndex
if variadicLen%2 != 0 {
firstArg := call.Args[startIndex]
lastArg := call.Args[nargs-1]
pass.Report(analysis.Diagnostic{
Pos: firstArg.Pos(),
End: lastArg.End(),
Category: "logging",
Message: "odd number of arguments passed as key-value pairs for logging",
})
}
}
func (l *logrlint) run(pass *analysis.Pass) (interface{}, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
insp.Preorder(nodeFilter, func(node ast.Node) {
call := node.(*ast.CallExpr)
typ := pass.TypesInfo.Types[call.Fun].Type
if typ == nil {
// Skip checking functions with unknown type.
return
}
l.checkLoggerArguments(pass, call)
})
return nil, nil
}