class Sorbet::Private::ConstantLookupCache
they won’t be visible through a previously returned instance.
If new modules were defined after an instance of this class was created,
At the time you ask for it it, it takes a “spashot” of the world.
This class walks global namespace to find all modules and discover all of their names.
def all_module_aliases
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
def all_module_names
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
def all_named_modules
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
def class_by_name(name)
def class_by_name(name) @consts_by_name[name] end
def dfs_module(mod, prefix, ret, owner)
def dfs_module(mod, prefix, ret, owner) "error #{prefix}: #{mod} is not a module" if !Sorbet::Private::RealStdlib.real_is_a?(mod, Module) Sorbet::Private::RealStdlib.real_name(mod) ::Private::Status.say("Naming #{name}", print_without_tty: false) if name == 'RSpec::ExampleGroups' # These are all anonymous classes and will be quadratic in the number of classes to name them. We also know they don't have any hidden definitions tants = Sorbet::Private::RealStdlib.real_constants(mod) TypeError "Failed to call `constants` on #{prefix}" rn nil per = [] # make this a bfs to prefer shorter names nts.each do |nested| n xt if Sorbet::Private::RealStdlib.real_autoload?(mod, nested) # some constants aren't autoloaded even after require_everything, e.g. StateMachine::Graph gin next if DEPRECATED_CONSTANTS.include?("#{prefix}::#{nested}") # avoid stdout spew scue NameError # If it isn't to_s-able, that is ok, it won't be in our deprecated list nil d gin nested_constant = Sorbet::Private::RealStdlib.real_const_get(mod, nested, false) # rubocop:disable PrisonGuard/NoDynamicConstAccess scue LoadError puts "Failed to load #{name}::#{nested}" next scue NameError, ArgumentError puts "Failed to load #{name}::#{nested}" # some stuff fails to load, like # `const_get': uninitialized constant YARD::Parser::Ruby::Legacy::RipperParser (NameError) # Did you mean? YARD::Parser::Ruby::Legacy::RipperParser d ject_id = Sorbet::Private::RealStdlib.real_object_id(nested_constant) ybe_seen_already = ret[object_id] Object.eql?(mod) || !prefix nested_name = nested.to_s se nested_name = prefix + "::" + nested.to_s d maybe_seen_already if nested_name != maybe_seen_already.primary_name maybe_seen_already.aliases << nested_name end if maybe_seen_already.primary_name.nil? && Sorbet::Private::RealStdlib.real_is_a?(nested_constant, Module) realName = Sorbet::Private::RealStdlib.real_name(nested_constant) maybe_seen_already.primary_name = realName end se entry = ConstantEntry.new(nested, nested_name, nil, [nested_name], nested_constant, owner) ret[object_id] = entry if Sorbet::Private::RealStdlib.real_is_a?(nested_constant, Module) && Object != nested_constant go_deeper << [entry, nested_constant, nested_name] end d ible horrible hack to get private constants that are `include`d. doesn't recurse so you can't get private constants inside the private tants, but I really hope you don't need those... ::Private::RealStdlib.real_ancestors(mod).each do |ancestor| ct_id = Sorbet::Private::RealStdlib.real_object_id(ancestor) = Sorbet::Private::RealStdlib.real_name(ancestor) unless name if ret[object_id] ix, _, const_name = name.rpartition('::') y = ConstantEntry.new(const_name.to_sym, name, name, [name], ancestor, nil) object_id] = entry _deeper = go_deeper.sort_by do |entry, nested_constant, nested_name| et::Private::RealStdlib.real_name(nested_constant) _deeper.each do |entry, nested_constant, nested_name| module(nested_constant, nested_name, ret, entry)
def fill_primary_name(struct)
def fill_primary_name(struct) if struct.primary_name ruct.owner ct.primary_name = struct.found_name _primary_name(struct.owner) ct.primary_name = struct.owner.primary_name + "::" + struct.const_name.to_s
def initialize
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
def name_by_class(klass)
def name_by_class(klass) @all_constants[Sorbet::Private::RealStdlib.real_object_id(klass)]&.primary_name end