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 15, 2025
1 parent 0297249 commit 0614069
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rbi/rbs_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,14 @@ def visit_attached_class(type)

sig { params(type: Type::Nilable).void }
def visit_nilable(type)
visit(type.type)
inner = type.type
if inner.is_a?(Type::Proc)
@string << "("
end
visit(inner)
if inner.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 @@ -589,6 +589,17 @@ def foo: { (?) -> untyped } -> void
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 0614069

Please sign in to comment.