Skip to content

Commit a31d590

Browse files
authored
refactor (rule/function-result-limit): replace AST walker by iteration over declarations (#1167)
1 parent a31d5af commit a31d590

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

rule/function_result_limit.go

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,33 @@ type FunctionResultsLimitRule struct {
1515
configureOnce sync.Once
1616
}
1717

18-
const defaultResultsLimit = 3
19-
20-
func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
21-
if len(arguments) < 1 {
22-
r.max = defaultResultsLimit
23-
return
24-
}
25-
26-
maxResults, ok := arguments[0].(int64) // Alt. non panicking version
27-
if !ok {
28-
panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]))
29-
}
30-
if maxResults < 0 {
31-
panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`)
32-
}
33-
34-
r.max = int(maxResults)
35-
}
36-
3718
// Apply applies the rule to given file.
3819
func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
3920
r.configureOnce.Do(func() { r.configure(arguments) })
4021

4122
var failures []lint.Failure
23+
for _, decl := range file.AST.Decls {
24+
funcDecl, ok := decl.(*ast.FuncDecl)
25+
if !ok {
26+
continue
27+
}
4228

43-
walker := lintFunctionResultsNum{
44-
max: r.max,
45-
onFailure: func(failure lint.Failure) {
46-
failures = append(failures, failure)
47-
},
48-
}
29+
num := 0
30+
hasResults := funcDecl.Type.Results != nil
31+
if hasResults {
32+
num = funcDecl.Type.Results.NumFields()
33+
}
34+
35+
if num <= r.max {
36+
continue
37+
}
4938

50-
ast.Walk(walker, file.AST)
39+
failures = append(failures, lint.Failure{
40+
Confidence: 1,
41+
Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", r.max, num),
42+
Node: funcDecl.Type,
43+
})
44+
}
5145

5246
return failures
5347
}
@@ -57,29 +51,21 @@ func (*FunctionResultsLimitRule) Name() string {
5751
return "function-result-limit"
5852
}
5953

60-
type lintFunctionResultsNum struct {
61-
max int
62-
onFailure func(lint.Failure)
63-
}
54+
const defaultResultsLimit = 3
6455

65-
func (w lintFunctionResultsNum) Visit(n ast.Node) ast.Visitor {
66-
node, ok := n.(*ast.FuncDecl)
67-
if ok {
68-
num := 0
69-
hasResults := node.Type.Results != nil
70-
if hasResults {
71-
num = node.Type.Results.NumFields()
72-
}
73-
if num > w.max {
74-
w.onFailure(lint.Failure{
75-
Confidence: 1,
76-
Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", w.max, num),
77-
Node: node.Type,
78-
})
79-
}
56+
func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
57+
if len(arguments) < 1 {
58+
r.max = defaultResultsLimit
59+
return
60+
}
8061

81-
return nil // skip visiting function's body
62+
maxResults, ok := arguments[0].(int64) // Alt. non panicking version
63+
if !ok {
64+
panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]))
65+
}
66+
if maxResults < 0 {
67+
panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`)
8268
}
8369

84-
return w
70+
r.max = int(maxResults)
8571
}

0 commit comments

Comments
 (0)