123456789_123456789_123456789_123456789_123456789_

Class: RBS::DefinitionBuilder::MethodBuilder

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rbs/definition_builder/method_builder.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(env:) ⇒ MethodBuilder

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 91

def initialize(env:)
  @env = env

  @instance_methods = {}
  @singleton_methods = {}
  @interface_methods = {}
end

Instance Attribute Details

#env (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 86

attr_reader :env

#instance_methods (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 87

attr_reader :instance_methods

#interface_methods (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 89

attr_reader :interface_methods

#singleton_methods (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 88

attr_reader :singleton_methods

Instance Method Details

#build_alias(methods, type, member:)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 194

def build_alias(methods, type, member:)
  defn = methods.methods[member.new_name] ||= Methods::Definition.empty(type: type, name: member.new_name)
  defn.originals << member
end

#build_attribute(methods, type, member:, accessibility:)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 199

def build_attribute(methods, type, member:, accessibility:)
  if member.is_a?(AST::Members::AttrReader) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

    defn.accessibilities << accessibility
    defn.originals << member
  end

  if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[:"#{member.name}="] ||= Methods::Definition.empty(type: type, name: :"#{member.name}=")

    defn.accessibilities << accessibility
    defn.originals << member
  end
end

#build_instance(type_name)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 99

def build_instance(type_name)
  instance_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      args = entry.type_params.map {|param| Types::Variable.new(name: param.name, location: param.location) }
      type = Types::ClassInstance.new(name: type_name, args: args, location: nil)
      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          subst = Substitution.build(d.decl.type_params.each.map(&:name), args)
          each_member_with_accessibility(d.decl.members) do |member, accessibility|
            case member
            when AST::Members::MethodDefinition
              case member.kind
              when :instance
                build_method(
                  methods,
                  type,
                  member: member.update(types: member.types.map {|type| type.sub(subst) }),
                  accessibility: member.visibility || accessibility
                )
              when :singleton_instance
                build_method(
                  methods,
                  type,
                  member: member.update(types: member.types.map {|type| type.sub(subst) }),
                  accessibility: :private
                )
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :instance
                build_attribute(methods,
                                type,
                                member: member.update(type: member.type.sub(subst)),
                                accessibility: member.visibility || accessibility)
              end
            when AST::Members::Alias
              if member.kind == :instance
                build_alias(methods, type, member: member)
              end
            end
          end
        end
      end.validate!
    end
end

#build_interface(type_name)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 174

def build_interface(type_name)
  interface_methods[type_name] ||=
    begin
      entry = env.interface_decls[type_name]
      args = Types::Variable.build(entry.decl.type_params.each.map(&:name))
      type = Types::Interface.new(name: type_name, args: args, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decl.members.each do |member|
          case member
          when AST::Members::MethodDefinition
            build_method(methods, type, member: member, accessibility: :public)
          when AST::Members::Alias
            build_alias(methods, type, member: member)
          end
        end
      end.validate!
    end
end

#build_method(methods, type, member:, accessibility:)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 215

def build_method(methods, type, member:, accessibility:)
  defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

  if member.overload?
    defn.overloads << member
  else
    defn.accessibilities << accessibility
    defn.originals << member
  end
end

#build_singleton(type_name)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 145

def build_singleton(type_name)
  singleton_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      type = Types::ClassSingleton.new(name: type_name, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          d.decl.members.each do |member|
            case member
            when AST::Members::MethodDefinition
              if member.singleton?
                build_method(methods, type, member: member, accessibility: member.visibility || :public)
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :singleton
                build_attribute(methods, type, member: member, accessibility: member.visibility || :public)
              end
            when AST::Members::Alias
              if member.kind == :singleton
                build_alias(methods, type, member: member)
              end
            end
          end
        end
      end.validate!
    end
end

#each_member_with_accessibility(members, accessibility: :public)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 226

def each_member_with_accessibility(members, accessibility: :public)
  members.each do |member|
    case member
    when AST::Members::Public
      accessibility = :public
    when AST::Members::Private
      accessibility = :private
    else
      yield member, accessibility
    end
  end
end

#update(env:, except:)

[ GitHub ]

  
# File 'lib/rbs/definition_builder/method_builder.rb', line 239

def update(env:, except:)
  MethodBuilder.new(env: env).tap do |copy|
    copy.instance_methods.merge!(instance_methods)
    copy.singleton_methods.merge!(singleton_methods)
    copy.interface_methods.merge!(interface_methods)

    except.each do |type_name|
      copy.instance_methods.delete(type_name)
      copy.singleton_methods.delete(type_name)
      copy.interface_methods.delete(type_name)
    end
  end
end