Class: RuboCop::Cop::LineBreakCorrector

Inherits:
Object
  • Object
show all
Extended by:
Alignment, TrailingBody, Util
Defined in:
lib/rubocop/cop/correctors/line_break_corrector.rb

Overview

This class handles autocorrection for code that needs to be moved to new lines.

Constant Summary

Constants included from Util

Util::LITERAL_REGEX

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Util

add_parentheses, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Methods included from TrailingBody

body_on_first_line?, first_part_of, trailing_body?

Class Attribute Details

.processed_sourceObject (readonly)

Returns the value of attribute processed_source


13
14
15
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 13

def processed_source
  @processed_source
end

Class Method Details

.break_line_before(range:, node:, corrector:, configured_width:, indent_steps: 1) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 28

def break_line_before(range:, node:, corrector:, configured_width:,
                      indent_steps: 1)
  corrector.insert_before(
    range,
    "\n#{' ' * (node.loc.keyword.column +
                  indent_steps * configured_width)}"
  )
end

.correct_trailing_body(configured_width:, corrector:, node:, processed_source:) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 15

def correct_trailing_body(configured_width:, corrector:, node:,
                          processed_source:)
  @processed_source = processed_source
  range = first_part_of(node.to_a.last)
  eol_comment = processed_source.comment_at_line(node.source_range.line)

  break_line_before(range: range, node: node, corrector: corrector,
                    configured_width: configured_width)
  move_comment(eol_comment: eol_comment, node: node,
               corrector: corrector)
  remove_semicolon(node, corrector)
end

.move_comment(eol_comment:, node:, corrector:) ⇒ Object


37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 37

def move_comment(eol_comment:, node:, corrector:)
  return unless eol_comment

  text = eol_comment.loc.expression.source
  corrector.insert_before(node,
                          "#{text}\n#{' ' * node.loc.keyword.column}")
  corrector.remove(eol_comment)
end