123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Style::ConditionalCorrectorHelper

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/rubocop/cop/style/conditional_assignment.rb

Overview

Helper module to provide common methods to ConditionalAssignment correctors

Instance Method Summary

Instance Method Details

#assignment(node)

[ GitHub ]

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

def assignment(node)
  condition = node.send_type? ? node.last_argument : node.expression

  node.source_range.begin.join(condition.source_range.begin)
end

#correct_branches(corrector, branches)

[ GitHub ]

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

def correct_branches(corrector, branches)
  branches.each do |branch|
    *_, assignment = *branch
    corrector.replace(branch, assignment.source)
  end
end

#correct_if_branches(corrector, cop, node)

[ GitHub ]

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

def correct_if_branches(corrector, cop, node)
  if_branch, elsif_branches, else_branch = extract_tail_branches(node)

  corrector.insert_before(node, lhs(if_branch))
  replace_branch_assignment(corrector, if_branch)
  correct_branches(corrector, elsif_branches)
  replace_branch_assignment(corrector, else_branch)
  corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
end

#remove_whitespace_in_branches(corrector, branch, condition, column)

[ GitHub ]

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

def remove_whitespace_in_branches(corrector, branch, condition, column)
  branch.each_node do |child|
    next if child.source_range.nil?

    white_space = white_space_range(child, column)
    corrector.remove(white_space) if white_space.source.strip.empty?
  end

  [condition.loc.else, condition.loc.end].each do |loc|
    corrector.remove_preceding(loc, loc.column - column)
  end
end

#replace_branch_assignment(corrector, branch)

[ GitHub ]

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

def replace_branch_assignment(corrector, branch)
  _variable, *_operator, assignment = *branch
  source = assignment.source

  replacement = if assignment.array_type? && !assignment.bracketed?
                  "[#{source}]"
                else
                  source
                end

  corrector.replace(branch, replacement)
end

#white_space_range(node, column)

[ GitHub ]

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

def white_space_range(node, column)
  expression = node.source_range
  begin_pos = expression.begin_pos - (expression.column - column - 2)

  Parser::Source::Range.new(expression.source_buffer, begin_pos, expression.begin_pos)
end