Skip to content

Commit

Permalink
refactor: reduce indentation for AddConstantRule.configure
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Nov 18, 2024
1 parent 655e606 commit ec20e77
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 65 deletions.
111 changes: 55 additions & 56 deletions rule/add_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,65 +202,64 @@ 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 {
args, ok := arguments[0].(map[string]any)
r.strLitLimit = defaultStrLitLimit
r.allowList = newAllowList()
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]))
}
for k, v := range args {
kind := ""
switch k {
case "allowFloats":
kind = kindFLOAT
fallthrough
case "allowInts":
if kind == "" {
kind = kindINT
}
fallthrough
case "allowStrs":
if kind == "" {
kind = kindSTRING
}
list, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v))
}
r.allowList.add(kind, list)
case "maxLitCount":
sl, ok := v.(string)
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 string representation of an integer. Got '%v' (%T)", v, v))
}
for k, v := range args {
kind := ""
switch k {
case "allowFloats":
kind = kindFLOAT
fallthrough
case "allowInts":
if kind == "" {
kind = kindINT
}
fallthrough
case "allowStrs":
if kind == "" {
kind = kindSTRING
}
list, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v))
}
r.allowList.add(kind, list)
case "maxLitCount":
sl, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v))
}

limit, err := strconv.Atoi(sl)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v))
}
r.strLitLimit = limit
case "ignoreFuncs":
excludes, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v))
}

for _, exclude := range strings.Split(excludes, ",") {
exclude = strings.Trim(exclude, " ")
if exclude == "" {
panic("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.")
}

exp, err := regexp.Compile(exclude)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err))
}

r.ignoreFunctions = append(r.ignoreFunctions, exp)
}

limit, err := strconv.Atoi(sl)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v))
}
r.strLitLimit = limit
case "ignoreFuncs":
excludes, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v))
}

for _, exclude := range strings.Split(excludes, ",") {
exclude = strings.Trim(exclude, " ")
if exclude == "" {
panic("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.")
}

exp, err := regexp.Compile(exclude)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err))
}

r.ignoreFunctions = append(r.ignoreFunctions, exp)
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions test/add_constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
"github.com/mgechev/revive/rule"
)

func TestAddConstant(t *testing.T) {
args := []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",
}}
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: args,
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",
}},
})
}
12 changes: 12 additions & 0 deletions testdata/add_constant_default.go
Original file line number Diff line number Diff line change
@@ -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/
}

0 comments on commit ec20e77

Please sign in to comment.