Class: RuboCop::Cop::Style::TernaryCorrector
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 ternary conditions.
Class Method Summary
- .correct(corrector, node)
- .move_assignment_inside_condition(corrector, node)
- .correction(node) private
- .element_assignment?(node) ⇒ Boolean private
- .extract_branches(node) private
- .move_branch_inside_condition(corrector, branch, assignment) private
- .remove_parentheses(corrector, node) private
- .ternary(node) 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, node)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 502
def correct(corrector, node) corrector.replace(node, correction(node)) end
.correction(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 520
def correction(node) "#{lhs(node.if_branch)}#{ternary(node)}" end
.element_assignment?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 533
def element_assignment?(node) node.send_type? && !node.method?(:[]=) end
.extract_branches(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 537
def extract_branches(node) rhs = node.send_type? ? node.last_argument : node.expression condition = rhs.children.first if rhs.begin_type? && rhs.children.one? _condition, if_branch, else_branch = *(condition || rhs) [if_branch, else_branch] end
.move_assignment_inside_condition(corrector, node)
[ GitHub ]# 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
.move_branch_inside_condition(corrector, branch, assignment) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 550
def move_branch_inside_condition(corrector, branch, assignment) corrector.insert_before(branch, assignment.source) end
.remove_parentheses(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 545
def remove_parentheses(corrector, node) corrector.remove(node.loc.begin) corrector.remove(node.loc.end) end
.ternary(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 524
def ternary(node) _variable, *_operator, if_rhs = *node.if_branch _else_variable, *_operator, else_rhs = *node.else_branch expr = "#{node.condition.source} ? #{if_rhs.source} : #{else_rhs.source}" element_assignment?(node.if_branch) ? "(#{expr})" : expr end