Skip to content

Commit 036f61c

Browse files
committed
RBS sig translate should generate multiline when the line is too long
1 parent 93ec969 commit 036f61c

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

lib/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ def visit_def_node(node)
5050

5151
@last_sigs.each do |node, sig|
5252
out = StringIO.new
53-
p = RBI::RBSPrinter.new(out: out, indent: node.location.start_column, positional_names: @positional_names)
53+
p = RBI::RBSPrinter.new(out: out, indent: node.location.start_column, positional_names: @positional_names, max_line_length: 120)
54+
5455
p.print("#: ")
5556
p.send(:print_method_sig, rbi_node, sig)
56-
p.print("\n")
57-
@rewriter << Source::Replace.new(node.location.start_offset, node.location.end_offset, out.string)
57+
rbs_sig = out.string
58+
rbs_sig.gsub!("\n", "\n#| ")
59+
rbs_sig << "\n"
60+
61+
@rewriter << Source::Replace.new(node.location.start_offset, node.location.end_offset, rbs_sig)
5862
end
5963

6064
@last_sigs.clear

test/spoom/sorbet/translate/sorbet_sigs_to_rbs_comments_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,66 @@ def foo
288288
RBS
289289
end
290290

291+
def test_translate_of_multiline_sig
292+
contents = <<~RB
293+
sig do
294+
params(
295+
merchandise_lines: T::Array[CheckoutPlatform::PurchaseOrderTerms::Merchandise::MerchandiseLine],
296+
destination_address: T.nilable(Web::Delivery::SimpleStreetDeliveryAddress),
297+
delivery_strategy: T.nilable(Web::Delivery::ConcreteDeliveryStrategy),
298+
delivery_option: T.nilable(::Delivery::Option),
299+
total_cost: T.nilable(MultiCurrency::MoneyBag),
300+
origin_location_id: ::Delivery::OriginLocationId,
301+
estimated_delivery_time_range: T.nilable(T::Array[Time]),
302+
fulfillment_constraints: T.nilable(::OrderRouting::FulfillmentConstraints::Constraints),
303+
auto_fulfillment_override: T.nilable(T::Boolean),
304+
available_on: T.nilable(CheckoutPlatform::Terms::TermConditions::DeliveryLine::AvailableOn)
305+
).returns(T.self_type)
306+
end
307+
def with_delivery(
308+
merchandise_lines: @merchandise.lines,
309+
destination_address: nil,
310+
delivery_strategy: nil,
311+
delivery_option: nil,
312+
total_cost: nil,
313+
origin_location_id: ::Delivery::OriginLocationId.new(1),
314+
estimated_delivery_time_range: nil,
315+
fulfillment_constraints: nil,
316+
auto_fulfillment_override: nil,
317+
available_on: nil
318+
)
319+
end
320+
RB
321+
322+
assert_equal(<<~RBS, sorbet_sigs_to_rbs_comments(contents))
323+
#: (
324+
#| ?merchandise_lines: Array[CheckoutPlatform::PurchaseOrderTerms::Merchandise::MerchandiseLine],
325+
#| ?destination_address: Web::Delivery::SimpleStreetDeliveryAddress?,
326+
#| ?delivery_strategy: Web::Delivery::ConcreteDeliveryStrategy?,
327+
#| ?delivery_option: ::Delivery::Option?,
328+
#| ?total_cost: MultiCurrency::MoneyBag?,
329+
#| ?origin_location_id: ::Delivery::OriginLocationId,
330+
#| ?estimated_delivery_time_range: Array[Time]?,
331+
#| ?fulfillment_constraints: ::OrderRouting::FulfillmentConstraints::Constraints?,
332+
#| ?auto_fulfillment_override: bool?,
333+
#| ?available_on: CheckoutPlatform::Terms::TermConditions::DeliveryLine::AvailableOn?
334+
#| ) -> self
335+
def with_delivery(
336+
merchandise_lines: @merchandise.lines,
337+
destination_address: nil,
338+
delivery_strategy: nil,
339+
delivery_option: nil,
340+
total_cost: nil,
341+
origin_location_id: ::Delivery::OriginLocationId.new(1),
342+
estimated_delivery_time_range: nil,
343+
fulfillment_constraints: nil,
344+
auto_fulfillment_override: nil,
345+
available_on: nil
346+
)
347+
end
348+
RBS
349+
end
350+
291351
private
292352

293353
#: (String, ?positional_names: bool) -> String

0 commit comments

Comments
 (0)