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, remove_whitespace_in_branches, white_space_range

Methods included from ConditionalAssignmentHelper

expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 563

def correct(cop, node)
  when_branches, else_branch = extract_tail_branches(node)
  _variable, *_operator, else_assignment = *else_branch

  lambda do |corrector|
    corrector.insert_before(node.source_range, lhs(else_branch))
    correct_branches(corrector, when_branches)
    corrector.replace(else_branch.source_range,
                      else_assignment.source)

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

.move_assignment_inside_condition(node) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 578

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