Skip to content

Commit

Permalink
Add non-breakable-word-threshold option to line-length rule (#350)
Browse files Browse the repository at this point in the history
Fixes #327

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Sep 28, 2023
1 parent 590ce2e commit 41e127d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 0 additions & 1 deletion bundle/regal/rules/imports/prefer_package_imports.rego
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import future.keywords.contains
import future.keywords.if
import future.keywords.in

import data.regal.ast
import data.regal.config
import data.regal.result

Expand Down
13 changes: 11 additions & 2 deletions bundle/regal/rules/style/line_length.rego
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import future.keywords.in
import data.regal.config
import data.regal.result

report contains violation if {
cfg := config.for_rule("style", "line-length")
cfg := config.for_rule("style", "line-length")

report contains violation if {
some i, line in input.regal.file.lines

line_length := count(line)
line_length > cfg["max-line-length"]

not has_word_above_threshold(line, cfg)

violation := result.fail(
rego.metadata.chain(),
{"location": {
Expand All @@ -27,3 +29,10 @@ report contains violation if {
}},
)
}

has_word_above_threshold(line, conf) if {
threshold := conf["non-breakable-word-threshold"]

some word in split(line, " ")
count(word) > threshold
}
36 changes: 36 additions & 0 deletions bundle/regal/rules/style/line_length_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ foo == bar; bar == baz; [a, b, c, d, e, f] := [1, 2, 3, 4, 5, 6]; qux := [q | so
}}
}

test_success_line_too_long_but_non_breakable_word if {
r := rule.report with input as ast.with_future_keywords(`
# Long url: https://www.example.com/this/is/a/very/long/url/that/cannot/be/shortened
allow := true
`)
with config.for_rule as {"level": "error", "max-line-length": 40, "non-breakable-word-threshold": 50}

r == set()
}

test_fail_line_too_long_but_below_breakable_word_threshold if {
r := rule.report with input as ast.with_future_keywords(`
# Long url: https://www.example.com/this/is/a/very/long
allow := true
`)
with config.for_rule as {"level": "error", "max-line-length": 40, "non-breakable-word-threshold": 60}

r == {{
"category": "style",
"description": "Line too long",
"level": "error",
"location": {
"col": 56,
"file": "policy.rego",
"row": 10, "text": "\t# Long url: https://www.example.com/this/is/a/very/long",
},
"related_resources": [{
"description": "documentation",
"ref": "https://docs.styra.com/regal/rules/style/line-length",
}],
"title": "line-length",
}}
}

test_success_line_not_too_long if {
r := rule.report with input as ast.policy(`allow { "foo" == "bar" }`)
with config.for_rule as {"level": "error", "max-line-length": 80}
Expand Down
9 changes: 9 additions & 0 deletions docs/rules/style/line-length.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ close to the maximum line length, consider refactoring your policy.

The default maximum line length is 120 characters.

## Exceptions

On a few rare occasions, a single word — like a really long URL in a metadata annotation — can't possibly be made any
shorter. Using an ignore directive isn't an option in that context, and ignoring the whole file is rarely what you'll
want. The `non-breakable-word-threshold` configuration option allows defining a threshold length for when a single word
should be considered so long that the line length rule should ignore the line entirely.

## Configuration Options

This linter rule provides the following configuration options:
Expand All @@ -27,6 +34,8 @@ rules:
level: error
# maximum line length
max-line-length: 120
# if any single word on a line exceeds this length, ignore it
non-breakable-word-threshold: 100
```

## Community
Expand Down

0 comments on commit 41e127d

Please sign in to comment.