Class: RuboCop::Cop::Style::RbsInline::MethodCommentSpacing

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

Overview

Checks that method-related ‘@rbs` annotations are placed immediately before method definitions.

Method-related annotations (‘@rbs param`, `@rbs return`, `@rbs &block`, `@rbs override`, `@rbs skip`, `@rbs %a…`, `# @rbs (…) -> Type`, `#: (…) -> Type`) must be placed directly before the method definition they describe, without any blank lines in between. These annotations should not appear in standalone locations without an immediately following method definition.

Examples:

# bad
# @rbs param x: Integer
# @rbs return: String

def method(x)
end

# bad
#: (Integer) -> String

def method(x)
end

# bad
# @rbs param x: Integer
# @rbs return: String
puts "something"

# good
# @rbs param x: Integer
# @rbs return: String
def method(x)
end

# good
#: (Integer) -> String
def method(x)
end

# good
# @rbs x: Integer
private_class_method def self.method(x)
end

# good
# @rbs x: Integer
private def method(x)
end

Constant Summary collapse

MSG =
'Method-related `@rbs` annotation must be immediately before a method definition.'
MSG_WITH_BLANK_LINE =
'Remove blank line between method annotation and method definition.'
METHOD_DEFINITION_PATTERN =
/\A(?:(?:private|protected|public|private_class_method|module_function)\s+)?def\s/

Instance Attribute Summary collapse

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 Attribute Details

#processed_commentsObject (readonly)

: Array



69
70
71
# File 'lib/rubocop/cop/style/rbs_inline/method_comment_spacing.rb', line 69

def processed_comments
  @processed_comments
end

Instance Method Details

#on_new_investigationObject

: void



71
72
73
74
75
# File 'lib/rubocop/cop/style/rbs_inline/method_comment_spacing.rb', line 71

def on_new_investigation #: void
  @processed_comments = []
  parse_comments
  check_method_comment_spacing
end