Class: RuboCop::Cop::Style::TernaryCorrector
- Inherits:
-
Object
- Object
- RuboCop::Cop::Style::TernaryCorrector
- 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::EQUAL
Class Method Summary collapse
Methods included from ConditionalAssignmentHelper
correct_branches, expand_elses, expand_when_branches, lhs, tail
Class Method Details
.correct(node) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 273 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 |