@@ -13,7 +13,7 @@ class SigHandler < YARD::Handlers::Ruby::Base
1313 # YARD types that can have docstrings attached to them
1414 Documentable = T . type_alias do
1515 T . any (
16- YARD ::CodeObjects ::MethodObject , YARD ::Parser ::Ruby ::MethodDefinitionNode , YARD ::Parser ::Ruby ::MethodCallNode
16+ YARD ::CodeObjects ::MethodObject , YARD ::Parser ::Ruby ::MethodCallNode , YARD ::Parser ::Ruby ::MethodDefinitionNode
1717 )
1818 end
1919 private_constant :Documentable
@@ -29,6 +29,8 @@ def process
2929 end
3030 end
3131
32+ private
33+
3234 sig { params ( def_node : YARD ::Parser ::Ruby ::MethodDefinitionNode ) . void }
3335 def process_def ( def_node )
3436 separator = scope == :instance && def_node . type == :def ? '#' : '.'
@@ -44,12 +46,27 @@ def process_def(def_node)
4446
4547 sig { params ( attr_node : YARD ::Parser ::Ruby ::MethodCallNode ) . void }
4648 def process_attr ( attr_node )
47- # TODO: Merge with existing attr documentation (#141)
49+ names = NodeUtils . validated_attribute_names ( attr_node )
50+ return if merged_into_attr? ( attr_node , names )
51+
4852 parse_node ( attr_node , statement . docstring , include_params : false )
4953 statement . docstring = nil
5054 end
5155
52- private
56+ # An attr* sig can be merged into a previous attr* docstring if it is the only parameter passed to the attr*
57+ # declaration. This is to avoid needing to rewrite the source code to separate merged and unmerged attr*
58+ # declarations.
59+ sig { params ( attr_node : YARD ::Parser ::Ruby ::MethodCallNode , names : T ::Array [ String ] ) . returns ( T ::Boolean ) }
60+ def merged_into_attr? ( attr_node , names )
61+ return false if names . size == 1
62+
63+ nodes = namespace . attributes [ scope ] [ names . fetch ( 0 ) ]
64+ return false if nodes . nil? || nodes . empty?
65+
66+ nodes . each_value { parse_node ( _1 , _1 . docstring , include_params : false ) }
67+ attr_node . docstring = statement . docstring = nil
68+ true
69+ end
5370
5471 sig { params ( attach_to : Documentable , docstring : T . nilable ( String ) , include_params : T ::Boolean ) . void }
5572 def parse_node ( attach_to , docstring , include_params : true )
0 commit comments