Skip to content

Commit

Permalink
Merge pull request #1373 from ruby/type-name-resolver
Browse files Browse the repository at this point in the history
Make TypeNameResolver more compatible with Ruby
  • Loading branch information
soutaro committed Jul 27, 2023
1 parent 2995ca3 commit 8bce365
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
40 changes: 34 additions & 6 deletions lib/rbs/resolver/type_name_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,50 @@ def resolve(type_name, context:)
end

try_cache([type_name, context]) do
resolve_in(type_name, context)
head, tail = partition(type_name)

head = resolve_in(head, context)

if head
if tail
has_name?(tail.with_prefix(head.to_namespace))
else
head
end
end
end
end

def partition(type_name)
if type_name.namespace.empty?
head = type_name.name
tail = nil
else
head, *tail = type_name.namespace.path

head or raise

tail = TypeName.new(
name: type_name.name,
namespace: Namespace.new(absolute: false, path: tail)
)
end

[head, tail]
end

def resolve_in(type_name, context)
def resolve_in(head, context)
if context
parent, child = context
case child
when false
resolve_in(type_name, parent)
resolve_in(head, parent)
when TypeName
name = type_name.with_prefix(child.to_namespace)
has_name?(name) || resolve_in(type_name, parent)
name = TypeName.new(name: head, namespace: child.to_namespace)
has_name?(name) || resolve_in(head, parent)
end
else
has_name?(type_name.absolute!)
has_name?(TypeName.new(name: head, namespace: Namespace.root))
end
end

Expand Down
4 changes: 3 additions & 1 deletion sig/resolver/type_name_resolver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ module RBS

def try_cache: (query) { () -> TypeName? } -> TypeName?

def resolve_in: (TypeName, context) -> TypeName?
def resolve_in: (Symbol, context) -> TypeName?

def partition: (TypeName) -> [Symbol, TypeName?]
end
end
end
20 changes: 20 additions & 0 deletions test/rbs/resolver/type_name_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ class Bar
end
end

def test_resolve_failure
SignatureManager.new do |manager|
manager.files[Pathname("foo.rbs")] = <<EOF
class Foo
class Foo
end
class Bar
end
end
EOF
manager.build do |env|
resolver = Resolver::TypeNameResolver.new(env)

assert_nil resolver.resolve(type_name("Foo::Bar"), context: [[nil, TypeName("::Foo")], TypeName("::Foo::Foo")])
end
end
end


def test_duplicated_resolve
SignatureManager.new do |manager|
manager.files[Pathname("foo.rbs")] = <<EOF
Expand Down

0 comments on commit 8bce365

Please sign in to comment.