Module: RuboCop::Cop::Style::ConditionalCorrectorHelper
- Included in:
- CaseCorrector, IfCorrector, TernaryCorrector
- Defined in:
- lib/rubocop/cop/style/conditional_assignment.rb
Overview
Helper module to provide common methods to ConditionalAssignment correctors
Instance Method Summary collapse
- #assignment(node) ⇒ Object
- #correct_branches(corrector, branches) ⇒ Object
- #correct_if_branches(corrector, cop, node) ⇒ Object
-
#remove_whitespace_in_branches(corrector, branch, condition, column) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity.
- #replace_branch_assignment(corrector, branch) ⇒ Object
-
#same_line?(node1, node2) ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity.
- #white_space_range(node, column) ⇒ Object
Instance Method Details
#assignment(node) ⇒ Object
471 472 473 474 475 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 471 def assignment(node) condition = node.send_type? ? node.last_argument : node.expression node.source_range.begin.join(condition.source_range.begin) end |
#correct_branches(corrector, branches) ⇒ Object
500 501 502 503 504 505 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 500 def correct_branches(corrector, branches) branches.each do |branch| *_, assignment = *branch corrector.replace(branch, assignment.source) end end |
#correct_if_branches(corrector, cop, node) ⇒ Object
477 478 479 480 481 482 483 484 485 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 477 def correct_if_branches(corrector, cop, node) if_branch, elsif_branches, else_branch = extract_tail_branches(node) corrector.insert_before(node, lhs(if_branch)) replace_branch_assignment(corrector, if_branch) correct_branches(corrector, elsif_branches) replace_branch_assignment(corrector, else_branch) corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch))) end |
#remove_whitespace_in_branches(corrector, branch, condition, column) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 441 def remove_whitespace_in_branches(corrector, branch, condition, column) branch.each_node do |child| next if child.source_range.nil? next if child.parent.dstr_type? white_space = white_space_range(child, column) corrector.remove(white_space) if white_space.source.strip.empty? end if condition.loc.else && !same_line?(condition.else_branch, condition) corrector.remove_preceding(condition.loc.else, condition.loc.else.column - column) end return unless condition.loc.end && !same_line?(condition.loc.end, condition) corrector.remove_preceding(condition.loc.end, condition.loc.end.column - column) end |
#replace_branch_assignment(corrector, branch) ⇒ Object
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 487 def replace_branch_assignment(corrector, branch) _variable, *_operator, assignment = *branch source = assignment.source replacement = if assignment.array_type? && !assignment.bracketed? "[#{source}]" else source end corrector.replace(branch, replacement) end |
#same_line?(node1, node2) ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
460 461 462 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 460 def same_line?(node1, node2) RuboCop::Cop::Util.same_line?(node1, node2) end |
#white_space_range(node, column) ⇒ Object
464 465 466 467 468 469 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 464 def white_space_range(node, column) expression = node.source_range begin_pos = expression.begin_pos - (expression.column - column - 2) Parser::Source::Range.new(expression.source_buffer, begin_pos, expression.begin_pos) end |