123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Style::ConditionalAssignmentHelper

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macros
Defined in: lib/rubocop/cop/style/conditional_assignment.rb

Overview

Helper module to provide common methods to classes needed for the ConditionalAssignment Cop.

Constant Summary

Instance Method Summary

Instance Method Details

#assignment_rhs_exist?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 106

def assignment_rhs_exist?(node)
  parent = node.parent
  return true unless parent

  !(parent.mlhs_type? || parent.resbody_type?)
end

#end_with_eq?(sym) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 60

def end_with_eq?(sym)
  sym.to_s.end_with?(EQUAL)
end

#expand_elses(branch)

elsif branches show up in the node as an else. We need to recursively iterate over all else branches and consider all but the last node an elsif branch and consider the last node the actual else branch.

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 20

def expand_elses(branch)
  elsif_branches = expand_elsif(branch)
  else_branch = elsif_branches.any? ? elsif_branches.pop : branch
  [elsif_branches, else_branch]
end

#expand_elsif(node, elsif_branches = []) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 66

def expand_elsif(node, elsif_branches = [])
  return [] if node.nil? || !node.if_type? || !node.elsif?

  elsif_branches << node.if_branch

  else_branch = node.else_branch
  if else_branch&.if_type? && else_branch.elsif?
    expand_elsif(else_branch, elsif_branches)
  else
    elsif_branches << else_branch
  end
end

#expand_when_branches(when_branches)

when nodes contain the entire branch including the condition. We only need the contents of the branch, not the condition.

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 28

def expand_when_branches(when_branches)
  when_branches.map(&:body)
end

#indent(cop, source)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 51

def indent(cop, source)
  conf = cop.config.for_cop(END_ALIGNMENT)
  if conf[ALIGN_WITH] == KEYWORD
    ' ' * source.length
  else
    ''
  end
end

#lhs(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 36

def lhs(node)
  case node.type
  when :send
    lhs_for_send(node)
  when :op_asgn, :and_asgn, :or_asgn
    "#{node.assignment_node.source} #{node.operator}= "
  when :casgn
    lhs_for_casgn(node)
  when *ConditionalAssignment::VARIABLE_ASSIGNMENT_TYPES
    "#{node.name} = "
  else
    node.source
  end
end

#lhs_for_casgn(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 92

def lhs_for_casgn(node)
  if node.namespace.nil?
    "#{node.name} = "
  elsif node.namespace.cbase_type?
    "::#{node.name} = "
  else
    "#{node.namespace.const_name}::#{node.name} = "
  end
end

#lhs_for_send(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 79

def lhs_for_send(node)
  receiver = node.receiver ? node.receiver.source : ''

  if node.method?(:[]=)
    indices = node.arguments[0...-1].map(&:source).join(', ')
    "#{receiver}[#{indices}] = "
  elsif node.setter_method?
    "#{receiver}.#{node.method_name[0...-1]} = "
  else
    "#{receiver} #{node.method_name} "
  end
end

#setter_method?(method_name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 102

def setter_method?(method_name)
  method_name.to_s.end_with?(EQUAL) && !%i[!= == === >= <=].include?(method_name)
end

#tail(branch)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 32

def tail(branch)
  branch.begin_type? ? Array(branch).last : branch
end