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

Methods included from ConditionalAssignmentHelper

expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 519

def correct(cop, node)
  _condition, *when_branches, else_branch = *node
  else_branch = tail(else_branch)
  when_branches = expand_when_branches(when_branches)
  when_branches.map! { |when_branch| tail(when_branch) }
  _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



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 537

def move_assignment_inside_condition(node)
  column = node.loc.expression.column
  *_var, condition = *node
  _condition, *when_branches, else_branch = *condition
  when_branches = expand_when_branches(when_branches)
  assignment = assignment(node)

  lambda do |corrector|
    corrector.remove(assignment)

    [*when_branches, else_branch].each do |branch|
      branch_assignment = tail(branch)
      corrector.insert_before(branch_assignment.loc.expression,
                              assignment.source)

      remove_whitespace_in_branches(corrector, branch,
                                    condition, column)

      corrector
        .remove_preceding(branch.parent.loc.keyword,
                          branch.parent.loc.keyword.column - column)
    end
  end
end