Skip to content

Commit 097f0bb

Browse files
authored
better messages for rules' config errors (#563)
1 parent 351bb12 commit 097f0bb

File tree

8 files changed

+24
-29
lines changed

8 files changed

+24
-29
lines changed

rule/argument-limit.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ type ArgumentsLimitRule struct{}
1212

1313
// Apply applies the rule to given file.
1414
func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
15-
if len(arguments) != 1 {
16-
panic(`invalid configuration for "argument-limit"`)
17-
}
15+
checkNumberOfArguments(1, arguments, r.Name())
1816

1917
total, ok := arguments[0].(int64) // Alt. non panicking version
2018
if !ok {

rule/cognitive-complexity.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ type CognitiveComplexityRule struct{}
1414

1515
// Apply applies the rule to given file.
1616
func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
17-
var failures []lint.Failure
17+
checkNumberOfArguments(1, arguments, r.Name())
1818

19-
const expectedArgumentsCount = 1
20-
if len(arguments) < expectedArgumentsCount {
21-
panic(fmt.Sprintf("not enough arguments for cognitive-complexity, expected %d, got %d", expectedArgumentsCount, len(arguments)))
22-
}
2319
complexity, ok := arguments[0].(int64)
2420
if !ok {
2521
panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0]))
2622
}
2723

24+
var failures []lint.Failure
25+
2826
linter := cognitiveComplexityLinter{
2927
file: file,
3028
maxComplexity: int(complexity),

rule/cyclomatic.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ type CyclomaticRule struct{}
1515

1616
// Apply applies the rule to given file.
1717
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
18-
var failures []lint.Failure
18+
checkNumberOfArguments(1, arguments, r.Name())
1919

20-
if len(arguments) == 0 {
21-
panic("not enough arguments for " + r.Name())
22-
}
2320
complexity, ok := arguments[0].(int64) // Alt. non panicking version
2421
if !ok {
2522
panic("invalid argument for cyclomatic complexity")
2623
}
2724

25+
var failures []lint.Failure
26+
2827
fileAst := file.AST
2928
walker := lintCyclomatic{
3029
file: file,

rule/file-header.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ var (
1616

1717
// Apply applies the rule to given file.
1818
func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
19-
if len(arguments) != 1 {
20-
panic(`invalid configuration for "file-header" rule`)
21-
}
19+
checkNumberOfArguments(1, arguments, r.Name())
2220

2321
header, ok := arguments[0].(string)
2422
if !ok {

rule/function-result-limit.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ type FunctionResultsLimitRule struct{}
1212

1313
// Apply applies the rule to given file.
1414
func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
15-
if len(arguments) != 1 {
16-
panic(`invalid configuration for "function-result-limit"`)
17-
}
15+
checkNumberOfArguments(1, arguments, r.Name())
1816

1917
max, ok := arguments[0].(int64) // Alt. non panicking version
2018
if !ok {

rule/line-length-limit.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ type LineLengthLimitRule struct{}
1616

1717
// Apply applies the rule to given file.
1818
func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
19-
if len(arguments) != 1 {
20-
panic(`invalid configuration for "line-length-limit"`)
21-
}
19+
checkNumberOfArguments(1, arguments, r.Name())
2220

2321
max, ok := arguments[0].(int64) // Alt. non panicking version
2422
if !ok || max < 0 {

rule/max-public-structs.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ type MaxPublicStructsRule struct{}
1313

1414
// Apply applies the rule to given file.
1515
func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
16-
var failures []lint.Failure
17-
if len(arguments) == 0 {
18-
panic("not enough arguments for " + r.Name())
16+
checkNumberOfArguments(1, arguments, r.Name())
17+
18+
max, ok := arguments[0].(int64) // Alt. non panicking version
19+
if !ok {
20+
panic(`invalid value passed as argument number to the "max-public-structs" rule`)
1921
}
2022

23+
var failures []lint.Failure
24+
2125
fileAst := file.AST
2226
walker := &lintMaxPublicStructs{
2327
fileAst: fileAst,
@@ -28,11 +32,6 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments)
2832

2933
ast.Walk(walker, fileAst)
3034

31-
max, ok := arguments[0].(int64) // Alt. non panicking version
32-
if !ok {
33-
panic(`invalid value passed as argument number to the "max-public-structs" rule`)
34-
}
35-
3635
if walker.current > max {
3736
walker.onFailure(lint.Failure{
3837
Failure: "you have exceeded the maximum number of public struct declarations",

rule/utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,10 @@ func gofmt(x interface{}) string {
190190
printer.Fprint(&buf, fs, x)
191191
return buf.String()
192192
}
193+
194+
// checkNumberOfArguments fails if the given number of arguments is not, at least, the expected one
195+
func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string) {
196+
if len(args) < expected {
197+
panic(fmt.Sprintf("not enough arguments for %s rule, expected %d, got %d. Please check the rule's documentation", ruleName, expected, len(args)))
198+
}
199+
}

0 commit comments

Comments
 (0)