Skip to content

Commit

Permalink
rule-length: add count-comments configuration option
Browse files Browse the repository at this point in the history
This is set to `false` by default, as we don't want to "punish"
comments in rules.

Fixes #432

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Nov 1, 2023
1 parent 20bd3f5 commit 0c067f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions bundle/regal/config/provided/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ rules:
ignore-nesting-level: 2
level: error
rule-length:
count-comments: false
except-empty-body: true
level: error
max-rule-length: 30
Expand Down
24 changes: 23 additions & 1 deletion bundle/regal/rules/style/rule_length.rego
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ report contains violation if {
some rule in input.rules
lines := split(base64.decode(rule.location.text), "\n")

count(lines) > cfg["max-rule-length"]
line_count(cfg, rule, lines) > cfg["max-rule-length"]

not generated_body_exception(cfg, rule)

Expand All @@ -27,3 +27,25 @@ generated_body_exception(conf, rule) if {
conf["except-empty-body"] == true
ast.generated_body(rule)
}

line_count(cfg, _, lines) := count(lines) if cfg["count-comments"] == true

line_count(cfg, rule, lines) := n if {
not cfg["count-comments"]

# Note that this assumes } on its own line
body_start := rule.location.row + 1
body_end := (body_start + count(lines)) - 3
body_total := (body_end - body_start) + 1

# This does not take into account comments that are
# on the same line as regular code
body_comments := sum([1 |
some comment in input.comments

comment.Location.row >= body_start
comment.Location.row <= body_end
])

n := body_total - body_comments
}
22 changes: 22 additions & 0 deletions bundle/regal/rules/style/rule_length_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test_fail_rule_longer_than_configured_max_length if {
r := rule.report with input as module with config.for_rule as {
"level": "error",
"max-rule-length": 3,
"count-comments": true,
}
r == {{
"category": "style",
Expand Down Expand Up @@ -49,6 +50,26 @@ test_success_rule_not_longer_than_configured_max_length if {
r := rule.report with input as module with config.for_rule as {
"level": "error",
"max-rule-length": 30,
"count-comments": true,
}
r == set()
}

test_success_rule_longer_than_configured_max_length_but_comments if {
module := regal.parse_module("policy.rego", `package p
my_short_rule {
# this rule is not longer than the configured max length
# which in this case is 30 lines
#
input.x
}
`)

r := rule.report with input as module with config.for_rule as {
"level": "error",
"max-rule-length": 2,
"count-comments": false,
}
r == set()
}
Expand All @@ -62,6 +83,7 @@ test_success_rule_length_equals_max_length if {
r := rule.report with input as module with config.for_rule as {
"level": "error",
"max-rule-length": 1,
"count-comments": false,
}
r == set()
}
3 changes: 3 additions & 0 deletions docs/rules/style/rule-length.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ rules:
level: error
# default limit is 30 lines
max-rule-length: 30
# whether to count comments as lines
# by default, this is set to false
count-comments: false
# except rules with empty bodies from this rule, as they're
# likely an assignment of long values rather than a "rule"
# with conditions:
Expand Down
1 change: 1 addition & 0 deletions e2e/testdata/violations/most_violations.rego
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ rule_length {
input.x28
input.x29
input.x30
input.x31
}

default_over_else := 1 {
Expand Down

0 comments on commit 0c067f0

Please sign in to comment.