Class: Deprewriter::CallSiteFinder

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/deprewriter/call_site_finder.rb

Overview

Finds method call sites in source code

Instance Method Summary collapse

Constructor Details

#initialize(method_name, line, from: nil) ⇒ Prism::CallNode?

Returns The found method call node.

Parameters:

  • method_name (Symbol)

    The name of the method to find

  • line (Integer)

    Line number where the method is called

  • from (String, nil) (defaults to: nil)

    Pattern to match for transformation



12
13
14
15
16
17
# File 'lib/deprewriter/call_site_finder.rb', line 12

def initialize(method_name, line, from: nil)
  @method_name = method_name
  @line = line
  @node_query = from ? NodeQuery.new(from, adapter: :prism) : nil
  super()
end

Instance Method Details

#find(source) ⇒ Prism::CallNode?

Returns The found method call node.

Parameters:

  • source (String)

    The source code to search in

Returns:

  • (Prism::CallNode, nil)

    The found method call node



21
22
23
24
25
# File 'lib/deprewriter/call_site_finder.rb', line 21

def find(source)
  parsed_result = Prism.parse(source)
  parsed_result.value.statements.accept(self)
  @node
end

#visit_call_node(node) ⇒ void

This method returns an undefined value.

Visits a call node in the AST

Parameters:

  • node (Prism::CallNode)

    The call node to visit



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/deprewriter/call_site_finder.rb', line 30

def visit_call_node(node)
  if node.name == @method_name && node.start_line == @line
    if @node_query
      matched = @node_query.query_nodes(node, {including_self: true, stop_at_first_match: true, recursive: true})
      @node = matched.first if matched.any?
    else
      @node = node
    end
  end

  super
end