Module: RuboCop::Cop::IndexSignatureHelp
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
Lint::ArgumentMismatch,
Lint::ConstantReassignment,
Lint::ConstantResolution,
Lint::DeprecatedReference,
Lint::DuplicateMethods,
Lint::InheritException,
Lint::MissingSuper,
Lint::NameTypo,
Lint::SuperArgumentMismatch,
Lint::UnusedPrivateMethod,
Naming::AccessorMethodName,
Naming::PredicatePrefix,
ProjectIndexHelp,
Style::ClassAndModuleChildren,
Style::Documentation,
Style::MissingRespondToMissing,
Style::RedundantConstantBase,
Style::StaticClass
| |
| Defined in: | lib/rubocop/cop/mixin/index_signature_help.rb |
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
-
CORE_ROOTS =
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 12
The classes and modules every object and every class has among its ancestors, before whatever the signatures mix into them.
%w[BasicObject Kernel Object Module Class].freeze
-
SIGNATURE_EXTENSION =
# File 'lib/rubocop/cop/mixin/index_signature_help.rb', line 9'.rbs'
Instance Method Summary
-
#core_root?(declaration) ⇒ Boolean
private
Whether every object or every class has the declaration among its ancestors.
-
#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).
-
#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.
-
#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.
-
#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.
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.
# 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.
# 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.
# 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.
# 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.
# 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