-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #940 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
7bc7868
commit 8a8b8ad
Showing
8 changed files
with
139 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# METADATA | ||
# description: Use `strings.count` where possible | ||
package regal.rules.idiomatic["use-strings-count"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.capabilities | ||
import data.regal.result | ||
|
||
# METADATA | ||
# description: Missing capability for built-in function `strings.count` | ||
# custom: | ||
# severity: warning | ||
notices contains result.notice(rego.metadata.chain()) if not capabilities.has_object_keys | ||
|
||
# METADATA | ||
# description: flag calls to `count` where the first argument is a call to `indexof_n` | ||
report contains violation if { | ||
some rule in input.rules | ||
|
||
ref := ast.refs[_][_] | ||
|
||
ref[0].value[0].type == "var" | ||
ref[0].value[0].value == "count" | ||
|
||
ref[1].type == "call" | ||
ref[1].value[0].value[0].type == "var" | ||
ref[1].value[0].value[0].value == "indexof_n" | ||
|
||
loc1 := result.location(ref[0]) | ||
loc2 := result.ranged_location_from_text(ref[1]) | ||
|
||
violation := result.fail(rego.metadata.chain(), object.union(loc1, {"location": {"end": loc2.location.end}})) | ||
} |
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,42 @@ | ||
package regal.rules.idiomatic["use-strings-count_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.idiomatic["use-strings-count"] as rule | ||
|
||
test_fail_can_use_strings_count if { | ||
module := ast.with_rego_v1(`x := count(indexof_n("foo", "o"))`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Use `strings.count` where possible", | ||
"level": "error", | ||
"location": { | ||
"col": 6, | ||
"file": "policy.rego", | ||
"row": 5, | ||
"text": `x := count(indexof_n("foo", "o"))`, | ||
"end": {"col": 34, "row": 5}, | ||
}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/use-strings-count", "idiomatic"), | ||
}], | ||
"title": "use-strings-count", | ||
}} | ||
} | ||
|
||
test_has_notice_if_unmet_capability if { | ||
r := rule.notices with config.capabilities as {} | ||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Missing capability for built-in function `strings.count`", | ||
"level": "notice", | ||
"severity": "warning", | ||
"title": "use-strings-count", | ||
}} | ||
} |
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,50 @@ | ||
# use-strings-count | ||
|
||
**Summary**: Use `strings.count` where possible | ||
|
||
**Category**: Idiomatic | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
num_as := count(indexof_n("foobarbaz", "a")) | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
num_as := strings.count("foobarbaz", "a") | ||
``` | ||
|
||
## Rationale | ||
|
||
The `strings.count` function added in [OPA v0.67.0](https://github.com/open-policy-agent/opa/releases/tag/v0.67.0) | ||
is both more readable and efficient compared to using `count(indexof_n(...))` and should therefore be preferred. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
idiomatic: | ||
use-strings-count: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## Related Resources | ||
- OPA Docs: [strings.count](https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-strings-stringscount) | ||
## 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
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