class RuboCop::Cop::AnnotationComment
Representation of an annotation comment in source code (eg. ‘# TODO: blah blah blah`).
Constants
- KEYWORDS_REGEX_CACHE
Attributes
colon[R]
comment[R]
keyword[R]
keywords[R]
margin[R]
note[R]
space[R]
Public Class Methods
new(comment, keywords)
click to toggle source
@param [Parser::Source::Comment] comment @param [Array<String>] keywords
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 11 def initialize(comment, keywords) @comment = comment @keywords = keywords @margin, @keyword, @colon, @space, @note = split_comment(comment) end
Public Instance Methods
annotation?()
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 17 def annotation? keyword_appearance? && !just_keyword_of_sentence? end
bounds()
click to toggle source
Returns the range bounds for just the annotation
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 29 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
correct?(colon:)
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 21 def correct?(colon:) return false unless keyword && space && note return false unless keyword == keyword.upcase self.colon.nil? == !colon end
Private Instance Methods
just_keyword_of_sentence?()
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 65 def just_keyword_of_sentence? keyword == keyword.capitalize && !colon && space && note end
keyword_appearance?()
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 61 def keyword_appearance? keyword && (colon || space) end
regex()
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 51 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
split_comment(comment)
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 39 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