Class: RuboCop::Cop::Style::RbsInline::InvalidComment

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/rbs_inline/invalid_comment.rb

Overview

IRB::Inline expects annotations comments to start with ‘#:` or `# @rbs`. This cop checks for comments that do not match the expected pattern.

Examples:

# bad
# () -> void
# : () -> void
# rbs param: String

# good
#: () -> void
# @rbs param: String

Constant Summary collapse

MSG =
'Invalid RBS annotation comment found.'
ANNOTATION_KEYWORDS =
%w[return inherits override use module-self generic in out
unchecked self skip yields module class].freeze
SIGNATURE_PATTERN =

: Array

'\(.*\)\s*(\??\s*{.*?}\s*)?->\s*.*'
RBS_INLINE_KEYWORDS =
%w[inherits override use module-self generic skip module class].freeze

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject

: Array



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/style/rbs_inline/invalid_comment.rb', line 32

def on_new_investigation #: void
  comments = consume_embedded_rbs(processed_source.comments)
  comments.each do |comment|
    add_offense(comment) if comment.text =~ /\A#\s+#{SIGNATURE_PATTERN}/
    add_offense(comment) if comment.text =~ /\A#\s+:\s*#{SIGNATURE_PATTERN}/

    add_offense(comment) if comment.text =~ /\A#:\s+@rbs\s+/
    if comment.text =~ /\A#\s*rbs\s+(#{RBS_INLINE_KEYWORDS.join('|')}|\S+:|%a{.*}|#{SIGNATURE_PATTERN})/
      add_offense(comment)
    end
  end
end