@@ -41,26 +41,25 @@ def visit_singleton_class_node(node)
4141 # @override
4242 #: (Prism::DefNode) -> void
4343 def visit_def_node ( node )
44- return if @last_sigs . empty?
45- return if @last_sigs . any? { |_ , sig | sig . is_abstract }
44+ last_sigs = collect_last_sigs
45+ return if last_sigs . empty?
46+ return if last_sigs . any? { |_ , sig | sig . is_abstract }
4647
47- apply_member_annotations ( @ last_sigs)
48+ apply_member_annotations ( last_sigs )
4849
4950 # Build the RBI::Method node so we can print the method signature as RBS.
5051 builder = RBI ::Parser ::TreeBuilder . new ( @ruby_contents , comments : [ ] , file : @file )
5152 builder . visit ( node )
5253 rbi_node = builder . tree . nodes . first #: as RBI::Method
5354
54- @ last_sigs. each do |node , sig |
55+ last_sigs . each do |node , sig |
5556 out = StringIO . new
5657 p = RBI ::RBSPrinter . new ( out : out , indent : node . location . start_column , positional_names : @positional_names )
5758 p . print ( "#: " )
5859 p . send ( :print_method_sig , rbi_node , sig )
5960 p . print ( "\n " )
6061 @rewriter << Source ::Replace . new ( node . location . start_offset , node . location . end_offset , out . string )
6162 end
62-
63- @last_sigs . clear
6463 end
6564
6665 # @override
@@ -155,25 +154,24 @@ def visit_attr(node)
155154 raise Error , "Expected attr_reader, attr_writer, or attr_accessor"
156155 end
157156
158- return if @last_sigs . empty?
159- return if @last_sigs . any? { |_ , sig | sig . is_abstract }
157+ last_sigs = collect_last_sigs
158+ return if last_sigs . empty?
159+ return if last_sigs . any? { |_ , sig | sig . is_abstract }
160160
161- apply_member_annotations ( @ last_sigs)
161+ apply_member_annotations ( last_sigs )
162162
163163 builder = RBI ::Parser ::TreeBuilder . new ( @ruby_contents , comments : [ ] , file : @file )
164164 builder . visit ( node )
165165 rbi_node = builder . tree . nodes . first #: as RBI::Attr
166166
167- @ last_sigs. each do |node , sig |
167+ last_sigs . each do |node , sig |
168168 out = StringIO . new
169169 p = RBI ::RBSPrinter . new ( out : out , indent : node . location . start_column , positional_names : @positional_names )
170170 p . print ( "#: " )
171171 p . print_attr_sig ( rbi_node , sig )
172172 p . print ( "\n " )
173173 @rewriter << Source ::Replace . new ( node . location . start_offset , node . location . end_offset , out . string )
174174 end
175-
176- @last_sigs . clear
177175 end
178176
179177 #: (Prism::CallNode node) -> void
@@ -336,6 +334,14 @@ def delete_extend_t_generics
336334
337335 @extend_t_generics . clear
338336 end
337+
338+ # Collects the last signatures visited and clears the current list
339+ #: -> Array[[Prism::CallNode, RBI::Sig]]
340+ def collect_last_sigs
341+ last_sigs = @last_sigs
342+ @last_sigs = [ ]
343+ last_sigs
344+ end
339345 end
340346 end
341347 end
0 commit comments