-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: - fix the one violation of this rule found in our code :P - add metadata annotations to a bunch of rules Fixes #867 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
300eef2
commit ce8b8ff
Showing
9 changed files
with
200 additions
and
8 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
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,39 @@ | ||
# METADATA | ||
# description: Pointless reassignment of variable | ||
package regal.rules.style["pointless-reassignment"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
# pointless reassignment in rule head | ||
report contains violation if { | ||
some rule in ast.rules | ||
|
||
ast.generated_body(rule) | ||
|
||
rule.head.value.type == "var" | ||
count(rule.head.ref) == 1 | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} | ||
|
||
# pointless reassignment in rule body | ||
report contains violation if { | ||
some call in ast.all_refs | ||
|
||
call[0].value[0].type == "var" | ||
call[0].value[0].value == "assign" | ||
|
||
call[2].type == "var" | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(call)) | ||
} | ||
|
||
assign_calls contains call if { | ||
some call in ast.all_refs | ||
|
||
call[0].value[0].type == "var" | ||
call[0].value[0].value == "assign" | ||
} |
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,52 @@ | ||
package regal.rules.style["pointless-reassignment_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.style["pointless-reassignment"] as rule | ||
|
||
test_pointless_reassignment_in_rule_head if { | ||
module := ast.with_rego_v1(` | ||
foo := "foo" | ||
bar := foo | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "style", | ||
"description": "Pointless reassignment of variable", | ||
"level": "error", | ||
"location": {"col": 2, "file": "policy.rego", "row": 8, "text": "\tbar := foo"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/pointless-reassignment", "style"), | ||
}], | ||
"title": "pointless-reassignment", | ||
}} | ||
} | ||
|
||
test_pointless_reassignment_in_rule_body if { | ||
module := ast.with_rego_v1(` | ||
rule if { | ||
foo := "foo" | ||
bar := foo | ||
} | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "style", | ||
"description": "Pointless reassignment of variable", | ||
"level": "error", | ||
"location": {"col": 7, "file": "policy.rego", "row": 9, "text": "\t\tbar := foo"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/pointless-reassignment", "style"), | ||
}], | ||
"title": "pointless-reassignment", | ||
}} | ||
} |
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,62 @@ | ||
# pointless-reassignment | ||
|
||
**Summary**: Pointless reassignment of variable | ||
|
||
**Category**: Style | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
allow if { | ||
users := all_users | ||
any_admin(users) | ||
} | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
allow if { | ||
any_admin(all_users) | ||
} | ||
``` | ||
|
||
## Rationale | ||
|
||
Values and variables are immutable in Rego, so reassigning the value of one variable to another only adds noise. | ||
|
||
## Exceptions | ||
|
||
Reassigning the value of a long reference often helps readability, and especially so when it needs to be referenced | ||
multiple times: | ||
|
||
```rego | ||
package policy | ||
allow if { | ||
users := input.context.permissions.users | ||
any_admin(users) | ||
} | ||
``` | ||
|
||
This rule does not consider such assignments violations. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
style: | ||
pointless-reassignment: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## 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