lib/rbs/environment.rb in rbs-0.5.0 vs lib/rbs/environment.rb in rbs-0.6.0
- old
+ new
@@ -32,60 +32,53 @@
@decls = []
end
def insert(decl:, outer:)
decls << D.new(decl: decl, outer: outer)
+ @primary = nil
end
+ def validate_type_params
+ unless decls.empty?
+ hd_decl, *tl_decls = decls
+ hd_params = hd_decl.decl.type_params
+ hd_names = hd_params.params.map(&:name)
+
+ tl_decls.each do |tl_decl|
+ tl_params = tl_decl.decl.type_params
+
+ unless hd_params.size == tl_params.size && hd_params == tl_params.rename_to(hd_names)
+ raise GenericParameterMismatchError.new(name: name, decl: tl_decl.decl)
+ end
+ end
+ end
+ end
+
def type_params
primary.decl.type_params
end
end
class ModuleEntry < MultiEntry
- def insert(decl:, outer:)
- unless decl.is_a?(AST::Declarations::Module)
- raise MixedClassModuleDeclarationError.new(name: name, decl: decl)
- end
-
- unless decls.empty?
- names = decls[0].decl.type_params.each.map(&:name)
- unless names.size == decl.type_params.each.size && decls[0].decl.type_params == decl.type_params.rename_to(names)
- raise GenericParameterMismatchError.new(name: name, decl: decl)
- end
- end
-
- super(decl: decl, outer: outer)
+ def self_types
+ decls.flat_map do |d|
+ d.decl.self_types
+ end.uniq
end
def primary
- @primary ||= decls.find {|d| d.decl.self_type } || decls.first
+ @primary ||= begin
+ validate_type_params
+ decls.first
+ end
end
-
- def self_type
- primary.decl.self_type
- end
end
class ClassEntry < MultiEntry
- def insert(decl:, outer:)
- unless decl.is_a?(AST::Declarations::Class)
- raise MixedClassModuleDeclarationError.new(name: name, decl: decl)
- end
-
- unless decls.empty?
- names = decls[0].decl.type_params.each.map(&:name)
- unless names.size == decl.type_params.each.size && decls[0].decl.type_params == decl.type_params.rename_to(names)
- raise GenericParameterMismatchError.new(name: name, decl: decl)
- end
- end
-
- super(decl: decl, outer: outer)
- end
-
def primary
@primary ||= begin
+ validate_type_params
decls.find {|d| d.decl.super_class } || decls.first
end
end
end
@@ -155,12 +148,23 @@
when AST::Declarations::Module
class_decls[name] ||= ModuleEntry.new(name: name)
end
end
- class_decls[name].insert(decl: decl, outer: outer)
+ existing_entry = class_decls[name]
+ case
+ when decl.is_a?(AST::Declarations::Module) && existing_entry.is_a?(ModuleEntry)
+ # OK
+ when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry)
+ # OK
+ else
+ raise DuplicatedDeclarationError.new(name, decl, existing_entry.primary.decl)
+ end
+
+ existing_entry.insert(decl: decl, outer: outer)
+
prefix = outer + [decl]
ns = name.to_namespace
decl.each_decl do |d|
insert_decl(d, outer: prefix, namespace: ns)
end
@@ -253,12 +257,16 @@
outer_ = outer + [decl]
prefix_ = prefix + decl.name.to_namespace
AST::Declarations::Module.new(
name: decl.name.with_prefix(prefix),
type_params: decl.type_params,
- self_type: decl.self_type&.yield_self do |self_type|
- absolute_type(resolver, self_type, context: context)
+ self_types: decl.self_types.map do |module_self|
+ AST::Declarations::Module::Self.new(
+ name: absolute_type_name(resolver, module_self.name, context: context),
+ args: module_self.args.map {|type| absolute_type(resolver, type, context: context) },
+ location: module_self.location
+ )
end,
members: decl.members.map do |member|
case member
when AST::Members::Base
resolve_member(resolver, member, context: context)
@@ -403,8 +411,13 @@
def absolute_type(resolver, type, context:)
type.map_type_name do |name|
absolute_type_name(resolver, name, context: context)
end
+ end
+
+ def inspect
+ ivars = %i[@buffers @declarations @class_decls @interface_decls @alias_decls @constant_decls @global_decls]
+ "\#<RBS::Environment #{ivars.map { |iv| "#{iv}=(#{instance_variable_get(iv).size} items)"}.join(' ')}>"
end
end
end