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



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

def child_node
  @child_node
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



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

def scope
  @scope
end

Class Method Details

.classesObject

rubocop:enable Metrics/BlockLength



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

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

.define_predicate(name, child_index: nil) ⇒ Object



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

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



49
50
51
# File 'lib/rubocop/cop/variable_force/branch.rb', line 49

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

.typeObject



53
54
55
# File 'lib/rubocop/cop/variable_force/branch.rb', line 53

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

Instance Method Details

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



123
124
125
126
127
# File 'lib/rubocop/cop/variable_force/branch.rb', line 123

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)


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

def always_run?
  raise NotImplementedError
end

#branched?Boolean

Returns:

  • (Boolean)


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

def branched?
  !always_run?
end

#control_nodeObject



71
72
73
# File 'lib/rubocop/cop/variable_force/branch.rb', line 71

def control_node
  child_node.parent
end

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

Yields:

  • (_self)

Yield Parameters:



80
81
82
83
84
85
86
87
88
# File 'lib/rubocop/cop/variable_force/branch.rb', line 80

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)


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

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



131
132
133
# File 'lib/rubocop/cop/variable_force/branch.rb', line 131

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

#may_jump_to_other_branch?Boolean

Returns:

  • (Boolean)


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

def may_jump_to_other_branch?
  false
end

#may_run_incompletely?Boolean

Returns:

  • (Boolean)


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

def may_run_incompletely?
  false
end

#parentObject



75
76
77
78
# File 'lib/rubocop/cop/variable_force/branch.rb', line 75

def parent
  return @parent if instance_variable_defined?(:@parent)
  @branch = Branch.of(control_node, scope: scope)
end