Class: Sorbet::Private::Serialize

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

Overview

typed: true

Constant Summary collapse

DENYLIST_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

Constructor Details

#initialize(constant_cache) ⇒ Serialize

Returns a new instance of Serialize.



12
13
14
# File 'lib/serialize.rb', line 12

def initialize(constant_cache)
  @constant_cache = constant_cache
end

Class Method Details

.header(typed = "true", subcommand = "update") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/serialize.rb', line 25

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



183
184
185
186
187
# File 'lib/serialize.rb', line 183

def alias(base, other_name)
  ret = String.new
  ret << "#{other_name} = #{base}"
  ret
end

#ancestor_has_method(method, klass) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/serialize.rb', line 207

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

#class_or_module(class_name) ⇒ Object



37
38
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
# File 'lib/serialize.rb', line 37

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")

  # We don't use .included_modules since that also has all the awful 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
  singleton_class = Sorbet::Private::RealStdlib.real_singleton_class(klass)
  Sorbet::Private::RealStdlib.real_ancestors(singleton_class).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 => err
      ret << "# Got #{err.class} when trying to get class constant symbol #{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 DENYLIST_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 => err
      ret << "# Got #{err.class} when trying to get class constant symbol #{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 appears 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 methods 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 denylisted_method(method)
    next if ancestor_has_method(method, klass)
    serialize_method(method, with_sig: false)
  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 denylisted_method(method)
    next if ancestor_has_method(method, Sorbet::Private::RealStdlib.real_singleton_class(klass))
    serialize_method(method, true, with_sig: false)
  end
  ret << methods.join("\n")
  ret << "end\n"

  ret
end

#comparable?(value) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
# File 'lib/serialize.rb', line 189

def comparable?(value)
  return false if Sorbet::Private::RealStdlib.real_is_a?(value, Numeric) &&
    Sorbet::Private::RealStdlib.real_respond_to?(value, :nan?) &&
    value.nan?
  return false if Sorbet::Private::RealStdlib.real_is_a?(value, Complex)
  true
end

#constant(const, value) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/serialize.rb', line 221

def constant(const, value)
  if KEYWORDS.include?(const.to_sym)
    return "# Illegal constant name: #{const}"
  end
  if defined?(T::Types::TypeMember) && Sorbet::Private::RealStdlib.real_is_a?(value, T::Types::TypeMember)
    value.variance == :invariant ? "  #{const} = type_member" : "  #{const} = type_member(#{value.variance.inspect})"
  elsif defined?(T::Types::TypeTemplate) && Sorbet::Private::RealStdlib.real_is_a?(value, T::Types::TypeTemplate)
    value.variance == :invariant ? "  #{const} = type_template" : "  #{const} = type_template(#{value.variance.inspect})"
  else
    "  #{const} = ::T.let(nil, ::T.untyped)"
  end
end

#denylisted_method(method) ⇒ Object



197
198
199
# File 'lib/serialize.rb', line 197

def denylisted_method(method)
  method.name =~ /__validator__[0-9]{8}/ || method.name =~ /.*:.*/
end

#from_method(method) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/serialize.rb', line 327

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 = 'arg' + (uniq == 0 ? '' : uniq.to_s)
        uniq += 1
      end
    end

    # Sanitize parameter names that are not valid.
    # Ruby 2.7+ argument forwarding works by expanding `foo(...)`
    # to `foo(**, ****, &&)`, in other words, to a rest parameter named
    # `*`, a keyword rest parameter named `**` and a block argument named
    # `&`. Those are all illegal parameter names which we should NOT
    # generate. (Coincidentally, that is why Ruby 2.7+ uses those names,
    # so that user code cannot mess with the forwarded arguments)
    if ["*", "**", "&"].include?(name.to_s)
      name = 'arg' + (uniq == 0 ? '' : uniq.to_s)
      uniq += 1
    end

    [kind, name]
  end
end

#serialize_method(method, static = false, with_sig: true) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/serialize.rb', line 234

def serialize_method(method, static=false, with_sig: true)
  name = method.name.to_s
  if !valid_method_name(name)
    return "# Illegal method name: #{name}\n"
  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 = String.new
  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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/serialize.rb', line 265

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



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/serialize.rb', line 356

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



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/serialize.rb', line 253

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



201
202
203
204
205
# File 'lib/serialize.rb', line 201

def valid_method_name(name)
  return true if SPECIAL_METHOD_NAMES.include?(name)
  return false if name =~ /^\d/
  name =~ /^[[:word:]]+[?!=]?$/
end