/
pipeline.rb
365 lines (297 loc) · 11.6 KB
/
pipeline.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
355
356
357
358
359
360
361
362
363
364
365
# typed: strict
# frozen_string_literal: true
require "pathname"
module Tapioca
module Gem
class Pipeline
extend T::Sig
include Runtime::Reflection
include SignaturesHelper
IGNORED_SYMBOLS = T.let(["YAML", "MiniTest", "Mutex"], T::Array[String])
sig { returns(Gemfile::GemSpec) }
attr_reader :gem
sig { params(gem: Gemfile::GemSpec, include_doc: T::Boolean).void }
def initialize(gem, include_doc: false)
@root = T.let(RBI::Tree.new, RBI::Tree)
@gem = gem
@seen = T.let(Set.new, T::Set[String])
@alias_namespace = T.let(Set.new, T::Set[String])
@events = T.let([], T::Array[Gem::Event])
@payload_symbols = T.let(Static::SymbolLoader.payload_symbols, T::Set[String])
@bootstrap_symbols = T.let(Static::SymbolLoader.gem_symbols(@gem).union(Static::SymbolLoader.engine_symbols),
T::Set[String])
@bootstrap_symbols.each { |symbol| push_symbol(symbol) }
@node_listeners = T.let([], T::Array[Gem::Listeners::Base])
@node_listeners << Gem::Listeners::SorbetTypeVariables.new(self)
@node_listeners << Gem::Listeners::Mixins.new(self)
@node_listeners << Gem::Listeners::DynamicMixins.new(self)
@node_listeners << Gem::Listeners::Methods.new(self)
@node_listeners << Gem::Listeners::SorbetHelpers.new(self)
@node_listeners << Gem::Listeners::SorbetEnums.new(self)
@node_listeners << Gem::Listeners::SorbetProps.new(self)
@node_listeners << Gem::Listeners::SorbetRequiredAncestors.new(self)
@node_listeners << Gem::Listeners::SorbetSignatures.new(self)
@node_listeners << Gem::Listeners::Subconstants.new(self)
@node_listeners << Gem::Listeners::YardDoc.new(self) if include_doc
@node_listeners << Gem::Listeners::RemoveEmptyPayloadScopes.new(self)
end
sig { returns(RBI::Tree) }
def compile
dispatch(next_event) until @events.empty?
@root
end
sig { params(symbol: String).void }
def push_symbol(symbol)
@events << Gem::SymbolFound.new(symbol)
end
sig { params(symbol: String, constant: BasicObject).void.checked(:never) }
def push_constant(symbol, constant)
@events << Gem::ConstantFound.new(symbol, constant)
end
sig { params(symbol: String, constant: Module, node: RBI::Const).void.checked(:never) }
def push_const(symbol, constant, node)
@events << Gem::ConstNodeAdded.new(symbol, constant, node)
end
sig { params(symbol: String, constant: Module, node: RBI::Scope).void.checked(:never) }
def push_scope(symbol, constant, node)
@events << Gem::ScopeNodeAdded.new(symbol, constant, node)
end
sig do
params(
symbol: String,
constant: Module,
node: RBI::Method,
signature: T.untyped,
parameters: T::Array[[Symbol, String]]
).void.checked(:never)
end
def push_method(symbol, constant, node, signature, parameters)
@events << Gem::MethodNodeAdded.new(symbol, constant, node, signature, parameters)
end
sig { params(symbol_name: String).returns(T::Boolean) }
def symbol_in_payload?(symbol_name)
symbol_name = symbol_name[2..-1] if symbol_name.start_with?("::")
return false unless symbol_name
@payload_symbols.include?(symbol_name)
end
sig { params(method: UnboundMethod).returns(T::Boolean) }
def method_in_gem?(method)
source_location = method.source_location&.first
return false if source_location.nil?
@gem.contains_path?(source_location)
end
sig { params(constant: Module).returns(T.nilable(String)) }
def name_of(constant)
name = name_of_proxy_target(constant, super(class_of(constant)))
return name if name
name = super(constant)
return if name.nil?
return unless are_equal?(constant, constantize(name, inherit: true))
name = "Struct" if name =~ /^(::)?Struct::[^:]+$/
name
end
private
sig { returns(Gem::Event) }
def next_event
T.must(@events.shift)
end
sig { params(event: Gem::Event).void }
def dispatch(event)
case event
when Gem::SymbolFound
on_symbol(event)
when Gem::ConstantFound
on_constant(event)
when Gem::NodeAdded
on_node(event)
else
raise "Unsupported event #{event.class}"
end
end
sig { params(event: Gem::SymbolFound).void }
def on_symbol(event)
symbol = event.symbol.delete_prefix("::")
return if symbol_in_payload?(symbol) && !@bootstrap_symbols.include?(symbol)
constant = constantize(symbol)
push_constant(symbol, constant) if constant
end
sig { params(event: Gem::ConstantFound).void.checked(:never) }
def on_constant(event)
name = event.symbol
return if name.strip.empty?
return if name.start_with?("#<")
return if name.downcase == name
return if alias_namespaced?(name)
return if seen?(name)
constant = event.constant
return if T::Enum === constant # T::Enum instances are defined via `compile_enums`
mark_seen(name)
compile_constant(name, constant)
end
sig { params(event: Gem::NodeAdded).void }
def on_node(event)
@node_listeners.each { |listener| listener.dispatch(event) }
end
# Compile
sig { params(symbol: String, constant: BasicObject).void.checked(:never) }
def compile_constant(symbol, constant)
case constant
when Module
if name_of(constant) != symbol
compile_alias(symbol, constant)
else
compile_module(symbol, constant)
end
else
compile_object(symbol, constant)
end
end
sig { params(name: String, constant: Module).void }
def compile_alias(name, constant)
return if symbol_in_payload?(name)
target = name_of(constant)
# If target has no name, let's make it an anonymous class or module with `Class.new` or `Module.new`
target = "#{constant.class}.new" unless target
add_to_alias_namespace(name)
return if IGNORED_SYMBOLS.include?(name)
node = RBI::Const.new(name, target)
push_const(name, constant, node)
@root << node
end
sig { params(name: String, value: BasicObject).void.checked(:never) }
def compile_object(name, value)
return if symbol_in_payload?(name)
klass = class_of(value)
klass_name = if klass == ObjectSpace::WeakMap
# WeakMap is an implicit generic with one type variable
"ObjectSpace::WeakMap[T.untyped]"
elsif T::Generic === klass
generic_name_of(klass)
else
name_of(klass)
end
if klass_name == "T::Private::Types::TypeAlias"
type_alias = sanitize_signature_types(T.unsafe(value).aliased_type.to_s)
node = RBI::Const.new(name, "T.type_alias { #{type_alias} }")
push_const(name, klass, node)
@root << node
return
end
return if klass_name&.start_with?("T::Types::", "T::Private::")
type_name = klass_name || "T.untyped"
node = RBI::Const.new(name, "T.let(T.unsafe(nil), #{type_name})")
push_const(name, klass, node)
@root << node
end
sig { params(name: String, constant: Module).void }
def compile_module(name, constant)
return unless defined_in_gem?(constant, strict: false)
return if Tapioca::TypeVariableModule === constant
scope =
if constant.is_a?(Class)
superclass = compile_superclass(constant)
RBI::Class.new(name, superclass_name: superclass)
else
RBI::Module.new(name)
end
push_scope(name, constant, scope)
@root << scope
end
sig { params(constant: Class).returns(T.nilable(String)) }
def compile_superclass(constant)
superclass = T.let(nil, T.nilable(Class)) # rubocop:disable Lint/UselessAssignment
while (superclass = superclass_of(constant))
constant_name = name_of(constant)
constant = superclass
# Some types have "themselves" as their superclass
# which can happen via:
#
# class A < Numeric; end
# A = Class.new(A)
# A.superclass #=> A
#
# We compare names here to make sure we skip those
# superclass instances and walk up the chain.
#
# The name comparison is against the name of the constant
# resolved from the name of the superclass, since
# this is also possible:
#
# B = Class.new
# class A < B; end
# B = A
# A.superclass.name #=> "B"
# B #=> A
superclass_name = name_of(superclass)
next unless superclass_name
resolved_superclass = constantize(superclass_name)
next unless Module === resolved_superclass
next if name_of(resolved_superclass) == constant_name
# We found a suitable superclass
break
end
return if superclass == ::Object || superclass == ::Delegator
return if superclass.nil?
name = name_of(superclass)
return if name.nil? || name.empty?
push_symbol(name)
"::#{name}"
end
sig { params(constant: Module, strict: T::Boolean).returns(T::Boolean) }
def defined_in_gem?(constant, strict: true)
files = Set.new(get_file_candidates(constant))
.merge(Runtime::Trackers::ConstantDefinition.files_for(constant))
return !strict if files.empty?
files.any? do |file|
@gem.contains_path?(file)
end
end
sig { params(constant: Module).returns(T::Array[String]) }
def get_file_candidates(constant)
wrapped_module = Pry::WrappedModule.new(constant)
wrapped_module.candidates.map(&:file).to_a.compact
rescue ArgumentError, NameError
[]
end
sig { params(name: String).void }
def add_to_alias_namespace(name)
@alias_namespace.add("#{name}::")
end
sig { params(name: String).returns(T::Boolean) }
def alias_namespaced?(name)
@alias_namespace.any? do |namespace|
name.start_with?(namespace)
end
end
sig { params(name: String).void }
def mark_seen(name)
@seen.add(name)
end
sig { params(name: String).returns(T::Boolean) }
def seen?(name)
@seen.include?(name)
end
sig { params(constant: T.all(Module, T::Generic)).returns(String) }
def generic_name_of(constant)
type_name = T.must(constant.name)
return type_name if type_name =~ /\[.*\]$/
type_variables = Runtime::GenericTypeRegistry.lookup_type_variables(constant)
return type_name unless type_variables
type_variable_names = type_variables.map { "T.untyped" }.join(", ")
"#{type_name}[#{type_variable_names}]"
end
sig { params(constant: Module, class_name: T.nilable(String)).returns(T.nilable(String)) }
def name_of_proxy_target(constant, class_name)
return unless class_name == "ActiveSupport::Deprecation::DeprecatedConstantProxy"
# We are dealing with a ActiveSupport::Deprecation::DeprecatedConstantProxy
# so try to get the name of the target class
begin
target = constant.__send__(:target)
rescue NoMethodError
return
end
name_of(target)
end
end
end
end