Class: RuboCop::Cop::Style::RbsInline::EmbeddedRbsSpacing

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

Overview

Checks that @rbs! comments (embedded RBS) are followed by a blank line.

RBS::Inline requires @rbs! comments to be standalone comments, meaning they should not have any code immediately following them. A blank line after the @rbs! block ensures proper separation.

Examples:

# bad
# @rbs! type foo = Integer
def method
end

# good
# @rbs! type foo = Integer

def method
end

Constant Summary collapse

MSG =

Returns:

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

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

#block_end_line?(line_number) ⇒ Boolean

Parameters:

  • line_number (Integer)

Returns:

  • (Boolean)


65
66
67
# File 'lib/rubocop/cop/style/rbs_inline/embedded_rbs_spacing.rb', line 65

def block_end_line?(line_number) #: bool
  source_code_at(line_number).strip.match?(/\Aend\b/)
end

#check_embedded_annotation(annotation) ⇒ void

This method returns an undefined value.

Parameters:

  • annotation (RBS::Inline::AST::Annotations::Embedded)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/style/rbs_inline/embedded_rbs_spacing.rb', line 52

def check_embedded_annotation(annotation) #: void
  last_comment = annotation.source.comments.last or return
  next_line_number = last_comment.location.start_line + 1

  return if blank_line?(next_line_number)
  return if block_end_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_embedded_rbs_spacingvoid

This method returns an undefined value.

: void



41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/style/rbs_inline/embedded_rbs_spacing.rb', line 41

def check_embedded_rbs_spacing #: void
  parse_comments.each do |result|
    result.each_annotation do |annotation|
      next unless annotation.is_a?(RBS::Inline::AST::Annotations::Embedded)

      check_embedded_annotation(annotation)
    end
  end
end

#on_new_investigationvoid

This method returns an undefined value.

: void



35
36
37
# File 'lib/rubocop/cop/style/rbs_inline/embedded_rbs_spacing.rb', line 35

def on_new_investigation #: void
  check_embedded_rbs_spacing
end