123456789_123456789_123456789_123456789_123456789_

Class: RBS::Environment::ModuleEntry

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/environment/module_entry.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name) ⇒ ModuleEntry

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 10

def initialize(name)
  @name = name
  @context_decls = []
end

Instance Attribute Details

#context_decls (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 8

attr_reader :context_decls

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 30

def empty?
  context_decls.empty?
end

#name (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 6

attr_reader :name

Instance Method Details

#<<(context_decl)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 15

def <<(context_decl)
  context_decls << context_decl
  self
end

#align_params(decl)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 62

def align_params(decl)
  entry_params = type_params
  decl_param_names = decl.type_params.map(&:name)

  return nil if decl_param_names == entry_params.map(&:name)

  Substitution.build(
    decl_param_names,
    entry_params.map {|param| Types::Variable.new(name: param.name, location: param.location) }
  )
end

#each_decl(&block)

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 20

def each_decl(&block)
  if block
    context_decls.each do |_, decl|
      yield decl
    end
  else
    enum_for(__method__ || raise)
  end
end

#primary_decl

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 34

def primary_decl
  each_decl.first or raise
end

#self_types

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 43

def self_types
  each_decl.flat_map do |decl|
    self_types = decl.self_types
    subst = align_params(decl)

    if self_types.empty? || subst.nil?
      self_types
    else
      self_types.map do |self_type|
        AST::Declarations::Module::Self.new(
          name: self_type.name,
          args: self_type.args.map {|type| type.sub(subst) },
          location: self_type.location
        )
      end
    end
  end.uniq
end

#type_params

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 38

def type_params
  validate_type_params
  primary_decl.type_params
end

#validate_type_params

[ GitHub ]

  
# File 'lib/rbs/environment/module_entry.rb', line 74

def validate_type_params
  unless context_decls.empty?
    first_decl, *rest_decls = each_decl.to_a
    first_decl or raise

    first_params = first_decl.type_params
    first_names = first_params.map(&:name)
    rest_decls.each do |other_decl|
      other_params = other_decl.type_params
      unless first_names.size == other_params.size && first_params == AST::TypeParam.rename(other_params, new_names: first_names)
        raise GenericParameterMismatchError.new(name: name, decl: other_decl)
      end
    end
  end
end