-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1044 Signed-off-by: Anders Eknert <anders@styra.com>
- Loading branch information
1 parent
a4e3592
commit 90be3f7
Showing
6 changed files
with
182 additions
and
0 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
70 changes: 70 additions & 0 deletions
70
bundle/regal/rules/idiomatic/use-object-keys/use_object_keys.rego
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,70 @@ | ||
# METADATA | ||
# description: Prefer to use `object.keys` | ||
package regal.rules.idiomatic["use-object-keys"] | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
# {k | some k, _ in object} | ||
report contains violation if { | ||
comprehension := ast.found.comprehensions[_][_] | ||
|
||
comprehension.type == "setcomprehension" | ||
comprehension.value.term.type == "var" | ||
count(comprehension.value.body) == 1 | ||
|
||
symbol := comprehension.value.body[0].terms.symbols[0] | ||
|
||
# some k, _ in object | ||
symbol.type == "call" | ||
symbol.value[0].type == "ref" | ||
symbol.value[0].value[0].value == "internal" | ||
symbol.value[0].value[1].value == "member_3" | ||
|
||
# same 'k' var value as in the head | ||
symbol.value[1].type == "var" | ||
symbol.value[1].value == comprehension.value.term.value | ||
|
||
# we don't care what symbol.value[2] is, as it's not assigned in the head | ||
# but symbol.value[3] should be a ref without vars | ||
|
||
symbol.value[3].type == "ref" | ||
|
||
ast.static_ref(symbol.value[3].value) | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(comprehension)) | ||
} | ||
|
||
# {k | input.object[k]} | ||
# {k | some k; input.object[k]} | ||
report contains violation if { | ||
comprehension := ast.found.comprehensions[_][_] | ||
|
||
comprehension.type == "setcomprehension" | ||
comprehension.value.term.type == "var" | ||
|
||
ref := _ref(comprehension.value.body) | ||
|
||
vars := [part | | ||
some part in array.slice(ref, 1, 128) | ||
part.type == "var" | ||
] | ||
|
||
count(vars) == 1 | ||
vars[0].value == comprehension.value.term.value | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(comprehension)) | ||
} | ||
|
||
# {k | input.object[k]} | ||
_ref(exprs) := exprs[0].terms.value if { | ||
count(exprs) == 1 | ||
exprs[0].terms.type == "ref" | ||
} | ||
|
||
# {k | some k; input.object[k]} | ||
_ref(exprs) := exprs[1].terms.value if { | ||
count(exprs) == 2 | ||
exprs[0].terms.symbols[0].type == "var" | ||
exprs[1].terms.type == "ref" | ||
} |
56 changes: 56 additions & 0 deletions
56
bundle/regal/rules/idiomatic/use-object-keys/use_object_keys_test.rego
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,56 @@ | ||
package regal.rules.idiomatic["use-object-keys_test"] | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.idiomatic["use-object-keys"] as rule | ||
|
||
test_fail_use_object_keys_not_some_in_comprehension if { | ||
r := rule.report with input as ast.policy(`comp := {k | some k, _ in input.object}`) | ||
|
||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Prefer to use `object.keys`", | ||
"level": "error", | ||
"location": { | ||
"col": 9, | ||
"end": { | ||
"col": 40, | ||
"row": 3, | ||
}, | ||
"file": "policy.rego", | ||
"row": 3, | ||
"text": "comp := {k | some k, _ in input.object}", | ||
}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/use-object-keys", "idiomatic"), | ||
}], | ||
"title": "use-object-keys", | ||
}} | ||
} | ||
|
||
test_fail_use_object_keys_not_some_comprehension if { | ||
r := rule.report with input as ast.policy(`comp := {k | some k; input.object[k]}`) | ||
|
||
r == {{ | ||
"category": "idiomatic", | ||
"description": "Prefer to use `object.keys`", | ||
"level": "error", | ||
"location": { | ||
"col": 9, | ||
"end": { | ||
"col": 38, | ||
"row": 3, | ||
}, | ||
"file": "policy.rego", | ||
"row": 3, | ||
"text": "comp := {k | some k; input.object[k]}", | ||
}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/use-object-keys", "idiomatic"), | ||
}], | ||
"title": "use-object-keys", | ||
}} | ||
} |
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,51 @@ | ||
# use-object-keys | ||
|
||
**Summary**: Prefer to use `object.keys` | ||
|
||
**Category**: Idiomatic | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
keys := {k | some k, _ in input.object} | ||
# or | ||
keys := {k | some k; input.object[k]} | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
keys := object.keys(input.object) | ||
``` | ||
|
||
## Rationale | ||
|
||
Instead of using a set comprehension to collect keys from an object, prefer to use the built-in function | ||
[object.keys](https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-object-objectkeys). | ||
This option is both more declarative and better conveys the intent of the code. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
idiomatic: | ||
use-object-keys: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## Related Resources | ||
- OPA Docs: [object.keys](https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-object-objectkeys) | ||
## 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