Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::AutocorrectAlignment

This module 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.

Public Instance Methods

autocorrect(arg) click to toggle source
# File lib/rubocop/cop/mixin/autocorrect_alignment.rb, line 24
def autocorrect(arg)
  heredoc_ranges = heredoc_ranges(arg)
  expr = arg.respond_to?(:loc) ? arg.loc.expression : arg

  # We can't use the instance variable inside the lambda. That would
  # just give each lambda the same reference and they would all get
  # the last value of @column_delta. A local variable fixes the
  # problem.
  column_delta = @column_delta

  fail CorrectionNotPossible if block_comment_within?(expr)

  @corrections << lambda do |corrector|
    each_line(expr) do |line_begin_pos|
      range = calculate_range(expr, line_begin_pos, column_delta)
      # We must not change indentation of heredoc stings.
      next if heredoc_ranges.any? { |h| within?(range, h) }

      if column_delta > 0
        unless range.source == "\n"
          corrector.insert_before(range, ' ' * column_delta)
        end
      else
        remove(range, corrector) if range.source =~ /^[ \t]+$/
      end
    end
  end
end
check_alignment(items, base_column = nil) click to toggle source
# File lib/rubocop/cop/mixin/autocorrect_alignment.rb, line 8
def check_alignment(items, base_column = nil)
  base_column ||= items.first.loc.column unless items.empty?
  prev_line = -1
  items.each do |current|
    if current.loc.line > prev_line && start_of_line?(current.loc)
      @column_delta = base_column - current.loc.column
      add_offense(current, :expression) if @column_delta != 0
    end
    prev_line = current.loc.line
  end
end
start_of_line?(loc) click to toggle source
# File lib/rubocop/cop/mixin/autocorrect_alignment.rb, line 20
def start_of_line?(loc)
  loc.expression.source_line[0...loc.column] =~ /^\s*$/
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.