@@ -461,7 +461,37 @@ def skip_comments_until(line_no_until)
461461 def consecutive_comment ( line_no )
462462 return unless @unprocessed_comments . first &.first == line_no
463463 _line_no , start_line , text = @unprocessed_comments . shift
464- parse_comment_text_to_directives ( text , start_line )
464+ type_signature = extract_type_signature! ( text )
465+ result = parse_comment_text_to_directives ( text , start_line )
466+ return unless result
467+ comment , directives = result
468+ [ comment , directives , type_signature ]
469+ end
470+
471+ # Extracts RBS type signature lines (#: ...) from raw comment text.
472+ # Mutates the input text to remove the extracted lines.
473+ # Returns the type signature string, or nil if none found.
474+ private def extract_type_signature! ( text )
475+ return nil unless text . include? ( '#:' )
476+
477+ lines = text . lines
478+ sig_lines , doc_lines = lines . partition { |l | l . match? ( /\A #:\s / ) }
479+ return nil if sig_lines . empty?
480+
481+ text . replace ( doc_lines . join )
482+ type_sig = sig_lines . map { |l | l . sub ( /\A #:\s ?/ , '' ) . chomp } . join ( "\n " )
483+ validate_type_signature ( type_sig )
484+ type_sig
485+ end
486+
487+ private def validate_type_signature ( sig )
488+ sig . split ( "\n " ) . each do |line |
489+ method_error = RDoc ::RbsSupport . validate_method_type ( line )
490+ next unless method_error
491+ type_error = RDoc ::RbsSupport . validate_type ( line )
492+ next unless type_error
493+ @options . warn "Invalid RBS type signature: #{ line . inspect } "
494+ end
465495 end
466496
467497 # Parses comment text and retuns a pair of RDoc::Comment and directives
@@ -594,14 +624,15 @@ def add_alias_method(old_name, new_name, line_no)
594624 # Handles `attr :a, :b`, `attr_reader :a, :b`, `attr_writer :a, :b` and `attr_accessor :a, :b`
595625
596626 def add_attributes ( names , rw , line_no )
597- comment , directives = consecutive_comment ( line_no )
627+ comment , directives , type_signature = consecutive_comment ( line_no )
598628 handle_code_object_directives ( @container , directives ) if directives
599629 return unless @container . document_children
600630
601631 names . each do |symbol |
602632 a = RDoc ::Attr . new ( nil , symbol . to_s , rw , comment , singleton : @singleton )
603633 a . store = @store
604634 a . line = line_no
635+ a . type_signature = type_signature
605636 record_location ( a )
606637 handle_modifier_directive ( a , line_no )
607638 @container . add_attribute ( a ) if should_document? ( a )
@@ -640,7 +671,7 @@ def add_extends(names, line_no) # :nodoc:
640671
641672 def add_method ( method_name , receiver_name :, receiver_fallback_type :, visibility :, singleton :, params :, calls_super :, block_params :, tokens :, start_line :, args_end_line :, end_line :)
642673 receiver = receiver_name ? find_or_create_module_path ( receiver_name , receiver_fallback_type ) : @container
643- comment , directives = consecutive_comment ( start_line )
674+ comment , directives , type_signature = consecutive_comment ( start_line )
644675 handle_code_object_directives ( @container , directives ) if directives
645676
646677 internal_add_method (
@@ -655,11 +686,12 @@ def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:
655686 params : params ,
656687 calls_super : calls_super ,
657688 block_params : block_params ,
658- tokens : tokens
689+ tokens : tokens ,
690+ type_signature : type_signature
659691 )
660692 end
661693
662- private def internal_add_method ( method_name , container , comment :, dont_rename_initialize : false , directives :, modifier_comment_lines : nil , line_no :, visibility :, singleton :, params :, calls_super :, block_params :, tokens :) # :nodoc:
694+ private def internal_add_method ( method_name , container , comment :, dont_rename_initialize : false , directives :, modifier_comment_lines : nil , line_no :, visibility :, singleton :, params :, calls_super :, block_params :, tokens :, type_signature : nil ) # :nodoc:
663695 meth = RDoc ::AnyMethod . new ( nil , method_name , singleton : singleton )
664696 meth . comment = comment
665697 handle_code_object_directives ( meth , directives ) if directives
@@ -680,6 +712,7 @@ def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:
680712 meth . params ||= params || '()'
681713 meth . calls_super = calls_super
682714 meth . block_params ||= block_params if block_params
715+ meth . type_signature = type_signature
683716 record_location ( meth )
684717 meth . start_collecting_tokens ( :ruby )
685718 tokens . each do |token |
0 commit comments