class RBSProtobuf::RBSFactory

Public Instance Methods

alias_type(name, location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 67
def alias_type(name, location: nil)
  type_name = case name
              when TypeName
                name
              else
                type_name(name)
              end

  Types::Alias.new(name: type_name, location: nil)
end
block(function, required: true) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 89
def block(function, required: true)
  MethodType::Block.new(
    type: function,
    required: required
  )
end
bool_type(location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 63
def bool_type(location: nil)
  RBS::Types::Bases::Bool.new(location: location)
end
function(return_type = Types::Bases::Void.new(location: nil)) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 78
def function(return_type = Types::Bases::Void.new(location: nil))
  Types::Function.empty(return_type)
end
instance_type(name, *args) click to toggle source
# 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) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 109
def literal_type(literal)
  Types::Literal.new(
    literal: literal,
    location: nil
  )
end
method_type(params: [], type:, block: nil, location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 100
def method_type(params: [], type:, block: nil, location: nil)
  MethodType.new(
    type_params: params,
    type: type,
    block: block,
    location: location
  )
end
module_type_params(*params) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 130
def module_type_params(*params)
  params.each.with_object(AST::Declarations::ModuleTypeParams.empty) do |param, type_params|
    type_params.add(AST::Declarations::ModuleTypeParams::TypeParam.new(
      name: param,
      variance: :invariant,
      skip_validation: false
    ))
  end
end
namespace(string) click to toggle source
# 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) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 59
def nil_type(location: nil)
  RBS::Types::Bases::Nil.new(location: location)
end
optional_type(type, location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 116
def optional_type(type, location: nil)
  Types::Optional.new(
    type: type,
    location: location
  )
end
param(type, name: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 82
def param(type, name: nil)
  Types::Function::Param.new(
    type: type,
    name: name
  )
end
singleton_type(name) click to toggle source
# 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) click to toggle source
# 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,
    namespace: Namespace.new(path: path, absolute: absolute)
  )
end
type_var(name, location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 123
def type_var(name, location: nil)
  Types::Variable.new(
    name: name,
    location: location
  )
end
union_type(type, *types) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 48
def union_type(type, *types)
  if types.empty?
    type
  else
    Types::Union.new(
      types: [type] + types,
      location: nil
    )
  end
end
untyped(location: nil) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 96
def untyped(location: nil)
  Types::Bases::Any.new(location: location)
end
unwrap_optional(type) click to toggle source
# File lib/rbs_protobuf/rbs_factory.rb, line 140
def unwrap_optional(type)
  case type
  when RBS::Types::Optional
    unwrap_optional(type.type)
  else
    type
  end
end