Class: RuboCop::Cop::Style::RbsInline::RedundantInstanceVariableAnnotation
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::RbsInline::RedundantInstanceVariableAnnotation
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, ASTUtils, CommentParser, FileFilter, SourceCodeHelper
- Defined in:
- lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb,
sig/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rbs
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_*.
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
Constants included from FileFilter
FileFilter::MAGIC_COMMENT_DISABLED, FileFilter::MAGIC_COMMENT_ENABLED, FileFilter::SUPPORTED_MODES
Instance Attribute Summary collapse
-
#attributes_scope_stack ⇒ Array[Set[Symbol]]
readonly
: Array[Set[Symbol]].
-
#ivar_annotations ⇒ Hash[Integer, RBS::Inline::AST::Annotations::IvarType]
readonly
: Hash[Integer, RBS::Inline::AST::Annotations::IvarType].
Attributes included from CommentParser
Instance Method Summary collapse
- #add_ivar_offense(annotation) ⇒ void
- #after_class(node) ⇒ void (also: #after_module)
- #attribute_names_from(node) ⇒ Array[Symbol]
- #check_offenses(start_line, end_line, attributes) ⇒ void
-
#collect_ivar_annotations ⇒ Hash[Integer, RBS::Inline::AST::Annotations::IvarType]
: Hash[Integer, RBS::Inline::AST::Annotations::IvarType].
-
#current_attributes_scope ⇒ Set[Symbol]
: Set.
- #extract_ivar_annotations(start_line, end_line) ⇒ Array[RBS::Inline::AST::Annotations::IvarType]
- #on_class(_node) ⇒ void (also: #on_module)
-
#on_investigation_end ⇒ void
: void.
-
#on_new_investigation ⇒ void
: void.
- #on_send(node) ⇒ void
-
#pop_attributes_scope ⇒ Set[Symbol]
: Set.
-
#push_attributes_scope ⇒ void
: void.
- #register_attribute_method(node) ⇒ void
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, #rbs_inline_file_skipped?
Methods included from ASTUtils
#args_node_for, #end_line, #method_parameter_list_end_line, #name_location, #source!, #value_to_sym
Methods included from FileFilter
#add_offense, #configured_mode, #rbs_inline_disabled?, #rbs_inline_enabled?, #rbs_inline_file_skipped?, #skip_by_mode?, warn_invalid_mode
Instance Attribute Details
#attributes_scope_stack ⇒ Array[Set[Symbol]] (readonly)
: Array[Set[Symbol]]
41 42 43 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 41 def attributes_scope_stack @attributes_scope_stack end |
#ivar_annotations ⇒ Hash[Integer, RBS::Inline::AST::Annotations::IvarType] (readonly)
: Hash[Integer, RBS::Inline::AST::Annotations::IvarType]
42 43 44 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 42 def ivar_annotations @ivar_annotations end |
Instance Method Details
#add_ivar_offense(annotation) ⇒ void
This method returns an undefined value.
142 143 144 145 146 147 148 149 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 142 def add_ivar_offense(annotation) #: void range = annotation_range(annotation) or return add_offense(range, message: MSG) do |corrector| remove_range = range_by_whole_lines(range, include_final_newline: true) remove_range = remove_range.adjust(end_pos: 1) if char_at(remove_range.end_pos) == "\n" corrector.remove(remove_range) end end |
#after_class(node) ⇒ void Also known as: after_module
This method returns an undefined value.
66 67 68 69 70 71 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 66 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 |
#attribute_names_from(node) ⇒ Array[Symbol]
152 153 154 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 152 def attribute_names_from(node) #: Array[Symbol] node.arguments.filter_map { value_to_sym(_1) } end |
#check_offenses(start_line, end_line, attributes) ⇒ void
This method returns an undefined value.
135 136 137 138 139 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 135 def check_offenses(start_line, end_line, attributes) #: void extract_ivar_annotations(start_line, end_line).each do |annotation| add_ivar_offense(annotation) if attributes.include?(annotation.name) end end |
#collect_ivar_annotations ⇒ Hash[Integer, RBS::Inline::AST::Annotations::IvarType]
: Hash[Integer, RBS::Inline::AST::Annotations::IvarType]
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 97 def collect_ivar_annotations #: Hash[Integer, RBS::Inline::AST::Annotations::IvarType] result = {} #: Hash[Integer, RBS::Inline::AST::Annotations::IvarType] parsed_comments.each do |r| r.each_annotation do |annotation| next unless annotation.is_a?(RBS::Inline::AST::Annotations::IvarType) line = annotation.source.comments.first&.location&.start_line || 0 result[line] = annotation end end result end |
#current_attributes_scope ⇒ Set[Symbol]
: Set
93 94 95 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 93 def current_attributes_scope #: Set[Symbol] attributes_scope_stack.last || raise end |
#extract_ivar_annotations(start_line, end_line) ⇒ Array[RBS::Inline::AST::Annotations::IvarType]
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 112 def extract_ivar_annotations(start_line, end_line) #: Array[RBS::Inline::AST::Annotations::IvarType] extracted = [] #: Array[RBS::Inline::AST::Annotations::IvarType] ivar_annotations.reject! do |line, annotation| next false unless line.between?(start_line, end_line) extracted << annotation true end extracted end |
#on_class(_node) ⇒ void Also known as: on_module
This method returns an undefined value.
59 60 61 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 59 def on_class(_node) #: void push_attributes_scope end |
#on_investigation_end ⇒ void
This method returns an undefined value.
: void
52 53 54 55 56 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 52 def on_investigation_end #: void attributes = pop_attributes_scope check_offenses(0, Float::INFINITY, attributes) super end |
#on_new_investigation ⇒ void
This method returns an undefined value.
: void
44 45 46 47 48 49 50 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 44 def on_new_investigation #: void super parse_comments @attributes_scope_stack = [] @ivar_annotations = collect_ivar_annotations push_attributes_scope end |
#on_send(node) ⇒ void
This method returns an undefined value.
76 77 78 79 80 81 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 76 def on_send(node) #: void return unless node.receiver.nil? return unless ATTRIBUTE_METHODS.include?(node.method_name) register_attribute_method(node) end |
#pop_attributes_scope ⇒ Set[Symbol]
: Set
89 90 91 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 89 def pop_attributes_scope #: Set[Symbol] attributes_scope_stack.pop || raise end |
#push_attributes_scope ⇒ void
This method returns an undefined value.
: void
85 86 87 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 85 def push_attributes_scope #: void attributes_scope_stack.push(Set.new) end |
#register_attribute_method(node) ⇒ void
This method returns an undefined value.
124 125 126 127 128 129 130 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_instance_variable_annotation.rb', line 124 def register_attribute_method(node) #: void return unless find_trailing_comment(node.loc.line) attribute_names_from(node).each do |name| current_attributes_scope.add(:"@#{name}") end end |