module RuboCop::Cop::Style::ConditionalAssignmentHelper
def assignment_rhs_exist?(node)
def assignment_rhs_exist?(node) parent = node.parent return true unless parent !(parent.mlhs_type? || parent.resbody_type?) end
def expand_elses(branch)
but the last `node` an `elsif` branch and consider the last `node`
to recursively iterate over all `else` branches and consider all
`elsif` branches show up in the `node` as an `else`. We need
def expand_elses(branch) elsif_branches = expand_elsif(branch) else_branch = elsif_branches.any? ? elsif_branches.pop : branch [elsif_branches, else_branch] end
def expand_elsif(node, elsif_branches = [])
def expand_elsif(node, elsif_branches = []) return [] if node.nil? || !node.if_type? _condition, elsif_branch, else_branch = *node elsif_branches << elsif_branch if else_branch && else_branch.if_type? expand_elsif(else_branch, elsif_branches) else elsif_branches << else_branch end elsif_branches end
def expand_when_branches(when_branches)
`when` nodes contain the entire branch including the condition.
def expand_when_branches(when_branches) when_branches.map { |branch| branch.children[1] } end
def indent(cop, source)
def indent(cop, source) if cop.config[END_ALIGNMENT] && cop.config[END_ALIGNMENT][ALIGN_WITH] && cop.config[END_ALIGNMENT][ALIGN_WITH] == KEYWORD ' ' * source.length else '' end end
def lhs(node) # rubocop:disable Metrics/MethodLength
def lhs(node) # rubocop:disable Metrics/MethodLength case node.type when :send lhs_for_send(node) when :op_asgn "#{node.children[0].source} #{node.children[1]}= " when :and_asgn "#{node.children[0].source} &&= " when :or_asgn "#{node.children[0].source} ||= " when :casgn "#{node.children[1]} = " when *ConditionalAssignment::VARIABLE_ASSIGNMENT_TYPES "#{node.children[0]} = " else node.source end end
def lhs_for_send(node)
def lhs_for_send(node) receiver = node.receiver.nil? ? '' : node.receiver.source method_name = node.method_name if method_name == :[]= indices = node.children[2...-1].map(&:source).join(', ') "#{receiver}[#{indices}] = " elsif setter_method?(method_name) "#{receiver}.#{method_name[0...-1]} = " else "#{receiver} #{method_name} " end end
def setter_method?(method_name)
def setter_method?(method_name) method_name.to_s.end_with?(EQUAL) && ![:!=, :==, :===, :>=, :<=].include?(method_name) end
def tail(branch)
def tail(branch) branch.begin_type? ? [*branch].last : branch end