Class: RBSProtobuf::RBSFactory

Inherits:
Object
  • Object
show all
Includes:
RBS
Defined in:
lib/rbs_protobuf/rbs_factory.rb

Instance Method Summary collapse

Instance Method Details

#alias_type(name, location: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/rbs_protobuf/rbs_factory.rb', line 68

def alias_type(name, location: nil)
  type_name = case name
              when TypeName
                name
              else
                type_name(name)
              end

  Types::Alias.new(name: type_name, args: [], location: nil)
end

#block(function, required: true) ⇒ Object



90
91
92
93
94
95
# File 'lib/rbs_protobuf/rbs_factory.rb', line 90

def block(function, required: true)
  Types::Block.new(
    type: function,
    required: required
  )
end

#bool_type(location: nil) ⇒ Object



64
65
66
# File 'lib/rbs_protobuf/rbs_factory.rb', line 64

def bool_type(location: nil)
  RBS::Types::Bases::Bool.new(location: location)
end

#function(return_type = Types::Bases::Void.new(location: nil)) ⇒ Object



79
80
81
# File 'lib/rbs_protobuf/rbs_factory.rb', line 79

def function(return_type = Types::Bases::Void.new(location: nil))
  Types::Function.empty(return_type)
end

#instance_type(name, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rbs_protobuf/rbs_factory.rb', line 26

def instance_type(name, *args)
  type_name = case name
              when TypeName
                name
              else
                type_name(name)
              end

  Types::ClassInstance.new(name: type_name, args: args, location: nil)
end

#literal_type(literal) ⇒ Object



114
115
116
117
118
119
# File 'lib/rbs_protobuf/rbs_factory.rb', line 114

def literal_type(literal)
  Types::Literal.new(
    literal: literal,
    location: nil
  )
end

#method_type(params: [], type:, block: nil, location: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbs_protobuf/rbs_factory.rb', line 101

def method_type(params: [], type:, block: nil, location: nil)
  type_params = params.map do |name|
    AST::TypeParam.new(name: name, variance: :invariant, upper_bound: nil, location: nil)
  end

  MethodType.new(
    type_params: type_params,
    type: type,
    block: block,
    location: location
  )
end

#module_type_params(*params) ⇒ Object



139
140
141
142
143
# File 'lib/rbs_protobuf/rbs_factory.rb', line 139

def module_type_params(*params)
  params.map do |name|
    AST::TypeParam.new(name: name, variance: :invariant, upper_bound: nil, location: nil)
  end
end

#namespace(string) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rbs_protobuf/rbs_factory.rb', line 16

def namespace(string)
  absolute = string.start_with?("::")
  path = string.delete_prefix("::").split("::").map(&:to_sym)

  Namespace.new(
    path: path,
    absolute: absolute
  )
end

#nil_type(location: nil) ⇒ Object



60
61
62
# File 'lib/rbs_protobuf/rbs_factory.rb', line 60

def nil_type(location: nil)
  RBS::Types::Bases::Nil.new(location: location)
end

#optional_type(type, location: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/rbs_protobuf/rbs_factory.rb', line 121

def optional_type(type, location: nil)
  if type.is_a?(Types::Optional)
    type
  else
    Types::Optional.new(
      type: type,
      location: location
    )
  end
end

#param(type, name: nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/rbs_protobuf/rbs_factory.rb', line 83

def param(type, name: nil)
  Types::Function::Param.new(
    type: type,
    name: name
  )
end

#singleton_type(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rbs_protobuf/rbs_factory.rb', line 37

def singleton_type(name)
  type_name = case name
              when TypeName
                name
              else
                type_name(name)
              end

  Types::ClassSingleton.new(name: type_name, location: nil)
end

#type_name(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/rbs_protobuf/rbs_factory.rb', line 5

def type_name(string)
  absolute = string.start_with?("::")

  *path, name = string.delete_prefix("::").split("::").map(&:to_sym)

  TypeName.new(
    name: name || raise,
    namespace: Namespace.new(path: path, absolute: absolute)
  )
end

#type_var(name, location: nil) ⇒ Object



132
133
134
135
136
137
# File 'lib/rbs_protobuf/rbs_factory.rb', line 132

def type_var(name, location: nil)
  Types::Variable.new(
    name: name,
    location: location
  )
end

#union_type(type, *types) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbs_protobuf/rbs_factory.rb', line 48

def union_type(type, *types)
  types = types.compact
  if types.empty?
    type
  else
    Types::Union.new(
      types: [type] + types,
      location: nil
    )
  end
end

#untyped(location: nil) ⇒ Object



97
98
99
# File 'lib/rbs_protobuf/rbs_factory.rb', line 97

def untyped(location: nil)
  Types::Bases::Any.new(location: location)
end

#unwrap_optional(type) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/rbs_protobuf/rbs_factory.rb', line 145

def unwrap_optional(type)
  case type
  when RBS::Types::Optional
    unwrap_optional(type.type)
  else
    type
  end
end