# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/lib/rbs/environment/module_entry.rb, line 34 def primary_decl each_decl.first or raise end
class RBS::Environment::ModuleEntry
Represents a class entry in the environment
entry = ModuleEntry.new(TypeName.parse("::Kernel")) entry << [nil, declaration] entry << [[nil, TypeName.parse("::Object")], declaration]
Attributes
Array[context_decl]
Public Class Methods
(TypeName) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/lib/rbs/environment/module_entry.rb, line 10 def initialize(name) @name = name @context_decls = [] end
Public Instance Methods
(context_decl) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/lib/rbs/environment/module_entry.rb, line 15 def <<(context_decl) context_decls << context_decl self end
(declaration | ClassEntry::declaration) → Substitution?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/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
Returns a substitution that renames the type parameters of the declaration to the entry’s type parameters (type_params)
Returns nil if the declaration uses the same type parameter names as type_params.
() { (declaration) → void } → void
() → Enumerator[declaration]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/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
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/lib/rbs/environment/module_entry.rb, line 30 def empty? context_decls.empty? end
Returns true if the entry doesn’t have any declaration
() → declaration
Source
Returns the first declaration
This method helps using the class with ClassEntry objects.
() → Array[AST::Declarations::Module::Self]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/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
Returns the self types of the declarations
The type variables in the self types are aligned to type_params, so that the self types from declarations with different type parameter names can be compared and used with type_params.
Note that the returned objects may be different from the ones in the declarations, but #location points to the original declaration.
() → Array[AST::TypeParam]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/lib/rbs/environment/module_entry.rb, line 38 def type_params validate_type_params primary_decl.type_params end
Returns the generics declaration
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/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
Confirms if the type parameters in the declaration are compatible
-
Raises
GenericParameterMismatchErrorif incompatible declaration is detected.