This repository was archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelseif_checker.go
71 lines (63 loc) · 1.5 KB
/
elseif_checker.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
package checkers
import (
"go/ast"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astp"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "elseif"
info.Tags = []string{"style"}
info.Params = lintpack.CheckerParams{
"skipBalanced": {
Value: true,
Usage: "whether to skip balanced if-else pairs",
},
}
info.Summary = "Detects else with nested if statement that can be replaced with else-if"
info.Before = `
if cond1 {
} else {
if x := cond2; x {
}
}`
info.After = `
if cond1 {
} else if x := cond2; x {
}`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
c := &elseifChecker{ctx: ctx}
c.skipBalanced = info.Params.Bool("skipBalanced")
return astwalk.WalkerForStmt(c)
})
}
type elseifChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
skipBalanced bool
}
func (c *elseifChecker) VisitStmt(stmt ast.Stmt) {
if stmt, ok := stmt.(*ast.IfStmt); ok {
elseBody, ok := stmt.Else.(*ast.BlockStmt)
if !ok || len(elseBody.List) != 1 {
return
}
innerIfStmt, ok := elseBody.List[0].(*ast.IfStmt)
if !ok {
return
}
balanced := len(stmt.Body.List) == 1 &&
astp.IsIfStmt(stmt.Body.List[0])
if balanced && c.skipBalanced {
return // Configured to skip balanced statements
}
if innerIfStmt.Else != nil {
return
}
c.warn(stmt.Else)
}
}
func (c *elseifChecker) warn(cause ast.Node) {
c.ctx.Warn(cause, "can replace 'else {if cond {}}' with 'else if cond {}'")
}