Class: RuboCop::Cop::Style::RbsInline::InvalidTypes

Inherits:
Base
  • Object
show all
Includes:
RBS::Inline::AST::Annotations, RBS::Inline::AST::Members, RangeHelp
Defined in:
lib/rubocop/cop/style/rbs_inline/invalid_types.rb

Overview

IRB::Inline expects the types of annotation comments are valid syntax. This cop checks for comments that do not match the expected pattern.

Examples:

# bad
# @rbs arg: Hash[Symbol,
# @rbs &block: String
def method(arg); end

# good
# @rbs arg: Hash[Symbol, String]
# @rbs &block: () -> void
def method(arg); end

Constant Summary collapse

MSG =
'Invalid annotation found.'

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject

: void # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/style/rbs_inline/invalid_types.rb', line 30

def on_new_investigation #: void # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  results = parse_comments
  results.each do |result|
    result.each_annotation do |annotation|
      location = annotation.source.comments.first&.location or raise
      range = range_between(character_offset(location.start_offset), character_offset(location.end_offset))

      case annotation
      when Application
        add_offense(range) unless annotation.types
      when BlockType, IvarType, SpecialVarTypeAnnotation, VarType
        add_offense(range) unless annotation.type
      when Generic
        add_offense(range) unless annotation.type_param
      when ModuleSelf
        add_offense(range) if annotation.self_types.empty?
      when SyntaxErrorAssertion
        add_offense(range)
      when Embedded
        comment = annotation.source.comments.fetch(0)
        parsing_result = RBS::Inline::AnnotationParser::ParsingResult.new(comment)
        embedded = RBSEmbedded.new(parsing_result, annotation)
        add_offense(range) if embedded.members.is_a?(RBS::ParsingError)
      end
    end
  end
end