Class: RuboCop::Cop::Style::RbsInline::RedundantAnnotationWithSkip
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::RbsInline::RedundantAnnotationWithSkip
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, ASTUtils, CommentParser, FileFilter, 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."
Constants included from FileFilter
FileFilter::MAGIC_COMMENT_DISABLED, FileFilter::MAGIC_COMMENT_ENABLED, FileFilter::SUPPORTED_MODES
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?
- #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, #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 Method Details
#check_doc_style_annotations(def_line) ⇒ void
This method returns an undefined value.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 149 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 117 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.
178 179 180 181 182 183 184 185 186 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 178 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?
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 165 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 |
#on_def(node) ⇒ void Also known as: on_defs
This method returns an undefined value.
84 85 86 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 84 def on_def(node) #: void process(node) end |
#on_new_investigation ⇒ void
This method returns an undefined value.
: void
78 79 80 81 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 78 def on_new_investigation #: void super parse_comments end |
#process(node) ⇒ void
This method returns an undefined value.
93 94 95 96 97 98 99 100 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 93 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
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 103 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
140 141 142 143 144 145 146 |
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 140 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 |