123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Struct
Instance Chain:
self, Struct
Inherits: Struct
  • Object
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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Method Details

.classes

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 43

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

.define_predicate(name, child_index: nil)

[ GitHub ]

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

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)

[ GitHub ]

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

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

.type

[ GitHub ]

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

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

Instance Attribute Details

#always_run?Boolean (readonly)

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 92

def always_run?
  raise NotImplementedError
end

#branched?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 88

def branched?
  !always_run?
end

#child_node (rw)

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
[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 42

Base = Struct.new(:child_node, :scope)

#may_jump_to_other_branch?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 96

def may_jump_to_other_branch?
  false
end

#may_run_incompletely?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 100

def may_run_incompletely?
  false
end

#scope (rw)

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
[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 42

Base = Struct.new(:child_node, :scope)

Instance Method Details

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

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 121

def ==(other)
  return false unless other

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

#control_node

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 70

def control_node
  child_node.parent
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Base)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 80

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

  yield self if include_self
  scan_ancestors(&block)
  self
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 127

alias_method :eql?, :==

#exclusive_with?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 104

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

#hash

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/branch.rb', line 129

def hash
  [control_node.object_id, control_node.object_id].hash
end

#parent

[ GitHub ]

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

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

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

#scan_ancestors (private)

[ GitHub ]

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

def scan_ancestors
  branch = self

  while (branch = branch.parent)
    yield branch
  end
end