@@ -15,17 +15,22 @@ class Writer
1515 attr_reader :output #: String
1616 attr_reader :writer #: RBS::Writer
1717
18+ attr_accessor :default_type #: Types::t
19+
1820 # @rbs buffer: String
1921 def initialize ( buffer = +"" ) #: void
2022 @output = buffer
2123 @writer = RBS ::Writer . new ( out : StringIO . new ( buffer ) )
24+ @default_type = Types ::Bases ::Any . new ( location : nil )
2225 end
2326
2427 # @rbs uses: Array[AST::Annotations::Use]
2528 # @rbs decls: Array[AST::Declarations::t]
2629 # @rbs rbs_decls: Array[RBS::AST::Declarations::t]
27- def self . write ( uses , decls , rbs_decls ) #: void
30+ # @rbs &: ? (Writer) -> void
31+ def self . write ( uses , decls , rbs_decls , &) #: void
2832 writer = Writer . new ( )
33+ yield writer if block_given?
2934 writer . write ( uses , decls , rbs_decls )
3035 writer . output
3136 end
@@ -200,7 +205,7 @@ def translate_data_assign_decl(decl, rbs) #: void
200205 attributes = decl . each_attribute . map do |name , type |
201206 RBS ::AST ::Members ::AttrReader . new (
202207 name : name ,
203- type : type &.type || Types :: Bases :: Any . new ( location : nil ) ,
208+ type : type &.type || default_type ,
204209 ivar_name : false ,
205210 comment : nil ,
206211 kind : :instance ,
@@ -220,7 +225,7 @@ def translate_data_assign_decl(decl, rbs) #: void
220225 type : Types ::Function . empty ( Types ::Bases ::Instance . new ( location : nil ) ) . update (
221226 required_positionals : decl . each_attribute . map do |name , attr |
222227 RBS ::Types ::Function ::Param . new (
223- type : attr &.type || Types :: Bases :: Any . new ( location : nil ) ,
228+ type : attr &.type || default_type ,
224229 name : name ,
225230 location : nil
226231 )
@@ -239,7 +244,7 @@ def translate_data_assign_decl(decl, rbs) #: void
239244 [
240245 name ,
241246 RBS ::Types ::Function ::Param . new (
242- type : attr &.type || Types :: Bases :: Any . new ( location : nil ) ,
247+ type : attr &.type || default_type ,
243248 name : nil ,
244249 location : nil
245250 )
@@ -317,7 +322,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
317322 if decl . readonly_attributes?
318323 RBS ::AST ::Members ::AttrReader . new (
319324 name : name ,
320- type : type &.type || Types :: Bases :: Any . new ( location : nil ) ,
325+ type : type &.type || default_type ,
321326 ivar_name : false ,
322327 comment : nil ,
323328 kind : :instance ,
@@ -328,7 +333,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
328333 else
329334 RBS ::AST ::Members ::AttrAccessor . new (
330335 name : name ,
331- type : type &.type || Types :: Bases :: Any . new ( location : nil ) ,
336+ type : type &.type || default_type ,
332337 ivar_name : false ,
333338 comment : nil ,
334339 kind : :instance ,
@@ -353,7 +358,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
353358 if decl . positional_init?
354359 attr_params = decl . each_attribute . map do |name , attr |
355360 RBS ::Types ::Function ::Param . new (
356- type : attr &.type || Types :: Bases :: Any . new ( location : nil ) ,
361+ type : attr &.type || default_type ,
357362 name : name ,
358363 location : nil
359364 )
@@ -378,7 +383,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
378383 [
379384 name ,
380385 RBS ::Types ::Function ::Param . new (
381- type : attr &.type || Types :: Bases :: Any . new ( location : nil ) ,
386+ type : attr &.type || default_type ,
382387 name : nil ,
383388 location : nil
384389 )
@@ -407,7 +412,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
407412 t . update ( required_positionals : [
408413 RBS ::Types ::Function ::Param . new (
409414 type : RBS ::Types ::Record . new ( all_fields : decl . each_attribute . map do |name , attr |
410- [ name , attr &.type || Types :: Bases :: Any . new ( location : nil ) ]
415+ [ name , attr &.type || default_type ]
411416 end . to_h , location : nil ) ,
412417 name : nil ,
413418 location : nil
@@ -430,7 +435,7 @@ def translate_struct_assign_decl(decl, rbs) #: void
430435 name : RBS ::TypeName . new ( name : :Struct , namespace : RBS ::Namespace . empty ) ,
431436 args : [
432437 RBS ::Types ::Union . new (
433- types : decl . each_attribute . map { |_ , attr | attr &.type || RBS :: Types :: Bases :: Any . new ( location : nil ) } . uniq ,
438+ types : decl . each_attribute . map { |_ , attr | attr &.type || default_type } . uniq ,
434439 location : nil
435440 )
436441 ] ,
@@ -485,7 +490,7 @@ def translate_member(member, decl, rbs)
485490 rbs << RBS ::AST ::Members ::MethodDefinition . new (
486491 name : member . method_name ,
487492 kind : kind ,
488- overloads : member . method_overloads ,
493+ overloads : member . method_overloads ( default_type ) ,
489494 annotations : member . method_annotations ,
490495 location : nil ,
491496 comment : comment ,
@@ -510,7 +515,7 @@ def translate_member(member, decl, rbs)
510515 rbs << m
511516 end
512517 when AST ::Members ::RubyAttr
513- if m = member . rbs
518+ if m = member . rbs ( default_type )
514519 rbs . concat m
515520 end
516521 when AST ::Members ::RubyPrivate
@@ -620,24 +625,19 @@ def translate_class_block_decl(block, rbs)
620625 # @rbs decl: AST::Declarations::ConstantDecl
621626 # @rbs return: RBS::Types::t
622627 def constant_decl_to_type ( decl )
623- type = decl . type
628+ type = decl . type ( default_type )
624629 return type unless type . is_a? ( RBS ::Types ::ClassInstance )
625630 return type if type . args . any?
626631
627632 case decl . node . value
628633 when Prism ::ArrayNode
629- RBS ::BuiltinNames ::Array . instance_type ( untyped )
634+ RBS ::BuiltinNames ::Array . instance_type ( default_type )
630635 when Prism ::HashNode
631- RBS ::BuiltinNames ::Hash . instance_type ( untyped , untyped )
636+ RBS ::BuiltinNames ::Hash . instance_type ( default_type , default_type )
632637 else
633638 type
634639 end
635640 end
636-
637- # @rbs return: RBS::Types::Bases::Any
638- def untyped
639- @untyped ||= RBS ::Types ::Bases ::Any . new ( location : nil )
640- end
641641 end
642642 end
643643end
0 commit comments