Skip to content

Commit 7e6178f

Browse files
Merge pull request #57 from sashamelentyev/refactor/ifstmt
refactor: refactor ifstmt case
2 parents 0803ef3 + 4aee2d0 commit 7e6178f

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

pkg/analyzer/analyzer.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ func flags() flag.FlagSet {
4949
func run(pass *analysis.Pass) (interface{}, error) {
5050
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
5151

52-
filter := []ast.Node{
52+
types := []ast.Node{
5353
(*ast.CallExpr)(nil),
5454
(*ast.BasicLit)(nil),
5555
(*ast.CompositeLit)(nil),
5656
(*ast.IfStmt)(nil),
5757
(*ast.SwitchStmt)(nil),
5858
}
5959

60-
insp.Preorder(filter, func(node ast.Node) {
60+
insp.Preorder(types, func(node ast.Node) {
6161
switch n := node.(type) {
6262
case *ast.CallExpr:
6363
selectorExpr, ok := n.Fun.(*ast.SelectorExpr)
@@ -194,35 +194,22 @@ func run(pass *analysis.Pass) (interface{}, error) {
194194
}
195195

196196
case *ast.IfStmt:
197-
binaryExpr, ok := n.Cond.(*ast.BinaryExpr)
197+
cond, ok := n.Cond.(*ast.BinaryExpr)
198198
if !ok {
199199
return
200200
}
201201

202-
selectorExpr, ok := binaryExpr.X.(*ast.SelectorExpr)
202+
x, ok := cond.X.(*ast.SelectorExpr)
203203
if !ok {
204204
return
205205
}
206206

207-
basicLit, ok := binaryExpr.Y.(*ast.BasicLit)
207+
y, ok := cond.Y.(*ast.BasicLit)
208208
if !ok {
209209
return
210210
}
211211

212-
switch selectorExpr.Sel.Name {
213-
case "StatusCode":
214-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
215-
return
216-
}
217-
218-
checkHTTPStatusCode(pass, basicLit)
219-
case "Method":
220-
if !lookupFlag(pass, HTTPMethodFlag) {
221-
return
222-
}
223-
224-
checkHTTPMethod(pass, basicLit)
225-
}
212+
ifstmt(pass, x, y)
226213

227214
case *ast.SwitchStmt:
228215
selectorExpr, ok := n.Tag.(*ast.SelectorExpr)
@@ -307,6 +294,23 @@ func run(pass *analysis.Pass) (interface{}, error) {
307294
return nil, nil
308295
}
309296

297+
func ifstmt(pass *analysis.Pass, x *ast.SelectorExpr, y *ast.BasicLit) {
298+
switch x.Sel.Name {
299+
case "StatusCode":
300+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
301+
return
302+
}
303+
304+
checkHTTPStatusCode(pass, y)
305+
case "Method":
306+
if !lookupFlag(pass, HTTPMethodFlag) {
307+
return
308+
}
309+
310+
checkHTTPMethod(pass, y)
311+
}
312+
}
313+
310314
func lookupFlag(pass *analysis.Pass, name string) bool {
311315
return pass.Analyzer.Flags.Lookup(name).Value.(flag.Getter).Get().(bool)
312316
}

0 commit comments

Comments
 (0)