Class: RBS::Environment::ModuleEntry

Inherits:
Object
  • Object
show all
Defined in:
sig/environment/module_entry.rbs,
lib/rbs/environment/module_entry.rb

Overview

Represents a class entry in the environment

entry = ModuleEntry.new(TypeName.parse("::Kernel"))
entry << [nil, declaration]
entry << [[nil, TypeName.parse("::Object")], declaration]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ModuleEntry

Returns a new instance of ModuleEntry.

Parameters:



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

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

Instance Attribute Details

#context_declsArray[context_decl] (readonly)

Returns the value of attribute context_decls.

Returns:

  • (Array[context_decl])


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

def context_decls
  @context_decls
end

#nameTypeName (readonly)

Returns the value of attribute name.

Returns:



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

def name
  @name
end

Instance Method Details

#<<(context_decl) ⇒ self

Parameters:

  • (context_decl)

Returns:

  • (self)


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

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

#each_declvoid #each_declEnumerator[declaration]

Overloads:

  • #each_declvoid

    This method returns an undefined value.

  • #each_declEnumerator[declaration]

    Returns:

    • (Enumerator[declaration])

Yields:

Yield Parameters:

  • arg0 (declaration)

Yield Returns:

  • (void)


20
21
22
23
24
25
26
27
28
# 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

#empty?Boolean

Returns true if the entry doesn't have any declaration

Returns:

  • (Boolean)


29
30
31
# File 'sig/environment/module_entry.rbs', line 29

def empty?
  context_decls.empty?
end

#primary_decldeclaration

Returns the first declaration

This method helps using the class with ClassEntry objects.

Returns:

  • (declaration)


35
36
37
# File 'sig/environment/module_entry.rbs', line 35

def primary_decl
  each_decl.first or raise
end

#self_typesArray[AST::Declarations::Module::Self]



43
44
45
46
47
# File 'lib/rbs/environment/module_entry.rb', line 43

def self_types
  each_decl.flat_map do |decl|
    decl.self_types
  end.uniq
end

#type_paramsArray[AST::TypeParam]

Returns the generics declaration

Returns:



39
40
41
42
# File 'sig/environment/module_entry.rbs', line 39

def type_params
  validate_type_params
  primary_decl.type_params
end

#validate_type_paramsvoid

This method returns an undefined value.

Confirms if the type parameters in the declaration are compatible

  • Raises GenericParameterMismatchError if incompatible declaration is detected.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'sig/environment/module_entry.rbs', line 45

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