Class: RuboCop::Cop::VariableForce::Branch::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/rubocop/cop/variable_force/branch.rb

Overview

Abstract base class for branch classes. A branch represents a conditional branch in a scope.

Examples:

def some_scope
  do_something     # no branch

  if foo
    do_something   # branch A
    do_something   # branch A
  else
    do_something   # branch B
    if bar
      do_something # branch C (whose parent is branch B)
    end
  end

  do_something     # no branch
end

Direct Known Subclasses

And, Case, Ensure, For, If, Or, Rescue, Until, UntilPost, While, WhilePost

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#child_nodeObject

Returns the value of attribute child_node

Returns:

  • (Object)

    the current value of child_node



44
45
46
# File 'lib/rubocop/cop/variable_force/branch.rb', line 44

def child_node
  @child_node
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



44
45
46
# File 'lib/rubocop/cop/variable_force/branch.rb', line 44

def scope
  @scope
end

Class Method Details

.classesObject

rubocop:enable Metrics/BlockLength



47
48
49
# File 'lib/rubocop/cop/variable_force/branch.rb', line 47

def self.classes
  @classes ||= []
end

.define_predicate(name, child_index: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/variable_force/branch.rb', line 59

def self.define_predicate(name, child_index: nil)
  define_method(name) do
    target_node = control_node.children[child_index]

    # We don't use Kernel#Array here
    # because it invokes Node#to_a rather than wrapping with an array.
    if target_node.is_a?(Array)
      target_node.any? { |node| node.equal?(child_node) }
    else
      target_node.equal?(child_node)
    end
  end
end

.inherited(subclass) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/variable_force/branch.rb', line 51

def self.inherited(subclass)
  classes << subclass
end

.typeObject



55
56
57
# File 'lib/rubocop/cop/variable_force/branch.rb', line 55

def self.type
  name.split('::').last.gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



126
127
128
129
130
131
# File 'lib/rubocop/cop/variable_force/branch.rb', line 126

def ==(other)
  return false unless other

  control_node.equal?(other.control_node) &&
    child_node.equal?(other.child_node)
end

#always_run?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/rubocop/cop/variable_force/branch.rb', line 97

def always_run?
  raise NotImplementedError
end

#branched?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/rubocop/cop/variable_force/branch.rb', line 93

def branched?
  !always_run?
end

#control_nodeObject



73
74
75
# File 'lib/rubocop/cop/variable_force/branch.rb', line 73

def control_node
  child_node.parent
end

#each_ancestor(include_self: false) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/variable_force/branch.rb', line 83

def each_ancestor(include_self: false, &block)
  unless block_given?
    return to_enum(__method__, include_self: include_self)
  end

  yield self if include_self
  scan_ancestors(&block)
  self
end

#exclusive_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubocop/cop/variable_force/branch.rb', line 109

def exclusive_with?(other)
  return false unless other
  return false if may_jump_to_other_branch?

  other.each_ancestor(include_self: true) do |other_ancestor|
    if control_node.equal?(other_ancestor.control_node)
      return !child_node.equal?(other_ancestor.child_node)
    end
  end

  if parent
    parent.exclusive_with?(other)
  else
    false
  end
end

#hashObject



135
136
137
# File 'lib/rubocop/cop/variable_force/branch.rb', line 135

def hash
  control_node.object_id.hash ^ child_node.object_id.hash
end

#may_jump_to_other_branch?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rubocop/cop/variable_force/branch.rb', line 101

def may_jump_to_other_branch?
  false
end

#may_run_incompletely?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/rubocop/cop/variable_force/branch.rb', line 105

def may_run_incompletely?
  false
end

#parentObject



77
78
79
80
81
# File 'lib/rubocop/cop/variable_force/branch.rb', line 77

def parent
  return @parent if instance_variable_defined?(:@parent)

  @branch = Branch.of(control_node, scope: scope)
end