forked from Shopify/tapioca
/
sorbet_props.rb
33 lines (27 loc) · 893 Bytes
/
sorbet_props.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
# typed: strict
# frozen_string_literal: true
module Tapioca
module Gem
module Listeners
class SorbetProps < Base
extend T::Sig
private
sig { override.params(event: ScopeNodeAdded).void }
def on_scope(event)
constant = event.constant
node = event.node
return unless T::Props::ClassMethods === constant
constant.props.map do |name, prop|
type = prop.fetch(:type_object, "T.untyped").to_s.gsub(".returns(<VOID>)", ".void")
default = prop.key?(:default) || prop.key?(:factory) ? "T.unsafe(nil)" : nil
node << if prop.fetch(:immutable, false)
RBI::TStructConst.new(name.to_s, type, default: default)
else
RBI::TStructProp.new(name.to_s, type, default: default)
end
end
end
end
end
end
end