Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolution of reference tags in a referenced docstring #1487

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 7 additions & 2 deletions lib/yard/docstring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def parser(*args) default_parser.new(*args) end
# @return [CodeObjects::Base] the object that owns the docstring.
attr_accessor :object

# @return [CodeObjects::Base] the namespace of the docstring
attr_reader :namespace

# @return [Range] line range in the {#object}'s file where the docstring was parsed from
attr_accessor :line_range

Expand Down Expand Up @@ -79,6 +82,7 @@ def self.new!(text, tags = [], object = nil, raw_data = nil, ref_object = nil)
docstring.replace(text, false)
docstring.object = object
docstring.add_tag(*tags)
docstring.instance_variable_set("@namespace", object)
docstring.instance_variable_set("@unresolved_reference", ref_object)
docstring.instance_variable_set("@all", raw_data) if raw_data
docstring
Expand All @@ -102,6 +106,7 @@ def self.new!(text, tags = [], object = nil, raw_data = nil, ref_object = nil)
# with.
def initialize(content = '', object = nil)
@object = object
@namespace = object
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kind of seems like .namespace is just a dupe of .object? Or, at the very least, that namespace could be a combination of @object || @unresolved_reference and thus no attribute would be needed.

@summary = nil
@hash_flag = false

Expand Down Expand Up @@ -330,9 +335,9 @@ def resolve_reference
return if defined?(@unresolved_reference).nil? || @unresolved_reference.nil?
return if CodeObjects::Proxy === @unresolved_reference

reference = @unresolved_reference
@namespace = @unresolved_reference
@unresolved_reference = nil
self.all = [reference.docstring.all, @all].join("\n")
self.all = [@namespace.docstring.all, @all].join("\n")
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/yard/docstring_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def create_tag(tag_name, tag_buf = '')

# Creates a {Tags::RefTag}
def create_ref_tag(tag_name, name, object_name)
@tags << Tags::RefTagList.new(tag_name, P(object, object_name), name)
namespace = object && object.docstring ? object.docstring.namespace : object
@tags << Tags::RefTagList.new(tag_name, P(namespace, object_name), name)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See above) I could be wrong but it seems like namespace could be replaced entirely by object || @unresolved_reference and thus be the only necessary line of implementation.

end

# Creates a new directive using the registered {#library}
Expand Down
19 changes: 19 additions & 0 deletions spec/docstring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@
expect(tags.first.owner).to eq o
end

it "uses the right namespace following ref tags through a docstring reference" do
YARD.parse_string <<-eof
class A
class B
# @param x
def b(x); end
end
# @param x (see B#b)
def a(x);end
end
class C
# (see A#a)
def c(x);end
end
eof

expect(YARD::Registry.at('C#c').tags.map(&:name)).to eq ['x']
end

it "returns an empty list (and warning) if circular reftags are found" do
YARD.parse_string <<-eof
class Foo
Expand Down