Skip to content

Commit

Permalink
Merge pull request #395 from Shopify/at-rbs-class-type
Browse files Browse the repository at this point in the history
Properly handle `T.class_of` and `T::Class` in RBS translation
  • Loading branch information
Morriar authored Jan 15, 2025
2 parents 0297249 + b7c2336 commit 61cdaf1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/rbi/rbs_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,9 @@ def visit_type_parameter(type)

sig { params(type: Type::Class).void }
def visit_class(type)
@string << "singleton("
@string << "Class["
visit(type.type)
@string << ")"
@string << "]"
end

private
Expand Down
35 changes: 31 additions & 4 deletions test/rbi/rbs_printer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ def test_print_methods_with_signatures
sig { params(a: A, b: T.nilable(B)).returns(T.anything) }
sig { params(a: T.nilable(A), b: B).returns(T.untyped) }
sig { params(a: A, b: B).returns(T.noreturn) }
sig { params(a: A, b: B).returns(T.class_of(C)) }
sig { params(a: A, b: B).returns(T::Class[C]) }
sig { params(a: A, b: B).returns(T::Array[C]) }
sig { params(a: A, b: B).returns(T::Hash[C, D]) }
sig { params(a: A, b: B).returns(T::Set[C]) }
Expand All @@ -365,8 +363,6 @@ def foo: (A a, B b) -> C?
| (A a, B? b) -> top
| (A? a, B b) -> untyped
| (A a, B b) -> bot
| (A a, B b) -> singleton(C)
| (A a, B b) -> singleton(C)
| (A a, B b) -> Array[C]
| (A a, B b) -> Hash[C, D]
| (A a, B b) -> Set[C]
Expand Down Expand Up @@ -589,6 +585,37 @@ def foo: { (?) -> untyped } -> void
RBI
end

def test_print_class_type
rbi = parse_rbi(<<~RBI)
sig { returns(T.class_of(String)) }
def a; end
sig { returns(T::Class[String]) }
def b; end
sig { returns(Class) }
def c; end
sig { returns(T::Class[T.anything]) }
def d; end
sig { returns(T::Class[T.untyped]) }
def e; end
RBI

assert_equal(<<~RBI, rbi.rbs_string)
def a: -> singleton(String)
def b: -> Class[String]
def c: -> Class
def d: -> Class[top]
def e: -> Class[untyped]
RBI
end

def test_print_t_structs
rbi = parse_rbi(<<~RBI)
class Foo < T::Struct; end
Expand Down

0 comments on commit 61cdaf1

Please sign in to comment.