[前][次][番号順一覧][スレッド一覧]

ruby-changes:70119

From: aycabta <ko1@a...>
Date: Thu, 9 Dec 2021 18:16:16 +0900 (JST)
Subject: [ruby-changes:70119] 2e50989ad3 (master): [ruby/rdoc] Resolve class and method of the same name correctly

https://git.ruby-lang.org/ruby.git/commit/?id=2e50989ad3

From 2e50989ad39a1085e04a901d072e7a2a77d1dc8f Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Sat, 4 Dec 2021 18:05:37 +0900
Subject: [ruby/rdoc] Resolve class and method of the same name correctly

https://github.com/ruby/rdoc/commit/1e16284fe5
---
 lib/rdoc/cross_reference.rb            | 48 ++++++++++++++++++++--------------
 test/rdoc/test_rdoc_cross_reference.rb |  9 +++++++
 test/rdoc/xref_data.rb                 | 17 ++++++++++++
 test/rdoc/xref_test_case.rb            |  8 ++++++
 4 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/lib/rdoc/cross_reference.rb b/lib/rdoc/cross_reference.rb
index 4a6abfa3ac0..ef8e21bde84 100644
--- a/lib/rdoc/cross_reference.rb
+++ b/lib/rdoc/cross_reference.rb
@@ -19,7 +19,7 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L19
   #
   # 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
@@ -34,12 +34,6 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L34
                        # 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
@@ -56,6 +50,12 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L50
                        # 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
@@ -82,12 +82,12 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L82
                        # 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\/.]+
 
@@ -115,15 +115,8 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L115
     @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
@@ -165,12 +158,27 @@ class RDoc::CrossReference https://github.com/ruby/ruby/blob/trunk/lib/rdoc/cross_reference.rb#L158
       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.]+$/
diff --git a/test/rdoc/test_rdoc_cross_reference.rb b/test/rdoc/test_rdoc_cross_reference.rb
index 94ddc1e1e47..f73681ebc85 100644
--- a/test/rdoc/test_rdoc_cross_reference.rb
+++ b/test/rdoc/test_rdoc_cross_reference.rb
@@ -88,6 +88,15 @@ class TestRDocCrossReference < XrefTestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_cross_reference.rb#L88
     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'
diff --git a/test/rdoc/xref_data.rb b/test/rdoc/xref_data.rb
index aa9faaecd95..de76a906021 100644
--- a/test/rdoc/xref_data.rb
+++ b/test/rdoc/xref_data.rb
@@ -115,6 +115,23 @@ class C9 https://github.com/ruby/ruby/blob/trunk/test/rdoc/xref_data.rb#L115
   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
diff --git a/test/rdoc/xref_test_case.rb b/test/rdoc/xref_test_case.rb
index 729e4a70b74..22b00d04bc4 100644
--- a/test/rdoc/xref_test_case.rb
+++ b/test/rdoc/xref_test_case.rb
@@ -70,6 +70,14 @@ class XrefTestCase < RDoc::TestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/xref_test_case.rb#L70
     @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
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]