Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add except-function-name-pattern option to argument-always-wildcard #924

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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