class RuboCop::Cop::Style::CommentAnnotation

This cop checks that comment annotation keywords are written according to guidelines.

Constants

MISSING_NOTE
MSG

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubocop/cop/style/comment_annotation.rb, line 17
def investigate(processed_source)
  processed_source.comments.each_with_index do |comment, ix|
    next unless first_comment_line?(processed_source.comments, ix)

    margin, first_word, colon, space, note = split_comment(comment)
    next unless annotation?(comment) &&
                !correct_annotation?(first_word, colon, space, note)

    length = first_word.length + colon.to_s.length + space.to_s.length
    add_offense(comment, annotation_range(comment, margin, length),
                format(note ? MSG : MISSING_NOTE, first_word))
  end
end

Private Instance Methods

annotation_range(comment, margin, length) click to toggle source
# File lib/rubocop/cop/style/comment_annotation.rb, line 37
def annotation_range(comment, margin, length)
  start = comment.loc.expression.begin_pos + margin.length
  source_buffer = comment.loc.expression.source_buffer
  Parser::Source::Range.new(source_buffer, start, start + length)
end
autocorrect(comment) click to toggle source
# File lib/rubocop/cop/style/comment_annotation.rb, line 43
def autocorrect(comment)
  margin, first_word, colon, space, note = split_comment(comment)
  start = comment.loc.expression.begin_pos + margin.length
  return if note.nil?

  lambda do |corrector|
    length = first_word.length + colon.to_s.length + space.to_s.length
    range = Parser::Source::Range.new(comment.loc.expression.source,
                                      start,
                                      start + length)
    corrector.replace(range, "#{first_word.upcase}: ")
  end
end
correct_annotation?(first_word, colon, space, note) click to toggle source
# File lib/rubocop/cop/style/comment_annotation.rb, line 57
def correct_annotation?(first_word, colon, space, note)
  keyword?(first_word) && (colon && space && note || !colon && !note)
end
first_comment_line?(comments, ix) click to toggle source
# File lib/rubocop/cop/style/comment_annotation.rb, line 33
def first_comment_line?(comments, ix)
  ix == 0 || comments[ix - 1].loc.line < comments[ix].loc.line - 1
end