class Sorbet::Private::Serialize
def class_or_module(class_name)
def class_or_module(class_name) if !valid_class_name(class_name) return " # Skipping serializing #{class_name} because it is an invalid name\n" end klass = @constant_cache.class_by_name(class_name) is_nil = nil.equal?(klass) raise "#{class_name} is not a Class or Module. Maybe it was miscategorized?" if is_nil ret = String.new superclass = Sorbet::Private::RealStdlib.real_is_a?(klass, Class) ? Sorbet::Private::RealStdlib.real_superclass(klass) : nil if superclass superclass_str = Sorbet::Private::RealStdlib.real_eqeq(superclass, Object) ? '' : @constant_cache.name_by_class(superclass) else superclass_str = '' end superclass_str = !superclass_str || superclass_str.empty? ? '' : " < #{superclass_str}" ret << (Sorbet::Private::RealStdlib.real_is_a?(klass, Class) ? "class #{class_name}#{superclass_str}\n" : "module #{class_name}\n") ret << " extend ::T::Sig\n" # We don't use .included_modules since that also has all the aweful things # that are mixed into Object. This way we at least have a delimiter before # the awefulness starts (the superclass). Sorbet::Private::RealStdlib.real_ancestors(klass).each do |ancestor| next if Sorbet::Private::RealStdlib.real_eqeq(ancestor, klass) break if Sorbet::Private::RealStdlib.real_eqeq(ancestor, superclass) ancestor_name = @constant_cache.name_by_class(ancestor) next unless ancestor_name next if ancestor_name == class_name if Sorbet::Private::RealStdlib.real_is_a?(ancestor, Class) ret << " # Skipping `include #{ancestor_name}` because it is a Class\n" next end if !valid_class_name(ancestor_name) ret << " # Skipping `include #{ancestor_name}` because it is an invalid name\n" next end ret << " include ::#{ancestor_name}\n" end Sorbet::Private::RealStdlib.real_singleton_class(klass).ancestors.each do |ancestor| next if ancestor == Sorbet::Private::RealStdlib.real_singleton_class(klass) break if superclass && ancestor == Sorbet::Private::RealStdlib.real_singleton_class(superclass) break if ancestor == Module break if ancestor == Object ancestor_name = @constant_cache.name_by_class(ancestor) next unless ancestor_name if Sorbet::Private::RealStdlib.real_is_a?(ancestor, Class) ret << " # Skipping `extend #{ancestor_name}` because it is a Class\n" next end if !valid_class_name(ancestor_name) ret << " # Skipping `extend #{ancestor_name}` because it is an invalid name\n" next end ret << " extend ::#{ancestor_name}\n" end constants = [] # Declare all the type_members and type_templates constants += get_constants(klass).uniq.map do |const_sym| # We have to not pass `false` because `klass.constants` intentionally is # pulling in all the ancestor constants next if Sorbet::Private::ConstantLookupCache::DEPRECATED_CONSTANTS.include?("#{class_name}::#{const_sym}") begin value = klass.const_get(const_sym) rescue LoadError, NameError, RuntimeError, ArgumentError ret << "# Failed to load #{class_name}::#{const_sym}\n" next end # next if !Sorbet::Private::RealStdlib.real_is_a?(value, T::Types::TypeVariable) next if Sorbet::Private::RealStdlib.real_is_a?(value, Module) next if !comparable?(value) [const_sym, value] end constants += get_constants(klass, false).uniq.map do |const_sym| next if BLACKLIST_CONSTANTS.include?([class_name, const_sym]) next if Sorbet::Private::ConstantLookupCache::DEPRECATED_CONSTANTS.include?("#{class_name}::#{const_sym}") begin value = klass.const_get(const_sym, false) rescue LoadError, NameError, RuntimeError, ArgumentError ret << "# Failed to load #{class_name}::#{const_sym}\n" next end next if Sorbet::Private::RealStdlib.real_is_a?(value, Module) next if !comparable?(value) [const_sym, value] end constants_sorted = constants.compact.sort_by do |const_sym, _value| const_sym end constants_uniq = constants_sorted.uniq do |const_sym, _value| const_sym.hash end constants_serialized = constants_uniq.map do |const_sym, value| constant(const_sym, value) end ret << constants_serialized.join("\n") ret << "\n\n" if !constants_serialized.empty? methods = [] instance_methods = Sorbet::Private::RealStdlib.real_instance_methods(klass, false) begin initialize = klass.instance_method(:initialize) rescue initialize = nil end if initialize && initialize.owner == klass # This method never apears in the reflection list... instance_methods += [:initialize] end Sorbet::Private::RealStdlib.real_ancestors(klass).reject {|ancestor| @constant_cache.name_by_class(ancestor)}.each do |ancestor| instance_methods += ancestor.instance_methods(false) end # uniq here is required because we populate additional methos from anonymous superclasses and there # might be duplicates methods += instance_methods.sort.uniq.map do |method_sym| begin method = klass.instance_method(method_sym) rescue => e ret << "# #{e}\n" next end next if blacklisted_method(method) next if ancestor_has_method(method, klass) serialize_method(method) end # uniq is not required here, but added to be on the safe side methods += Sorbet::Private::RealStdlib.real_singleton_methods(klass, false).sort.uniq.map do |method_sym| begin method = klass.singleton_method(method_sym) rescue => e ret << "# #{e}\n" next end next if blacklisted_method(method) next if ancestor_has_method(method, Sorbet::Private::RealStdlib.real_singleton_class(klass)) serialize_method(method, true) end ret << methods.join("\n") ret << "end\n" ret end