Class: RuboCop::Cop::Style::RbsInline::InvalidComment
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::RbsInline::InvalidComment
- 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.
Constant Summary collapse
- MSG =
'Invalid RBS annotation comment found.'- RBS_INLINE_KEYWORDS =
%w[inherits override use module-self generic skip module class].freeze
- RBS_INLINE_KEYWORD_PATTERN =
: Array
RBS_INLINE_KEYWORDS.join('|')
- SIGNATURE_PATTERN =
'\(.*\)\s*(\??\s*{.*?}\s*)?->\s*.*'- MISSING_HASH_COLON =
/\A#\s+#{SIGNATURE_PATTERN}/- SPACE_BEFORE_COLON =
/\A#\s+:\s*#{SIGNATURE_PATTERN}/- MIXED_ANNOTATION =
/\A#:\s+@rbs\s+/- MISSING_AT_SIGN =
/\A#\s*rbs\s+(#{RBS_INLINE_KEYWORD_PATTERN}|\S+:|%a\{.*\}|#{SIGNATURE_PATTERN})/
Instance Method Summary collapse
- #check_comment(comment) ⇒ void
- #consume_embedded_rbs(comments) ⇒ Array[Parser::Source::Comment]
- #corrected_text(text) ⇒ String?
-
#on_new_investigation ⇒ void
: void.
Instance Method Details
#check_comment(comment) ⇒ void
This method returns an undefined value.
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]
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 (comments) #: Array[Parser::Source::Comment] = false indent = 1 line = 0 comments.reject do |comment| if (match = comment.text.match(/\A#(\s+)@rbs!(\s+|\Z)/)) = true indent = match[1].to_s.size line = comment.loc.line true elsif && comment.loc.line == line + 1 && comment.text.match?(/\A#(\s{#{indent + 1},}.*|\s*)\Z/) line += 1 true else = false false end end end |
#corrected_text(text) ⇒ String?
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_investigation ⇒ void
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 (processed_source.comments).each do |comment| check_comment(comment) end end |