Module: RuboCop::Cop::Style::ConditionalCorrectorHelper
- Included in:
- CaseCorrector, IfCorrector, TernaryCorrector, UnlessCorrector
- 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
- #replace_branch_assignment(corrector, branch) ⇒ Object
- #white_space_range(node, column) ⇒ Object
Instance Method Details
#assignment(node) ⇒ Object
455 456 457 458 459 460 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 455 def assignment(node) *_, condition = *node Parser::Source::Range.new(node.loc.expression.source_buffer, node.loc.expression.begin_pos, condition.loc.expression.begin_pos) end |
#correct_branches(corrector, branches) ⇒ Object
485 486 487 488 489 490 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 485 def correct_branches(corrector, branches) branches.each do |branch| *_, assignment = *branch corrector.replace(branch.source_range, assignment.source) end end |
#correct_if_branches(corrector, cop, node) ⇒ Object
462 463 464 465 466 467 468 469 470 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 462 def correct_if_branches(corrector, cop, node) if_branch, elsif_branches, else_branch = extract_tail_branches(node) corrector.insert_before(node.source_range, 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
435 436 437 438 439 440 441 442 443 444 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 435 def remove_whitespace_in_branches(corrector, branch, condition, column) branch.each_node do |child| white_space = white_space_range(child, column) corrector.remove(white_space) if white_space.source.strip.empty? end [condition.loc.else, condition.loc.end].each do |loc| corrector.remove_preceding(loc, loc.column - column) end end |
#replace_branch_assignment(corrector, branch) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 472 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.source_range, replacement) end |
#white_space_range(node, column) ⇒ Object
446 447 448 449 450 451 452 453 |
# File 'lib/rubocop/cop/style/conditional_assignment.rb', line 446 def white_space_range(node, column) expression = node.loc.expression begin_pos = expression.begin_pos - (expression.column - column - 2) Parser::Source::Range.new(expression.source_buffer, begin_pos, expression.begin_pos) end |