Class: Yoda::Typing::Types::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/typing/types/generator.rb

Overview

Generator provides construction methods for various type classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment:) ⇒ Generator

Returns a new instance of Generator.

Parameters:



10
11
12
# File 'lib/yoda/typing/types/generator.rb', line 10

def initialize(environment:)
  @environment = environment
end

Instance Attribute Details

#environmentModel::Environment (readonly)

Returns:



7
8
9
# File 'lib/yoda/typing/types/generator.rb', line 7

def environment
  @environment
end

Instance Method Details

#any_typeType<RBS::Types::Bases::Any>

Returns:

  • (Type<RBS::Types::Bases::Any>)


140
141
142
# File 'lib/yoda/typing/types/generator.rb', line 140

def any_type
  wrap_rbs_type(RBS::Types::Bases::Any.new(location: nil))
end

#array_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


73
74
75
# File 'lib/yoda/typing/types/generator.rb', line 73

def array_type
  instance_type_at('::Array')
end

#boolean_typeType<RBS::Types::Bases::Bool>

Returns:

  • (Type<RBS::Types::Bases::Bool>)


27
28
29
# File 'lib/yoda/typing/types/generator.rb', line 27

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

#class_classType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


152
153
154
# File 'lib/yoda/typing/types/generator.rb', line 152

def class_class
  singleton_type_at('::Class')
end

#false_typeType<RBS::Types::Literal>

Returns:

  • (Type<RBS::Types::Literal>)


37
38
39
# File 'lib/yoda/typing/types/generator.rb', line 37

def false_type
  literal_type(false)
end

#float_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


107
108
109
# File 'lib/yoda/typing/types/generator.rb', line 107

def float_type
  instance_type_at('::Float')
end

#fresh_params_of_method_type(method_type) ⇒ RBS::MethodType

Parameters:

  • method_type (RBS::MethodType)

Returns:

  • (RBS::MethodType)


244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/yoda/typing/types/generator.rb', line 244

def fresh_params_of_method_type(method_type)
  new_type_params = method_type.type_params.map(&method(:append_id_to_type_var))
  new_type_variables = RBS::Types::Variable.build(new_type_params)

  subst = RBS::Substitution.build(method_type.type_params, new_type_variables)

  method_type.update(
    type_params: new_type_params,
    type: method_type.type.sub(subst),
    block: method_type.block&.sub(subst),
  )
end

#function_type(return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], required_keyword_parameters: [], optional_keyword_parameters: [], keyword_rest_parameter: nil) ⇒ Type<RBS::Types::Function>

Parameters:

  • required_parameters (Array<Base>) (defaults to: [])
  • optional_parameters (Array<Base>) (defaults to: [])
  • rest_parameter (Base, nil) (defaults to: nil)
  • post_parameters (Array<Base>) (defaults to: [])
  • required_keyword_parameters (Array<(String, Base)>) (defaults to: [])
  • optional_keyword_parameters (Array<(String, Base)>) (defaults to: [])
  • keyword_rest_parameter (Base, nil) (defaults to: nil)
  • return_type (Base)

Returns:

  • (Type<RBS::Types::Function>)


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/yoda/typing/types/generator.rb', line 199

def function_type(
  return_type:,
  required_parameters: [],
  optional_parameters: [],
  rest_parameter: nil,
  post_parameters: [],
  required_keyword_parameters: [],
  optional_keyword_parameters: [],
  keyword_rest_parameter: nil
)
  build_param = lambda { |type| RBS::Types::Function::Param.new(type: unwrap(type), name: nil) }
  build_keyword = lambda { |(name, type)| [name.to_sym, RBS::Types::Function::Param.new(type: unwrap(type), name: name.to_sym)] }
  function_type = RBS::Types::Function.new(
    required_positionals: required_parameters.map(&build_param),
    optional_positionals: optional_parameters.map(&build_param),
    rest_positionals: rest_parameter&.yield_self(&build_param),
    trailing_positionals: post_parameters.map(&build_param),
    required_keywords: required_keyword_parameters.map(&build_keyword).to_h,
    optional_keywords: optional_keyword_parameters.map(&build_keyword).to_h,
    rest_keywords: keyword_rest_parameter&.yield_self(&build_param),
    return_type: unwrap(return_type),
  )
  wrap_rbs_type(function_type)
end

#hash_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


78
79
80
# File 'lib/yoda/typing/types/generator.rb', line 78

def hash_type
  instance_type_at('::Hash')
end

#instance_type(object_class) ⇒ Type<RBS::Types::ClassInstance>

Parameters:

Returns:

  • (Type<RBS::Types::ClassInstance>)


129
130
131
# File 'lib/yoda/typing/types/generator.rb', line 129

def instance_type(object_class)
  instance_type_at(object_class.path)
end

#instance_type_at(path, args: []) ⇒ Type<RBS::Types::ClassInstance>

Parameters:

  • args (Array<Type, RBS::Types::t>) (defaults to: [])

Returns:

  • (Type<RBS::Types::ClassInstance>)


173
174
175
176
# File 'lib/yoda/typing/types/generator.rb', line 173

def instance_type_at(path, args: [])
  name = to_type_name(path)
  name ? wrap_rbs_type(RBS::Types::ClassInstance.new(name: name, args: args.map(&method(:unwrap)), location: nil)) : unknown_type(reason: "#{path} does not exists")
end

#integer_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::Literal, RBS::Types::ClassInstance>)


98
99
100
101
102
103
104
# File 'lib/yoda/typing/types/generator.rb', line 98

def integer_type(literal = nil)
  if literal
    literal_type(literal.to_i)
  else
    instance_type_at('::Integer')
  end
end

#literal_type(literal) ⇒ Type<RBS::Types::Literal>

Parameters:

  • literal (Integer, Symbol, String, TrueClass, FalseClass)

Returns:

  • (Type<RBS::Types::Literal>)


68
69
70
# File 'lib/yoda/typing/types/generator.rb', line 68

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

#module_classType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


157
158
159
# File 'lib/yoda/typing/types/generator.rb', line 157

def module_class
  singleton_type_at('::Module')
end

#nil_typeType<RBS::Types::Bases::Nil>

Returns:

  • (Type<RBS::Types::Bases::Nil>)


42
43
44
# File 'lib/yoda/typing/types/generator.rb', line 42

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

#numeric_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


112
113
114
# File 'lib/yoda/typing/types/generator.rb', line 112

def numeric_type
  instance_type_at('::Numeric')
end

#object_classType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


162
163
164
# File 'lib/yoda/typing/types/generator.rb', line 162

def object_class
  singleton_type_at('::Object')
end

#object_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


117
118
119
# File 'lib/yoda/typing/types/generator.rb', line 117

def object_type
  instance_type_at('::Object')
end

#proc_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


93
94
95
# File 'lib/yoda/typing/types/generator.rb', line 93

def proc_type
  instance_type_at('::Proc')
end

#range_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


83
84
85
# File 'lib/yoda/typing/types/generator.rb', line 83

def range_type
  instance_type_at('::Range')
end

#rbs_method_type(**kwargs) ⇒ RBS::MethodType

Parameters:

  • required_parameters (Array<Base>)
  • optional_parameters (Array<Base>)
  • rest_parameter (Base, nil)
  • post_parameters (Array<Base>)
  • required_keyword_parameters (Array<(String, Base)>)
  • optional_keyword_parameters (Array<(String, Base)>)
  • keyword_rest_parameter (Base, nil)
  • return_type (Base)

Returns:

  • (RBS::MethodType)


233
234
235
236
237
238
239
240
# File 'lib/yoda/typing/types/generator.rb', line 233

def rbs_method_type(**kwargs)
  RBS::MethodType.new(
    type_params: [],
    type: unwrap(function_type(**kwargs)),
    block: nil,
    location: nil,
  )
end

#record_type(record) ⇒ Type<RBS::Types::Record>

Parameters:

  • record (Hash{Symbol => RBS::Types::t})

Returns:

  • (Type<RBS::Types::Record>)


135
136
137
# File 'lib/yoda/typing/types/generator.rb', line 135

def record_type(record)
  wrap_rbs_type(RBS::Types::Record.new(fields: record, location: nil))
end

#regexp_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


88
89
90
# File 'lib/yoda/typing/types/generator.rb', line 88

def regexp_type
  instance_type_at('::RegExp')
end

#singleton_type(object_class) ⇒ Type<RBS::Types::ClassInstance>

Parameters:

Returns:

  • (Type<RBS::Types::ClassInstance>)


123
124
125
# File 'lib/yoda/typing/types/generator.rb', line 123

def singleton_type(object_class)
  singleton_type_at(object_class.path)
end

#singleton_type_at(path) ⇒ Type<RBS::Types::ClassSingleton>

Returns:

  • (Type<RBS::Types::ClassSingleton>)


179
180
181
182
# File 'lib/yoda/typing/types/generator.rb', line 179

def singleton_type_at(path)
  name = to_type_name(path)
  name ? wrap_rbs_type(RBS::Types::ClassSingleton.new(name: name, location: nil)) : unknown_type(reason: "#{path} does not exists")
end

#standard_error_typeType<RBS::Types::ClassInstance>

Returns:

  • (Type<RBS::Types::ClassInstance>)


167
168
169
# File 'lib/yoda/typing/types/generator.rb', line 167

def standard_error_type
  instance_type_at('::StandardError')
end

#string_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>

Parameters:

  • literal (Integer, Symbol, String, TrueClass, FalseClass) (defaults to: nil)

Returns:

  • (Type<RBS::Types::Literal, RBS::Types::ClassInstance>)


48
49
50
51
52
53
54
# File 'lib/yoda/typing/types/generator.rb', line 48

def string_type(literal = nil)
  if literal
    literal_type(literal.to_s)
  else
    instance_type_at('::String')
  end
end

#symbol_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>

Parameters:

  • literal (Integer, Symbol, String, TrueClass, FalseClass) (defaults to: nil)

Returns:

  • (Type<RBS::Types::Literal, RBS::Types::ClassInstance>)


58
59
60
61
62
63
64
# File 'lib/yoda/typing/types/generator.rb', line 58

def symbol_type(literal = nil)
  if literal
    literal_type(literal.to_sym)
  else
    instance_type_at('::Symbol')
  end
end

#true_typeType<RBS::Types::Literal>

Returns:

  • (Type<RBS::Types::Literal>)


32
33
34
# File 'lib/yoda/typing/types/generator.rb', line 32

def true_type
  literal_type(true)
end

#union_type(*types) ⇒ Type<RBS::Types::Union>

Parameters:

  • types (Array<Base>)

Returns:

  • (Type<RBS::Types::Union>)


186
187
188
# File 'lib/yoda/typing/types/generator.rb', line 186

def union_type(*types)
  wrap_rbs_type(RBS::Types::Union.new(types: types.map(&method(:unwrap)), location: nil))
end

#unknown_type(reason: nil) ⇒ Type<RBS::Types::Bases::Any>

Parameters:

  • reason (String, nil) (defaults to: nil)

Returns:

  • (Type<RBS::Types::Bases::Any>)


146
147
148
149
# File 'lib/yoda/typing/types/generator.rb', line 146

def unknown_type(reason: nil)
  Logger.trace("Use unknown type because #{reason}") if reason
  any_type
end

#value_resolve_context(self_type:) ⇒ Model::Environment::ValueResolveContext

Parameters:

Returns:



16
17
18
# File 'lib/yoda/typing/types/generator.rb', line 16

def value_resolve_context(self_type:)
  Model::Environment::ValueResolveContext.new(self_type: self_type.rbs_type)
end

#wrap_rbs_type(rbs_type) ⇒ Type

Parameters:

  • (RBS::Types::t)

Returns:



22
23
24
# File 'lib/yoda/typing/types/generator.rb', line 22

def wrap_rbs_type(rbs_type)
  Type.new(environment: environment, rbs_type: rbs_type)
end