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



567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 567

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



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

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