mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
refactor: replace panic with error in rules (#1126)
Co-authored-by: chavacava <salvadorcavadini+github@gmail.com> Co-authored-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
This commit is contained in:
committed by
GitHub
parent
10d9697cc2
commit
9b15f3fcb6
@@ -1,6 +1,7 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"strings"
|
||||
@@ -18,24 +19,33 @@ type MaxPublicStructsRule struct {
|
||||
|
||||
const defaultMaxPublicStructs = 5
|
||||
|
||||
func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) {
|
||||
func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error {
|
||||
if len(arguments) < 1 {
|
||||
r.max = defaultMaxPublicStructs
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
checkNumberOfArguments(1, arguments, r.Name())
|
||||
err := checkNumberOfArguments(1, arguments, r.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
maxStructs, ok := arguments[0].(int64) // Alt. non panicking version
|
||||
if !ok {
|
||||
panic(`invalid value passed as argument number to the "max-public-structs" rule`)
|
||||
return errors.New(`invalid value passed as argument number to the "max-public-structs" rule`)
|
||||
}
|
||||
r.max = maxStructs
|
||||
return nil
|
||||
}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
r.configureOnce.Do(func() { r.configure(arguments) })
|
||||
var configureErr error
|
||||
r.configureOnce.Do(func() { configureErr = r.configure(arguments) })
|
||||
|
||||
if configureErr != nil {
|
||||
return newInternalFailureError(configureErr)
|
||||
}
|
||||
|
||||
var failures []lint.Failure
|
||||
|
||||
|
||||
Reference in New Issue
Block a user