-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces `if-empty-object`, and covers any object literal. Fixes #822 Signed-off-by: Anders Eknert <anders@styra.com>
- Loading branch information
1 parent
e3e12a9
commit 34fc452
Showing
7 changed files
with
127 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
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,24 @@ | ||
# METADATA | ||
# description: Object literal following `if` | ||
package regal.rules.bugs["if-object-literal"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.capabilities | ||
import data.regal.result | ||
|
||
# METADATA | ||
# description: Missing capability for keyword `if` | ||
# custom: | ||
# severity: warning | ||
notices contains result.notice(rego.metadata.chain()) if not capabilities.has_if | ||
|
||
report contains violation if { | ||
some rule in input.rules | ||
|
||
count(rule.body) == 1 | ||
|
||
rule.body[0].terms.type == "object" | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} |
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,46 @@ | ||
package regal.rules.bugs["if-object-literal_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.bugs["if-object-literal"] as rule | ||
|
||
test_fail_if_empty_object if { | ||
module := ast.with_rego_v1("rule if {}") | ||
r := rule.report with input as module | ||
r == {{ | ||
"category": "bugs", | ||
"description": "Object literal following `if`", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 5, "text": "rule if {}"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/if-object-literal", "bugs"), | ||
}], | ||
"title": "if-object-literal", | ||
}} | ||
} | ||
|
||
test_fail_non_empty_object if { | ||
module := ast.with_rego_v1(`rule if {"x": input.x}`) | ||
r := rule.report with input as module | ||
r == {{ | ||
"category": "bugs", | ||
"description": "Object literal following `if`", | ||
"level": "error", | ||
"location": {"col": 1, "file": "policy.rego", "row": 5, "text": `rule if {"x": input.x}`}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/if-object-literal", "bugs"), | ||
}], | ||
"title": "if-object-literal", | ||
}} | ||
} | ||
|
||
test_success_not_an_object if { | ||
module := ast.with_rego_v1(`rule if { true }`) | ||
r := rule.report with input as module | ||
r == set() | ||
} |
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,45 @@ | ||
# if-object-literal | ||
|
||
**Summary**: Object literal following `if` | ||
|
||
**Category**: Bugs | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
import rego.v1 | ||
# {} interpreted as object, not a rule body | ||
allow if {} | ||
allow if { | ||
# perhaps meant to be comparison? | ||
# but this too is an object | ||
input.x: 10 | ||
} | ||
``` | ||
|
||
## Rationale | ||
|
||
An object literal immediately following an `if` is almost certainly a mistake, and the intention was likely to express | ||
a rule body in its place. This isn't too common, but can happen when either an empty object `{}` is all that follows the | ||
`if`, or an expression in the "body" mistakenly is written as a `key: value` pair. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
bugs: | ||
if-object-literal: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
## 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)! |