Class: RuboCop::Cop::AnnotationComment

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubocop/cop/mixin/annotation_comment.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment, keywords) ⇒ AnnotationComment

Returns a new instance of AnnotationComment.

Parameters:

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


13
14
15
16
17
# 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

#colonObject (readonly)

Returns the value of attribute colon.



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

def colon
  @colon
end

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#keywordObject (readonly)

Returns the value of attribute keyword.



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

def keyword
  @keyword
end

#marginObject (readonly)

Returns the value of attribute margin.



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

def margin
  @margin
end

#noteObject (readonly)

Returns the value of attribute note.



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

def note
  @note
end

#spaceObject (readonly)

Returns the value of attribute space.



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

def space
  @space
end

Instance Method Details

#annotation?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rubocop/cop/mixin/annotation_comment.rb', line 19

def annotation?
  keyword_appearance? && !just_keyword_of_sentence?
end

#boundsObject

Returns the range bounds for just the annotation



31
32
33
34
35
# 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

Returns:

  • (Boolean)


23
24
25
26
27
28
# 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