Class: RuboCop::Cop::Style::RbsInline::RedundantAnnotationWithSkip

Inherits:
Base
  • Object
show all
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.

Examples:

# bad - redundant method type signature with @rbs skip
# @rbs skip
#: (Integer) -> void
def method(a)
end

# bad - redundant doc-style method type annotation with @rbs skip
# @rbs skip
# @rbs (Integer) -> void
def method(a)
end

# bad - redundant param annotation with @rbs skip
# @rbs skip
# @rbs a: Integer
def method(a)
end

# bad - redundant trailing return type with @rbs skip
# @rbs skip
def method(a) #: void
end

# bad - redundant type annotations with @rbs override
# @rbs override
# @rbs a: Integer
def method(a)
end

# good
# @rbs skip
def method(a)
end

# good
# @rbs override
def method(a)
end

Constant Summary collapse

MSG_METHOD_TYPE_SIGNATURE =

Returns:

  • (::String)
'Redundant method type signature. ' \
'`@rbs skip` and `@rbs override` skip RBS generation.'
MSG_DOC_STYLE_ANNOTATION =

Returns:

  • (::String)
'Redundant `@rbs` annotation. ' \
'`@rbs skip` and `@rbs override` skip RBS generation.'
MSG_TRAILING_RETURN =

Returns:

  • (::String)
'Redundant trailing return type annotation. ' \
'`@rbs skip` and `@rbs override` skip RBS generation.'
MSG_DUPLICATE_SKIP =

Returns:

  • (::String)
'Duplicate `@rbs skip` annotation.'
MSG_DUPLICATE_OVERRIDE =

Returns:

  • (::String)
'Duplicate `@rbs override` annotation.'
MSG_CONFLICTING_SKIP_OVERRIDE =

Returns:

  • (::String)
'`@rbs skip` and `@rbs override` cannot both be specified.'

Instance Attribute Summary

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

Instance Method Details

#check_doc_style_annotations(def_line) ⇒ void

This method returns an undefined value.

Parameters:

  • def_line (Integer)


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 = doc_style_annotation_message(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.

Parameters:

  • def_line (Integer)


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 = skip_override_message(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.

Parameters:

  • param_list_end_line (Integer)


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?

Parameters:

  • annotation (Object)

Returns:

  • (String, nil)


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 doc_style_annotation_message(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).

Parameters:

  • node (RuboCop::AST::DefNode)

Returns:

  • (Integer)


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.

Parameters:

  • node (RuboCop::AST::DefNode)


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_investigationvoid

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.

Parameters:

  • node (RuboCop::AST::DefNode)


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

Parameters:

  • line (Integer)

Returns:

  • (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

Parameters:

  • first (RBS::Inline::AST::Annotations::Skip, RBS::Inline::AST::Annotations::Override)
  • current (RBS::Inline::AST::Annotations::Skip, RBS::Inline::AST::Annotations::Override)

Returns:

  • (String)


138
139
140
141
142
143
144
# File 'lib/rubocop/cop/style/rbs_inline/redundant_annotation_with_skip.rb', line 138

def skip_override_message(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