Class: RuboCop::Cop::Style::ParallelAssignment::AssignmentSorter
- Inherits:
-
Object
- Object
- RuboCop::Cop::Style::ParallelAssignment::AssignmentSorter
- Extended by:
- Macros
- Includes:
- TSort
- Defined in:
- lib/rubocop/cop/style/parallel_assignment.rb
Overview
Helper class necessitated by silly design of TSort prior to Ruby 2.1 Newer versions have a better API, but that doesn’t help us
Instance Method Summary collapse
-
#accesses?(rhs, lhs) ⇒ Boolean
‘lhs` is an assignment method call like `obj.attr=` or `ary=`.
- #dependency?(lhs, rhs) ⇒ Boolean
-
#initialize(assignments) ⇒ AssignmentSorter
constructor
A new instance of AssignmentSorter.
- #matching_calls(node, receiver, method_name) ⇒ Object
- #tsort_each_child(assignment) ⇒ Object
- #tsort_each_node ⇒ Object
- #uses_var?(node) ⇒ Object
- #var_name(node) ⇒ Object
Constructor Details
#initialize(assignments) ⇒ AssignmentSorter
Returns a new instance of AssignmentSorter.
136 137 138 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 136 def initialize(assignments) @assignments = assignments end |
Instance Method Details
#accesses?(rhs, lhs) ⇒ Boolean
‘lhs` is an assignment method call like `obj.attr=` or `ary=`. Does `rhs` access the same value which is assigned by `lhs`?
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 165 def accesses?(rhs, lhs) if lhs.method?(:[]=) # FIXME: Workaround `rubocop:disable` comment for JRuby. # rubocop:disable Performance/RedundantEqualityComparisonBlock matching_calls(rhs, lhs.receiver, :[]).any? { |args| args == lhs.arguments } # rubocop:enable Performance/RedundantEqualityComparisonBlock else access_method = lhs.method_name.to_s.chop.to_sym matching_calls(rhs, lhs.receiver, access_method).any? end end |
#dependency?(lhs, rhs) ⇒ Boolean
158 159 160 161 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 158 def dependency?(lhs, rhs) uses_var?(rhs, var_name(lhs)) || (lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs)) end |
#matching_calls(node, receiver, method_name) ⇒ Object
134 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 134 def_node_search :matching_calls, '(send %1 %2 $...)' |
#tsort_each_child(assignment) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 144 def tsort_each_child(assignment) # yield all the assignments which must come after `assignment` # (due to dependencies on the previous value of the assigned var) my_lhs, _my_rhs = *assignment @assignments.each do |other| _other_lhs, other_rhs = *other next unless dependency?(my_lhs, other_rhs) yield other end end |
#tsort_each_node ⇒ Object
140 141 142 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 140 def tsort_each_node(...) @assignments.each(...) end |
#uses_var?(node) ⇒ Object
131 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 131 def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}' |
#var_name(node) ⇒ Object
128 |
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 128 def_node_matcher :var_name, '{(casgn _ $_) (_ $_)}' |