|
| 1 | +module Anchor::Types::Inference |
| 2 | + module RBS |
| 3 | + class << self |
| 4 | + # @param class_name [Symbol] e.g. :Exhaustive |
| 5 | + # @param method_name [Symbol] e.g. :model_overridden |
| 6 | + def from(class_name, method_name) |
| 7 | + @loader ||= ::RBS::EnvironmentLoader.new.tap do |l| |
| 8 | + l.add(path: Rails.root.join("sig")) |
| 9 | + end |
| 10 | + |
| 11 | + @env ||= ::RBS::Environment.from_loader(@loader).resolve_type_names |
| 12 | + |
| 13 | + # TODO: Do we need both the absolute and non-absolute namespaces? |
| 14 | + klass = @env.class_decls.keys.find { |kl| [class_name.to_s, "::#{class_name}"].include?(kl.to_s) } |
| 15 | + return Types::Unknown unless klass |
| 16 | + |
| 17 | + @builder ||= ::RBS::DefinitionBuilder.new(env: @env) |
| 18 | + instance = @builder.build_instance(klass) |
| 19 | + |
| 20 | + instance_method = instance.methods[method_name] |
| 21 | + return Types::Unknown unless instance_method |
| 22 | + |
| 23 | + method_types = instance.methods[method_name].method_types |
| 24 | + return Types::Unknown unless method_types.length == 1 |
| 25 | + |
| 26 | + return_type = method_types.first.type.return_type |
| 27 | + from_rbs_type(return_type) |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def from_rbs_type(type) |
| 33 | + case type |
| 34 | + when ::RBS::Types::ClassInstance then from_class_instance(type) |
| 35 | + when ::RBS::Types::Literal then Types::Literal.new(type.literal) |
| 36 | + when ::RBS::Types::Bases::Bool then Types::Boolean |
| 37 | + when ::RBS::Types::Bases::Nil then Types::Null |
| 38 | + when ::RBS::Types::Bases::Void then Types::Unknown |
| 39 | + when ::RBS::Types::Bases::Any then Types::Unknown |
| 40 | + when ::RBS::Types::Optional then Types::Maybe.new(from_rbs_type(type.type)) |
| 41 | + when ::RBS::Types::Record then from_record(type) |
| 42 | + when ::RBS::Types::Union then from_union(type) |
| 43 | + when ::RBS::Types::Intersection then from_intersection(type) |
| 44 | + when ::RBS::Types::Tuple then Types::Unknown # TODO |
| 45 | + else Types::Unknown |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def from_record(type) |
| 50 | + properties = type.fields.map do |name, type| |
| 51 | + Types::Property.new(name, from_rbs_type(type)) |
| 52 | + end |
| 53 | + optional_properties = type.optional_fields.map do |name, type| |
| 54 | + Types::Property.new(name, from_rbs_type(type), true) |
| 55 | + end |
| 56 | + Types::Object.new(properties + optional_properties) |
| 57 | + end |
| 58 | + |
| 59 | + def from_union(type) |
| 60 | + types = type.types.map { |type| from_rbs_type(type) } |
| 61 | + Types::Union.new(types) |
| 62 | + end |
| 63 | + |
| 64 | + def from_intersection(type) |
| 65 | + types = type.types.map { |type| from_rbs_type(type) } |
| 66 | + Types::Intersection.new(types) |
| 67 | + end |
| 68 | + |
| 69 | + def from_class_instance(type) |
| 70 | + case type.name.to_s |
| 71 | + when "::String" then Types::String |
| 72 | + when "::Numeric" then Types::Float |
| 73 | + when "::Integer" then Types::Integer |
| 74 | + when "::Float" then Types::Float |
| 75 | + when "::BigDecimal" then Types::String |
| 76 | + when "::Boolean" then Types::Boolean |
| 77 | + when "::Array" then Types::Array.new(from_rbs_type(type.args.first)) |
| 78 | + else Types::Unknown |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments