class RuboCop::Cop::Style::ParallelAssignment::AssignmentSorter
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
Public Class Methods
new(assignments)
click to toggle source
# File lib/rubocop/cop/style/parallel_assignment.rb, line 141 def initialize(assignments) @assignments = assignments end
Public Instance Methods
accesses?(rhs, lhs)
click to toggle source
‘lhs` is an assignment method call like `obj.attr=` or `ary=`. Does `rhs` access the same value which is assigned by `lhs`?
# File lib/rubocop/cop/style/parallel_assignment.rb, line 170 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)
click to toggle source
# File lib/rubocop/cop/style/parallel_assignment.rb, line 163 def dependency?(lhs, rhs) uses_var?(rhs, var_name(lhs)) || (lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs)) end
tsort_each_child(assignment) { |other| ... }
click to toggle source
# File lib/rubocop/cop/style/parallel_assignment.rb, line 149 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(...)
click to toggle source
# File lib/rubocop/cop/style/parallel_assignment.rb, line 145 def tsort_each_node(...) @assignments.each(...) end