Class: Prism::NodeFind::SourceRangeFind

Inherits:
Find
  • Object
show all
Defined in:
lib/prism/node_find.rb,
sig/generated/prism/node_find.rbs

Overview

Finds the AST node for a Method, UnboundMethod, Proc or Thread::Backtrace::Location using the #source_range

Instance Method Summary collapse

Instance Method Details

#find(callable) ⇒ Node?

Find the node for the given callable using the #source_range.

: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable) -> Node?

Parameters:

  • callable (Method, UnboundMethod, Proc, Thread::Backtrace::Location)

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prism/node_find.rb', line 69

def find(callable)
  begin
    range = callable.source_range
  rescue ArgumentError # eval
    return
  end
  return unless range
  return unless (result = parse_file(range.absolute_path))

  start_offset = result.source.byte_offset(range.start_line, range.start_column)
  end_offset = result.source.byte_offset(range.end_line, range.end_column)
  result.value.tunnel(range.start_line, range.start_column).reverse_each do |node|
    if node.start_offset == start_offset && node.end_offset == end_offset
      return node
    end
  end
  nil
end