Class: SorbetAutoTyper::MethodTrace

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet_auto_typer/method_trace.rb

Defined Under Namespace

Classes: Param

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_trace_line(line) ⇒ 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
# File 'lib/sorbet_auto_typer/method_trace.rb', line 37

def self.from_trace_line(line)
  type, container, method_type, method_name, args_or_return = line.split(Tracer::OUTPUT_DELIMITER, 5)
  case type
  when Tracer::OUTPUT_TYPE_CALL
    SorbetAutoTyper::MethodTrace.new(
      container: Object.const_get(T.must(container)),
      method_type: T.must(method_type),
      method_name: T.must(method_name),
      args: args_or_return.nil? ? [] : Param.param_list_from_args_string(args_or_return),
      return_class: nil,
    )
  when Tracer::OUTPUT_TYPE_RETURN
    SorbetAutoTyper::MethodTrace.new(
      container: Object.const_get(T.must(container)),
      method_type: T.must(method_type),
      method_name: T.must(method_name),
      args: nil,
      return_class: sorbet_type_from_string_encoding(T.must(args_or_return)),
    )
  else
    raise ArgumentError, "Invalid type in data: '#{type}'"
  end
rescue NameError => e
  puts "Invalid trace line: #{e.message}"
  nil
end

.sorbet_type_from_string_encoding(type_str) ⇒ Object



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
# File 'lib/sorbet_auto_typer/method_trace.rb', line 65

def self.sorbet_type_from_string_encoding(type_str)
  return T.untyped if type_str == ''

  subtypes_def = unpack_subtypes_one_level(type_str)
  subtypes = []
  while subtypes_def.size > 0
    output_type = subtypes_def.shift
    case output_type
    when Tracer::OUTPUT_TYPE_HASH
      key_type = sorbet_type_from_string_encoding(T.must(subtypes_def.shift))
      value_type = sorbet_type_from_string_encoding(T.must(subtypes_def.shift))
      subtypes << T::Types::TypedHash.new(keys: key_type, values: value_type)
    when Tracer::OUTPUT_TYPE_RANGE
      inner_type = sorbet_type_from_string_encoding(T.must(subtypes_def.shift))
      subtypes << T::Types::TypedRange.new(inner_type)
    when Tracer::OUTPUT_TYPE_ARRAY
      inner_type = sorbet_type_from_string_encoding(T.must(subtypes_def.shift))
      subtypes << T::Types::TypedArray.new(inner_type)
    when Tracer::OUTPUT_TYPE_SET
      inner_type = sorbet_type_from_string_encoding(T.must(subtypes_def.shift))
      subtypes << T::Types::TypedSet.new(inner_type)
    when Tracer::OUTPUT_TYPE_DEFAULT
      begin
        subtypes << T::Types::Simple.new(Object.const_get(T.must(subtypes_def.shift)))
      rescue NameError
        subtypes << T.untyped
      end
    else
      raise ArgumentError, "Invalid type string: '#{type_str}'"
    end
  end
  T::Types::Union.new(subtypes)
end

.unpack_subtypes_one_level(str) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sorbet_auto_typer/method_trace.rb', line 100

def self.unpack_subtypes_one_level(str)
  level = 0
  str.split('').reduce(['']) do |memo, char|
    if char == Tracer::OUTPUT_TYPE_INNER_LEFT
      memo.last << char if level > 0
      level += 1
    elsif char == Tracer::OUTPUT_TYPE_INNER_RIGHT
      level -= 1
      memo.last << char if level > 0
    elsif char == Tracer::OUTPUT_TYPE_DELIMITER && level == 0
      memo << ''
    else
      memo.last << char
    end
    memo
  end
end

Instance Method Details

#methodObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sorbet_auto_typer/method_trace.rb', line 137

def method
  if method_type == 'instance'
    method = container.instance_method(method_name.to_sym)
  elsif method_type == 'class'
    method = container.method(method_name.to_sym)
  else
    raise ArgumentError, "Invalid method_type '#{method_type}'"
  end
  # if method.owner.singleton_class?
  #   real_owner = method.receiver.ancestors.find { |a| !(a.singleton_method(@method_name.to_sym) rescue nil).nil? }
  #   real_owner.singleton_method(@method_name.to_sym)
  # end
  method
end

#method_fileObject



132
133
134
# File 'lib/sorbet_auto_typer/method_trace.rb', line 132

def method_file
  method.source_location.first
end

#ownerObject



123
124
125
126
127
128
129
# File 'lib/sorbet_auto_typer/method_trace.rb', line 123

def owner
  if method.owner.singleton_class?
    T.cast(method, Method).receiver.ancestors.find { |a| !(a.singleton_method(method_name.to_sym) rescue nil).nil? }
  else
    method.owner
  end
end

#params?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/sorbet_auto_typer/method_trace.rb', line 153

def params?
  !args.nil?
end

#return?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/sorbet_auto_typer/method_trace.rb', line 158

def return?
  !return_class.nil?
end