Class: RBI::RBS::TypeTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbi/rbs/type_translator.rb

Class Method Summary collapse

Class Method Details

.translate(type) ⇒ Object

: ( | ::RBS::Types::Alias | | ::RBS::Types::Bases::Any | | ::RBS::Types::Bases::Bool | | ::RBS::Types::Bases::Bottom | | ::RBS::Types::Bases::Class | | ::RBS::Types::Bases::Instance | | ::RBS::Types::Bases::Nil | | ::RBS::Types::Bases::Self | | ::RBS::Types::Bases::Top | | ::RBS::Types::Bases::Void | | ::RBS::Types::ClassSingleton | | ::RBS::Types::ClassInstance | | ::RBS::Types::Function | | ::RBS::Types::Interface | | ::RBS::Types::Intersection | | ::RBS::Types::Literal | | ::RBS::Types::Optional | | ::RBS::Types::Proc | | ::RBS::Types::Record | | ::RBS::Types::Tuple | | ::RBS::Types::Union | | ::RBS::Types::UntypedFunction | | ::RBS::Types::Variable | ) -> Type



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
# File 'lib/rbi/rbs/type_translator.rb', line 33

def translate(type)
  case type
  when ::RBS::Types::Alias
    translate_type_alias(type)
  when ::RBS::Types::Bases::Any
    Type.untyped
  when ::RBS::Types::Bases::Bool
    Type.boolean
  when ::RBS::Types::Bases::Bottom
    Type.noreturn
  when ::RBS::Types::Bases::Class
    Type.simple("Class")
  when ::RBS::Types::Bases::Instance
    Type.attached_class
  when ::RBS::Types::Bases::Nil
    Type.simple("NilClass")
  when ::RBS::Types::Bases::Self
    Type.self_type
  when ::RBS::Types::Bases::Top
    Type.anything
  when ::RBS::Types::Bases::Void
    Type.void
  when ::RBS::Types::ClassSingleton
    type_parameter = type.args.first ? translate(type.args.first) : nil
    Type.class_of(Type.simple(type.name.to_s), type_parameter)
  when ::RBS::Types::ClassInstance
    translate_class_instance(type)
  when ::RBS::Types::Function
    translate_function(type)
  when ::RBS::Types::Interface
    # TODO: unsupported yet
    # RBS interfaces (like _Foo) don't have a direct Sorbet equivalent
    # and the names aren't valid Ruby identifiers
    Type.untyped
  when ::RBS::Types::Intersection
    Type.all(*type.types.map { |t| translate(t) })
  when ::RBS::Types::Literal
    # Sorbet doesn't support literal types, so map to their base type
    case type.literal
    when nil then Type.simple("NilClass")
    when true then Type.simple("TrueClass")
    when false then Type.simple("FalseClass")
    else
      Type.simple(type.literal.class.to_s)
    end
  when ::RBS::Types::Optional
    Type.nilable(translate(type.type))
  when ::RBS::Types::Proc
    proc = translate(type.type) #: as Type::Proc
    proc.bind(translate(type.self_type)) if type.self_type
    proc
  when ::RBS::Types::Record
    Type.shape(type.fields.map { |name, type| [name, translate(type)] }.to_h)
  when ::RBS::Types::Tuple
    Type.tuple(type.types.map { |t| translate(t) })
  when ::RBS::Types::Union
    Type.any(*type.types.map { |t| translate(t) })
  when ::RBS::Types::UntypedFunction
    Type.proc.params(arg0: Type.untyped).returns(Type.untyped)
  when ::RBS::Types::Variable
    Type.type_parameter(type.name)
  else
    type #: absurd
  end
end