Class: RuboCop::Cop::Style::RbsInline::RedundantInstanceVariableAnnotation

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

Overview

Checks for redundant ‘@rbs` instance variable type annotation when `attr_*` with an inline type annotation is already defined.

When ‘attr_reader :foo #: Integer` is defined, a separate `# @rbs @foo: Integer` declaration for the instance variable is redundant. RBS::Inline automatically generates the instance variable type from the inline annotation on `attr_*`.

Examples:

# bad
# @rbs @foo: Integer

attr_reader :foo #: Integer

# good
attr_reader :foo #: Integer

# good - no inline annotation, so ivar annotation is not redundant
# @rbs @foo: Integer

attr_reader :foo

Constant Summary collapse

MSG =
'Redundant instance variable type annotation. `attr_*` already declares the type inline.'
ATTRIBUTE_METHODS =

: Array

%i[attr_reader attr_writer attr_accessor].freeze

Instance Attribute Summary collapse

Attributes included from CommentParser

#parsed_comments

Instance Method Summary collapse

Methods included from SourceCodeHelper

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

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 ASTUtils

#end_line, #name_location, #source!, #value_to_sym

Instance Attribute Details

#attributes_scope_stackObject (readonly)



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

def attributes_scope_stack
  @attributes_scope_stack
end

#ivar_annotationsObject (readonly)

: Hash[Integer, RBS::Inline::AST::Annotations::IvarType]



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

def ivar_annotations
  @ivar_annotations
end

Instance Method Details

#after_class(node) ⇒ Object Also known as: after_module



65
66
67
68
69
70
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 65

def after_class(node) #: void
  start_line = node.location.line
  end_line = end_line(node, default: start_line)
  attributes = pop_attributes_scope
  check_offenses(start_line, end_line, attributes)
end

#on_class(_node) ⇒ Object Also known as: on_module



58
59
60
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 58

def on_class(_node) #: void
  push_attributes_scope
end

#on_investigation_endObject

: void



51
52
53
54
55
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 51

def on_investigation_end #: void
  attributes = pop_attributes_scope
  check_offenses(0, Float::INFINITY, attributes)
  super
end

#on_new_investigationObject

: void



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

def on_new_investigation #: void
  super
  parse_comments
  @attributes_scope_stack = []
  @ivar_annotations = collect_ivar_annotations
  push_attributes_scope
end

#on_send(node) ⇒ Object



75
76
77
78
79
80
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 75

def on_send(node) #: void
  return unless node.receiver.nil?
  return unless ATTRIBUTE_METHODS.include?(node.method_name)

  register_attribute_method(node)
end