lib/rbs/definition_builder.rb in rbs-1.0.6 vs lib/rbs/definition_builder.rb in rbs-1.1.0
- old
+ new
@@ -8,15 +8,15 @@
attr_reader :instance_cache
attr_reader :singleton_cache
attr_reader :singleton0_cache
attr_reader :interface_cache
- def initialize(env:)
+ def initialize(env:, ancestor_builder: nil, method_builder: nil)
@env = env
@type_name_resolver = TypeNameResolver.from_env(env)
- @ancestor_builder = AncestorBuilder.new(env: env)
- @method_builder = MethodBuilder.new(env: env)
+ @ancestor_builder = ancestor_builder || AncestorBuilder.new(env: env)
+ @method_builder = method_builder || MethodBuilder.new(env: env)
@instance_cache = {}
@singleton_cache = {}
@singleton0_cache = {}
@interface_cache = {}
@@ -78,10 +78,11 @@
alias_of: nil
)
when AST::Members::Alias
unless definition.methods.key?(original.old_name)
raise UnknownMethodAliasError.new(
+ type_name: type_name,
original_name: original.old_name,
aliased_name: original.new_name,
location: original.location
)
end
@@ -358,19 +359,14 @@
case entry
when Environment::ClassEntry, Environment::ModuleEntry
ancestors = ancestor_builder.singleton_ancestors(type_name)
self_type = Types::ClassSingleton.new(name: type_name, location: nil)
- instance_type = Types::ClassInstance.new(
- name: type_name,
- args: entry.type_params.each.map { Types::Bases::Any.new(location: nil) },
- location: nil
- )
Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
def0 = build_singleton0(type_name)
- subst = Substitution.build([], [], instance_type: instance_type)
+ subst = Substitution.new
merge_definition(src: def0, dest: definition, subst: subst, keep_super: true)
if entry.is_a?(Environment::ClassEntry)
new_method = definition.methods[:new]
@@ -559,10 +555,11 @@
existing_method = interface_methods[method_name] || definition.methods[method_name]
original_method = interface_methods[original.old_name] || definition.methods[original.old_name]
unless original_method
raise UnknownMethodAliasError.new(
+ type_name: definition.type_name,
original_name: original.old_name,
aliased_name: original.new_name,
location: original.location
)
end
@@ -728,15 +725,17 @@
declared_in: variable.declared_in
)
end
def merge_method(type_name, methods, name, method, sub, implemented_in: :keep, keep_super: false)
- defs = method.defs.yield_self do |defs|
+ if sub.empty? && implemented_in == :keep && keep_super
+ methods[name] = method
+ else
if sub.empty? && implemented_in == :keep
- defs
+ defs = method.defs
else
- defs.map do |defn|
+ defs = method.defs.map do |defn|
defn.update(
type: sub.empty? ? defn.type : defn.type.sub(sub),
implemented_in: case implemented_in
when :keep
defn.implemented_in
@@ -746,20 +745,20 @@
implemented_in
end
)
end
end
- end
- super_method = methods[name]
+ super_method = methods[name]
- methods[name] = Definition::Method.new(
- super_method: keep_super ? method.super_method : super_method,
- accessibility: method.accessibility,
- defs: defs,
- alias_of: method.alias_of
- )
+ methods[name] = Definition::Method.new(
+ super_method: keep_super ? method.super_method : super_method,
+ accessibility: method.accessibility,
+ defs: defs,
+ alias_of: method.alias_of
+ )
+ end
end
def try_cache(type_name, cache:, key: type_name)
# @type var cc: Hash[untyped, Definition | false | nil]
cc = _ = cache
@@ -785,8 +784,27 @@
def expand_alias(type_name)
entry = env.alias_decls[type_name] or raise "Unknown name for expand_alias: #{type_name}"
ensure_namespace!(type_name.namespace, location: entry.decl.location)
entry.decl.type
+ end
+
+ def update(env:, except:, ancestor_builder:)
+ method_builder = self.method_builder.update(env: env, except: except)
+
+ DefinitionBuilder.new(env: env, ancestor_builder: ancestor_builder, method_builder: method_builder).tap do |builder|
+ builder.instance_cache.merge!(instance_cache)
+ builder.singleton_cache.merge!(singleton_cache)
+ builder.singleton0_cache.merge!(singleton0_cache)
+ builder.interface_cache.merge!(interface_cache)
+
+ except.each do |name|
+ builder.instance_cache.delete([name, true])
+ builder.instance_cache.delete([name, false])
+ builder.singleton_cache.delete(name)
+ builder.singleton0_cache.delete(name)
+ builder.interface_cache.delete(name)
+ end
+ end
end
end
end