Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor (rule/function-result-limit): replace AST walker by iteration over declarations #1167

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 33 additions & 47 deletions rule/function_result_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,33 @@ type FunctionResultsLimitRule struct {
configureOnce sync.Once
}

const defaultResultsLimit = 3

func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
if len(arguments) < 1 {
r.max = defaultResultsLimit
return
}

maxResults, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]))
}
if maxResults < 0 {
panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`)
}

r.max = int(maxResults)
}

// Apply applies the rule to given file.
func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
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)
},
}
num := 0
hasResults := funcDecl.Type.Results != nil
if hasResults {
num = funcDecl.Type.Results.NumFields()
}

if num <= r.max {
continue
}

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

return failures
}
Expand All @@ -57,29 +51,21 @@ func (*FunctionResultsLimitRule) Name() string {
return "function-result-limit"
}

type lintFunctionResultsNum struct {
max int
onFailure func(lint.Failure)
}
const defaultResultsLimit = 3

func (w lintFunctionResultsNum) Visit(n ast.Node) ast.Visitor {
node, ok := n.(*ast.FuncDecl)
if ok {
num := 0
hasResults := node.Type.Results != nil
if hasResults {
num = node.Type.Results.NumFields()
}
if num > w.max {
w.onFailure(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,
})
}
func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
if len(arguments) < 1 {
r.max = defaultResultsLimit
return
}

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

return w
r.max = int(maxResults)
}
Loading