Class: RuboCop::Cop::AlignmentCorrector

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

Overview

This class does auto-correction of nodes that should just be moved to the left or to the right, amount being determined by the instance variable column_delta.

Constant Summary

Constants included from Util

Util::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::CONDITIONAL_NODES, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::LOGICAL_OPERATOR_NODES, Util::MODIFIER_NODES, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Util

comment_line?, double_quotes_required?, effective_column, ends_its_line?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, operator?, parentheses?, parenthesized_call?, precede?, range_between, range_by_whole_lines, range_with_surrounding_comma, range_with_surrounding_space, same_line?, source_range, strip_quotes, stripped_source_upto, symbol_without_quote?, to_string_literal, to_supported_styles, to_symbol_literal, within_node?

Methods included from AST::Sexp

#s

Methods included from PathUtil

absolute?, find_file_upwards, 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/alignment_corrector.rb', line 13

def processed_source
  @processed_source
end

Class Method Details

.align_end(processed_source, node, align_to) ⇒ Object



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

def align_end(processed_source, node, align_to)
  @processed_source = processed_source
  whitespace = whitespace_range(node)
  return false unless whitespace.source.strip.empty?

  column = alignment_column(align_to)
  ->(corrector) { corrector.replace(whitespace, ' ' * column) }
end

.correct(processed_source, node, column_delta) ⇒ Object



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

def correct(processed_source, node, column_delta)
  return unless node
  @processed_source = processed_source
  expr = node.respond_to?(:loc) ? node.loc.expression : node
  return if block_comment_within?(expr)

  lambda do |corrector|
    each_line(expr) do |line_begin_pos|
      autocorrect_line(corrector, line_begin_pos, expr, column_delta,
                       heredoc_ranges(node))
    end
  end
end