Parent

Methods

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::CommentIndentation

This cops checks the indentation of comments.

Constants

MSG

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubocop/cop/style/comment_indentation.rb, line 11
def investigate(processed_source)
  processed_source.comments.each do |comment|
    lines = processed_source.lines
    own_line = lines[comment.loc.line - 1]
    next unless own_line =~ /\A\s*#/

    next_line =
      lines[comment.loc.line..-1].find { |line| !line.blank? }

    correct_comment_indentation = correct_indentation(next_line)
    column = comment.loc.column

    @column_delta = correct_comment_indentation - column
    next if @column_delta == 0

    if two_alternatives?(next_line)
      correct_comment_indentation +=
        IndentationWidth::CORRECT_INDENTATION # Try the other
      # We keep @column_delta unchanged so that autocorrect changes to
      # the preferred style of aligning the comment with the keyword.
    end

    next if column == correct_comment_indentation
    add_offense(comment, comment.loc.expression,
                format(MSG, column, correct_comment_indentation))
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.