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

expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 599

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

  lambda do |corrector|
    corrector.insert_before(node.source_range, 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
end

.move_assignment_inside_condition(node) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 612

def move_assignment_inside_condition(node)
  column = node.loc.expression.column
  *_var, condition = *node
  assignment = assignment(node)

  lambda do |corrector|
    corrector.remove(assignment)

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