Skip to content

Commit 9b4047f

Browse files
committed
fix: process directives in empty classes
1 parent 5b93b3a commit 9b4047f

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/yard/parser/ruby/ruby_parser.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,23 @@ def insert_comments
658658
end
659659
end unless @comments.empty?
660660

661+
# Attach comments that fall within an otherwise empty
662+
# class or module body. Without this step, a comment used
663+
# solely for directives (like @!method) would be treated as
664+
# a top-level comment and its directives would not be scoped
665+
# to the namespace.
666+
unless @comments.empty?
667+
root.traverse do |node|
668+
next unless [:class, :module, :sclass].include?(node.type)
669+
body = node.children.last
670+
next unless body && body.type == :list && body.empty?
671+
@comments.keys.each do |line|
672+
next unless node.line_range.include?(line)
673+
add_comment(line, nil, body, true)
674+
end
675+
end
676+
end
677+
661678
# insert all remaining comments
662679
@comments.each do |line, _comment|
663680
add_comment(line, nil, root, true)

spec/tags/directives_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ class Foo
263263
expect(Registry.at('#foo').parameters).to eq [['a', nil], ['b', nil], ['c', 'nil']]
264264
end
265265

266+
it "processes @!method inside an empty class" do
267+
YARD.parse_string <<-eof
268+
class Foo
269+
# @!method bar
270+
# Docstring
271+
end
272+
eof
273+
expect(Registry.at('Foo#bar').docstring).to eq 'Docstring'
274+
end
275+
266276
it "is able to define method with module scope (module function)" do
267277
YARD.parse_string <<-eof
268278
# @!method foo

0 commit comments

Comments
 (0)