/
noannotationformat.go
62 lines (53 loc) · 1.39 KB
/
noannotationformat.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
package rules
import (
"go/ast"
"go/types"
"github.com/dustinspecker/gomega-lint/internal/visitor"
"golang.org/x/tools/go/analysis"
)
var NoAnnotationFormat = &analysis.Analyzer{
Name: "noannotationformat",
Doc: "Avoid fmt.Sprintf in annotations",
Run: noAnnotationFormatRun,
}
func noAnnotationFormatRun(pass *analysis.Pass) (interface{}, error) {
var fmtSprintFyncType types.Type
for _, pkg := range pass.Pkg.Imports() {
if pkg.Path() == "fmt" {
fmtSprintFyncType = pkg.Scope().Lookup("Sprintf").Type()
}
}
visitor.VistGomegaAssertions(pass, func(gomegaAssertion visitor.GomegaAssertion) *analysis.Diagnostic {
if len(gomegaAssertion.AssertionArgs) > 1 {
callExpr, ok := gomegaAssertion.AssertionArgs[1].(*ast.CallExpr)
if !ok {
return nil
}
if fmtSprintFyncType != pass.TypesInfo.TypeOf(callExpr.Fun) {
return nil
}
return &analysis.Diagnostic{
Message: "Annotation should not use fmt.Sprintf",
Pos: gomegaAssertion.Node.Pos(),
SuggestedFixes: []analysis.SuggestedFix{
{
TextEdits: []analysis.TextEdit{
{
Pos: gomegaAssertion.AssertionArgs[1].Pos(),
End: callExpr.Lparen + 1,
NewText: []byte(""),
},
{
Pos: callExpr.Rparen,
End: callExpr.Rparen + 1,
NewText: []byte(""),
},
},
},
},
}
}
return nil
})
return nil, nil
}