Class: RuboCop::Cop::Style::IfCorrector

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

Overview

Corrector to correct conditional assignment in ‘if` 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

correct_branches, expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(cop, node) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 317

def correct(cop, node)
  _condition, if_branch, else_branch = *node
  if_branch = tail(if_branch)
  _variable, *_operator, if_assignment = *if_branch
  elsif_branches, else_branch = expand_elses(else_branch)
  elsif_branches.map! { |branch| tail(branch) }
  else_branch = tail(else_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)
    correct_branches(corrector, elsif_branches)
    corrector.replace(else_branch.source_range,
                      else_assignment.source)
    corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
  end
end