Class: RuboCop::Cop::Style::IfCorrector
- Inherits:
-
Object
- Object
- RuboCop::Cop::Style::IfCorrector
- Extended by:
- ConditionalAssignmentHelper, ConditionalCorrectorHelper
- 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 ConditionalCorrectorHelper
assignment, correct_branches, remove_whitespace_in_branches
Methods included from ConditionalAssignmentHelper
expand_elses, expand_when_branches, indent, lhs, tail
Class Method Details
.correct(cop, node) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 467 def correct(cop, node) if_branch, elsif_branches, else_branch = extract_branches(node) _variable, *_operator, if_assignment = *if_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 |
.move_assignment_inside_condition(node) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 482 def move_assignment_inside_condition(node) column = node.loc.expression.column *_var, condition = *node _condition, if_branch, else_branch = *condition elsif_branches, else_branch = (else_branch) assignment = assignment(node) lambda do |corrector| corrector.remove(assignment) [if_branch, *elsif_branches, else_branch].each do |branch| branch_assignment = tail(branch) corrector.insert_before(branch_assignment.loc.expression, assignment.source) remove_whitespace_in_branches(corrector, branch, condition, column) corrector .remove_preceding(branch.parent.loc.else, branch.parent.loc.else.column - column) end end end |