Class: Prism::NodeFind::LineMethodFind

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 or UnboundMethod using best-effort line matching. Used on non-CRuby implementations.

Instance Method Summary collapse

Instance Method Details

#find(callable) ⇒ Node?

Find the node for the given method by matching on name and line.

: (Method | UnboundMethod callable) -> Node?

Parameters:

  • callable (Method, UnboundMethod)

Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/prism/node_find.rb', line 130

def find(callable)
  return unless (source_location = callable.source_location)
  return unless (result = parse_file(source_location[0]))

  name = callable.name
  start_line = source_location[1]

  result.value.find do |node|
    case node
    when DefNode
      node.name == name && node.location.start_line == start_line
    when CallNode
      node.block.is_a?(BlockNode) && node.location.start_line == start_line
    else
      false
    end
  end
end