class Ivar::TargetedPrismAnalysis

and precisely locates instance variable references within each method definition
Unlike PrismAnalysis, this targets only the class’s own methods (not inherited)
Analyzes a class to find instance variable references in specific instance methods

def analyze_methods

def analyze_methods
  # Group methods by file to avoid parsing the same file multiple times
  methods_by_file = @method_locations.group_by { |_, location| location[:path] }
  methods_by_file.each do |file_path, methods_in_file|
    code = File.read(file_path)
    result = Prism.parse(code)
    methods_in_file.each do |method_name, location|
      visitor = MethodTargetedInstanceVariableReferenceVisitor.new(
        file_path,
        method_name,
        location[:line]
      )
      result.value.accept(visitor)
      @references.concat(visitor.references)
    end
  end
end

def collect_method_locations

def collect_method_locations
  # Get all instance methods defined directly on this class (not inherited)
  instance_methods = @klass.instance_methods(false) | @klass.private_instance_methods(false)
  instance_methods.each do |method_name|
    # Try to get the method from the stash first, then fall back to the current method
    method_obj = Ivar.get_stashed_method(@klass, method_name) || @klass.instance_method(method_name)
    next unless method_obj.source_location
    file_path, line_number = method_obj.source_location
    @method_locations[method_name] = {path: file_path, line: line_number}
  end
end

def initialize(klass)

def initialize(klass)
  @klass = klass
  @references = []
  @method_locations = {}
  collect_method_locations
  analyze_methods
  @ivars = unique_ivar_names
end

def ivar_references

Each hash includes var name, path, line number, and column number
Returns a list of hashes each representing a code reference to an ivar
def ivar_references
  @references
end

def unique_ivar_names

def unique_ivar_names
  @references.map { |ref| ref[:name] }.uniq.sort
end