Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/yard/parser/ruby/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,23 @@ def insert_comments
end
end unless @comments.empty?

# Attach comments that fall within an otherwise empty
# class or module body. Without this step, a comment used
# solely for directives (like @!method) would be treated as
# a top-level comment and its directives would not be scoped
# to the namespace.
unless @comments.empty?
root.traverse do |node|
next unless [:class, :module, :sclass].include?(node.type)
body = node.children.last
next unless body && body.type == :list && body.empty?
@comments.keys.each do |line|
next unless node.line_range.include?(line)
add_comment(line, nil, body, true)
end
end
end

# insert all remaining comments
@comments.each do |line, _comment|
add_comment(line, nil, root, true)
Expand Down
10 changes: 10 additions & 0 deletions spec/tags/directives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ class Foo
expect(Registry.at('#foo').parameters).to eq [['a', nil], ['b', nil], ['c', 'nil']]
end

it "processes @!method inside an empty class" do
YARD.parse_string <<-eof
class Foo
# @!method bar
# Docstring
end
eof
expect(Registry.at('Foo#bar').docstring).to eq 'Docstring'
end

it "is able to define method with module scope (module function)" do
YARD.parse_string <<-eof
# @!method foo
Expand Down
Loading