Class: RuboCop::Cop::Style::RbsInline::VariableCommentSpacing

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

Overview

Checks that @rbs variable comments are followed by a blank line.

RBS::Inline requires @rbs comments for instance variables, class variables, and class instance variables to be standalone comments, meaning they should not have any code immediately following them. A blank line after the variable comment ensures proper separation.

Examples:

# bad
# @rbs @ivar: Integer
# @rbs @@cvar: Float
# @rbs self.@civar: String
def method
end

# good
# @rbs @ivar: Integer
# @rbs @@cvar: Float
# @rbs self.@civar: String

def method
end

Constant Summary collapse

MSG =

Returns:

  • (::String)
'`@rbs` variable comment must be followed by a blank line.'
VARIABLE_COMMENT_PATTERN =

Returns:

  • (::Regexp)
/\A#\s+@rbs\s+(?:self\.)?@@?[a-zA-Z_]/

Instance Attribute Summary

Attributes included from CommentParser

#parsed_comments

Instance Method Summary collapse

Methods included from CommentParser

#find_doc_style_param_annotations, #find_doc_style_return_annotation, #find_last_consecutive_comment, #find_leading_annotation, #find_method_type_signature_comments, #find_trailing_comment, #overload_type_signatures?, #parse_comments

Methods included from SourceCodeHelper

#annotation_range, #blank_line?, #char_at, #character_offset, #comment_at, #comment_range, #line_range, #location_to_range, #source_code_at

Instance Method Details

#check_variable_comment(comment) ⇒ void

This method returns an undefined value.

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/style/rbs_inline/variable_comment_spacing.rb', line 54

def check_variable_comment(comment) #: void
  return unless comment.text.match?(VARIABLE_COMMENT_PATTERN)

  last_comment_line = find_last_consecutive_comment(comment) { |c| c.text.match?(VARIABLE_COMMENT_PATTERN) }
  next_line_number = last_comment_line.loc.line + 1

  return if blank_line?(next_line_number)

  add_offense(line_range(next_line_number)) do |corrector|
    corrector.insert_before(line_range(next_line_number), "\n")
  end
end

#check_variable_comment_spacingvoid

This method returns an undefined value.

: void



47
48
49
50
51
# File 'lib/rubocop/cop/style/rbs_inline/variable_comment_spacing.rb', line 47

def check_variable_comment_spacing #: void
  processed_source.comments.each do |comment|
    check_variable_comment(comment)
  end
end

#on_new_investigationvoid

This method returns an undefined value.

: void



41
42
43
# File 'lib/rubocop/cop/style/rbs_inline/variable_comment_spacing.rb', line 41

def on_new_investigation #: void
  check_variable_comment_spacing
end