Skip to content

Commit

Permalink
Protect nilable procs with parenthesis when translating to RBS
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Jan 14, 2025
1 parent e9c6406 commit b043e8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rbi/rbs_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,13 @@ def visit_attached_class(type)

sig { params(type: Type::Nilable).void }
def visit_nilable(type)
if type.type.is_a?(Type::Proc)
@string << "("
end
visit(type.type)
if type.type.is_a?(Type::Proc)
@string << ")"
end
@string << "?"
end

Expand Down
11 changes: 11 additions & 0 deletions test/rbi/rbs_printer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,17 @@ def foo: -> instance
RBI
end

def test_print_nilable_proc
rbi = parse_rbi(<<~RBI)
sig { params(x: T.nilable(T.proc.void)) }
def foo(x); end
RBI

assert_equal(<<~RBI, rbi.rbs_string)
def foo: ((^-> void)? x) -> void
RBI
end

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

0 comments on commit b043e8f

Please sign in to comment.