Skip to content

Commit

Permalink
Fix type generation for types named AttachedClass that are not `T::Ty…
Browse files Browse the repository at this point in the history
…pes::AttachedClassType`

Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Aug 29, 2024
1 parent d444243 commit 6dcabe5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/runtime/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def signature_of(method)

sig { params(type: T::Types::Base).returns(String) }
def name_of_type(type)
type.to_s.gsub(/\bAttachedClass\b/, "T.attached_class")
type.to_s
end

sig { params(constant: Module, method: Symbol).returns(Method) }
Expand Down
2 changes: 2 additions & 0 deletions sorbet/rbi/shims/sorbet.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ module T::Private
end
end

class T::Types::AttachedClassType < T::Types::Base; end

class T::Enum
def values; end
end
6 changes: 4 additions & 2 deletions spec/tapioca/gem/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3949,6 +3949,7 @@ def foo(*args, &blk); end
add_ruby_file("foo.rb", <<~RUBY)
class Foo
class FooAttachedClass; end
class AttachedClass; end
class << self
extend(T::Sig)
Expand All @@ -3962,7 +3963,7 @@ def b
{ Foo.new => [Foo.new] }
end
sig { returns(FooAttachedClass) }
sig { returns(T.any(FooAttachedClass, AttachedClass)) }
def c
FooAttachedClass.new
end
Expand All @@ -3979,11 +3980,12 @@ def a; end
sig { returns(T::Hash[T.attached_class, T::Array[T.attached_class]]) }
def b; end
sig { returns(::Foo::FooAttachedClass) }
sig { returns(T.any(::Foo::AttachedClass, ::Foo::FooAttachedClass)) }
def c; end
end
end
class Foo::AttachedClass; end
class Foo::FooAttachedClass; end
RBI

Expand Down
15 changes: 14 additions & 1 deletion spec/tapioca/runtime/reflection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Runtime
class LyingFoo < BasicObject
include ::Kernel

class AttachedClass; end

class << self
def constants
[::Symbol, ::String]
Expand Down Expand Up @@ -106,7 +108,7 @@ class ReflectionSpec < Minitest::Spec
it "return the correct results with Reflection helpers" do
foo = LyingFoo.new

assert_equal([], Runtime::Reflection.constants_of(LyingFoo))
assert_equal([:AttachedClass], Runtime::Reflection.constants_of(LyingFoo))
assert_equal("Tapioca::Runtime::LyingFoo", Runtime::Reflection.name_of(LyingFoo))
assert_equal([Tapioca::Runtime::LyingFoo, Kernel, BasicObject], Runtime::Reflection.ancestors_of(LyingFoo))
assert_equal(BasicObject, Runtime::Reflection.superclass_of(LyingFoo))
Expand Down Expand Up @@ -135,6 +137,17 @@ class ReflectionSpec < Minitest::Spec
assert_equal("::Tapioca::Runtime::LyingFoo", Runtime::Reflection.qualified_name_of(LyingFoo))
end

it "returns the right name for attached classes" do
assert_equal(
"::Tapioca::Runtime::LyingFoo::AttachedClass",
Runtime::Reflection.name_of_type(T::Types::Simple.new(LyingFoo::AttachedClass)),
)
assert_equal(
"T.attached_class",
Runtime::Reflection.name_of_type(T::Types::AttachedClassType.new),
)
end

describe "signature_for" do
it "returns a valid signature" do
method = SignatureFoo.instance_method(:good_method)
Expand Down

0 comments on commit 6dcabe5

Please sign in to comment.