From 06140698522c39d264fd8552cd0b90c7ba66a904 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 14 Jan 2025 16:31:08 -0500 Subject: [PATCH] Protect nilable procs with parenthesis when translating to RBS Signed-off-by: Alexandre Terrasa --- lib/rbi/rbs_printer.rb | 9 ++++++++- test/rbi/rbs_printer_test.rb | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rbi/rbs_printer.rb b/lib/rbi/rbs_printer.rb index b4e41de3..32883936 100644 --- a/lib/rbi/rbs_printer.rb +++ b/lib/rbi/rbs_printer.rb @@ -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 diff --git a/test/rbi/rbs_printer_test.rb b/test/rbi/rbs_printer_test.rb index e80c2d42..56045bcb 100644 --- a/test/rbi/rbs_printer_test.rb +++ b/test/rbi/rbs_printer_test.rb @@ -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