-
Notifications
You must be signed in to change notification settings - Fork 2
/
unwrap_command.rb
45 lines (38 loc) · 1.48 KB
/
unwrap_command.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
# frozen_string_literal: true
module PrySorbet
class UnwrapCommand < Pry::ClassCommand
match "sorbet-unwrap"
group "Sorbet"
description "Unwrap all methods wrapped by Sorbet runtime"
command_options requires_gem: "sorbet-runtime"
banner <<~BANNER
Usage: sorbet-unwrap
Unwrap all methods wrapped by Sorbet runtime
BANNER
def process
return unless defined?(T::Private::Methods)
# For each signature wrapper, call the lambda associated with the wrapper
# to finalize the method wrapping
sig_wrappers = T::Private::Methods.instance_variable_get(:@sig_wrappers) || {}
sig_wrappers.values.each do |sig_wrapper|
sig_wrapper.call
rescue NameError
end
# Now that all methods are properly wrapped with optimized wrappers
# we can replace them with the original methods
signatures_by_method = T::Private::Methods.instance_variable_get(:@signatures_by_method) || {}
signatures_by_method.values.each do |signature|
T::Configuration.without_ruby_warnings do
T::Private::DeclState.current.without_on_method_added do
if signature.mode == T::Private::Methods::Modes.abstract
signature.owner.send(:remove_method, signature.method_name)
else
signature.owner.send(:define_method, signature.method_name, signature.method)
end
end
end
end
end
end
end
Pry::Commands.add_command(PrySorbet::UnwrapCommand)