Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate correct column of = in use-assignment-operator #655

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions bundle/regal/rules/style/use_assignment_operator.rego
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ report contains violation if {
not ast.implicit_boolean_assignment(rule)
not ast.is_chained_rule_body(rule, input.regal.file.lines)

violation := result.fail(rego.metadata.chain(), result.location(rule))
loc := result.location(rule)

violation := result.fail(rego.metadata.chain(), object.union(loc, {"location": {"col": eq_col(loc)}}))
}

report contains violation if {
Expand All @@ -28,7 +30,9 @@ report contains violation if {
rule.head.value
not rule.head.assign

violation := result.fail(rego.metadata.chain(), result.location(rule.head.ref[0]))
loc := result.location(result.location(rule.head.ref[0]))

violation := result.fail(rego.metadata.chain(), object.union(loc, {"location": {"col": eq_col(loc)}}))
}

report contains violation if {
Expand All @@ -51,5 +55,14 @@ report contains violation if {
text := base64.decode(value["else"].head.location.text)
regex.match(`^else\s*=`, text)

violation := result.fail(rego.metadata.chain(), result.location(value["else"].head))
loc := result.location(value["else"].head)

violation := result.fail(rego.metadata.chain(), object.union(loc, {"location": {"col": eq_col(loc)}}))
}

default eq_col(_) := 1

eq_col(loc) := pos if {
pos := indexof(loc.location.text, "=")
pos != -1
}
18 changes: 9 additions & 9 deletions bundle/regal/rules/style/use_assignment_operator_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_fail_unification_in_regular_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "foo = false"},
"location": {"col": 4, "file": "policy.rego", "row": 3, "text": "foo = false"},
"level": "error",
}}
}
Expand All @@ -31,7 +31,7 @@ test_fail_not_implicit_boolean_assignment_with_body if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "allow = true { true }"},
"location": {"col": 6, "file": "policy.rego", "row": 3, "text": "allow = true { true }"},
"level": "error",
}}
}
Expand All @@ -46,7 +46,7 @@ test_fail_not_implicit_boolean_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "foo = true"},
"location": {"col": 4, "file": "policy.rego", "row": 3, "text": "foo = true"},
"level": "error",
}}
}
Expand All @@ -66,7 +66,7 @@ test_fail_unification_in_default_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "default x = false"},
"location": {"col": 10, "file": "policy.rego", "row": 3, "text": "default x = false"},
"level": "error",
}}
}
Expand All @@ -81,7 +81,7 @@ test_fail_unification_in_default_function_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": "default x(_) = false"},
"location": {"col": 13, "file": "policy.rego", "row": 3, "text": "default x(_) = false"},
"level": "error",
}}
}
Expand All @@ -106,7 +106,7 @@ test_fail_unification_in_object_rule_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": `x["a"] = 1`},
"location": {"col": 7, "file": "policy.rego", "row": 3, "text": `x["a"] = 1`},
"level": "error",
}}
}
Expand All @@ -126,7 +126,7 @@ test_fail_unification_in_function_assignment if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 1, "file": "policy.rego", "row": 3, "text": `foo(bar) = "baz"`},
"location": {"col": 9, "file": "policy.rego", "row": 3, "text": `foo(bar) = "baz"`},
"level": "error",
}}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ test_fail_unification_in_else if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 4, "file": "policy.rego", "row": 8, "text": "\t} else = true if {"},
"location": {"col": 8, "file": "policy.rego", "row": 8, "text": "\t} else = true if {"},
"level": "error",
},
{
Expand All @@ -185,7 +185,7 @@ test_fail_unification_in_else if {
"ref": config.docs.resolve_url("$baseUrl/$category/use-assignment-operator", "style"),
}],
"title": "use-assignment-operator",
"location": {"col": 4, "file": "policy.rego", "row": 10, "text": "\t} else = false"},
"location": {"col": 8, "file": "policy.rego", "row": 10, "text": "\t} else = false"},
"level": "error",
},
}
Expand Down
Loading