Class: Sorbet::Private::Serialize
- Inherits:
-
Object
- Object
- Sorbet::Private::Serialize
- Defined in:
- lib/serialize.rb
Constant Summary collapse
- BLACKLIST_CONSTANTS =
[ ['DidYouMean', :NameErrorCheckers], # https://github.com/yuki24/did_you_mean/commit/b72fdbe194401f1be21f8ad7b6e3f784a0ad197d ['Net', :OpenSSL], # https://github.com/yuki24/did_you_mean/commit/b72fdbe194401f1be21f8ad7b6e3f784a0ad197d ]
- SPECIAL_METHOD_NAMES =
%w[! ~ +@ ** -@ * / % + - << >> & | ^ < <= => > >= == === != =~ !~ <=> [] []= `]
- KEYWORDS =
[ :__ENCODING__, :__LINE__, :__FILE__, :BEGIN, :END, :alias, :and, :begin, :break, :case, :class, :def, :defined?, :do, :else, :elsif, :end, :ensure, :false, :for, :if, :in, :module, :next, :nil, :not, :or, :redo, :rescue, :retry, :return, :self, :super, :then, :true, :undef, :unless, :until, :when, :while, :yield, ]
Class Method Summary collapse
Instance Method Summary collapse
- #alias(base, other_name) ⇒ Object
- #ancestor_has_method(method, klass) ⇒ Object
- #blacklisted_method(method) ⇒ Object
- #class_or_module(class_name) ⇒ Object
- #comparable?(value) ⇒ Boolean
- #constant(const, value) ⇒ Object
- #from_method(method) ⇒ Object
-
#initialize(constant_cache) ⇒ Serialize
constructor
A new instance of Serialize.
- #serialize_method(method, static = false, with_sig: true) ⇒ Object
- #serialize_sig(parameters) ⇒ Object
- #to_sig(kind, name) ⇒ Object
- #valid_class_name(name) ⇒ Object
- #valid_method_name(name) ⇒ Object
Constructor Details
#initialize(constant_cache) ⇒ Serialize
Returns a new instance of Serialize.
14 15 16 |
# File 'lib/serialize.rb', line 14 def initialize(constant_cache) @constant_cache = constant_cache end |
Class Method Details
.header(typed = "true", subcommand = "update") ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/serialize.rb', line 27 def self.header(typed="true", subcommand="update") buffer = [] buffer << "# This file is autogenerated. Do not edit it by hand. Regenerate it with:" buffer << "# srb rbi #{subcommand}" if typed buffer << "" buffer << "# typed: #{typed}" end buffer << "" buffer.join("\n") end |
Instance Method Details
#alias(base, other_name) ⇒ Object
185 186 187 188 189 |
# File 'lib/serialize.rb', line 185 def alias(base, other_name) ret = String.new ret << "#{other_name} = #{base}" ret end |
#ancestor_has_method(method, klass) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/serialize.rb', line 208 def ancestor_has_method(method, klass) return false if !Sorbet::Private::RealStdlib.real_is_a?(klass, Class) first_ancestor = klass.ancestors.find do |ancestor| next if ancestor == klass begin ancestor.instance_method(method.name) rescue NameError nil end end return false unless first_ancestor first_ancestor.instance_method(method.name).parameters == method.parameters end |
#blacklisted_method(method) ⇒ Object
198 199 200 |
# File 'lib/serialize.rb', line 198 def blacklisted_method(method) method.name =~ /__validator__[0-9]{8}/ || method.name =~ /.*:.*/ end |
#class_or_module(class_name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/serialize.rb', line 39 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 |
#comparable?(value) ⇒ Boolean
191 192 193 194 195 196 |
# File 'lib/serialize.rb', line 191 def comparable?(value) return false if Sorbet::Private::RealStdlib.real_is_a?(value, BigDecimal) && value.nan? return false if Sorbet::Private::RealStdlib.real_is_a?(value, Float) && value.nan? return false if Sorbet::Private::RealStdlib.real_is_a?(value, Complex) true end |
#constant(const, value) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/serialize.rb', line 222 def constant(const, value) #if Sorbet::Private::RealStdlib.real_is_a?(value, T::Types::TypeTemplate) #" #{const} = type_template" #elsif Sorbet::Private::RealStdlib.real_is_a?(value, T::Types::TypeMember) #" #{const} = type_member" #else #" #{const} = ::T.let(nil, ::T.untyped)" #end if KEYWORDS.include?(const.to_sym) return "# Illegal constant name: #{const}" end " #{const} = ::T.let(nil, ::T.untyped)" end |
#from_method(method) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/serialize.rb', line 330 def from_method(method) uniq = 0 method.parameters.map.with_index do |(kind, name), index| if !name arg_name = method.name.to_s[0...-1] if (!KEYWORDS.include?(arg_name.to_sym)) && method.name.to_s.end_with?('=') && arg_name =~ /\A[a-z_][a-z0-9A-Z_]*\Z/ && index == 0 name = arg_name else name = '_' + (uniq == 0 ? '' : uniq.to_s) uniq += 1 end end [kind, name] end end |
#serialize_method(method, static = false, with_sig: true) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/serialize.rb', line 236 def serialize_method(method, static=false, with_sig: true) name = method.name.to_s ret = String.new if !valid_method_name(name) ret << "# Illegal method name: #{name}" return end parameters = from_method(method) # a hack for appeasing Sorbet in the presence of the Enumerable interface if name == 'each' && !parameters.any? {|(kind, _)| kind == :block} parameters.push([:block, "blk"]) end ret << serialize_sig(parameters) if with_sig args = parameters.map do |(kind, param_name)| to_sig(kind, param_name) end.compact.join(', ') ret << " def #{static ? 'self.' : ''}#{name}(#{args}); end\n" ret end |
#serialize_sig(parameters) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/serialize.rb', line 268 def serialize_sig(parameters) ret = String.new if !parameters.empty? ret << " sig do\n" ret << " params(\n" parameters.each do |(_kind, name)| ret << " #{name}: ::T.untyped,\n" end ret << " )\n" ret << " .returns(::T.untyped)\n" ret << " end\n" else ret << " sig {returns(::T.untyped)}\n" end ret end |
#to_sig(kind, name) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/serialize.rb', line 346 def to_sig(kind, name) case kind when :req name.to_s when :opt "#{name}=T.unsafe(nil)" when :rest "*#{name}" when :keyreq "#{name}:" when :key "#{name}: T.unsafe(nil)" when :keyrest "**#{name}" when :block "&#{name}" end end |
#valid_class_name(name) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/serialize.rb', line 256 def valid_class_name(name) name.split("::").each do |piece| return false if piece[0].upcase != piece[0] end return false if [ 'Sorbet::Private::GemGeneratorTracepoint::Tracer::ClassOverride', 'Sorbet::Private::GemGeneratorTracepoint::Tracer::ModuleOverride', 'Sorbet::Private::GemGeneratorTracepoint::Tracer::ObjectOverride', ].include?(name) true end |
#valid_method_name(name) ⇒ Object
202 203 204 205 206 |
# File 'lib/serialize.rb', line 202 def valid_method_name(name) return true if SPECIAL_METHOD_NAMES.include?(name) return false if name =~ /^\d/ name =~ /^[[:word:]]+[?!=]?$/ end |