Class: RuboCop::Cop::Style::IfCorrector
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Inherits: | Object |
Defined in: | lib/rubocop/cop/style/conditional_assignment.rb |
Overview
Corrector to correct conditional assignment in if
statements.
Class Method Summary
- .correct(corrector, cop, node)
- .move_assignment_inside_condition(corrector, node)
- .extract_tail_branches(node) private
- .move_branch_inside_condition(corrector, branch, condition, assignment, column) private
ConditionalCorrectorHelper
- Extended
assignment, correct_branches, correct_if_branches, remove_whitespace_in_branches, replace_branch_assignment, white_space_range |
ConditionalAssignmentHelper
- Extended
end_with_eq?, | |
expand_elses |
|
expand_when_branches |
|
indent, lhs, tail, assignment_rhs_exist?, expand_elsif, lhs_for_casgn, lhs_for_send, setter_method? |
Class Method Details
.correct(corrector, cop, node)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 562
def correct(corrector, cop, node) correct_if_branches(corrector, cop, node) end
.extract_tail_branches(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 580
def extract_tail_branches(node) if_branch, *elsif_branches, else_branch = *node.branches elsif_branches.map! { |branch| tail(branch) } [tail(if_branch), elsif_branches, tail(else_branch)] end
.move_assignment_inside_condition(corrector, node)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 566
def move_assignment_inside_condition(corrector, node) column = node.source_range.column condition = node.send_type? ? node.last_argument : node.expression assignment = assignment(node) corrector.remove(assignment) condition.branches.flatten.each do |branch| move_branch_inside_condition(corrector, branch, condition, assignment, column) end end
.move_branch_inside_condition(corrector, branch, condition, assignment, column) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 587
def move_branch_inside_condition(corrector, branch, condition, assignment, column) branch_assignment = tail(branch) corrector.insert_before(branch_assignment, assignment.source) remove_whitespace_in_branches(corrector, branch, condition, column) return unless (branch_else = branch.parent.loc.else) corrector.remove_preceding(branch_else, branch_else.column - column) end