Class: RuboCop::Cop::Style::TernaryCorrector

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 ternary conditions.

Constant Summary

Constants included from ConditionalAssignmentHelper

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

Class Method Summary collapse

Methods included from ConditionalCorrectorHelper

assignment, correct_branches, correct_if_branches, remove_whitespace_in_branches, replace_branch_assignment, white_space_range

Methods included from ConditionalAssignmentHelper

end_with_eq?, expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(corrector, node) ⇒ Object



502
503
504
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 502

def correct(corrector, node)
  corrector.replace(node, correction(node))
end

.move_assignment_inside_condition(corrector, node) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 506

def move_assignment_inside_condition(corrector, node)
  rhs = node.send_type? ? node.last_argument : node.expression
  if_branch, else_branch = extract_branches(node)
  assignment = assignment(node)

  remove_parentheses(corrector, rhs) if Util.parentheses?(rhs)
  corrector.remove(assignment)

  move_branch_inside_condition(corrector, if_branch, assignment)
  move_branch_inside_condition(corrector, else_branch, assignment)
end