@@ -272,14 +272,27 @@ found.expressions[rule_index] contains value if {
272272 value.terms
273273}
274274
275+ # METADATA
276+ # description: |
277+ # answers whether a variable of the given name (value) is declared in the
278+ # local scope of the provided rule at the provided location
279+ is_in_local_scope (rule, location, value) if {
280+ some var
281+ found.vars[_rule_index (rule)][_][var].value == value
282+ var.location != location
283+
284+ _before_location (rule.head, var, util.to_location_object (location))
285+ }
286+
275287# METADATA
276288# description: |
277289# finds all vars declared in `rule` *before* the `location` provided
278290# note: this isn't 100% accurate, as it doesn't take into account `=`
279291# assignments / unification, but it's likely good enough since other rules
280292# recommend against those
281293find_vars_in_local_scope (rule, location) := [var |
282- var := found.vars[_rule_index (rule)][_][_]
294+ some var
295+ found.vars[_rule_index (rule)][_][var]
283296
284297 not startswith (var.value, " $" )
285298 _before_location (rule.head, var, util.to_location_object (location))
@@ -309,32 +322,25 @@ _before_location(_, var, loc) if {
309322
310323# METADATA
311324# description: find *only* names in the local scope, and not e.g. rule names
312- find_names_in_local_scope (rule, location) := names if {
313- fn_arg_names := {arg.value |
314- some arg in rule.head.args
315- arg.type == " var"
316- }
317- var_names := {var.value | some var in find_vars_in_local_scope (rule, util.to_location_object (location))}
318-
319- names := fn_arg_names | var_names
325+ find_names_in_local_scope (rule, location) := {var.value |
326+ some var in find_vars_in_local_scope (rule, util.to_location_object (location))
320327}
321328
322329# METADATA
323330# description: |
324331# similar to `find_vars_in_local_scope`, but returns all variable names in scope
325332# of the given location *and* the rule names present in the scope (i.e. module)
326- find_names_in_scope (rule, location) := names if {
327- locals := find_names_in_local_scope (rule, util.to_location_object (location))
328-
329- # parens below added by opa-fmt :)
330- names := (rule_names | imported_identifiers) | locals
331- }
333+ find_names_in_scope (rule, location) := (rule_names | imported_identifiers) | find_names_in_local_scope (
334+ rule,
335+ util.to_location_object (location),
336+ )
332337
333338# METADATA
334339# description: |
335340# find all variables declared via `some` declarations (and *not* `some .. in`)
336341# in the scope of the given location
337342find_some_decl_names_in_scope (rule, location) := {some_var.value |
343+ loc := util.to_location_object (location)
338344 some some_var in found.vars[_rule_index (rule)].some
339- _before_location (rule.head, some_var, util. to_location_object (location) )
345+ _before_location (rule.head, some_var, loc )
340346}
0 commit comments