Module: RuboCop::Cop::Style::ConditionalCorrectorHelper

Included in:
CaseCorrector, IfCorrector, TernaryCorrector
Defined in:
lib/rubocop/cop/style/conditional_assignment.rb

Overview

Helper module to provide common methods to ConditionalAssignment correctors

Instance Method Summary collapse

Instance Method Details

#assignment(node) ⇒ Object



400
401
402
403
404
405
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 400

def assignment(node)
  *_, condition = *node
  Parser::Source::Range.new(node.loc.expression.source_buffer,
                            node.loc.expression.begin_pos,
                            condition.loc.expression.begin_pos)
end

#correct_branches(corrector, branches) ⇒ Object



407
408
409
410
411
412
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 407

def correct_branches(corrector, branches)
  branches.each do |branch|
    *_, assignment = *branch
    corrector.replace(branch.source_range, assignment.source)
  end
end

#remove_whitespace_in_branches(corrector, branch, condition, column) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 382

def remove_whitespace_in_branches(corrector, branch, condition, column)
  branch.each_node do |child|
    child_expression = child.loc.expression
    white_space =
      Parser::Source::Range.new(child_expression.source_buffer,
                                child_expression.begin_pos -
                                 (child.loc.expression.column -
                                  column - 2),
                                child_expression.begin_pos)

    corrector.remove(white_space) if white_space.source.strip.empty?
  end

  [condition.loc.else, condition.loc.end].each do |loc|
    corrector.remove_preceding(loc, loc.column - column)
  end
end