Skip to content

Commit d4e3e86

Browse files
committed
WIP: Point RBI to main
1 parent 59a1994 commit d4e3e86

6 files changed

Lines changed: 68 additions & 20 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ group :development do
1313
gem "rubocop-sorbet", require: false
1414
gem "tapioca", require: false
1515
end
16+
17+
gem "rbi", git: "https://github.com/Shopify/rbi.git", branch: "main"

Gemfile.lock

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
GIT
2+
remote: https://github.com/Shopify/rbi.git
3+
revision: 6ee489b84ea07be6e6a2f4d9533a4aaa8f8aaf4d
4+
branch: main
5+
specs:
6+
rbi (0.3.6)
7+
prism (~> 1.0)
8+
rbs (>= 3.4.4)
9+
110
PATH
211
remote: .
312
specs:
@@ -53,9 +62,6 @@ GEM
5362
racc (1.8.1)
5463
rainbow (3.1.1)
5564
rake (13.3.0)
56-
rbi (0.3.6)
57-
prism (~> 1.0)
58-
rbs (>= 3.4.4)
5965
rbs (4.0.0.dev.4)
6066
logger
6167
prism (>= 1.3.0)
@@ -129,6 +135,7 @@ DEPENDENCIES
129135
minitest
130136
minitest-reporters
131137
rake (~> 13.3.0)
138+
rbi!
132139
rubocop-shopify
133140
rubocop-sorbet
134141
spoom!

lib/spoom/rbs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def initialize(string, location)
6464

6565
class Annotation < Comment; end
6666
class Signature < Comment; end
67+
class TypeAlias < Comment; end
6768

6869
module ExtractRBSComments
6970
#: (Prism::Node) -> Comments

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,58 @@ def already_extends?(node, constant_regex)
282282
end
283283
end
284284

285+
#: (Array[Prism::Comment]) -> Array[RBS::TypeAlias]
286+
def collect_type_aliases(comments)
287+
type_aliases = [] #: Array[RBS::TypeAlias]
288+
289+
return type_aliases if comments.empty?
290+
291+
continuation_comments = [] #: Array[Prism::Comment]
292+
293+
comments.reverse_each do |comment|
294+
string = comment.slice
295+
296+
if string.start_with?("#:")
297+
string = string.delete_prefix("#:").strip
298+
location = comment.location
299+
300+
if string.start_with?("type ")
301+
continuation_comments.reverse_each do |continuation_comment|
302+
string = "#{string}#{continuation_comment.slice.delete_prefix("#|")}"
303+
location = location.join(continuation_comment.location)
304+
end
305+
306+
type_aliases << Spoom::RBS::TypeAlias.new(string, location)
307+
end
308+
309+
# Clear the continuation comments regardless of whether we found a type alias or not
310+
continuation_comments.clear
311+
elsif string.start_with?("#|")
312+
continuation_comments << comment
313+
else
314+
continuation_comments.clear
315+
end
316+
end
317+
318+
type_aliases
319+
end
320+
285321
#: (Array[Prism::Comment]) -> void
286322
def apply_type_aliases(comments)
287-
comments.each do |comment|
288-
string = comment.slice.strip
289-
next unless string.start_with?("#: type ") && string.include?(" = ")
323+
type_aliases = collect_type_aliases(comments)
290324

291-
# Extract the type alias content
292-
type_alias_content = string.delete_prefix("#:").strip
325+
type_aliases.each do |type_alias|
326+
indent = " " * type_alias.location.start_column
327+
insert_pos = adjust_to_line_start(type_alias.location.start_offset)
293328

294-
location = comment.location
295-
from = adjust_to_line_start(location.start_offset)
296-
to = adjust_to_line_end(location.end_offset)
329+
from = insert_pos
330+
to = adjust_to_line_end(type_alias.location.end_offset)
297331

298-
insert_pos = from
299-
indent = " " * location.start_column
332+
*, decls = ::RBS::Parser.parse_signature(type_alias.string)
300333

301-
*, decls = ::RBS::Parser.parse_signature(type_alias_content)
334+
# We only expect there to be a single type alias declaration
302335
next unless decls.size == 1 && decls.first.is_a?(::RBS::AST::Declarations::TypeAlias)
336+
303337
rbs_type = decls.first
304338
sorbet_type = RBI::RBS::TypeTranslator.translate(rbs_type.type)
305339

@@ -312,7 +346,6 @@ def apply_type_aliases(comments)
312346
)
313347

314348
@rewriter << Source::Delete.new(from, to)
315-
316349
@rewriter << Source::Insert.new(insert_pos, "#{indent}#{alias_name} = T.type_alias { #{sorbet_type.to_rbi} }\n")
317350
rescue ::RBS::ParsingError, ::RBI::Error
318351
# Ignore type aliases with errors

rbi/spoom.rbi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,7 @@ module Spoom::RBS::ExtractRBSComments
25842584
end
25852585

25862586
class Spoom::RBS::Signature < ::Spoom::RBS::Comment; end
2587+
class Spoom::RBS::TypeAlias < ::Spoom::RBS::Comment; end
25872588
Spoom::SPOOM_PATH = T.let(T.unsafe(nil), String)
25882589
module Spoom::Sorbet; end
25892590
Spoom::Sorbet::BIN_PATH = T.let(T.unsafe(nil), String)

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,12 @@ def test_translate_type_alias
450450
contents = <<~RB
451451
module Aliases
452452
#: type foo = Integer | String
453+
#: type multiLine =
454+
#| Integer |
455+
#| String
453456
end
454457
455-
#: (a: Aliases::foo) -> Integer
458+
#: (Aliases::foo a) -> Aliases::multiLine
456459
def bar(a)
457460
42
458461
end
@@ -461,9 +464,10 @@ def bar(a)
461464
assert_equal(<<~RB, rbs_comments_to_sorbet_sigs(contents))
462465
module Aliases
463466
Foo = T.type_alias { T.any(Integer, String) }
467+
MultiLine = T.type_alias { T.any(Integer, String) }
464468
end
465469
466-
sig { params(a: Aliases::Foo).returns(Integer) }
470+
sig { params(a: Aliases::Foo).returns(Aliases::MultiLine) }
467471
def bar(a)
468472
42
469473
end
@@ -475,7 +479,7 @@ def test_translate_type_alias_with_complex_type
475479
#: type Foo::user_id = Integer
476480
#: type ::Bar::user_data = { id: Foo::user_id, name: String }
477481
478-
#: (data: ::Bar::user_data) -> Foo::user_id
482+
#: (::Bar::user_data data) -> Foo::user_id
479483
def process_user(data)
480484
data[:id]
481485
end
@@ -520,7 +524,7 @@ def test_translate_type_alias_with_generics
520524
contents = <<~RB
521525
#: type list = Array[Integer]
522526
523-
#: (items: list) -> list
527+
#: (list items) -> list
524528
def double_items(items)
525529
items.map { |x| x * 2 }
526530
end
@@ -540,7 +544,7 @@ def test_translate_type_alias_with_union
540544
contents = <<~RB
541545
#: type nullable_string = String?
542546
543-
#: (text: nullable_string) -> String
547+
#: (nullable_string text) -> String
544548
def ensure_string(text)
545549
text || ""
546550
end

0 commit comments

Comments
 (0)