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

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

correct_branches, expand_elses, expand_when_branches, indent, lhs, tail

Class Method Details

.correct(node) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 286

def correct(node)
  condition, if_branch, else_branch = *node
  _variable, *_operator, if_rhs = *if_branch
  _else_variable, *_operator, else_rhs = *else_branch
  condition = condition.source
  if_rhs = if_rhs.source
  else_rhs = else_rhs.source

  ternary = "#{condition} ? #{if_rhs} : #{else_rhs}"
  if if_branch.send_type? && if_branch.method_name != :[]=
    ternary = "(#{ternary})"
  end
  correction = "#{lhs(if_branch)}#{ternary}"

  lambda do |corrector|
    corrector.replace(node.source_range, correction)
  end
end