Skip to content

Commit

Permalink
refactor (rule/function-result-limit): replace AST walker by iteratio…
Browse files Browse the repository at this point in the history
…n over declarations
  • Loading branch information
chavacava committed Dec 4, 2024
1 parent 09fb350 commit 58f8a7e
Showing 1 changed file with 18 additions and 34 deletions.
52 changes: 18 additions & 34 deletions rule/function_result_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,31 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen
r.configureOnce.Do(func() { r.configure(arguments) })

var failures []lint.Failure
for _, decl := range file.AST.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}

walker := lintFunctionResultsNum{
max: r.max,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}

ast.Walk(walker, file.AST)

return failures
}

// Name returns the rule name.
func (*FunctionResultsLimitRule) Name() string {
return "function-result-limit"
}

type lintFunctionResultsNum struct {
max int
onFailure func(lint.Failure)
}

func (w lintFunctionResultsNum) Visit(n ast.Node) ast.Visitor {
node, ok := n.(*ast.FuncDecl)
if ok {
num := 0
hasResults := node.Type.Results != nil
hasResults := funcDecl.Type.Results != nil
if hasResults {
num = node.Type.Results.NumFields()
num = funcDecl.Type.Results.NumFields()
}
if num > w.max {
w.onFailure(lint.Failure{

if num > r.max {
failures = append(failures, lint.Failure{
Confidence: 1,
Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", w.max, num),
Node: node.Type,
Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", r.max, num),
Node: funcDecl.Type,
})
}

return nil // skip visiting function's body
}

return w
return failures
}

// Name returns the rule name.
func (*FunctionResultsLimitRule) Name() string {
return "function-result-limit"
}

0 comments on commit 58f8a7e

Please sign in to comment.