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',
  '::SortedSet',
  '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.



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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/constant_cache.rb', line 96

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
    if struct.owner != nil
      ret[struct.primary_name] = struct.aliases.reject do |name|
        # ignore the primary
        next true if name == struct.primary_name

        prefix, _, _ = name.rpartition('::')

        # an alias that exists at the top-level
        next false if prefix == ""

        # if the prefix is the same syntactically, then this is a good alias
        next false if prefix == struct.owner.primary_name

        # skip the alias if the owner is the same
        other_owner_const = Sorbet::Private::RealStdlib.real_const_get(Object, prefix, false)
        struct.owner.const == other_owner_const
      end
    else
      # top-level names
      ret[struct.primary_name] = struct.aliases.reject {|name| name == struct.primary_name }
    end
  end
  ret
end

#all_module_namesObject



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

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



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

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



126
127
128
# File 'lib/constant_cache.rb', line 126

def class_by_name(name)
  @consts_by_name[name]
end

#name_by_class(klass) ⇒ Object



130
131
132
# File 'lib/constant_cache.rb', line 130

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