Skip to content

Commit

Permalink
Make prefer-snake-case check package name (#1206)
Browse files Browse the repository at this point in the history
Previously, only rules and vars were checked.

Fixes #1205

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Oct 16, 2024
1 parent 8504347 commit d04be3d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 10 additions & 0 deletions bundle/regal/rules/style/prefer-snake-case/prefer_snake_case.rego
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ import data.regal.ast
import data.regal.result
import data.regal.util

report contains violation if {
some part in input["package"].path

not util.is_snake_case(part.value)

violation := result.fail(rego.metadata.chain(), result.location(part))
}

report contains violation if {
some rule in input.rules
some part in ast.named_refs(rule.head.ref)

not util.is_snake_case(part.value)

violation := result.fail(rego.metadata.chain(), result.location(part))
}

report contains violation if {
var := ast.found.vars[_][_][_]

not util.is_snake_case(var.value)

violation := result.fail(rego.metadata.chain(), result.location(var))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ test_success_snake_cased_rule_name if {
r == set()
}

test_fail_camel_cased_package_name if {
r := rule.report with input as regal.parse_module("p.rego", `package camelCase`)
r == expected_with_locations([{
"col": 9,
"end": {
"col": 18,
"row": 1,
},
"file": "p.rego",
"row": 1,
"text": "package camelCase",
}])
}

test_success_snake_cased_package_name if {
r := rule.report with input as regal.parse_module("p.rego", `package snake_case`)
r == set()
}

test_fail_camel_cased_some_declaration if {
r := rule.report with input as ast.policy(`p {some fooBar; input[_]}`)
r == expected_with_locations([{
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/style/prefer-snake-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ user_is_admin if "admin" in input.user.roles

## Rationale

The built-in functions use `snake_case` for naming — follow that convention for your own rules, functions, and
variables, unless you have a really good reason not to.
The built-in functions use `snake_case` for naming — follow that convention for your own packages, rules, functions,
and variables, unless you have a really good reason not to.

## Exceptions

Expand Down

0 comments on commit d04be3d

Please sign in to comment.