Skip to content

Commit dbf800b

Browse files
committed
Print long RBI signatures over multiple lines
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent 7da3bcf commit dbf800b

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

lib/spoom/sorbet/translate.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def sorbet_sigs_to_rbs_comments(ruby_contents, file:, positional_names: true, ma
3232

3333
# Converts all the RBS comments in the given Ruby code to `sig` nodes.
3434
# It also handles type members and class annotations.
35-
#: (String ruby_contents, file: String) -> String
36-
def rbs_comments_to_sorbet_sigs(ruby_contents, file:)
37-
RBSCommentsToSorbetSigs.new(ruby_contents, file: file).rewrite
35+
#: (String ruby_contents, file: String, ?max_line_length: Integer?) -> String
36+
def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: nil)
37+
RBSCommentsToSorbetSigs.new(ruby_contents, file: file, max_line_length: max_line_length).rewrite
3838
end
3939

4040
# Converts all `T.let` and `T.cast` nodes to RBS comments in the given Ruby code.

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module Translate
77
class RBSCommentsToSorbetSigs < Translator
88
include Spoom::RBS::ExtractRBSComments
99

10+
#: (String, file: String, ?max_line_length: Integer?) -> void
11+
def initialize(ruby_contents, file:, max_line_length: nil)
12+
super(ruby_contents, file: file)
13+
14+
@max_line_length = max_line_length
15+
end
16+
1017
# @override
1118
#: (Prism::ClassNode node) -> void
1219
def visit_class_node(node)
@@ -53,7 +60,7 @@ def visit_def_node(node)
5360
@rewriter << Source::Replace.new(
5461
signature.location.start_offset,
5562
signature.location.end_offset,
56-
sig.string,
63+
sig.string(max_line_length: @max_line_length),
5764
)
5865
rescue ::RBS::ParsingError, ::RBI::Error
5966
# Ignore signatures with errors
@@ -104,7 +111,7 @@ def visit_attr(node)
104111
@rewriter << Source::Replace.new(
105112
signature.location.start_offset,
106113
signature.location.end_offset,
107-
sig.string,
114+
sig.string(max_line_length: @max_line_length),
108115
)
109116
rescue ::RBS::ParsingError, ::RBI::Error
110117
# Ignore signatures with errors

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,31 @@ def foo
376376
RB
377377
end
378378

379+
def test_translate_to_rbi_max_line_length
380+
contents = <<~RB
381+
#: (
382+
#| param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
383+
#| param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
384+
#| ) -> void
385+
def foo(param1:, param2:); end
386+
RB
387+
388+
assert_equal(<<~RB, rbs_comments_to_sorbet_sigs(contents, max_line_length: 120))
389+
sig do
390+
params(
391+
param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
392+
param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
393+
).void
394+
end
395+
def foo(param1:, param2:); end
396+
RB
397+
end
398+
379399
private
380400

381-
#: (String) -> String
382-
def rbs_comments_to_sorbet_sigs(ruby_contents)
383-
Translate.rbs_comments_to_sorbet_sigs(ruby_contents, file: "test.rb")
401+
#: (String, ?max_line_length: Integer?) -> String
402+
def rbs_comments_to_sorbet_sigs(ruby_contents, max_line_length: nil)
403+
Translate.rbs_comments_to_sorbet_sigs(ruby_contents, file: "test.rb", max_line_length: max_line_length)
384404
end
385405
end
386406
end

0 commit comments

Comments
 (0)