Skip to content

RBS Rewriting produces invalid Ruby for anonymous block parameters #928

@bfad

Description

@bfad

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood-first-issueGood for newcomers

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions