Class: RuboCop::Cop::AnnotationComment

Inherits:
Object
  • Object
show all
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>)


11
12
13
14
15
# 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

#colonObject (readonly)

Returns the value of attribute colon.



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

def colon
  @colon
end

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#keywordObject (readonly)

Returns the value of attribute keyword.



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

def keyword
  @keyword
end

#marginObject (readonly)

Returns the value of attribute margin.



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

def margin
  @margin
end

#noteObject (readonly)

Returns the value of attribute note.



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

def note
  @note
end

#spaceObject (readonly)

Returns the value of attribute space.



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

def space
  @space
end

Instance Method Details

#annotation?Boolean

Returns:

  • (Boolean)


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

def annotation?
  keyword_appearance? && !just_keyword_of_sentence?
end

#boundsObject

Returns the range bounds for just the annotation



29
30
31
32
33
# 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

Returns:

  • (Boolean)


21
22
23
24
25
26
# 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