Class: RuboCop::Cop::Style::UnlessCorrector

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

Overview

Corrector to correct conditional assignment in ‘unless` statements.

Constant Summary

Constants included from ConditionalAssignmentHelper

ConditionalAssignmentHelper::ALIGN_WITH, ConditionalAssignmentHelper::END_ALIGNMENT, ConditionalAssignmentHelper::EQUAL, ConditionalAssignmentHelper::KEYWORD

Class Method Summary collapse

Methods included from ConditionalAssignmentHelper

expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 627

def correct(cop, node)
  _condition, else_branch, if_branch = *node
  if_branch = tail(if_branch)
  else_branch = tail(else_branch)
  _variable, *_operator, if_assignment = *if_branch
  _else_variable, *_operator, else_assignment = *else_branch

  lambda do |corrector|
    corrector.insert_before(node.source_range, lhs(if_branch))
    corrector.replace(if_branch.source_range, if_assignment.source)
    corrector.replace(else_branch.source_range,
                      else_assignment.source)
    corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
  end
end