1
0
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:
Marcin Federowicz
2024-12-11 19:35:58 +01:00
committed by GitHub
parent 10d9697cc2
commit 9b15f3fcb6
35 changed files with 514 additions and 239 deletions

View File

@@ -18,15 +18,15 @@ type ReceiverNamingRule struct {
const defaultReceiverNameMaxLength = -1 // thus will not check
func (r *ReceiverNamingRule) configure(arguments lint.Arguments) {
func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error {
r.receiverNameMaxLength = defaultReceiverNameMaxLength
if len(arguments) < 1 {
return
return nil
}
args, ok := arguments[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Unable to get arguments for rule %s. Expected object of key-value-pairs.", r.Name()))
return fmt.Errorf("unable to get arguments for rule %s. Expected object of key-value-pairs", r.Name())
}
for k, v := range args {
@@ -34,18 +34,24 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) {
case "maxLength":
value, ok := v.(int64)
if !ok {
panic(fmt.Sprintf("Invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v))
return fmt.Errorf("invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v)
}
r.receiverNameMaxLength = int(value)
default:
panic(fmt.Sprintf("Unknown argument %s for %s rule.", k, r.Name()))
return fmt.Errorf("unknown argument %s for %s rule", k, r.Name())
}
}
return nil
}
// Apply applies the rule to given file.
func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
r.configureOnce.Do(func() { r.configure(args) })
func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var configureErr error
r.configureOnce.Do(func() { configureErr = r.configure(arguments) })
if configureErr != nil {
return newInternalFailureError(configureErr)
}
typeReceiver := map[string]string{}
var failures []lint.Failure