class RuboCop::Cop::AnnotationComment
Representation of an annotation comment in source code (eg. `# TODO: blah blah blah`).
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 13 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 19 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 31 def bounds start = comment.loc.expression.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 23 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 60 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 56 def keyword_appearance? keyword && (colon || space) end
split_comment(comment)
click to toggle source
# File lib/rubocop/cop/mixin/annotation_comment.rb, line 41 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. keywords_regex = Regexp.new( Regexp.union(keywords.sort_by { |w| -w.length }).source, Regexp::IGNORECASE ) regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i match = comment.text.match(regex) return false unless match match.captures end