Skip to content

Commit

Permalink
Add except-function-name-pattern option to argument-always-wildcard
Browse files Browse the repository at this point in the history
And have functions starting with "mock_" excepted by default.

Fixes #923

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Jul 17, 2024
1 parent 3abd5c0 commit b13f9ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions bundle/regal/config/provided/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rules:
level: error
argument-always-wildcard:
level: error
except-function-name-pattern: "^mock_"
constant-condition:
level: error
deprecated-builtin:
Expand Down
7 changes: 6 additions & 1 deletion bundle/regal/rules/bugs/argument_always_wildcard.rego
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package regal.rules.bugs["argument-always-wildcard"]
import rego.v1

import data.regal.ast
import data.regal.config
import data.regal.result

report contains violation if {
some functions in _function_groups
some name, functions in _function_groups

fn := _any_member(functions)

Expand All @@ -19,6 +20,8 @@ report contains violation if {
startswith(function.head.args[pos].value, "$")
}

not _function_name_excepted(config.for_rule("bugs", "argument-always-wildcard"), name)

violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(fn.head.args[pos]))
}

Expand All @@ -28,4 +31,6 @@ _function_groups[name] contains fn if {
name := ast.ref_to_string(fn.head.ref)
}

_function_name_excepted(cfg, name) if regex.match(cfg["except-function-name-pattern"], name)

_any_member(s) := [x | some x in s][0]
9 changes: 9 additions & 0 deletions bundle/regal/rules/bugs/argument_always_wildcard_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ test_fail_single_function_single_argument_always_a_wildcard if {
}}
}

test_success_single_function_single_argument_always_a_wildcard_except_function_name if {
module := ast.with_rego_v1(`
mock_f(_) := 1
`)

r := rule.report with input as module with config.for_rule as {"except-function-name-pattern": "^mock_"}
r == set()
}

test_fail_single_argument_always_a_wildcard if {
module := ast.with_rego_v1(`
f(_) := 1
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/bugs/argument-always-wildcard.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ rules:
argument-always-wildcard:
# one of "error", "warning", "ignore"
level: error
# function name patterns for which this rule should make an exception
# default is to ignore any function name starting with "mock_" as these
# commonly don't need named arguments
except-function-name-pattern: "^mock_"
```
## Community
Expand Down

0 comments on commit b13f9ad

Please sign in to comment.