Skip to content

Commit 5592a9e

Browse files
committed
Add tests
1 parent a31f403 commit 5592a9e

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

lib/yard-sorbet/handlers/sig_handler.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def process_def(def_node)
4646

4747
sig { params(attr_node: YARD::Parser::Ruby::MethodCallNode).void }
4848
def process_attr(attr_node)
49-
names = NodeUtils.validated_attribute_names(attr_node)
50-
return if merged_into_attr?(attr_node, names)
49+
return if merged_into_attr?(attr_node)
5150

5251
parse_node(attr_node, statement.docstring, include_params: false)
5352
statement.docstring = nil
@@ -56,18 +55,24 @@ def process_attr(attr_node)
5655
# An attr* sig can be merged into a previous attr* docstring if it is the only parameter passed to the attr*
5756
# declaration. This is to avoid needing to rewrite the source code to separate merged and unmerged attr*
5857
# 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
58+
sig { params(attr_node: YARD::Parser::Ruby::MethodCallNode).returns(T::Boolean) }
59+
def merged_into_attr?(attr_node)
60+
names = NodeUtils.validated_attribute_names(attr_node)
61+
return false if names.size != 1
6262

6363
nodes = namespace.attributes[scope][names.fetch(0)]
6464
return false if nodes.nil? || nodes.empty?
6565

66-
nodes.each_value { parse_node(_1, _1.docstring, include_params: false) }
66+
document_attr_methods(nodes.values.compact)
6767
attr_node.docstring = statement.docstring = nil
6868
true
6969
end
7070

71+
sig { params(method_objects: T::Array[YARD::CodeObjects::MethodObject]).void }
72+
def document_attr_methods(method_objects)
73+
method_objects.each { parse_node(_1, _1.docstring, include_params: false) }
74+
end
75+
7176
sig { params(attach_to: Documentable, docstring: T.nilable(String), include_params: T::Boolean).void }
7277
def parse_node(attach_to, docstring, include_params: true)
7378
docstring_, directives = Directives.extract_directives(docstring)

lib/yard-sorbet/node_utils.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def self.validated_attribute_names(attr_node)
6060
case obj.type
6161
when :symbol_literal then obj.jump(:ident, :op, :kw, :const).source
6262
when :string_literal then obj.jump(:string_content).source
63-
else raise YARD::Parser::UndocumentableError, obj.source
6463
end
6564
end
6665
end

spec/data/sig_handler.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ end
294294

295295
class AttrSigs
296296
sig {returns(String)}
297-
attr_accessor :my_accessor
297+
attr_accessor 'my_accessor'
298298

299299
sig {returns(Integer)}
300300
attr_reader :my_reader

spec/yard_sorbet/handlers/sig_handler_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@
1212
end
1313

1414
describe 'Merging an RBI file' do
15+
it 'includes docstring from original attr_accessor' do
16+
expect(YARD::Registry.at('Merge::A#a_foo').docstring).to eq('annotated attr_accessor')
17+
end
18+
19+
it 'merges attr_accessor sig' do
20+
expect(YARD::Registry.at('Merge::A#a_foo').tag(:return).types).to eq(['Numeric'])
21+
end
22+
23+
it 'includes docstring from original attr_reader' do
24+
expect(YARD::Registry.at('Merge::A#a_bar').docstring).to eq('Returns the value of attribute a_bar.')
25+
end
26+
27+
it 'merges attr_reader sig' do
28+
expect(YARD::Registry.at('Merge::A#a_bar').tag(:return).types).to eq(%w[String nil])
29+
end
30+
31+
it 'includes docstring from original attr_writer' do
32+
expect(YARD::Registry.at('Merge::A#a_baz=').docstring).to eq('Sets the attribute a_baz')
33+
end
34+
35+
it 'merges attr_writer sig' do
36+
expect(YARD::Registry.at('Merge::A#a_baz=').tag(:return).types).to eq(['Integer'])
37+
end
38+
1539
it 'includes docstring from original instance def' do
1640
expect(YARD::Registry.at('Merge::A#foo').docstring).to eq('The foo instance method for A')
1741
end

0 commit comments

Comments
 (0)