forked from krdln/gomegalint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
269 lines (229 loc) · 5.79 KB
/
main.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"fmt"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/singlechecker"
)
func main() {
singlechecker.Main(Analyzer)
}
var Analyzer = &analysis.Analyzer{
Name: "gomegalint",
Doc: "reports non-idiomatic usage of gomega matchers",
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
ass := getAssertion(n)
if ass == nil {
// FIXME perhaps don't always recurse?
return true
}
emittedShouldFix := checkNilnessAssertions(*ass, pass)
checkStyle(*ass, pass, !emittedShouldFix)
// FIXME what should we return here? Can assertions be nested?
return true
})
}
return nil, nil
}
// assertion describes a `Ω(X).Should(Y)`-like call
type assertion struct {
*ast.CallExpr // whole
Omega *ast.Ident // Ω part
Subject ast.Expr // X part
Should *ast.Ident // Should part
Matcher ast.Expr // Y part
Negated bool // whether the matcher is negated (eg. when using `ShouldNot`)
}
func checkStyle(ass assertion, pass *analysis.Pass, emitFixes bool) {
if getStyle(ass.Omega.Name) == getStyle(ass.Should.Name) {
return
}
d := analysis.Diagnostic{
Pos: ass.Pos(),
End: ass.End(),
Message: fmt.Sprintf(
"inconsistent assertion style (%s + %s)",
ass.Omega.Name, ass.Should.Name,
),
}
if emitFixes {
fixedShould := renderInStyle(getStyle(ass.Omega.Name), ass.Negated)
d.SuggestedFixes = []analysis.SuggestedFix{{
Message: fmt.Sprintf("change %s to %s", ass.Should.Name, fixedShould),
TextEdits: []analysis.TextEdit{{
Pos: ass.Should.Pos(),
End: ass.Should.End(),
NewText: []byte(fixedShould),
}},
}}
}
pass.Report(d)
}
const Omega = "Ω"
const Expect = "Expect"
const Should = "Should"
const ShouldNot = "ShouldNot"
const To = "To"
const ToNot = "ToNot"
const NotTo = "NotTo"
type Style int
const (
ShouldStyle Style = iota
ExpectStyle
)
// checkNilnessAssertions checks which of IsNil/HaveOccurred/Succeed matchers
// is the most appropriate and returns, whether a TextEdit for 'Should' part of
// an assertion was emitted (because of possible need of inverting the
// condition)
func checkNilnessAssertions(ass assertion, pass *analysis.Pass) (emittedShouldFix bool) {
matcherIdent, matcher := getKnownMatcher(ass)
if matcher == UnknownMatcher {
return false
}
var expectedMatcher KnownMatcher
if isErrorExpr(ass.Subject, pass.TypesInfo) {
if _, isCall := ass.Subject.(*ast.CallExpr); isCall {
expectedMatcher = Succeed
} else {
expectedMatcher = HaveOccurred
}
} else {
expectedMatcher = IsNil
}
if matcher == expectedMatcher {
return false
}
d := analysis.Diagnostic{
Pos: ass.Pos(),
End: ass.End(),
Message: fmt.Sprintf(
"unidiomatic matcher: consider using %s instead of %s in this assertion",
expectedMatcher, matcher,
),
SuggestedFixes: []analysis.SuggestedFix{{
Message: fmt.Sprintf("change matcher to %s", expectedMatcher),
TextEdits: []analysis.TextEdit{{
Pos: matcherIdent.Pos(),
End: matcherIdent.End(),
NewText: []byte(expectedMatcher),
}},
}},
}
needsInverting := matchesNil(matcher) != matchesNil(expectedMatcher)
if needsInverting {
d.SuggestedFixes[0].TextEdits = append(d.SuggestedFixes[0].TextEdits, analysis.TextEdit{
Pos: ass.Should.Pos(),
End: ass.Should.End(),
NewText: []byte(renderInStyle(getStyle(ass.Omega.Name), ass.Negated != needsInverting)),
})
d.SuggestedFixes[0].Message += " and invert the assertion"
}
pass.Report(d)
return needsInverting
}
type KnownMatcher string
const (
UnknownMatcher KnownMatcher = ""
IsNil KnownMatcher = "BeNil"
HaveOccurred KnownMatcher = "HaveOccurred"
Succeed KnownMatcher = "Succeed"
)
func matchesNil(m KnownMatcher) bool { return m != HaveOccurred }
func getAssertion(n ast.Node) *assertion {
call, ok := n.(*ast.CallExpr)
if !ok || len(call.Args) != 1 {
return nil
}
shouldGetter, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return nil
}
omegaCall, ok := shouldGetter.X.(*ast.CallExpr)
if !ok || len(call.Args) != 1 {
return nil
}
omega, ok := omegaCall.Fun.(*ast.Ident)
if !ok {
return nil
}
switch omega.Name {
case Omega, Expect:
break
default:
return nil
}
negated := false
switch shouldGetter.Sel.Name {
case Should, To:
break
case ShouldNot, ToNot, NotTo:
negated = true
break
default:
return nil
}
return &assertion{
CallExpr: call,
Omega: omega,
Subject: omegaCall.Args[0],
Should: shouldGetter.Sel,
Negated: negated,
Matcher: call.Args[0],
}
}
func getStyle(s string) Style {
switch s {
case Omega, Should, ShouldNot:
return ShouldStyle
case Expect, To, ToNot, NotTo:
return ExpectStyle
default:
panic("foo")
}
}
func renderInStyle(style Style, negated bool) string {
switch style {
case ShouldStyle:
if negated {
return ShouldNot
} else {
return Should
}
case ExpectStyle:
if negated {
return NotTo
} else {
return To
}
default:
panic("bar")
}
}
func getKnownMatcher(ass assertion) (*ast.Ident, KnownMatcher) {
call, ok := ass.Matcher.(*ast.CallExpr)
if !ok || len(call.Args) != 0 {
return nil, UnknownMatcher
}
matcherIdent, ok := call.Fun.(*ast.Ident)
if !ok {
return nil, UnknownMatcher
}
knownMatcher := KnownMatcher(matcherIdent.Name)
switch knownMatcher {
case IsNil, HaveOccurred, Succeed:
return matcherIdent, knownMatcher
default:
return nil, UnknownMatcher
}
}
// isErrorExpr returns whether expr's type is an error or something that implements it
func isErrorExpr(e ast.Expr, info *types.Info) bool {
t := info.Types[e].Type
return types.Implements(t, errorInterface)
}
var errorInterface = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)