Class: CallMap::MethodCall

Inherits:
Object
  • Object
show all
Defined in:
lib/call_map/method_call.rb

Overview

A single method call extracted from a method body.

This is a plain value object and must NOT depend on the parser (Prism). Building a MethodCall from an AST is the job of CallExtractor.

Constant Summary collapse

KNOWN_FRAMEWORK_METHODS =

Common Rails methods that appear as bare calls inside controllers and models. A bare call that stays unresolved and matches this list is displayed as a framework leaf.

%w[
  redirect_to redirect_back render render_to_string head respond_to respond_with
  params request response session cookies flash logger helpers url_for
  current_user authenticate_user! sign_in sign_out authorize policy_scope
  raise puts pp
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver:, method_name:, line:, dynamic: false, absolute: false, callback: nil) ⇒ MethodCall

Returns a new instance of MethodCall.

Parameters:

  • receiver (String, nil)

    receiver expression ("OrderDeleteService", "self", nil for bare calls)

  • method_name (String)

    name of the called method

  • line (Integer)

    line number of the call site

  • dynamic (Boolean) (defaults to: false)

    true for send/public_send style calls

  • absolute (Boolean) (defaults to: false)

    true for ::Foo style absolute constant paths

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

    callback type (e.g. "before_action") if this call originates from a DSL callback



25
26
27
28
29
30
31
32
# File 'lib/call_map/method_call.rb', line 25

def initialize(receiver:, method_name:, line:, dynamic: false, absolute: false, callback: nil)
  @receiver = receiver
  @method_name = method_name
  @line = line
  @dynamic = dynamic
  @absolute = absolute
  @callback = callback
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



34
35
36
# File 'lib/call_map/method_call.rb', line 34

def callback
  @callback
end

#lineObject (readonly)

Returns the value of attribute line.



34
35
36
# File 'lib/call_map/method_call.rb', line 34

def line
  @line
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



34
35
36
# File 'lib/call_map/method_call.rb', line 34

def method_name
  @method_name
end

#receiverObject (readonly)

Returns the value of attribute receiver.



34
35
36
# File 'lib/call_map/method_call.rb', line 34

def receiver
  @receiver
end

Instance Method Details

#absolute?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/call_map/method_call.rb', line 40

def absolute?
  @absolute
end

#bare?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/call_map/method_call.rb', line 48

def bare?
  receiver.nil?
end

#callback?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/call_map/method_call.rb', line 44

def callback?
  !@callback.nil?
end

#dynamic?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/call_map/method_call.rb', line 36

def dynamic?
  @dynamic
end

#framework_leaf?Boolean

Whether this call, IF it stays unresolved, should be shown as a framework leaf. An explicit receiver that did not resolve points outside the indexed app code; a bare call (including a callback filter like Devise's before_action :authenticate_user!) is framework-ish only when it matches the known Rails method list — an unlisted bare call may just be an analysis miss, so it gets no suffix instead.

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/call_map/method_call.rb', line 58

def framework_leaf?
  return false if dynamic?
  return KNOWN_FRAMEWORK_METHODS.include?(method_name) if bare?

  true
end

#labelObject

Human-readable label for tree output.



66
67
68
69
70
71
72
# File 'lib/call_map/method_call.rb', line 66

def label
  base = receiver ? "#{receiver}.#{method_name}" : method_name
  return "#{callback} #{base}" if callback?
  return "#{base} [dynamic]" if dynamic?

  base
end