Class: RuboCop::Cop::RBS::Lint::DuplicateAnnotation

Inherits:
RBS::CopBase
  • Object
show all
Defined in:
lib/rubocop/cop/rbs/lint/duplicate_annotation.rb

Overview

Checks that there are no repeated annotations.

Examples:

# bad
def %a{foo} %a{foo} def foo: () -> void

# bad
def %a{foo} foo: %a{foo} () -> void

# bad
def foo: %a{foo} %a{foo} () -> void

# not bad
def foo: %a{foo} () -> void
       | %a{foo} (Integer) -> void

Constant Summary collapse

MSG =
'Duplicate annotation detected.'

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

documentation_url, #investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#check_annotations(left_annotation, right_annotations) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rbs/lint/duplicate_annotation.rb', line 44

def check_annotations(left_annotation, right_annotations)
  right_annotations.each do |right_annotation|
    next unless left_annotation == right_annotation
    next unless right_annotation.location

    range = location_to_range(right_annotation.location)
    add_offense(range)
  end
end

#on_rbs_def(decl) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rbs/lint/duplicate_annotation.rb', line 26

def on_rbs_def(decl)
  decl.annotations.each_with_index do |annotation, idx|
    next_annotations = decl.annotations[(idx + 1)..] or next
    check_annotations(annotation, next_annotations)

    decl.overloads.each do |overload|
      check_annotations(annotation, overload.annotations)
    end
  end

  decl.overloads.each do |overload|
    overload.annotations.each_with_index do |overload_annotation, idx|
      next_annotations = overload.annotations[(idx + 1)..] or next
      check_annotations(overload_annotation, next_annotations)
    end
  end
end