Skip to content

Commit 9dcf6d3

Browse files
committed
Resolve class and method of the same name correctly
1 parent c81b52e commit 9dcf6d3

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

Diff for: lib/rdoc/cross_reference.rb

+28-20
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RDoc::CrossReference
1919
#
2020
# See CLASS_REGEXP_STR
2121

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

2424
##
2525
# Regular expressions matching text that should potentially have
@@ -34,12 +34,6 @@ class RDoc::CrossReference
3434
# A::B::C.meth
3535
#{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
3636
37-
# Stand-alone method (preceded by a #)
38-
| \\?\##{METHOD_REGEXP_STR}
39-
40-
# Stand-alone method (preceded by ::)
41-
| ::#{METHOD_REGEXP_STR}
42-
4337
# A::B::C
4438
# The stuff after CLASS_REGEXP_STR is a
4539
# nasty hack. CLASS_REGEXP_STR unfortunately matches
@@ -56,6 +50,12 @@ class RDoc::CrossReference
5650
# marker.
5751
| #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
5852
53+
# Stand-alone method (preceded by a #)
54+
| \\?\##{METHOD_REGEXP_STR}
55+
56+
# Stand-alone method (preceded by ::)
57+
| ::#{METHOD_REGEXP_STR}
58+
5959
# Things that look like filenames
6060
# The key thing is that there must be at least
6161
# one special character (period, slash, or
@@ -82,12 +82,12 @@ class RDoc::CrossReference
8282
# A::B::C.meth
8383
#{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
8484
85-
# Stand-alone method
86-
| \\?#{METHOD_REGEXP_STR}
87-
8885
# A::B::C
8986
| #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
9087
88+
# Stand-alone method
89+
| \\?#{METHOD_REGEXP_STR}
90+
9191
# Things that look like filenames
9292
| (?:\.\.\/)*[-\/\w]+[_\/.][-\w\/.]+
9393
@@ -115,15 +115,8 @@ def initialize context
115115
@seen = {}
116116
end
117117

118-
##
119-
# Returns a reference to +name+.
120-
#
121-
# If the reference is found and +name+ is not documented +text+ will be
122-
# returned. If +name+ is escaped +name+ is returned. If +name+ is not
123-
# found +text+ is returned.
124-
125-
def resolve name, text
126-
return @seen[name] if @seen.include? name
118+
def resolve_method name
119+
ref = nil
127120

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

161+
ref
162+
end
163+
164+
##
165+
# Returns a reference to +name+.
166+
#
167+
# If the reference is found and +name+ is not documented +text+ will be
168+
# returned. If +name+ is escaped +name+ is returned. If +name+ is not
169+
# found +text+ is returned.
170+
171+
def resolve name, text
172+
return @seen[name] if @seen.include? name
173+
168174
ref = case name
169175
when /^\\(#{CLASS_REGEXP_STR})$/o then
170176
@context.find_symbol $1
171177
else
172178
@context.find_symbol name
173-
end unless ref
179+
end
180+
181+
ref = resolve_method name unless ref
174182

175183
# Try a page name
176184
ref = @store.page name if not ref and name =~ /^[\w.]+$/

Diff for: test/rdoc/test_rdoc_cross_reference.rb

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def test_resolve_C4_C4
8888
assert_ref @c4_c4, 'C4'
8989
end
9090

91+
def test_resolve_class_and_method_of_the_same_name
92+
assert_ref @c10_class, 'C10'
93+
assert_ref @c10_method, '#C10'
94+
assert_ref @c11_class, 'C11'
95+
assert_ref @c11_method, '#C11'
96+
assert_ref @c10_c11_class, 'C10::C11'
97+
assert_ref @c10_c11_method, 'C10#C11'
98+
end
99+
91100
def test_resolve_class
92101
assert_ref @c1, 'C1'
93102
refute_ref 'H1'

Diff for: test/rdoc/xref_data.rb

+17
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ def bar() end
115115
end
116116
end
117117
118+
class C10
119+
class C11
120+
end
121+
122+
def C11
123+
end
124+
end
125+
126+
def C10
127+
end
128+
129+
class C11
130+
end
131+
132+
def C11
133+
end
134+
118135
module M1
119136
def m
120137
end

Diff for: test/rdoc/xref_test_case.rb

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def generator.file_dir() nil end
7070
@c9_b_c_foo = @c9_b.method_list.first
7171
@c9_b_i_bar = @c9_b.method_list.last
7272

73+
@object = @xref_data.find_module_named 'Object'
74+
@c10_class = @xref_data.find_module_named 'C10'
75+
@c10_method = @object.find_method_named 'C10'
76+
@c11_class = @xref_data.find_module_named 'C11'
77+
@c10_c11_class = @c10_class.find_module_named 'C11'
78+
@c10_c11_method = @c10_class.find_method_named 'C11'
79+
@c11_method = @object.find_method_named 'C11'
80+
7381
@m1 = @xref_data.find_module_named 'M1'
7482
@m1_m = @m1.method_list.first
7583

0 commit comments

Comments
 (0)