123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::LineBreakCorrector

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Inherits: Object
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.

Class Attribute Summary

Class Method Summary

Util - Extended

add_parentheses

Metrics/MethodLength.

any_descendant?

Metrics/MethodLength.

args_begin, args_end, begins_its_line?,
comment_line?

This is a bad API.

comment_lines?, compatible_external_encoding_for?,
double_quotes_required?

If converting a string to Ruby string literal source code, must double quotes be used?

escape_string,
first_part_of_call_chain

Returns, for example, a bare if node if the given node is an if with calls chained to the end of it.

include_or_equal?, indent, interpret_string_escapes, line, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interpolation_escape_character

::RuboCop::PathUtil - Included

absolute?

Returns true for an absolute Unix or Windows path.

glob?

Returns true for a glob.

hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?,
match_path?

Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.

maybe_hidden_file?

Loose check to reduce memory allocations.

relative_path, smart_path

TrailingBody - Extended

Alignment - Extended

Class Attribute Details

.processed_source (readonly)

[ GitHub ]

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

attr_reader :processed_source

Class Method Details

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

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 27

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:)

[ GitHub ]

  
# 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:)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 35

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

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

.remove_semicolon(node, corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 45

def remove_semicolon(node, corrector)
  return unless semicolon(node)

  corrector.remove(semicolon(node).pos)
end

.semicolon(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 51

def semicolon(node)
  @semicolon ||= {}.compare_by_identity
  @semicolon[node] ||= processed_source.sorted_tokens.select(&:semicolon?).find do |token|
    next if token.pos.end_pos <= node.source_range.begin_pos

    same_line?(token, node.body) && trailing_class_definition?(token, node.body)
  end
end

.trailing_class_definition?(token, body) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/line_break_corrector.rb', line 60

def trailing_class_definition?(token, body)
  token.column < body.loc.column
end