Skip to content

Commit 8382e1c

Browse files
authoredJul 18, 2024··
Add except-function-name-pattern option to argument-always-wildcard (#924)
And have functions starting with "mock_" excepted by default. Fixes #923 Signed-off-by: Anders Eknert <anders@styra.com>
1 parent d3a77ce commit 8382e1c

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed
 

‎bundle/regal/config/provided/data.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rules:
77
level: error
88
argument-always-wildcard:
99
level: error
10+
except-function-name-pattern: "^mock_"
1011
constant-condition:
1112
level: error
1213
deprecated-builtin:

‎bundle/regal/rules/bugs/argument_always_wildcard.rego

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package regal.rules.bugs["argument-always-wildcard"]
55
import rego.v1
66

77
import data.regal.ast
8+
import data.regal.config
89
import data.regal.result
910

1011
report contains violation if {
11-
some functions in _function_groups
12+
some name, functions in _function_groups
1213

1314
fn := _any_member(functions)
1415

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

23+
not _function_name_excepted(config.for_rule("bugs", "argument-always-wildcard"), name)
24+
2225
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(fn.head.args[pos]))
2326
}
2427

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

34+
_function_name_excepted(cfg, name) if regex.match(cfg["except-function-name-pattern"], name)
35+
3136
_any_member(s) := [x | some x in s][0]

‎bundle/regal/rules/bugs/argument_always_wildcard_test.rego

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ test_fail_single_function_single_argument_always_a_wildcard if {
2626
}}
2727
}
2828

29+
test_success_single_function_single_argument_always_a_wildcard_except_function_name if {
30+
module := ast.with_rego_v1(`
31+
mock_f(_) := 1
32+
`)
33+
34+
r := rule.report with input as module with config.for_rule as {"except-function-name-pattern": "^mock_"}
35+
r == set()
36+
}
37+
2938
test_fail_single_argument_always_a_wildcard if {
3039
module := ast.with_rego_v1(`
3140
f(_) := 1

‎docs/rules/bugs/argument-always-wildcard.md

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ rules:
8585
argument-always-wildcard:
8686
# one of "error", "warning", "ignore"
8787
level: error
88+
# function name patterns for which this rule should make an exception
89+
# default is to ignore any function name starting with "mock_" as these
90+
# commonly don't need named arguments
91+
except-function-name-pattern: "^mock_"
8892
```
8993
9094
## Community

0 commit comments

Comments
 (0)
Please sign in to comment.