Class: RuboCop::Cop::Style::CaseCorrector

Inherits:
Object
  • Object
show all
Extended by:
ConditionalAssignmentHelper, ConditionalCorrectorHelper
Defined in:
lib/rubocop/cop/style/conditional_assignment.rb

Overview

Corrector to correct conditional assignment in ‘case` statements.

Constant Summary

Constants included from ConditionalAssignmentHelper

RuboCop::Cop::Style::ConditionalAssignmentHelper::ALIGN_WITH, RuboCop::Cop::Style::ConditionalAssignmentHelper::END_ALIGNMENT, RuboCop::Cop::Style::ConditionalAssignmentHelper::EQUAL, RuboCop::Cop::Style::ConditionalAssignmentHelper::KEYWORD

Class Method Summary collapse

Methods included from ConditionalCorrectorHelper

assignment, correct_branches, correct_if_branches, remove_whitespace_in_branches, replace_branch_assignment, white_space_range

Methods included from ConditionalAssignmentHelper

end_with_eq?, expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(corrector, cop, node) ⇒ Object



609
610
611
612
613
614
615
616
617
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 609

def correct(corrector, cop, node)
  when_branches, else_branch = extract_tail_branches(node)

  corrector.insert_before(node, lhs(else_branch))
  correct_branches(corrector, when_branches)
  replace_branch_assignment(corrector, else_branch)

  corrector.insert_before(node.loc.end, indent(cop, lhs(else_branch)))
end

.move_assignment_inside_condition(corrector, node) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 619

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

  corrector.remove(assignment)

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