class RuboCop::Cop::AnnotationComment

Representation of an annotation comment in source code (eg. ‘# TODO: blah blah blah`).

def annotation?

def annotation?
  keyword_appearance? && !just_keyword_of_sentence?
end

def bounds

Returns the range bounds for just the annotation
def bounds
  start = comment.source_range.begin_pos + margin.length
  length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
  [start, start + length]
end

def correct?(colon:)

def correct?(colon:)
  return false unless keyword && space && note
  return false unless keyword == keyword.upcase
  self.colon.nil? == !colon
end

def initialize(comment, keywords)

Parameters:
  • keywords (Array) --
  • comment (Parser::Source::Comment) --
def initialize(comment, keywords)
  @comment = comment
  @keywords = keywords
  @margin, @keyword, @colon, @space, @note = split_comment(comment)
end

def just_keyword_of_sentence?

def just_keyword_of_sentence?
  keyword == keyword.capitalize && !colon && space && note
end

def keyword_appearance?

def keyword_appearance?
  keyword && (colon || space)
end

def regex

def regex
  KEYWORDS_REGEX_CACHE[keywords] ||= begin
    keywords_regex = Regexp.new(
      Regexp.union(keywords.sort_by { |w| -w.length }).source,
      Regexp::IGNORECASE
    )
    /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
  end
end

def split_comment(comment)

def split_comment(comment)
  # Sort keywords by reverse length so that if a keyword is in a phrase
  # but also on its own, both will match properly.
  match = comment.text.match(regex)
  return false unless match
  match.captures
end