-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rule: messy-rule
#714
Merged
Merged
Rule: messy-rule
#714
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/#)! |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if 'disjointed' is would be more specific here? Fragmented might be another option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either way you'd need to read the rule docs to learn what it is about, so I think "messy" works well to catch the user's attention :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I guess that's fair enough.