Module: KatakataIrb::Types
- Defined in:
- lib/katakata_irb/types.rb
Defined Under Namespace
Classes: InstanceType, SingletonType, Splat, UnionType
Constant Summary collapse
- MODULE_NAME_METHOD =
Module.instance_method(:name)
- NIL =
InstanceType.new NilClass
- OBJECT =
InstanceType.new Object
- TRUE =
InstanceType.new TrueClass
- FALSE =
InstanceType.new FalseClass
- SYMBOL =
InstanceType.new Symbol
- STRING =
InstanceType.new String
- INTEGER =
InstanceType.new Integer
- RANGE =
InstanceType.new Range
- REGEXP =
InstanceType.new Regexp
- FLOAT =
InstanceType.new Float
- RATIONAL =
InstanceType.new Rational
- COMPLEX =
InstanceType.new Complex
- ARRAY =
InstanceType.new Array
- HASH =
InstanceType.new Hash
- CLASS =
InstanceType.new Class
- MODULE =
InstanceType.new Module
- PROC =
InstanceType.new Proc
- BOOLEAN =
Instance Attribute Summary collapse
-
#rbs_builder ⇒ Object
readonly
Returns the value of attribute rbs_builder.
-
#rbs_load_error ⇒ Object
readonly
Returns the value of attribute rbs_load_error.
Class Method Summary collapse
- ._match_free_variable(vars, rbs_type, value, accumulator) ⇒ Object
- .array_of(*types) ⇒ Object
- .class_name_of(klass) ⇒ Object
- .from_rbs_type(return_type, self_type, extra_vars = {}) ⇒ Object
- .intersect?(a, b) ⇒ Boolean
- .load_rbs_builder ⇒ Object
- .match_free_variables(vars, types, values) ⇒ Object
- .method_return_bottom?(method) ⇒ Boolean
- .method_return_type(type, method_name) ⇒ Object
- .preload_in_thread ⇒ Object
- .rbs_methods(type, method_name, args_types, kwargs_type, has_block) ⇒ Object
- .rbs_search_method(klass, method_name, singleton) ⇒ Object
- .type_from_object(object) ⇒ Object
- .type_from_object_recursive(object, max_level:) ⇒ Object
Instance Attribute Details
#rbs_builder ⇒ Object (readonly)
Returns the value of attribute rbs_builder.
5 6 7 |
# File 'lib/katakata_irb/types.rb', line 5 def rbs_builder @rbs_builder end |
#rbs_load_error ⇒ Object (readonly)
Returns the value of attribute rbs_load_error.
5 6 7 |
# File 'lib/katakata_irb/types.rb', line 5 def rbs_load_error @rbs_load_error end |
Class Method Details
._match_free_variable(vars, rbs_type, value, accumulator) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/katakata_irb/types.rb', line 403 def self._match_free_variable(vars, rbs_type, value, accumulator) case [rbs_type, value] in [RBS::Types::Variable,] (accumulator[rbs_type.name] ||= []) << value if vars.include? rbs_type.name in [RBS::Types::ClassInstance, InstanceType] names = rbs_builder.build_singleton(rbs_type.name).type_params names.zip(rbs_type.args).each do |name, arg| v = value.params[name] _match_free_variable vars, arg, v, accumulator if v end in [RBS::Types::Tuple, InstanceType] if value.klass == Array v = value.params[:Elem] rbs_type.types.each do |t| _match_free_variable vars, t, v, accumulator end in [RBS::Types::Record, InstanceType] if value.klass == Hash # TODO in [RBS::Types::Interface,] definition = rbs_builder.build_interface rbs_type.name convert = {} definition.type_params.zip(rbs_type.args).each do |from, arg| convert[from] = arg.name if arg.is_a? RBS::Types::Variable end return if convert.empty? ac = {} definition.methods.each do |method_name, method| return_type = method_return_type value, method_name method.defs.each do |method_def| interface_return_type = method_def.type.type.return_type _match_free_variable convert, interface_return_type, return_type, ac end end convert.each do |from, to| values = ac[from] (accumulator[to] ||= []).concat values if values end else end end |
.array_of(*types) ⇒ Object
306 307 308 309 |
# File 'lib/katakata_irb/types.rb', line 306 def self.array_of(*types) type = types.size >= 2 ? UnionType[*types] : types.first || OBJECT InstanceType.new Array, Elem: type end |
.class_name_of(klass) ⇒ Object
30 31 32 33 |
# File 'lib/katakata_irb/types.rb', line 30 def self.class_name_of(klass) klass = klass.superclass if klass.singleton_class? MODULE_NAME_METHOD.bind_call klass end |
.from_rbs_type(return_type, self_type, extra_vars = {}) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/katakata_irb/types.rb', line 311 def self.from_rbs_type(return_type, self_type, extra_vars = {}) case return_type when RBS::Types::Bases::Self self_type when RBS::Types::Bases::Bottom, RBS::Types::Bases::Nil NIL when RBS::Types::Bases::Any, RBS::Types::Bases::Void OBJECT when RBS::Types::Bases::Class self_type.transform do |type| case type in SingletonType InstanceType.new(self_type.module_or_class.is_a?(Class) ? Class : Module) in InstanceType SingletonType.new type.klass end end UnionType[*types] when RBS::Types::Bases::Bool BOOLEAN when RBS::Types::Bases::Instance self_type.transform do |type| if type.is_a?(SingletonType) && type.module_or_class.is_a?(Class) InstanceType.new type.module_or_class else OBJECT end end when RBS::Types::Union UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }] when RBS::Types::Proc PROC when RBS::Types::Tuple elem = UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }] InstanceType.new Array, Elem: elem when RBS::Types::Record InstanceType.new Hash, K: SYMBOL, V: OBJECT when RBS::Types::Literal InstanceType.new return_type.literal.class when RBS::Types::Variable if extra_vars.key? return_type.name extra_vars[return_type.name] elsif self_type.is_a? InstanceType self_type.params[return_type.name] || OBJECT elsif self_type.is_a? UnionType types = self_type.types.filter_map do |t| t.params[return_type.name] if t.is_a? InstanceType end UnionType[*types] else OBJECT end when RBS::Types::Optional UnionType[from_rbs_type(return_type.type, self_type, extra_vars), NIL] when RBS::Types::Alias case return_type.name.name when :int INTEGER when :boolish BOOLEAN when :string STRING else # TODO: ??? OBJECT end when RBS::Types::Interface # unimplemented OBJECT when RBS::Types::ClassInstance klass = return_type.name.to_namespace.path.reduce(Object) { _1.const_get _2 } if return_type.args args = return_type.args.map { from_rbs_type _1, self_type, extra_vars } names = rbs_builder.build_singleton(return_type.name).type_params params = names.map.with_index { [_1, args[_2] || OBJECT] }.to_h end InstanceType.new klass, params || {} end end |
.intersect?(a, b) ⇒ Boolean
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/katakata_irb/types.rb', line 121 def self.intersect?(a, b) atypes = a.types.group_by(&:class) btypes = b.types.group_by(&:class) if atypes[SingletonType] && btypes[SingletonType] aa, bb = [atypes, btypes].map {|types| types[SingletonType].map(&:module_or_class) } return true if (aa & bb).any? end aa, bb = [atypes, btypes].map {|types| (types[InstanceType] || []).map(&:klass) } (aa.flat_map(&:ancestors) & bb).any? end |
.load_rbs_builder ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/katakata_irb/types.rb', line 13 def self.load_rbs_builder require 'rbs' require 'rbs/cli' loader = RBS::CLI::LibraryOptions.new.loader loader.add path: Pathname('sig') RBS::DefinitionBuilder.new env: RBS::Environment.from_loader(loader).resolve_type_names rescue LoadError, StandardError => e @rbs_load_error = e puts "\r\nKatakataIRB failed to initialize RBS::DefinitionBuilder: #{e.class}\r\n" puts "See `KatakataIrb::Types.rbs_load_error` for more details.\r\n" nil end |
.match_free_variables(vars, types, values) ⇒ Object
395 396 397 398 399 400 401 |
# File 'lib/katakata_irb/types.rb', line 395 def self.match_free_variables(vars, types, values) accumulator = {} types.zip values do |t, v| _match_free_variable(vars, t, v, accumulator) if v end accumulator.transform_values { UnionType[*_1] } end |
.method_return_bottom?(method) ⇒ Boolean
391 392 393 |
# File 'lib/katakata_irb/types.rb', line 391 def self.method_return_bottom?(method) method.type.return_type.is_a? RBS::Types::Bases::Bottom end |
.method_return_type(type, method_name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/katakata_irb/types.rb', line 47 def self.method_return_type(type, method_name) receivers = type.types.map do |t| case t in SingletonType [t, t.module_or_class, true] in InstanceType [t, t.klass, false] end end types = receivers.flat_map do |receiver_type, klass, singleton| method = rbs_search_method klass, method_name, singleton next [] unless method method.method_types.map do |method| from_rbs_type(method.type.return_type, receiver_type, {}) end end UnionType[*types] end |
.preload_in_thread ⇒ Object
7 8 9 10 11 |
# File 'lib/katakata_irb/types.rb', line 7 def self.preload_in_thread @loader_thread ||= Thread.new do @rbs_builder = load_rbs_builder end end |
.rbs_methods(type, method_name, args_types, kwargs_type, has_block) ⇒ Object
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/katakata_irb/types.rb', line 66 def self.rbs_methods(type, method_name, args_types, kwargs_type, has_block) return [] unless rbs_builder receivers = type.types.map do |t| case t in SingletonType [t, t.module_or_class, true] in InstanceType [t, t.klass, false] end end has_splat = args_types.any? { _1.is_a? Splat } methods_with_score = receivers.flat_map do |receiver_type, klass, singleton| method = rbs_search_method klass, method_name, singleton next [] unless method method.method_types.map do |method_type| score = 0 score += 2 if !!method_type.block == has_block reqs = method_type.type.required_positionals opts = method_type.type.optional_positionals rest = method_type.type.rest_positionals trailings = method_type.type.trailing_positionals keyreqs = method_type.type.required_keywords keyopts = method_type.type.optional_keywords keyrest = method_type.type.rest_keywords args = args_types if kwargs_type&.any? && keyreqs.empty? && keyopts.empty? && keyrest.nil? kw_value_type = UnionType[*kwargs_type.values] args += [InstanceType.new(Hash, K: SYMBOL, V: kw_value_type)] end if has_splat score += 1 if args.count { !(_1.is_a? Splat) } <= reqs.size + opts.size + trailings.size elsif reqs.size + trailings.size <= args.size && (rest || args.size <= reqs.size + opts.size + trailings.size) score += 2 centers = args[reqs.size...-trailings.size] given = args.first(reqs.size) + centers.take(opts.size) + args.last(trailings.size) expected = (reqs + opts.take(centers.size) + trailings).map(&:type) if rest given << UnionType[*centers.drop(opts.size)] expected << rest.type end if given.any? score += given.zip(expected).count do |t, e| e = from_rbs_type e, receiver_type intersect?(t, e) || (intersect?(STRING, e) && t.methods.include?(:to_str)) || (intersect?(INTEGER, e) && t.methods.include?(:to_int)) || (intersect?(ARRAY, e) && t.methods.include?(:to_ary)) end.fdiv(given.size) end end [[method_type, given || [], expected || []], score] end end max_score = methods_with_score.map(&:last).max methods_with_score.select { _2 == max_score }.map(&:first) end |
.rbs_search_method(klass, method_name, singleton) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/katakata_irb/types.rb', line 35 def self.rbs_search_method(klass, method_name, singleton) klass.ancestors.each do |ancestor| name = class_name_of ancestor next unless name && rbs_builder type_name = RBS::TypeName(name).absolute! definition = (singleton ? rbs_builder.build_singleton(type_name) : rbs_builder.build_instance(type_name)) rescue nil method = definition.methods[method_name] if definition return method if method end nil end |
.type_from_object(object) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/katakata_irb/types.rb', line 133 def self.type_from_object(object) case object when Array, Hash, Module type_from_object_recursive(object, max_level: 4) else klass = object.singleton_class rescue object.class InstanceType.new klass end end |
.type_from_object_recursive(object, max_level:) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/katakata_irb/types.rb', line 143 def self.type_from_object_recursive(object, max_level:) max_level -= 1 sample_size = 1000 case object when Array values = object.size > sample_size ? object.sample(sample_size) : object if max_level > 0 InstanceType.new Array, { Elem: UnionType[*values.map { type_from_object_recursive(_1, max_level: max_level) }] } else InstanceType.new Array, { Elem: UnionType[*values.map(&:class).uniq.map { InstanceType.new _1 }] } end when Hash keys = object.size > sample_size ? object.keys.sample(sample_size) : object.keys values = object.size > sample_size ? object.values.sample(sample_size) : object.values if max_level > 0 key_types = keys.map { type_from_object_recursive(_1, max_level: max_level) } value_types = values.map { type_from_object_recursive(_1, max_level: max_level) } InstanceType.new Hash, { K: UnionType[*key_types], V: UnionType[*value_types] } else key_types = keys.map(&:class).uniq.map { InstanceType.new _1 } value_types = values.map(&:class).uniq.map { InstanceType.new _1 } InstanceType.new Hash, { K: UnionType[*key_types], V: UnionType[*value_types] } end when Module SingletonType.new object else InstanceType.new object.class end end |