123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::IfCorrector

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Inherits: Object
Defined in: lib/rubocop/cop/style/conditional_assignment.rb

Overview

Corrector to correct conditional assignment in if statements.

Class Method Summary

Class Method Details

.correct(corrector, cop, node)

[ GitHub ]

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

def correct(corrector, cop, node)
  correct_if_branches(corrector, cop, node)
end

.extract_tail_branches(node) (private)

[ GitHub ]

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

def extract_tail_branches(node)
  if_branch, *elsif_branches, else_branch = *node.branches
  elsif_branches.map! { |branch| tail(branch) }

  [tail(if_branch), elsif_branches, tail(else_branch)]
end

.move_assignment_inside_condition(corrector, node)

[ GitHub ]

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

def move_assignment_inside_condition(corrector, node)
  column = node.source_range.column
  *_var, condition = *node
  assignment = assignment(node)

  corrector.remove(assignment)

  condition.branches.flatten.each do |branch|
    move_branch_inside_condition(corrector, branch, condition, assignment, column)
  end
end

.move_branch_inside_condition(corrector, branch, condition, assignment, column) (private)

[ GitHub ]

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

def move_branch_inside_condition(corrector, branch, condition,
                                 assignment, column)
  branch_assignment = tail(branch)
  corrector.insert_before(branch_assignment, assignment.source)

  remove_whitespace_in_branches(corrector, branch, condition, column)

  return unless (branch_else = branch.parent.loc.else)

  corrector.remove_preceding(branch_else, branch_else.column - column)
end