Class: Sorbet::Private::ConstantLookupCache

Inherits:
Object
  • Object
show all
Defined in:
lib/constant_cache.rb

Overview

This class walks global namespace to find all modules and discover all of their names. At the time you ask for it it, it takes a “spashot” of the world. If new modules were defined after an instance of this class was created, they won’t be visible through a previously returned instance.

Defined Under Namespace

Classes: ConstantEntry

Constant Summary collapse

DEPRECATED_CONSTANTS =

We won’t be able to see if someone mankeypatches these.

[
  '::Bignum',
  '::Config',
  '::Data',
  '::FALSE',
  '::Fixnum',
  '::NIL',
  '::TRUE',
  '::TimeoutError',
  'ERB::Compiler::SimpleScanner2',
  'Net::OpenSSL',
  'Object::Bignum',
  'Object::Config',
  'Object::Data',
  'Object::FALSE',
  'Object::Fixnum',
  'Object::NIL',
  'Object::TRUE',
  'Object::TimeoutError',
  'OpenSSL::Cipher::Cipher',
  'OpenSSL::Cipher::Digest',
  'OpenSSL::Digest::Cipher',
  'OpenSSL::Digest::Digest',
  'OpenSSL::SSL::SSLContext::METHODS',
  'Pry::Platform',
  'Pry::Prompt::MAP',
  'Sequel::BeforeHookFailed',
  'Sequel::Database::ResetIdentifierMangling',
  'Sequel::Error::AdapterNotFound',
  'Sequel::Error::InvalidOperation',
  'Sequel::Error::InvalidValue',
  'Sequel::Error::PoolTimeoutError',
  'Sequel::Error::Rollback',
  'Sequel::Model::ANONYMOUS_MODEL_CLASSES',
  'Sequel::Model::ANONYMOUS_MODEL_CLASSES_MUTEX',
  'Sequel::UnbindDuplicate',
  'Sequel::Unbinder',
  'YARD::Parser::Ruby::Legacy::RipperParser',
  'Java::ComHeadiusRacc::Cparse::CparseParams::NEVER',
  'Java::ComHeadiusRacc::Cparse::CparseParams::UNDEF',
  'Java::ComHeadiusRacc::Cparse::Parser::NEVER',
  'Java::ComHeadiusRacc::Cparse::Parser::UNDEF',
  'Java::OrgJruby::RubyBasicObject::NEVER',
  'Java::OrgJruby::RubyBasicObject::UNDEF',
  'Java::OrgJruby::RubyClass::NEVER',
  'Java::OrgJruby::RubyClass::UNDEF',
  'Java::OrgJruby::RubyModule::NEVER',
  'Java::OrgJruby::RubyModule::UNDEF',
  'Java::OrgJruby::RubyObject::NEVER',
  'Java::OrgJruby::RubyObject::UNDEF',
].freeze

Instance Method Summary collapse

Constructor Details

#initializeConstantLookupCache

Returns a new instance of ConstantLookupCache.



68
69
70
71
72
73
74
75
76
77
# File 'lib/constant_cache.rb', line 68

def initialize
  @all_constants = {}
  dfs_module(Object, nil, @all_constants, nil)
  Sorbet::Private::Status.done
  @consts_by_name = {}
  @all_constants.each_value do |struct|
    fill_primary_name(struct)
    @consts_by_name[struct.primary_name] = struct.const
  end
end

Instance Method Details

#all_module_aliasesObject



95
96
97
98
99
100
101
102
103
# File 'lib/constant_cache.rb', line 95

def all_module_aliases
  ret = {}

  @all_constants.map do |_key, struct|
    next if struct.nil? || !Sorbet::Private::RealStdlib.real_is_a?(struct.const, Module) || struct.aliases.size < 2
    ret[struct.primary_name] = struct.aliases.reject {|name| name == struct.primary_name}
  end
  ret
end

#all_module_namesObject



79
80
81
82
83
84
85
# File 'lib/constant_cache.rb', line 79

def all_module_names
  ret = @all_constants.select {|_k, v| Sorbet::Private::RealStdlib.real_is_a?(v.const, Module)}.map do |_key, struct|
    raise "should never happen" if !struct.primary_name
    struct.primary_name
  end
  ret
end

#all_named_modulesObject



87
88
89
90
91
92
93
# File 'lib/constant_cache.rb', line 87

def all_named_modules
  ret = @all_constants.select {|_k, v| Sorbet::Private::RealStdlib.real_is_a?(v.const, Module)}.map do |_key, struct|
    raise "should never happen" if !struct.primary_name
    struct.const
  end
  ret
end

#class_by_name(name) ⇒ Object



105
106
107
# File 'lib/constant_cache.rb', line 105

def class_by_name(name)
  @consts_by_name[name]
end

#name_by_class(klass) ⇒ Object



109
110
111
# File 'lib/constant_cache.rb', line 109

def name_by_class(klass)
  @all_constants[Sorbet::Private::RealStdlib.real_object_id(klass)]&.primary_name
end