Skip to content

Commit

Permalink
Remove some code made redundant by roast (StyraInc#1013)
Browse files Browse the repository at this point in the history
Some special case we previously needed to handle no longer apply
since we moved to the roast format. Also fixed a TODO asking to
move over to `strings.count` when released, and that is now :)

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored and srenatus committed Oct 1, 2024
1 parent 5e2aa70 commit 8e76483
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
6 changes: 1 addition & 5 deletions bundle/regal/lsp/completion/providers/snippet/snippet.rego
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ items contains item if {
some label, snippet in _snippets

strings.any_prefix_match(snippet.prefix, word.text)

# regal ignore:todo-comment
# TODO: use https://github.com/open-policy-agent/opa/pull/6841 when
# released.
count(regex.find_n(snippet.prefix[0], line, 2)) < 2
strings.count(line, snippet.prefix[0]) < 2

not endswith(trim_space(line), "=")

Expand Down
26 changes: 8 additions & 18 deletions bundle/regal/result.rego
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ _fail_annotated(metadata, details) := violation if {
})

without_custom_and_scope := object.remove(with_category, ["custom", "scope", "schemas"])
related_resources := resource_urls(without_custom_and_scope.related_resources, category)
related_resources := _resource_urls(without_custom_and_scope.related_resources, category)

violation := json.patch(
without_custom_and_scope,
Expand All @@ -171,7 +171,7 @@ _fail_annotated_custom(metadata, details) := violation if {
violation := object.remove(with_category, ["custom", "scope", "schemas"])
}

resource_urls(related_resources, category) := [r |
_resource_urls(related_resources, category) := [r |
some item in related_resources
r := object.union(object.remove(item, ["ref"]), {"ref": config.docs.resolve_url(item.ref, category)})
]
Expand All @@ -185,26 +185,16 @@ _with_text(loc_obj) := loc if {
loc_obj.row
} else := {"location": loc_obj}

# METADATA
# description: |
# returns a "normalized" location object from the location value found in the AST.
# new code should most often use one of the ranged_ location functions instea, as
# that will also include an `"end"` location attribute
# scope: document
location(x) := _with_text(util.to_location_object(x.location))

location(x) := _with_text(util.to_location_object(x[0].location)) if is_array(x)

# Special case for rule refs, where location is currently only assigned to the value
# In this case, we'll just assume that the column is 1, as that's the most likely place
# See: https://github.com/open-policy-agent/opa/issues/5790
location(x) := _with_text(loc_obj) if {
not x.location
count(x.ref) == 1
x.ref[0].type == "var"
loc_obj := object.union(util.to_location_object(x.value.location), {"col": 1})
}

location(x) := {} if {
not x.location
not x.Location
count(x.ref) != 1
}

# METADATA
# description: |
# similar to `location` but includes an `end` attribute where `end.row` is the same
Expand Down

0 comments on commit 8e76483

Please sign in to comment.