123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::IndexSignatureHelp

Overview

Tells what the project index knows from Ruby source apart from what it only knows from RBS signatures, which is how Ruby’s core and standard library reach it. Included by ProjectIndexHelp.

Constant Summary

Instance Method Summary

Instance Method Details

#core_root?(declaration) ⇒ Boolean (private)

Whether every object or every class has the declaration among its ancestors. The standard library signatures mix a module of their own into Object (PP::ObjectMixin), hence the lookup.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 55

def core_root?(declaration)
  @core_root_names ||= begin
    ancestors = project_index['Class']&.ancestors.to_a
    CORE_ROOTS | ancestors.map(&:name)
  end

  @core_root_names.include?(declaration.name)
end

#declared_in_signatures?(declaration) ⇒ Boolean (private)

Whether the index knows the declaration, at least in part, from RBS signatures (or Rubydex’s built-in stubs). Ruby’s core and standard library reach the index that way. Reopening such a namespace in the project does not fill the gaps in its signatures.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 39

def declared_in_signatures?(declaration)
  declaration.definitions.any? { |definition| !ruby_definition?(definition) }
end

#defined_in_ruby?(declaration) ⇒ Boolean (private)

Whether the declaration is defined in Ruby source at all, as opposed to one the index only knows from signatures.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 31

def defined_in_ruby?(declaration)
  declaration.definitions.any? { |definition| ruby_definition?(definition) }
end

#ruby_definition?(definition) ⇒ Boolean (private)

Whether the definition comes from Ruby source, as opposed to an RBS signature file or one of Rubydex’s built-in declarations. A signature declares a method or a constant, it does not define one, so an implementation and its .rbs declaration are not two definitions of the same thing. Ruby’s core and standard library reach the index as the signature files of the rbs gem, so without this every reopening of a core class and every constant that shares a core name would look like a redefinition.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 23

def ruby_definition?(definition)
  uri = definition.location.uri

  uri.start_with?(ProjectIndexHelp::FILE_URI_PREFIX) && !uri.end_with?(SIGNATURE_EXTENSION)
end

#signature_declared_ancestry?(declaration) ⇒ Boolean (private)

Whether the declaration has an ancestor from Ruby’s core or standard library other than the roots everything descends from. The signatures of the roots are thorough; those of the rest have gaps.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 46

def signature_declared_ancestry?(declaration)
  declaration.ancestors.any? do |ancestor|
    !core_root?(ancestor) && declared_in_signatures?(ancestor)
  end
end