123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::AnnotationComment

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/cop/mixin/annotation_comment.rb

Overview

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

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(comment, keywords) ⇒ AnnotationComment

Parameters:

  • comment (Parser::Source::Comment)
  • keywords (Array<String>)
[ GitHub ]

  
# 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

Instance Attribute Details

#annotation?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 17

def annotation?
  keyword_appearance? && !just_keyword_of_sentence?
end

#colon (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

#comment (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

#just_keyword_of_sentence?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 65

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

#keyword (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

#keyword_appearance?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 61

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

#keywords (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 37

attr_reader :keywords

#margin (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

#note (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

#space (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 7

attr_reader :comment, :margin, :keyword, :colon, :space, :note

Instance Method Details

#bounds

Returns the range bounds for just the annotation

[ GitHub ]

  
# 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:) ⇒ Boolean

[ GitHub ]

  
# 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

#regex (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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