123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::AnnotationComment

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
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 13

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 19

def annotation?
  keyword_appearance? && !just_keyword_of_sentence?
end

#colon (readonly)

[ GitHub ]

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

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

#comment (readonly)

[ GitHub ]

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

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 67

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

#keyword (readonly)

[ GitHub ]

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

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

#keyword_appearance?Boolean (readonly, private)

[ GitHub ]

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

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

#keywords (readonly, private)

[ GitHub ]

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

attr_reader :keywords

#margin (readonly)

[ GitHub ]

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

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

#note (readonly)

[ GitHub ]

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

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

#space (readonly)

[ GitHub ]

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

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 31

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 23

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 53

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 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.
  match = comment.text.match(regex)
  return false unless match

  match.captures
end