module RuboCop::Cop::VariableForce::Locatable

This module provides a way to locate the conditional branch the node is in. This is intended to be used as mix-in.

Constants

BRANCH_TYPES
CONDITION_INDEX_OF_BRANCH_NODE
ENSURE_INDEX_OF_ENSURE_NODE
ENSURE_TYPE
FOR_LOOP_CHILD_INDEX
FOR_LOOP_TYPE
LEFT_SIDE_INDEX_OF_LOGICAL_OPERATOR_NODE
LOGICAL_OPERATOR_TYPES
NON_FOR_LOOP_TYPES
NON_FOR_LOOP_TYPES_CHILD_INDEX

Public Instance Methods

branch_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 85
def branch_body_name
  case branch_point_node.type
  when :if                     then if_body_name
  when :case                   then case_body_name
  when RESCUE_TYPE             then rescue_body_name
  when ENSURE_TYPE             then ensure_body_name
  when *LOGICAL_OPERATOR_TYPES then logical_operator_body_name
  when *LOOP_TYPES             then loop_body_name
  else raise InvalidBranchBodyError
  end
rescue InvalidBranchBodyError
  raise InvalidBranchBodyError,
        "Invalid body index #{body_index} of #{branch_point_node.type}"
end
branch_body_node() click to toggle source

A child node of branch_point_node this assignment belongs.

# File lib/rubocop/cop/variable_force/locatable.rb, line 76
def branch_body_node
  if instance_variable_defined?(:@branch_body_node)
    return @branch_body_node
  end

  set_branch_point_and_body_nodes!
  @branch_body_node
end
branch_id() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 55
def branch_id
  return nil unless inside_of_branch?
  @branch_id ||= [branch_point_node.object_id, branch_type].join('_')
end
branch_point_node() click to toggle source

Inner if, case, rescue, or ensure node.

# File lib/rubocop/cop/variable_force/locatable.rb, line 66
def branch_point_node
  if instance_variable_defined?(:@branch_point_node)
    return @branch_point_node
  end

  set_branch_point_and_body_nodes!
  @branch_point_node
end
branch_type() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 60
def branch_type
  return nil unless inside_of_branch?
  @branch_type ||= [branch_point_node.type, branch_body_name].join('_')
end
inside_of_branch?() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 32
def inside_of_branch?
  branch_point_node
end
node() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 24
def node
  raise '#node must be declared!'
end
run_exclusively_with?(other) click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 36
def run_exclusively_with?(other)
  return false unless branch_point_node.equal?(other.branch_point_node)
  return false if branch_body_node.equal?(other.branch_body_node)

  # Main body of rescue is always run:
  #
  #   begin
  #     # main
  #   rescue
  #     # resbody
  #   end
  if branch_point_node.type == :rescue &&
     (branch_body_name == 'main' || other.branch_body_name == 'main')
    return false
  end

  true
end
scope() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 28
def scope
  raise '#scope must be declared!'
end

Private Instance Methods

ancestor_nodes_in_scope() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 165
def ancestor_nodes_in_scope
  node.each_ancestor.take_while do |ancestor_node|
    !ancestor_node.equal?(scope.node)
  end
end
body_index() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 150
def body_index
  branch_point_node.children.index { |n| n.equal?(branch_body_node) }
end
branch?(parent_node, child_node) click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 171
def branch?(parent_node, child_node)
  child_index = parent_node.children.index(child_node)

  case parent_node.type
  when RESCUE_TYPE
    true
  when ENSURE_TYPE
    child_index != ENSURE_INDEX_OF_ENSURE_NODE
  when FOR_LOOP_TYPE
    child_index == FOR_LOOP_CHILD_INDEX
  when *BRANCH_TYPES
    child_index != CONDITION_INDEX_OF_BRANCH_NODE
  when *LOGICAL_OPERATOR_TYPES
    child_index != LEFT_SIDE_INDEX_OF_LOGICAL_OPERATOR_NODE
  when *NON_FOR_LOOP_TYPES
    child_index == NON_FOR_LOOP_TYPES_CHILD_INDEX
  else
    false
  end
end
case_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 110
def case_body_name
  if branch_body_node.type == :when
    "when#{body_index - 1}"
  else
    'else'
  end
end
ensure_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 135
def ensure_body_name
  case body_index
  when 0 then 'main'
  else raise InvalidBranchBodyError
  end
end
if_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 102
def if_body_name
  case body_index
  when 1 then 'true'
  when 2 then 'false'
  else raise InvalidBranchBodyError
  end
end
logical_operator_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 118
def logical_operator_body_name
  case body_index
  when 1 then 'right'
  else raise InvalidBranchBodyError
  end
end
loop_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 142
def loop_body_name
  loop_indices = [FOR_LOOP_CHILD_INDEX, NON_FOR_LOOP_TYPES_CHILD_INDEX]

  raise InvalidBranchBodyError unless loop_indices.include?(body_index)

  'main'
end
rescue_body_name() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 125
def rescue_body_name
  if body_index == 0
    'main'
  elsif branch_body_node.type == :resbody
    "rescue#{body_index - 1}"
  else
    'else'
  end
end
set_branch_point_and_body_nodes!() click to toggle source
# File lib/rubocop/cop/variable_force/locatable.rb, line 154
def set_branch_point_and_body_nodes!
  self_and_ancestor_nodes = [node] + ancestor_nodes_in_scope

  self_and_ancestor_nodes.each_cons(2) do |child, parent|
    next unless branch?(parent, child)
    @branch_point_node = parent
    @branch_body_node = child
    break
  end
end