Skip to content

Commit

Permalink
Resolve class and method of the same name correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Dec 9, 2021
1 parent c81b52e commit 9dcf6d3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
48 changes: 28 additions & 20 deletions lib/rdoc/cross_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RDoc::CrossReference
#
# See CLASS_REGEXP_STR

METHOD_REGEXP_STR = '([a-z]\w*[!?=]?|%|===?|\[\]=?|<<|>>|\+@|-@|-|\+|\*)(?:\([\w.+*/=<>-]*\))?'
METHOD_REGEXP_STR = '([A-Za-z]\w*[!?=]?|%|===?|\[\]=?|<<|>>|\+@|-@|-|\+|\*)(?:\([\w.+*/=<>-]*\))?'

##
# Regular expressions matching text that should potentially have
Expand All @@ -34,12 +34,6 @@ class RDoc::CrossReference
# A::B::C.meth
#{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
# Stand-alone method (preceded by a #)
| \\?\##{METHOD_REGEXP_STR}
# Stand-alone method (preceded by ::)
| ::#{METHOD_REGEXP_STR}
# A::B::C
# The stuff after CLASS_REGEXP_STR is a
# nasty hack. CLASS_REGEXP_STR unfortunately matches
Expand All @@ -56,6 +50,12 @@ class RDoc::CrossReference
# marker.
| #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
# Stand-alone method (preceded by a #)
| \\?\##{METHOD_REGEXP_STR}
# Stand-alone method (preceded by ::)
| ::#{METHOD_REGEXP_STR}
# Things that look like filenames
# The key thing is that there must be at least
# one special character (period, slash, or
Expand All @@ -82,12 +82,12 @@ class RDoc::CrossReference
# A::B::C.meth
#{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
# Stand-alone method
| \\?#{METHOD_REGEXP_STR}
# A::B::C
| #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
# Stand-alone method
| \\?#{METHOD_REGEXP_STR}
# Things that look like filenames
| (?:\.\.\/)*[-\/\w]+[_\/.][-\w\/.]+
Expand Down Expand Up @@ -115,15 +115,8 @@ def initialize context
@seen = {}
end

##
# Returns a reference to +name+.
#
# If the reference is found and +name+ is not documented +text+ will be
# returned. If +name+ is escaped +name+ is returned. If +name+ is not
# found +text+ is returned.

def resolve name, text
return @seen[name] if @seen.include? name
def resolve_method name
ref = nil

if /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
type = $2
Expand Down Expand Up @@ -165,12 +158,27 @@ def resolve name, text
end
end

ref
end

##
# Returns a reference to +name+.
#
# If the reference is found and +name+ is not documented +text+ will be
# returned. If +name+ is escaped +name+ is returned. If +name+ is not
# found +text+ is returned.

def resolve name, text
return @seen[name] if @seen.include? name

ref = case name
when /^\\(#{CLASS_REGEXP_STR})$/o then
@context.find_symbol $1
else
@context.find_symbol name
end unless ref
end

ref = resolve_method name unless ref

# Try a page name
ref = @store.page name if not ref and name =~ /^[\w.]+$/
Expand Down
9 changes: 9 additions & 0 deletions test/rdoc/test_rdoc_cross_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def test_resolve_C4_C4
assert_ref @c4_c4, 'C4'
end

def test_resolve_class_and_method_of_the_same_name
assert_ref @c10_class, 'C10'
assert_ref @c10_method, '#C10'
assert_ref @c11_class, 'C11'
assert_ref @c11_method, '#C11'
assert_ref @c10_c11_class, 'C10::C11'
assert_ref @c10_c11_method, 'C10#C11'
end

def test_resolve_class
assert_ref @c1, 'C1'
refute_ref 'H1'
Expand Down
17 changes: 17 additions & 0 deletions test/rdoc/xref_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ def bar() end
end
end
class C10
class C11
end
def C11
end
end
def C10
end
class C11
end
def C11
end
module M1
def m
end
Expand Down
8 changes: 8 additions & 0 deletions test/rdoc/xref_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def generator.file_dir() nil end
@c9_b_c_foo = @c9_b.method_list.first
@c9_b_i_bar = @c9_b.method_list.last

@object = @xref_data.find_module_named 'Object'
@c10_class = @xref_data.find_module_named 'C10'
@c10_method = @object.find_method_named 'C10'
@c11_class = @xref_data.find_module_named 'C11'
@c10_c11_class = @c10_class.find_module_named 'C11'
@c10_c11_method = @c10_class.find_method_named 'C11'
@c11_method = @object.find_method_named 'C11'

@m1 = @xref_data.find_module_named 'M1'
@m1_m = @m1.method_list.first

Expand Down

0 comments on commit 9dcf6d3

Please sign in to comment.