Class: RuboCop::Cop::Style::CaseCorrector

Inherits:
Object
  • Object
show all
Extended by:
ConditionalAssignmentHelper
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 ConditionalAssignmentHelper

correct_branches, expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 338

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