Class: RuboCop::Cop::Style::RbsInline::RedundantAnnotationWithSkip
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::RbsInline::RedundantAnnotationWithSkip
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, CommentParser, SourceCodeHelper
- Defined in:
- lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb,
sig/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rbs
Overview
Checks for redundant type annotations when @rbs skip or @rbs override is present.
@rbs skip tells RBS::Inline to skip RBS generation for this method entirely.
@rbs override tells RBS::Inline to inherit the type signature from the parent class.
In both cases, any additional type annotations (method type signatures, parameter
annotations, return type annotations) are redundant and will be ignored by RBS::Inline.
Constant Summary collapse
- MSG_METHOD_TYPE_SIGNATURE =
'Redundant method type signature. ' \ '`@rbs skip` and `@rbs override` skip RBS generation.'
- MSG_DOC_STYLE_ANNOTATION =
'Redundant `@rbs` annotation. ' \ '`@rbs skip` and `@rbs override` skip RBS generation.'
- MSG_TRAILING_RETURN =
'Redundant trailing return type annotation. ' \ '`@rbs skip` and `@rbs override` skip RBS generation.'
- MSG_DUPLICATE_SKIP =
'Duplicate `@rbs skip` annotation.'- MSG_DUPLICATE_OVERRIDE =
'Duplicate `@rbs override` annotation.'- MSG_CONFLICTING_SKIP_OVERRIDE =
'`@rbs skip` and `@rbs override` cannot both be specified.'
Instance Attribute Summary
Attributes included from CommentParser
Instance Method Summary collapse
- #check_doc_style_annotations(def_line) ⇒ void
- #check_skip_override(def_line) ⇒ void
- #check_trailing_return(param_list_end_line) ⇒ void
- #doc_style_annotation_message(annotation) ⇒ String?
-
#method_parameter_list_end_line(node) ⇒ Integer
Returns the last line of the method parameter list (the closing ) line, or the def line if no parens).
- #on_def(node) ⇒ void (also: #on_defs)
-
#on_new_investigation ⇒ void
: void.
- #process(node) ⇒ void
- #skip_or_override_annotation?(line) ⇒ Boolean
- #skip_override_message(first, current) ⇒ String
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
Instance Method Details
#check_doc_style_annotations(def_line) ⇒ void
This method returns an undefined value.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 147 def check_doc_style_annotations(def_line) #: void leading_annotation = find_leading_annotation(def_line) return unless leading_annotation leading_annotation.each_annotation do |annotation| msg = (annotation) next unless msg range = annotation_range(annotation) or next add_offense(range, message: msg) do |corrector| corrector.remove(range_by_whole_lines(range, include_final_newline: true)) end end end |
#check_skip_override(def_line) ⇒ void
This method returns an undefined value.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 115 def check_skip_override(def_line) #: void leading_annotation = find_leading_annotation(def_line) return unless leading_annotation first = nil #: (RBS::Inline::AST::Annotations::Skip | RBS::Inline::AST::Annotations::Override)? leading_annotation.each_annotation do |annotation| case annotation when RBS::Inline::AST::Annotations::Skip, RBS::Inline::AST::Annotations::Override if first.nil? first = annotation else msg = (first, annotation) range = annotation_range(annotation) or next add_offense(range, message: msg) do |corrector| corrector.remove(range_by_whole_lines(range, include_final_newline: true)) end end end end end |
#check_trailing_return(param_list_end_line) ⇒ void
This method returns an undefined value.
176 177 178 179 180 181 182 183 184 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 176 def check_trailing_return(param_list_end_line) #: void comment = find_trailing_comment(param_list_end_line) return unless comment add_offense(comment, message: MSG_TRAILING_RETURN) do |corrector| removal_range = range_with_surrounding_space(range: comment.loc.expression, side: :left, newlines: false) corrector.remove(removal_range) end end |
#doc_style_annotation_message(annotation) ⇒ String?
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 163 def (annotation) #: String? case annotation when RBS::Inline::AST::Annotations::MethodTypeAssertion MSG_METHOD_TYPE_SIGNATURE when RBS::Inline::AST::Annotations::Method, RBS::Inline::AST::Annotations::VarType, RBS::Inline::AST::Annotations::BlockType, RBS::Inline::AST::Annotations::ReturnType MSG_DOC_STYLE_ANNOTATION end end |
#method_parameter_list_end_line(node) ⇒ Integer
Returns the last line of the method parameter list (the closing ) line, or the def line if no parens).
188 189 190 191 192 193 194 195 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 188 def method_parameter_list_end_line(node) #: Integer args_node = case node.type when :def then node.children[1] when :defs then node.children[2] else raise end args_node.location.end&.line || node.location.line end |
#on_def(node) ⇒ void Also known as: on_defs
This method returns an undefined value.
82 83 84 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 82 def on_def(node) #: void process(node) end |
#on_new_investigation ⇒ void
This method returns an undefined value.
: void
76 77 78 79 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 76 def on_new_investigation #: void super parse_comments end |
#process(node) ⇒ void
This method returns an undefined value.
91 92 93 94 95 96 97 98 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 91 def process(node) #: void def_line = node.location.line return unless skip_or_override_annotation?(def_line) check_skip_override(def_line) check_doc_style_annotations(def_line) check_trailing_return(method_parameter_list_end_line(node)) end |
#skip_or_override_annotation?(line) ⇒ Boolean
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 101 def skip_or_override_annotation?(line) #: bool leading_annotation = find_leading_annotation(line) return false unless leading_annotation leading_annotation.each_annotation do |annotation| case annotation when RBS::Inline::AST::Annotations::Skip, RBS::Inline::AST::Annotations::Override return true end end false end |
#skip_override_message(first, current) ⇒ String
138 139 140 141 142 143 144 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 138 def (first, current) #: String if first.instance_of?(current.class) first.is_a?(RBS::Inline::AST::Annotations::Skip) ? MSG_DUPLICATE_SKIP : MSG_DUPLICATE_OVERRIDE else MSG_CONFLICTING_SKIP_OVERRIDE end end |