Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect nilable procs with parenthesis when translating to RBS #397

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Protect nilable procs with parenthesis when translating to RBS
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Jan 15, 2025
commit 06bb85d97875883390329adbe41719722a181bed
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 @@ -616,6 +616,17 @@ def e: -> Class[untyped]
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