Skip to content

Commit db954b7

Browse files
authored
Merge pull request #853 from aycabta/resolve-class-and-method-of-the-same-name-correctly
Resolve class and method of the same name correctly
2 parents c81b52e + 23747b4 commit db954b7

11 files changed

+83
-38
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_class_module.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_add_comment_stopdoc
6363
end
6464

6565
def test_ancestors
66-
assert_equal [@parent, "Object"], @child.ancestors
66+
assert_equal [@parent, @object, "BasicObject"], @child.ancestors
6767
end
6868

6969
def test_comment_equals
@@ -129,7 +129,7 @@ def test_documented_eh
129129
end
130130

131131
def test_each_ancestor
132-
assert_equal [@parent], @child.each_ancestor.to_a
132+
assert_equal [@parent, @object], @child.each_ancestor.to_a
133133
end
134134

135135
def test_each_ancestor_cycle

Diff for: test/rdoc/test_rdoc_context.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_add_class_basic_object
125125

126126
basic = @c1.find_module_named 'BasicObject'
127127

128-
assert_equal 'Object', basic.superclass
128+
assert_equal @object, basic.superclass
129129
end
130130

131131
def test_add_class_object

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/test_rdoc_extend.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_module_extended
4343
m1_m2_k0.add_extend e0_m3
4444

4545
assert_equal [e0_m4, e0_m5, e0_m6, e0_m1, e0_m2, e0_m3], m1_m2_k0.extends
46-
assert_equal ['Object'], m1_m2_k0.ancestors
46+
assert_equal [@object, 'BasicObject'], m1_m2_k0.ancestors
4747

4848
m1_k1 = m1.add_class RDoc::NormalClass, 'Klass1'
4949

@@ -60,7 +60,7 @@ def test_module_extended
6060
m1_k1.add_extend e1_k0_m4
6161

6262
assert_equal [e1_m1, e1_m2, e1_m3, e1_m4, e1_k0_m4], m1_k1.extends
63-
assert_equal ['Object'], m1_k1.ancestors
63+
assert_equal [@object, 'BasicObject'], m1_k1.ancestors
6464

6565
m1_k2 = m1.add_class RDoc::NormalClass, 'Klass2'
6666

@@ -75,7 +75,7 @@ def test_module_extended
7575
m1_k2.add_extend e2_k0_m4
7676

7777
assert_equal [e2_m1, e2_m3, e2_m2, e2_k0_m4], m1_k2.extends
78-
assert_equal ['Object'], m1_k2.ancestors
78+
assert_equal [@object, 'BasicObject'], m1_k2.ancestors
7979

8080
m1_k3 = m1.add_class RDoc::NormalClass, 'Klass3'
8181

@@ -88,7 +88,7 @@ def test_module_extended
8888
m1_k3.add_extend e3_m4
8989

9090
assert_equal [e3_m1, e3_m2, e3_m4], m1_k3.extends
91-
assert_equal ['Object'], m1_k3.ancestors
91+
assert_equal [@object, 'BasicObject'], m1_k3.ancestors
9292
end
9393

9494
end

Diff for: test/rdoc/test_rdoc_include.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_module_extended
4646

4747
assert_equal [i0_m4, i0_m5, i0_m6, i0_m1, i0_m2, i0_m3], m1_m2_k0.includes
4848
assert_equal [m1_m2_m3, m1_m2, m1, m1_m2_k0_m4_m6, m1_m2_k0_m5,
49-
m1_m2_k0_m4, 'Object'], m1_m2_k0.ancestors
49+
m1_m2_k0_m4, @object, 'BasicObject'], m1_m2_k0.ancestors
5050

5151
m1_k1 = m1.add_class RDoc::NormalClass, 'Klass1'
5252

@@ -63,8 +63,8 @@ def test_module_extended
6363
m1_k1.add_include i1_k0_m4
6464

6565
assert_equal [i1_m1, i1_m2, i1_m3, i1_m4, i1_k0_m4], m1_k1.includes
66-
assert_equal [m1_m2_k0_m4, m1_m2_m3_m4, m1_m2_m3, m1_m2, m1, 'Object'],
67-
m1_k1.ancestors
66+
assert_equal [m1_m2_k0_m4, m1_m2_m3_m4, m1_m2_m3, m1_m2, m1, @object,
67+
'BasicObject'], m1_k1.ancestors
6868

6969
m1_k2 = m1.add_class RDoc::NormalClass, 'Klass2'
7070

@@ -79,7 +79,8 @@ def test_module_extended
7979
m1_k2.add_include i2_k0_m4
8080

8181
assert_equal [i2_m1, i2_m3, i2_m2, i2_k0_m4], m1_k2.includes
82-
assert_equal [m1_m2_k0_m4, m1_m2, m1_m3, m1, 'Object'], m1_k2.ancestors
82+
assert_equal [m1_m2_k0_m4, m1_m2, m1_m3, m1, @object, 'BasicObject'],
83+
m1_k2.ancestors
8384

8485
m1_k3 = m1.add_class RDoc::NormalClass, 'Klass3'
8586

@@ -92,7 +93,7 @@ def test_module_extended
9293
m1_k3.add_include i3_m4
9394

9495
assert_equal [i3_m1, i3_m2, i3_m4], m1_k3.includes
95-
assert_equal [m1_m2_m4, m1_m2, m1, 'Object'], m1_k3.ancestors
96+
assert_equal [m1_m2_m4, m1_m2, m1, @object, 'BasicObject'], m1_k3.ancestors
9697
end
9798

9899
def test_store_equals

Diff for: test/rdoc/test_rdoc_normal_class.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def test_ancestors
1111
sub_klass.superclass = klass
1212
sub_klass.add_include incl
1313

14-
assert_equal [incl.name, klass, 'Object'], sub_klass.ancestors
14+
assert_equal [incl.name, klass, @object, 'BasicObject'], sub_klass.ancestors
1515
end
1616

1717
def test_ancestors_multilevel
1818
c1 = @top_level.add_class RDoc::NormalClass, 'Outer'
1919
c2 = @top_level.add_class RDoc::NormalClass, 'Middle', c1.full_name
2020
c3 = @top_level.add_class RDoc::NormalClass, 'Inner', c2.full_name
2121

22-
assert_equal [c2, c1, 'Object'], c3.ancestors
22+
assert_equal [c2, c1, @object, 'BasicObject'], c3.ancestors
2323
end
2424

2525
def test_aref

Diff for: test/rdoc/test_rdoc_store.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ def test_add_file_relative
161161

162162
def test_all_classes_and_modules
163163
expected = %w[
164-
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
164+
C1 C10 C10::C11 C11 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
165165
Child
166166
M1 M1::M2
167+
Object
167168
Parent
168169
]
169170

@@ -212,8 +213,9 @@ def test_class_path
212213

213214
def test_classes
214215
expected = %w[
215-
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
216+
C1 C10 C10::C11 C11 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
216217
Child
218+
Object
217219
Parent
218220
]
219221

Diff for: test/rdoc/test_rdoc_top_level.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_add_method
9090
@top_level.add_method method
9191

9292
object = @store.find_class_named 'Object'
93-
assert_equal [method], object.method_list
93+
assert_equal [@c10_method, @c11_method, method], object.method_list
9494
assert_includes object.in_files, @top_level
9595
end
9696

@@ -101,7 +101,7 @@ def test_add_method_stopdoc
101101
@top_level.add_method method
102102

103103
object = @store.find_class_named('Object')
104-
assert_empty object.method_list
104+
assert_equal [@c10_method, @c11_method], object.method_list
105105
assert_includes object.in_files, @top_level
106106
end
107107

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)