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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/rbs_inline/invalid_comment.rb,
sig/rubocop/cop/style/rbs_inline/invalid_comment.rbs

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 =

Returns:

  • (::String)
'Invalid RBS annotation comment found.'
RBS_INLINE_KEYWORDS =

Returns:

  • (Array[String])
%w[inherits override use module-self generic skip module class].freeze
RBS_INLINE_KEYWORD_PATTERN =

: Array

Returns:

  • (String)
RBS_INLINE_KEYWORDS.join('|')
SIGNATURE_PATTERN =

Returns:

  • (::String)
'\(.*\)\s*(\??\s*{.*?}\s*)?->\s*.*'
MISSING_HASH_COLON =

Returns:

  • (::Regexp)
/\A#\s+#{SIGNATURE_PATTERN}/
SPACE_BEFORE_COLON =

Returns:

  • (::Regexp)
/\A#\s+:\s*#{SIGNATURE_PATTERN}/
MIXED_ANNOTATION =

Returns:

  • (::Regexp)
/\A#:\s+@rbs\s+/
MISSING_AT_SIGN =

Returns:

  • (::Regexp)
/\A#\s*rbs\s+(#{RBS_INLINE_KEYWORD_PATTERN}|\S+:|%a\{.*\}|#{SIGNATURE_PATTERN})/

Instance Method Summary collapse

Instance Method Details

#check_comment(comment) ⇒ void

This method returns an undefined value.

Parameters:



47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/style/rbs_inline/invalid_comment.rb', line 47

def check_comment(comment) #: void
  corrected = corrected_text(comment.text)
  return unless corrected

  add_offense(comment) do |corrector|
    corrector.replace(comment.source_range, corrected)
  end
end

#consume_embedded_rbs(comments) ⇒ Array[Parser::Source::Comment]

Parameters:

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/style/rbs_inline/invalid_comment.rb', line 67

def consume_embedded_rbs(comments) #: Array[Parser::Source::Comment]
  in_embedded = false
  indent = 1
  line = 0

  comments.reject do |comment|
    if (match = comment.text.match(/\A#(\s+)@rbs!(\s+|\Z)/))
      in_embedded = true
      indent = match[1].to_s.size
      line = comment.loc.line
      true
    elsif in_embedded && comment.loc.line == line + 1 &&
          comment.text.match?(/\A#(\s{#{indent + 1},}.*|\s*)\Z/)
      line += 1
      true
    else
      in_embedded = false
      false
    end
  end
end

#corrected_text(text) ⇒ String?

Parameters:

  • text (String)

Returns:

  • (String, nil)


57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/style/rbs_inline/invalid_comment.rb', line 57

def corrected_text(text) #: String?
  case text
  when MISSING_HASH_COLON then text.sub(/\A#\s+/, '#: ')
  when SPACE_BEFORE_COLON then text.sub(/\A#\s+:\s*/, '#: ')
  when MIXED_ANNOTATION   then text.sub(/\A#:\s+/, '# ')
  when MISSING_AT_SIGN    then text.sub(/\A#\s*rbs\s+/, '# @rbs ')
  end
end

#on_new_investigationvoid

This method returns an undefined value.

: void



38
39
40
41
42
# File 'lib/rubocop/cop/style/rbs_inline/invalid_comment.rb', line 38

def on_new_investigation #: void
  consume_embedded_rbs(processed_source.comments).each do |comment|
    check_comment(comment)
  end
end