lib/steep/interface/builder.rb in steep-0.8.2 vs lib/steep/interface/builder.rb in steep-0.9.0
- old
+ new
@@ -130,15 +130,15 @@
cache_interface(module_cache, key: signature.name) do
module_to_interface(signature)
end
end
- def build_instance(module_name, with_initialize:)
+ def build_instance(module_name)
assert_absolute_name! module_name
signature = signatures.find_class_or_module(module_name, current_module: AST::Namespace.root)
- cache_interface(instance_cache, key: [signature.name, with_initialize]) do
- instance_to_interface(signature, with_initialize: with_initialize)
+ cache_interface(instance_cache, key: signature.name) do
+ instance_to_interface(signature)
end
end
def build_interface(interface_name)
signature = signatures.find_interface(interface_name)
@@ -171,12 +171,22 @@
def add_method(type_name, method, methods:, extra_attributes: [], current:)
super_method = methods[method.name]
new_method = Method.new(
type_name: type_name,
name: method.name,
- types: method.types.map do |method_type|
- method_type_to_method_type(method_type, current: current)
+ types: method.types.flat_map do |method_type|
+ case method_type
+ when AST::MethodType
+ [method_type_to_method_type(method_type, current: current)]
+ when AST::MethodType::Super
+ if super_method
+ super_method.types
+ else
+ Steep.logger.error "`super` specified in method type, but cannot find super method of `#{method.name}` in `#{type_name}` (#{method.location.name || "-"}:#{method.location})"
+ []
+ end
+ end
end,
super_method: super_method,
attributes: method.attributes + extra_attributes
)
@@ -207,11 +217,11 @@
super_method: nil,
attributes: [:incompatible]
)
}
- klass = build_instance(AST::Builtin::Class.module_name, with_initialize: false)
+ klass = build_instance(AST::Builtin::Class.module_name)
instantiated = klass.instantiate(
type: AST::Types::Self.new,
args: [],
instance_type: AST::Types::Instance.new,
module_type: AST::Types::Class.new
@@ -236,11 +246,11 @@
ivars: {},
current: namespace)
end
when AST::Signature::Members::Extend
member_name = signatures.find_module(member.name, current_module: namespace).name
- build_instance(member_name, with_initialize: false).yield_self do |module_interface|
+ build_instance(member_name).yield_self do |module_interface|
merge_mixin(module_interface,
member.args.map {|type| absolute_type(type, current: namespace) },
methods: methods,
ivars: {},
supers: supers,
@@ -316,11 +326,11 @@
supers = [sig.self_type].compact.map {|type| absolute_type(type, current: namespace) }
methods = {}
ivar_chains = {}
- module_instance = build_instance(AST::Builtin::Module.module_name, with_initialize: false)
+ module_instance = build_instance(AST::Builtin::Module.module_name)
instantiated = module_instance.instantiate(
type: AST::Types::Self.new,
args: [],
instance_type: AST::Types::Instance.new,
module_type: AST::Types::Class.new
@@ -339,11 +349,11 @@
supers: supers,
current: namespace)
end
when AST::Signature::Members::Extend
member_name = signatures.find_module(member.name, current_module: namespace).name
- build_instance(member_name, with_initialize: false).yield_self do |module_interface|
+ build_instance(member_name).yield_self do |module_interface|
merge_mixin(module_interface,
member.args.map {|type| absolute_type(type, current: namespace) },
methods: methods,
ivars: ivar_chains,
supers: supers,
@@ -385,11 +395,11 @@
supers: supers,
ivar_chains: ivar_chains
)
end
- def instance_to_interface(sig, with_initialize:)
+ def instance_to_interface(sig)
module_name = sig.name
namespace = module_name.namespace.append(module_name.name)
params = sig.params&.variables || []
supers = []
@@ -400,12 +410,11 @@
unless sig.name == AST::Builtin::BasicObject.module_name
super_class_name = sig.super_class&.name || AST::Builtin::Object.module_name
if super_class_name.relative?
super_class_name = signatures.find_class(super_class_name, current_module: namespace).name
end
- super_class_interface = build_instance(super_class_name,
- with_initialize: with_initialize)
+ super_class_interface = build_instance(super_class_name)
supers.push(*super_class_interface.supers)
instantiated = super_class_interface.instantiate(
type: AST::Types::Self.new,
args: (sig.super_class&.args || []).map {|type| absolute_type(type, current: namespace) },
@@ -426,11 +435,11 @@
sig.members.each do |member|
case member
when AST::Signature::Members::Include
member_name = signatures.find_module(member.name, current_module: namespace).name
- build_instance(member_name, with_initialize: false).yield_self do |module_interface|
+ build_instance(member_name).yield_self do |module_interface|
merge_mixin(module_interface,
member.args.map {|type| absolute_type(type, current: namespace) },
methods: methods,
ivars: ivar_chains,
supers: supers,
@@ -441,19 +450,35 @@
sig.members.each do |member|
case member
when AST::Signature::Members::Method
if member.instance_method?
- if with_initialize || member.name != :initialize
- extra_attrs = member.name == :initialize ? [:incompatible] : []
- add_method(module_name, member, methods: methods, extra_attributes: extra_attrs, current: namespace)
- end
+ extra_attrs = member.name == :initialize ? [:incompatible, :private] : []
+ add_method(module_name, member, methods: methods, extra_attributes: extra_attrs, current: namespace)
end
when AST::Signature::Members::Ivar
merge_ivars(ivar_chains,
{ member.name => absolute_type(member.type, current: namespace) })
when AST::Signature::Members::Attr
merge_attribute(sig, ivar_chains, methods, module_name, member, current: namespace)
+ end
+ end
+
+ sig.members.each do |member|
+ case member
+ when AST::Signature::Members::MethodAlias
+ method = methods[member.original_name]
+ if method
+ methods[member.new_name] = Method.new(
+ type_name: module_name,
+ name: member.new_name,
+ types: method.types,
+ super_method: nil,
+ attributes: method.attributes
+ )
+ else
+ Steep.logger.error "Cannot alias find original method `#{member.original_name}` for `#{member.new_name}` in #{module_name} (#{member.location.name || '-'}:#{member.location})"
+ end
end
end
signatures.find_extensions(sig.name).each do |ext|
ext.members.each do |member|