Class: CallMap::CallExtractor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/call_map/call_extractor.rb

Overview

Extracts method calls from a method body's AST.

Like DefinitionCollector, this is a Prism boundary class — Prism node types are referenced only here.

Constant Summary collapse

DYNAMIC_METHODS =
%w[send public_send __send__].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallExtractor

Returns a new instance of CallExtractor.



24
25
26
27
# File 'lib/call_map/call_extractor.rb', line 24

def initialize
  super
  @calls = []
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



29
30
31
# File 'lib/call_map/call_extractor.rb', line 29

def calls
  @calls
end

Class Method Details

.extract(def_node) ⇒ Array<MethodCall>

Extract calls from a DefNode's body.

Parameters:

  • def_node (Prism::DefNode)

    the method definition node

Returns:



18
19
20
21
22
# File 'lib/call_map/call_extractor.rb', line 18

def self.extract(def_node)
  extractor = new
  def_node.body&.accept(extractor)
  extractor.calls
end

Instance Method Details

#visit_call_node(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/call_map/call_extractor.rb', line 31

def visit_call_node(node)
  @calls << build_call(node)
  # Walk arguments and block, but not the receiver itself (it is already
  # captured in the receiver label). However, walk the receiver chain's
  # arguments/blocks so that calls like `SomeClass.new(build_order).execute`
  # still extract `build_order`.
  node.arguments&.accept(self)
  node.block&.accept(self)
  walk_receiver_args(node.receiver)
end

#visit_def_node(_node) ⇒ Object

Do not recurse into nested def bodies — they are not executed as part of the enclosing method's call path.



44
# File 'lib/call_map/call_extractor.rb', line 44

def visit_def_node(_node); end