forked from mgechev/revive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_control_nesting.go
126 lines (104 loc) · 3.12 KB
/
max_control_nesting.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
package rule
import (
"errors"
"fmt"
"go/ast"
"github.com/mgechev/revive/lint"
)
// MaxControlNestingRule sets restriction for maximum nesting of control structures.
type MaxControlNestingRule struct {
max int64
}
const defaultMaxControlNesting = 5
// Apply applies the rule to given file.
func (r *MaxControlNestingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
fileAst := file.AST
walker := &lintMaxControlNesting{
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
max: int(r.max),
}
ast.Walk(walker, fileAst)
return failures
}
// Name returns the rule name.
func (*MaxControlNestingRule) Name() string {
return "max-control-nesting"
}
type lintMaxControlNesting struct {
max int
onFailure func(lint.Failure)
nestingLevelAcc int
lastCtrlStmt ast.Node
}
func (w *lintMaxControlNesting) Visit(n ast.Node) ast.Visitor {
if w.nestingLevelAcc > w.max { // we are visiting a node beyond the max nesting level
w.onFailure(lint.Failure{
Failure: fmt.Sprintf("control flow nesting exceeds %d", w.max),
Confidence: 1,
Node: w.lastCtrlStmt,
Category: "complexity",
})
return nil // stop visiting deeper
}
switch v := n.(type) {
case *ast.IfStmt:
w.lastCtrlStmt = v
w.walkControlledBlock(v.Body) // "then" branch block
if v.Else != nil {
w.walkControlledBlock(v.Else) // "else" branch block
}
return nil // stop re-visiting nesting blocks (already visited by w.walkControlledBlock)
case *ast.ForStmt:
w.lastCtrlStmt = v
w.walkControlledBlock(v.Body)
return nil // stop re-visiting nesting blocks (already visited by w.walkControlledBlock)
case *ast.CaseClause: // switch case
w.lastCtrlStmt = v
for _, s := range v.Body { // visit each statement in the case clause
w.walkControlledBlock(s)
}
return nil // stop re-visiting nesting blocks (already visited by w.walkControlledBlock)
case *ast.CommClause: // select case
w.lastCtrlStmt = v
for _, s := range v.Body { // visit each statement in the select case clause
w.walkControlledBlock(s)
}
return nil // stop re-visiting nesting blocks (already visited by w.walkControlledBlock)
case *ast.FuncLit:
walker := &lintMaxControlNesting{
onFailure: w.onFailure,
max: w.max,
}
ast.Walk(walker, v.Body)
return nil
}
return w
}
func (w *lintMaxControlNesting) walkControlledBlock(b ast.Node) {
oldNestingLevel := w.nestingLevelAcc
w.nestingLevelAcc++
ast.Walk(w, b)
w.nestingLevelAcc = oldNestingLevel
}
// Configure validates the rule configuration, and configures the rule accordingly.
//
// Configuration implements the [lint.ConfigurableRule] interface.
func (r *MaxControlNestingRule) Configure(arguments lint.Arguments) error {
if len(arguments) < 1 {
r.max = defaultMaxControlNesting
return nil
}
check := checkNumberOfArguments(1, arguments, r.Name())
if check != nil {
return check
}
maxNesting, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
return errors.New(`invalid value passed as argument number to the "max-control-nesting" rule`)
}
r.max = maxNesting
return nil
}