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 TrailingBody

body_on_first_line?, first_part_of, trailing_body?

Methods included from Util

begins_its_line?, comment_line?, 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, tokens

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path

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:, indent_steps: 1, configured_width:) ⇒ 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:, indent_steps: 1,
                      configured_width:)
  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 = end_of_line_comment(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.source_range,
                          text + "\n" + (' ' * node.loc.keyword.column))
  corrector.remove(eol_comment.loc.expression)
end