Skip to content

Commit 540a936

Browse files
committed
Code cleanup
1 parent 53eabf4 commit 540a936

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

lib/yard-sorbet/handlers/struct_prop_handler.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,21 @@ def decorate_object(object, prop)
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
41-
end
42-
4337
sig { returns(T::Boolean) }
4438
def immutable?
45-
return true if statement.method_name(true) == :const
39+
statement.method_name(true) == :const || kw_arg('immutable') == 'true'
40+
end
4641

47-
kw_args = statement.parameters(false).last
48-
kw_args.any? { _1.jump(:label).source == 'immutable:' && _1.jump(:kw).source == 'true' }
42+
# @return the value passed to the keyword argument, or nil
43+
sig { params(keyword: String).returns(T.nilable(String)) }
44+
def kw_arg(keyword)
45+
statement.parameters(false)[2]&.find { _1.jump(:label).source == "#{keyword}:" }&.[](1)&.source
4946
end
5047

5148
sig { params(name: String).returns(TStructProp) }
5249
def make_prop(name)
5350
TStructProp.new(
54-
default: default_value,
51+
default: kw_arg('default'),
5552
doc: statement.docstring.to_s,
5653
prop_name: name,
5754
source: statement.source,

0 commit comments

Comments
 (0)