Module: Rubocop::Cop::VariableInspector::Locatable

Included in:
Assignment, Reference
Defined in:
lib/rubocop/cop/variable_inspector/locatable.rb

Overview

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

Defined Under Namespace

Classes: InvalidBranchBodyError

Constant Summary collapse

BRANCH_TYPES =
[:if, :case].freeze
CONDITION_INDEX_OF_BRANCH_NODE =
0
LOGICAL_OPERATOR_TYPES =
[:and, :or].freeze
LEFT_SIDE_INDEX_OF_LOGICAL_OPERATOR_NODE =
0
ENSURE_TYPE =
:ensure
ENSURE_INDEX_OF_ENSURE_NODE =
1

Instance Method Summary collapse

Instance Method Details

#ancestor_nodes_in_scopeObject



60
61
62
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 60

def ancestor_nodes_in_scope
  @ancestor_nodes_in_scope ||= scope.ancestors_of_node(@node)
end

#branch_body_nodeObject

A child node of #branch_point_node this assignment belongs.



51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 51

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_idObject



30
31
32
33
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 30

def branch_id
  return nil unless inside_of_branch?
  @branch_id ||= [branch_point_node.object_id, branch_type].join('_')
end

#branch_point_nodeObject

Inner if, case, rescue, or ensure node.



41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 41

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_typeObject



35
36
37
38
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 35

def branch_type
  return nil unless inside_of_branch?
  @branch_type ||= [branch_point_node.type, branch_body_name].join('_')
end

#inside_of_branch?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 26

def inside_of_branch?
  !branch_point_node.nil?
end

#nodeObject



18
19
20
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 18

def node
  fail '#node must be declared!'
end

#scopeObject



22
23
24
# File 'lib/rubocop/cop/variable_inspector/locatable.rb', line 22

def scope
  fail '#scope must be declared!'
end