Skip to content

Commit

Permalink
better messages for rules' config errors (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava authored Aug 17, 2021
1 parent 351bb12 commit 097f0bb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 29 deletions.
4 changes: 1 addition & 3 deletions rule/argument-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions rule/cognitive-complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 3 additions & 4 deletions rule/cyclomatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions rule/file-header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions rule/function-result-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions rule/line-length-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 7 additions & 8 deletions rule/max-public-structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions rule/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}

0 comments on commit 097f0bb

Please sign in to comment.