Class: Rubocop::Cop::Style::CommentAnnotation

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/comment_annotation.rb

Overview

This cop checks that comment annotation keywords are written according to guidelines.

Constant Summary collapse

MSG =
'Annotation keywords shall be all upper case, followed by a ' +
'colon and a space, then a note describing the problem.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Class Method Details

.keywordsObject



30
31
32
# File 'lib/rubocop/cop/style/comment_annotation.rb', line 30

def self.keywords
  CommentAnnotation.config['Keywords']
end

Instance Method Details

#investigate(processed_source) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/style/comment_annotation.rb', line 12

def investigate(processed_source)
  processed_source.comments.each do |comment|
    match = comment.text.match(/^(# ?)([A-Za-z]+)(\s*:)?(\s+)?(\S+)?/)
    if match
      margin, first_word, colon, space, note = *match.captures
      if annotation?(first_word, colon, space, note) &&
          !correct_annotation?(first_word, colon, space, note)
        start = comment.loc.expression.begin_pos + margin.length
        length = first_word.length + (colon || '').length
        range = Parser::Source::Range.new(processed_source.buffer,
                                          start,
                                          start + length)
        add_offence(:convention, range, MSG)
      end
    end
  end
end