Class: RuboCop::Cop::VariableForce::Branch::Base
- Inherits:
-
Struct
- Object
- Struct
- RuboCop::Cop::VariableForce::Branch::Base
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.
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_node ⇒ Object
Returns the value of attribute child_node
44
45
46
|
# File 'lib/rubocop/cop/variable_force/branch.rb', line 44
def child_node
@child_node
end
|
#scope ⇒ Object
Returns the value of attribute scope
44
45
46
|
# File 'lib/rubocop/cop/variable_force/branch.rb', line 44
def scope
@scope
end
|
Class Method Details
.classes ⇒ Object
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]
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
|
.type ⇒ Object
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
97
98
99
|
# File 'lib/rubocop/cop/variable_force/branch.rb', line 97
def always_run?
raise NotImplementedError
end
|
#branched? ⇒ Boolean
93
94
95
|
# File 'lib/rubocop/cop/variable_force/branch.rb', line 93
def branched?
!always_run?
end
|
#control_node ⇒ Object
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
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
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
|
#hash ⇒ Object
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
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
105
106
107
|
# File 'lib/rubocop/cop/variable_force/branch.rb', line 105
def may_run_incompletely?
false
end
|
#parent ⇒ Object
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
|