Class: RuboCop::Cop::RSpecParity::PrivateMethodCallGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rspec_parity/private_method_call_graph.rb

Overview

Builds a static call graph for the methods defined directly inside a class/module node. Used by SufficientContexts to inline branches from private/protected helpers that are called from exactly one place in the same container.

Defined Under Namespace

Classes: Result

Constant Summary collapse

DYNAMIC_DISPATCH_SENDS =
%i[send public_send __send__].freeze
EVAL_METHODS =
%i[class_eval instance_eval module_eval].freeze

Instance Method Summary collapse

Constructor Details

#initialize(container_node) ⇒ PrivateMethodCallGraph

Returns a new instance of PrivateMethodCallGraph.



16
17
18
19
20
21
# File 'lib/rubocop/cop/rspec_parity/private_method_call_graph.rb', line 16

def initialize(container_node)
  @container = container_node
  @methods = {}
  @order = []
  @built = false
end

Instance Method Details

#dynamic_dispatch?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/rubocop/cop/rspec_parity/private_method_call_graph.rb', line 23

def dynamic_dispatch?
  return false unless @container

  @dynamic_dispatch ||= detect_dynamic_dispatch
  @dynamic_dispatch == :yes
end

#inlinable_from(method_node, branch_counter) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/rspec_parity/private_method_call_graph.rb', line 30

def inlinable_from(method_node, branch_counter)
  return Result.new(0, []) unless @container
  return Result.new(0, []) if dynamic_dispatch?

  build! unless @built

  key = key_for(method_node)
  return Result.new(0, []) unless @methods.key?(key)

  traverse(key, branch_counter)
end