@@ -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
0 commit comments