diff --git a/rule/argument-limit.go b/rule/argument-limit.go index 2b11d49..03bfa7f 100644 --- a/rule/argument-limit.go +++ b/rule/argument-limit.go @@ -12,9 +12,7 @@ type ArgumentsLimitRule struct{} // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - if len(arguments) != 1 { - panic(`invalid configuration for "argument-limit"`) - } + checkNumberOfArguments(1, arguments, r.Name()) total, ok := arguments[0].(int64) // Alt. non panicking version if !ok { diff --git a/rule/cognitive-complexity.go b/rule/cognitive-complexity.go index ccd36bd..7176c99 100644 --- a/rule/cognitive-complexity.go +++ b/rule/cognitive-complexity.go @@ -14,17 +14,15 @@ type CognitiveComplexityRule struct{} // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - var failures []lint.Failure + checkNumberOfArguments(1, arguments, r.Name()) - const expectedArgumentsCount = 1 - if len(arguments) < expectedArgumentsCount { - panic(fmt.Sprintf("not enough arguments for cognitive-complexity, expected %d, got %d", expectedArgumentsCount, len(arguments))) - } complexity, ok := arguments[0].(int64) if !ok { panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0])) } + var failures []lint.Failure + linter := cognitiveComplexityLinter{ file: file, maxComplexity: int(complexity), diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index f3af290..f597909 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -15,16 +15,15 @@ type CyclomaticRule struct{} // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - var failures []lint.Failure + checkNumberOfArguments(1, arguments, r.Name()) - if len(arguments) == 0 { - panic("not enough arguments for " + r.Name()) - } complexity, ok := arguments[0].(int64) // Alt. non panicking version if !ok { panic("invalid argument for cyclomatic complexity") } + var failures []lint.Failure + fileAst := file.AST walker := lintCyclomatic{ file: file, diff --git a/rule/file-header.go b/rule/file-header.go index 6df974e..7855c85 100644 --- a/rule/file-header.go +++ b/rule/file-header.go @@ -16,9 +16,7 @@ var ( // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - if len(arguments) != 1 { - panic(`invalid configuration for "file-header" rule`) - } + checkNumberOfArguments(1, arguments, r.Name()) header, ok := arguments[0].(string) if !ok { diff --git a/rule/function-result-limit.go b/rule/function-result-limit.go index 1850fc4..51a4713 100644 --- a/rule/function-result-limit.go +++ b/rule/function-result-limit.go @@ -12,9 +12,7 @@ type FunctionResultsLimitRule struct{} // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - if len(arguments) != 1 { - panic(`invalid configuration for "function-result-limit"`) - } + checkNumberOfArguments(1, arguments, r.Name()) max, ok := arguments[0].(int64) // Alt. non panicking version if !ok { diff --git a/rule/line-length-limit.go b/rule/line-length-limit.go index 5ee0570..939ef22 100644 --- a/rule/line-length-limit.go +++ b/rule/line-length-limit.go @@ -16,9 +16,7 @@ type LineLengthLimitRule struct{} // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - if len(arguments) != 1 { - panic(`invalid configuration for "line-length-limit"`) - } + checkNumberOfArguments(1, arguments, r.Name()) max, ok := arguments[0].(int64) // Alt. non panicking version if !ok || max < 0 { diff --git a/rule/max-public-structs.go b/rule/max-public-structs.go index 551b370..aa15628 100644 --- a/rule/max-public-structs.go +++ b/rule/max-public-structs.go @@ -13,11 +13,15 @@ type MaxPublicStructsRule struct{} // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - var failures []lint.Failure - if len(arguments) == 0 { - panic("not enough arguments for " + r.Name()) + checkNumberOfArguments(1, arguments, r.Name()) + + max, ok := arguments[0].(int64) // Alt. non panicking version + if !ok { + panic(`invalid value passed as argument number to the "max-public-structs" rule`) } + var failures []lint.Failure + fileAst := file.AST walker := &lintMaxPublicStructs{ fileAst: fileAst, @@ -28,11 +32,6 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ast.Walk(walker, fileAst) - max, ok := arguments[0].(int64) // Alt. non panicking version - if !ok { - panic(`invalid value passed as argument number to the "max-public-structs" rule`) - } - if walker.current > max { walker.onFailure(lint.Failure{ Failure: "you have exceeded the maximum number of public struct declarations", diff --git a/rule/utils.go b/rule/utils.go index d2b764f..0d57448 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -190,3 +190,10 @@ func gofmt(x interface{}) string { printer.Fprint(&buf, fs, x) return buf.String() } + +// checkNumberOfArguments fails if the given number of arguments is not, at least, the expected one +func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string) { + if len(args) < expected { + panic(fmt.Sprintf("not enough arguments for %s rule, expected %d, got %d. Please check the rule's documentation", ruleName, expected, len(args))) + } +}