Skip to content

Commit 54cdcb6

Browse files
authored
Add prefer-value-in-head option to count interpolated string as scalar (#1817)
I noticed in some rule I worked on that Regal didn't flag this, which was right given that interpolated strings aren't exactly scalar values. But I _do_ want to have Regal recommend moving them to the head assignment when possible, while keeping the `only-scalars` option set as `true`. This PR adds a new `include-interpolated` option to allow this precisely, and makes it enabled in our own settings. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
1 parent 28121f0 commit 54cdcb6

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

.regal/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rules:
2727
prefer-value-in-head:
2828
level: error
2929
only-scalars: true
30+
include-interpolated: true
3031
style:
3132
line-length:
3233
level: error

bundle/regal/rules/custom/prefer-value-in-head/prefer_value_in_head.rego

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ report contains violation if {
1717
terms[1].type == "var"
1818
terms[1].value == var
1919

20-
not _scalar_fail(terms[2].type, ast.scalar_types)
20+
not _scalar_fail(terms[2].type, _scalar_types)
2121
not _excepted_var_name(var)
2222

2323
violation := result.fail(rego.metadata.chain(), result.location(terms[2]))
@@ -36,3 +36,6 @@ _scalar_fail(term_type, scalar_types) if {
3636
}
3737

3838
_excepted_var_name(name) if name in config.rules.custom["prefer-value-in-head"]["except-var-names"]
39+
40+
_scalar_types contains type if some type in ast.scalar_types
41+
_scalar_types contains "templatestring" if config.rules.custom["prefer-value-in-head"]["include-interpolated"] == true

bundle/regal/rules/custom/prefer-value-in-head/prefer_value_in_head_test.rego

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,51 @@ test_fail_value_could_be_in_head_but_not_a_scalar if {
8080
r == set()
8181
}
8282

83+
test_fail_value_could_be_in_head_templatestring if {
84+
r := rule.report with input as ast.policy(`value := x if {
85+
input.x
86+
x := $"{input.y}"
87+
}`)
88+
89+
r == expected_with_location({
90+
"col": 8,
91+
"row": 5,
92+
"end": {
93+
"col": 20,
94+
"row": 5,
95+
},
96+
"text": "\t\tx := $\"{input.y}\"",
97+
})
98+
}
99+
100+
test_success_only_scalar_no_include_interpolated if {
101+
r := rule.report with input as ast.policy(`value := x if {
102+
input.x
103+
x := $"{input.y}"
104+
}`)
105+
with config.rules as {"custom": {"prefer-value-in-head": {"only-scalars": true}}}
106+
107+
r == set()
108+
}
109+
110+
test_fail_value_could_be_in_head_only_scalars_with_include_interpolated if {
111+
r := rule.report with input as ast.policy(`value := x if {
112+
input.x
113+
x := $"{input.y}"
114+
}`)
115+
with config.rules as {"custom": {"prefer-value-in-head": {"only-scalars": true, "include-interpolated": true}}}
116+
117+
r == expected_with_location({
118+
"col": 8,
119+
"row": 5,
120+
"end": {
121+
"col": 20,
122+
"row": 5,
123+
},
124+
"text": "\t\tx := $\"{input.y}\"",
125+
})
126+
}
127+
83128
test_fail_value_could_be_in_head_and_is_a_scalar if {
84129
module := ast.policy(`value := x if {
85130
input.x

docs/rules/custom/prefer-value-in-head.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Category**: Custom
66

77
**Avoid**
8+
89
```rego
910
package policy
1011
@@ -20,6 +21,7 @@ deny contains message if {
2021
```
2122

2223
**Prefer**
24+
2325
```rego
2426
package policy
2527
@@ -58,6 +60,9 @@ deny contains message if {
5860
}
5961
```
6062

63+
The `include-interpolated` configuration option may be used to count interpolated strings as a scalar (string) values,
64+
which will have Regal recommend moving them to the head even when `only-scalars` is set to `true`.
65+
6166
## Configuration Options
6267

6368
This linter rule provides the following configuration options:
@@ -74,10 +79,13 @@ rules:
7479
# whether to only suggest moving scalar values (strings, numbers, booleans, null)
7580
# to the head, and not expressions or functions
7681
only-scalars: false
82+
# when set to true, counts interpolated strings as a scalar value, and will suggest
83+
# moving them to the head even when `only-scalars` is true
84+
include-interpolated: false
7785
# variable names to exempt from the rule (by default, none)
7886
except-var-names:
79-
- report
80-
- violation
87+
- report
88+
- violation
8189
```
8290
8391
## Related Resources

0 commit comments

Comments
 (0)