-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer boolean expression in rule body over assigning the result in the rule head. Also did some consistency fixes in the docs "Related Resources" sections. Fixes #465 Signed-off-by: Anders Eknert <anders@styra.com>
- Loading branch information
1 parent
60668b9
commit f70e631
Showing
16 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# METADATA | ||
# description: Prefer `if` over boolean assignment | ||
package regal.rules.idiomatic["boolean-assignment"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.config | ||
import data.regal.result | ||
|
||
report contains violation if { | ||
some rule in input.rules | ||
|
||
rhv := rule.head.value | ||
|
||
rhv.type == "call" | ||
rhv.value[0].type == "ref" | ||
rhv.value[0].value[0].type == "var" | ||
|
||
ref_name := rhv.value[0].value[0].value | ||
|
||
config.capabilities.builtins[ref_name].decl.result == "boolean" | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package regal.rules.idiomatic["boolean-assignment_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.capabilities | ||
import data.regal.config | ||
|
||
import data.regal.rules.idiomatic["boolean-assignment"] as rule | ||
|
||
test_boolean_assignment_in_rule_head if { | ||
module := ast.policy("more_than_one := input.count > 1") | ||
|
||
r := rule.report with input as module | ||
with data.internal.combined_config as {"capabilities": capabilities.provided} | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Prefer `if` over boolean assignment", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "more_than_one := input.count > 1"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/boolean-assignment", "idiomatic"), | ||
}], | ||
"title": "boolean-assignment", | ||
}} | ||
} | ||
|
||
test_success_uses_if_instead_of_boolean_assignment_in_rule_head if { | ||
module := ast.with_rego_v1("more_than_one if input.count > 1") | ||
|
||
r := rule.report with input as module | ||
with data.internal.combined_config as {"capabilities": capabilities.provided} | ||
r == set() | ||
} | ||
|
||
test_success_non_boolean_assignment_in_rule_head if { | ||
module := ast.with_rego_v1(`foo := lower("FOO")`) | ||
|
||
r := rule.report with input as module | ||
with data.internal.combined_config as {"capabilities": capabilities.provided} | ||
r == set() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# boolean-assignment | ||
|
||
**Summary**: Prefer `if` over boolean assignment | ||
|
||
**Category**: Idiomatic | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
more_than_one_member := count(input.members) > 1 | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
more_than_one_member if count(input.members) > 1 | ||
``` | ||
|
||
## Rationale | ||
|
||
Assigning the result of a boolean function is almost always redundant, as the boolean value returned by the expression | ||
rarely is used for anything but to determine whether to continue evaluation. Moving the condition to the body following | ||
an `if` will have the rule either evaluate to `true` or be undefined. For the few cases where `false` should be | ||
returned, using a `default` rule assignment is preferable, as it is guaranteed to be assigned a value even on undefined | ||
input: | ||
|
||
```rego | ||
package policy | ||
import rego.v1 | ||
default more_than_one_member := false | ||
# will be assigned `false` even if input.members is undefined | ||
more_than_one_member if count(input.members) > 1 | ||
``` | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
idiomatic: | ||
boolean-assignment: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
|
||
## Related Resources | ||
|
||
- Styra Blog: [How to express OR in Rego](https://www.styra.com/blog/how-to-express-or-in-rego/) | ||
- Regal Docs: [default-over-else](https://docs.styra.com/regal/rules/style/default-over-else) | ||
|
||
## Community | ||
|
||
If you think you've found a problem with this rule or its documentation, would like to suggest improvements, new rules, | ||
or just talk about Regal in general, please join us in the `#regal` channel in the Styra Community | ||
[Slack](https://communityinviter.com/apps/styracommunity/signup)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters