Description
Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs generates invalid Ruby when translating an RBS block type annotation on a method that uses Ruby 3.1+ anonymous block forwarding (&). The generated sig contains &block: inside params(), which is a syntax error. Named block parameters (&block) are translated correctly.
Reproduction
This code:
require "spoom"
source = <<~RUBY
# typed: true
class Foo
#: (String) ?{ (String) -> void } -> String
def bar(request, &); end
end
RUBY
rewritten = Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs(source, file: "test.rb")
puts rewritten
RubyVM::InstructionSequence.compile(rewritten)
Produces
# typed: true
class Foo
sig { params(request: String, &block: ::T.nilable(::T.proc.params(arg0: String).void)).returns(String) }
def bar(request, &); end
end
(shopify):36:in 'RubyVM::InstructionSequence.compile': <compiled>:3: syntax errors found (SyntaxError)
1 | # typed: true
2 | class Foo
> 3 | ... : ::T.nilable(::T.proc.params(arg0: String).void)).returns(String) }
| ^ unexpected ':', ignoring it
| ^ unexpected ':', expecting end-of-input
| ^ unexpected ':'; expected a `)` to close the arguments
| ^ unexpected ')', ignoring it
| ^ unexpected ')', expecting end-of-input
| ^ unexpected '.', ignoring it
4 | def bar(request, &); end
5 | end
Expected
# typed: true
class Foo
sig { params(request: String, block: ::T.nilable(::T.proc.params(arg0: String).void)).returns(String) }
def bar(request, &); end
end
=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
Environment
- Spoom 1.7.15
- Ruby 3.4.9
- Tapioca 0.19.1
Description
Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigsgenerates invalid Ruby when translating an RBS block type annotation on a method that uses Ruby 3.1+ anonymous block forwarding (&). The generatedsigcontains&block:insideparams(), which is a syntax error. Named block parameters (&block) are translated correctly.Reproduction
This code:
Produces
Expected
Environment