@@ -13,7 +13,7 @@ class StructPropHandler < YARD::Handlers::Ruby::Base
1313
1414 sig { void }
1515 def process
16- name = statement . parameters . first . last . last . source
16+ name = params . dig ( 0 , - 1 , - 1 ) . source
1717 prop = make_prop ( name )
1818 update_state ( prop )
1919 object = YARD ::CodeObjects ::MethodObject . new ( namespace , name , scope )
@@ -27,36 +27,45 @@ def process
2727 sig { params ( object : YARD ::CodeObjects ::MethodObject , prop : TStructProp ) . void }
2828 def decorate_object ( object , prop )
2929 object . source = prop . source
30- # TODO: this should use `+` to delimit the attribute name when markdown is disabled
31- reader_docstring = prop . doc . empty? ? "Returns the value of attribute `#{ prop . prop_name } `." : prop . doc
30+ # TODO: this should use `+` to delimit the prop name when markdown is disabled
31+ reader_docstring = prop . doc . empty? ? "Returns the value of prop `#{ prop . prop_name } `." : prop . doc
3232 docstring = YARD ::DocstringParser . new . parse ( reader_docstring ) . to_docstring
3333 docstring . add_tag ( YARD ::Tags ::Tag . new ( :return , '' , prop . types ) )
3434 object . docstring = docstring . to_raw
3535 end
3636
37- # Get the default prop value
38- sig { returns ( T . nilable ( String ) ) }
39- def default_value
40- statement . traverse { break _1 if _1 . type == :label && _1 . source == 'default:' } &.parent &.[]( 1 ) &.source
37+ sig { returns ( T ::Boolean ) }
38+ def immutable?
39+ statement . method_name ( true ) == :const || kw_arg ( 'immutable:' ) == 'true'
40+ end
41+
42+ # @return the value passed to the keyword argument, or nil
43+ sig { params ( kwd : String ) . returns ( T . nilable ( String ) ) }
44+ def kw_arg ( kwd )
45+ params [ 2 ] &.find { _1 [ 0 ] . source == kwd } &.[]( 1 ) &.source
4146 end
4247
4348 sig { params ( name : String ) . returns ( TStructProp ) }
4449 def make_prop ( name )
4550 TStructProp . new (
46- default : default_value ,
51+ default : kw_arg ( 'default:' ) ,
4752 doc : statement . docstring . to_s ,
4853 prop_name : name ,
4954 source : statement . source ,
50- types : SigToYARD . convert ( statement . parameters [ 1 ] )
55+ types : SigToYARD . convert ( params [ 1 ] )
5156 )
5257 end
5358
59+ sig { returns ( T ::Array [ T . untyped ] ) }
60+ def params
61+ @params ||= T . let ( statement . parameters ( false ) , T . nilable ( T ::Array [ T . untyped ] ) )
62+ end
63+
5464 # Register the field explicitly as an attribute.
55- # While `const` attributes are immutable, `prop` attributes may be reassigned.
5665 sig { params ( object : YARD ::CodeObjects ::MethodObject , name : String ) . void }
5766 def register_attrs ( object , name )
58- # Create the virtual method in our current scope
59- write = statement . method_name ( true ) == :prop ? object : nil
67+ write = immutable? ? nil : object
68+ # Create the virtual attribute in our current scope
6069 namespace . attributes [ scope ] [ name ] ||= SymbolHash [ read : object , write : write ]
6170 end
6271
0 commit comments