-
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.
Help identify incremental rule definitions that aren't grouped together. Also fix two violations against this rule that we had in our own codebase. Fixes #672 Signed-off-by: Anders Eknert <anders@styra.com>
- Loading branch information
1 parent
e225bda
commit 3e92381
Showing
8 changed files
with
212 additions
and
22 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,26 @@ | ||
# METADATA | ||
# description: Messy incremental rule | ||
package regal.rules.style["messy-rule"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
report contains violation if { | ||
some i, rule1 in input.rules | ||
|
||
cur_name := ast.ref_to_string(rule1.head.ref) | ||
|
||
some j, rule2 in input.rules | ||
|
||
j > i | ||
|
||
nxt_name := ast.ref_to_string(rule2.head.ref) | ||
cur_name == nxt_name | ||
|
||
previous_name := ast.ref_to_string(input.rules[j - 1].head.ref) | ||
previous_name != nxt_name | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule2)) | ||
} |
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,99 @@ | ||
package regal.rules.style["messy-rule_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.style["messy-rule"] as rule | ||
|
||
test_success_non_messy_definition if { | ||
module := ast.with_rego_v1(` | ||
foo if true | ||
|
||
foo if 5 == 1 | ||
|
||
bar if false | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_fail_messy_definition if { | ||
module := ast.with_rego_v1(` | ||
foo if true | ||
|
||
bar if false | ||
|
||
foo if 5 == 1 | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\tfoo if 5 == 1"}) | ||
} | ||
|
||
test_fail_messy_default_definition if { | ||
module := ast.with_rego_v1(` | ||
default foo := true | ||
|
||
bar if false | ||
|
||
foo if 5 == 1 | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\tfoo if 5 == 1"}) | ||
} | ||
|
||
test_fail_messy_nested_rule_definiton if { | ||
module := ast.with_rego_v1(` | ||
base.foo if true | ||
|
||
bar if false | ||
|
||
base.foo if 5 == 1 | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\tbase.foo if 5 == 1"}) | ||
} | ||
|
||
test_success_non_incremental_nested_rule_definiton if { | ||
module := ast.with_rego_v1(` | ||
base.foo if true | ||
|
||
bar if false | ||
|
||
base.bar if 5 == 1 | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == set() | ||
} | ||
|
||
test_fail_messy_incremental_nested_variable_rule_definiton if { | ||
module := ast.with_rego_v1(` | ||
base[x].foo := 5 if { x := 1 } | ||
|
||
bar if false | ||
|
||
base[x].foo := 1 if { x := 1 } | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == expected_with_location({"col": 2, "file": "policy.rego", "row": 10, "text": "\tbase[x].foo := 1 if { x := 1 }"}) | ||
} | ||
|
||
expected := { | ||
"category": "style", | ||
"description": "Messy incremental rule", | ||
"level": "error", | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/messy-rule", "style"), | ||
}], | ||
"title": "messy-rule", | ||
} | ||
|
||
expected_with_location(location) := {object.union(expected, {"location": location})} if is_object(location) |
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,57 @@ | ||
# messy-rule | ||
|
||
**Summary**: Messy incremental rule | ||
|
||
**Category**: Style | ||
|
||
**Avoid** | ||
|
||
```rego | ||
package policy | ||
allow if something | ||
unrelated_rule if { | ||
# ... | ||
} | ||
allow if something_else | ||
``` | ||
|
||
**Prefer** | ||
|
||
```rego | ||
package policy | ||
allow if something | ||
allow if something_else | ||
unrelated_rule if { | ||
# ... | ||
} | ||
``` | ||
|
||
## Rationale | ||
|
||
Rules that are definecd incrementally should have their definitions grouped together, as this makes the code easier to | ||
follow. While this is mostly a style preference, having incremental rules grouped also allows editors like VS Code to | ||
"know" that the rules belong together, allowing them to be smarter when displaying the symbols of a workspace. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
style: | ||
messy-rule: | ||
# 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