mirror of
https://github.com/mgechev/revive.git
synced 2024-11-21 17:16:40 +02:00
refactor: reduce control nesting in AddConstantRule.configure (#1128)
This commit is contained in:
parent
655e6060b2
commit
d81fc8fab3
@ -202,13 +202,14 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool {
|
||||
}
|
||||
|
||||
func (r *AddConstantRule) configure(arguments lint.Arguments) {
|
||||
if r.allowList == nil {
|
||||
r.strLitLimit = defaultStrLitLimit
|
||||
r.allowList = newAllowList()
|
||||
if len(arguments) > 0 {
|
||||
if len(arguments) == 0 {
|
||||
return
|
||||
}
|
||||
args, ok := arguments[0].(map[string]any)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Invalid argument to the add-constant rule. Expecting a k,v map, got %T", arguments[0]))
|
||||
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting a k,v map. Got %T", arguments[0]))
|
||||
}
|
||||
for k, v := range args {
|
||||
kind := ""
|
||||
@ -262,6 +263,4 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,18 @@ import (
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestAddConstant(t *testing.T) {
|
||||
args := []any{map[string]any{
|
||||
func TestAddConstantWithDefaultArguments(t *testing.T) {
|
||||
testRule(t, "add_constant_default", &rule.AddConstantRule{}, &lint.RuleConfig{})
|
||||
}
|
||||
|
||||
func TestAddConstantWithArguments(t *testing.T) {
|
||||
testRule(t, "add_constant", &rule.AddConstantRule{}, &lint.RuleConfig{
|
||||
Arguments: []any{map[string]any{
|
||||
"maxLitCount": "2",
|
||||
"allowStrs": "\"\"",
|
||||
"allowInts": "0,1,2",
|
||||
"allowFloats": "0.0,1.0",
|
||||
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc,\\.Info",
|
||||
}}
|
||||
|
||||
testRule(t, "add_constant", &rule.AddConstantRule{}, &lint.RuleConfig{
|
||||
Arguments: args,
|
||||
}},
|
||||
})
|
||||
}
|
||||
|
12
testdata/add_constant_default.go
vendored
Normal file
12
testdata/add_constant_default.go
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
package fixtures
|
||||
|
||||
func foo() {
|
||||
a = "ignore"
|
||||
b = "ignore"
|
||||
|
||||
c = "match"
|
||||
d = "match"
|
||||
e = "match" // MATCH /string literal "match" appears, at least, 3 times, create a named constant for it/
|
||||
|
||||
f = 5 // MATCH /avoid magic numbers like '5', create a named constant for it/
|
||||
}
|
Loading…
Reference in New Issue
Block a user