Module: RuboCop::Cop::VariableForce::Locatable

Included in:
Assignment, Reference
Defined in:
lib/rubocop/cop/variable_force/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
FOR_LOOP_TYPE =
:for
FOR_LOOP_CHILD_INDEX =
2
NON_FOR_LOOP_TYPES =
LOOP_TYPES - [FOR_LOOP_TYPE]
NON_FOR_LOOP_TYPES_CHILD_INDEX =
1

Instance Method Summary collapse

Instance Method Details

#branch_body_nameObject

TODO: Replace case statement with function mapping



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 88

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, "Invalid body index #{body_index} " \
                                  "of #{branch_point_node.type}"
  end
end

#branch_body_nodeObject

A child node of #branch_point_node this assignment belongs.



80
81
82
83
84
85
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 80

def branch_body_node
  return @branch_body_node if @branch_body_node

  set_branch_point_and_body_nodes!
  @branch_body_node
end

#branch_idObject



61
62
63
64
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 61

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.



72
73
74
75
76
77
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 72

def branch_point_node
  return @branch_point_node if @branch_point_node

  set_branch_point_and_body_nodes!
  @branch_point_node
end

#branch_typeObject



66
67
68
69
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 66

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

#initializeObject



24
25
26
27
28
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 24

def initialize(*)
  @branch_point_node = @branch_body_node = nil

  super()
end

#inside_of_branch?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 38

def inside_of_branch?
  branch_point_node
end

#nodeObject



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

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

#run_exclusively_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 42

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.rescue_type? &&
     (branch_body_name == 'main' || other.branch_body_name == 'main')
    return false
  end

  true
end

#scopeObject



34
35
36
# File 'lib/rubocop/cop/variable_force/locatable.rb', line 34

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