From 810913a7ea9b1398245e4de996bc925c4f067fb8 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 7 Jan 2024 19:44:14 +0900 Subject: [PATCH 1/2] Singleton method visibility should be isolated Each singleton method definition of the form `def recv.method` has visibility separate from the outer scope and is set to `public` by default. --- lib/rdoc/parser/ruby.rb | 6 ++++++ test/rdoc/test_rdoc_context.rb | 2 ++ test/rdoc/xref_data.rb | 2 ++ 3 files changed, 10 insertions(+) diff --git a/lib/rdoc/parser/ruby.rb b/lib/rdoc/parser/ruby.rb index 39c3bae4c4..aab91e0790 100644 --- a/lib/rdoc/parser/ruby.rb +++ b/lib/rdoc/parser/ruby.rb @@ -1453,6 +1453,12 @@ def parse_method(container, single, tk, comment) meth = RDoc::AnyMethod.new get_tkread, name look_for_directives_in meth, comment meth.singleton = single == SINGLE ? true : singleton + if singleton + # `current_line_visibility' is useless because it works against + # the normal method named as same as the singleton method, after + # the latter was defined. Of course these are different things. + container.current_line_visibility = :public + end record_location meth meth.line = line_no diff --git a/test/rdoc/test_rdoc_context.rb b/test/rdoc/test_rdoc_context.rb index 85665599fb..ac425349d8 100644 --- a/test/rdoc/test_rdoc_context.rb +++ b/test/rdoc/test_rdoc_context.rb @@ -927,6 +927,8 @@ def test_visibility_def assert_equal :private, @c6.find_method_named('priv6').visibility assert_equal :protected, @c6.find_method_named('prot6').visibility assert_equal :public, @c6.find_method_named('pub6').visibility + assert_equal :public, @c6.find_method_named('s_pub1').visibility + assert_equal :public, @c6.find_method_named('s_pub3').visibility end def util_visibilities diff --git a/test/rdoc/xref_data.rb b/test/rdoc/xref_data.rb index c9315b7806..42b5d1baf3 100644 --- a/test/rdoc/xref_data.rb +++ b/test/rdoc/xref_data.rb @@ -74,6 +74,7 @@ def priv3() end def priv4() end public def pub5() end def priv5() end + def self.s_pub1() end protected private def priv6() end @@ -82,6 +83,7 @@ def prot3() end def prot5() end public def pub6() end def prot6() end + def self.s_pub3() end end class C7 From baf26363b9a0b730e2e3ded0edc0ee7947386e90 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 7 Jan 2024 20:14:09 +0900 Subject: [PATCH 2/2] Visibility should begin from `public` for each scope Even for singleton class definition such as `class << self` that shares the same container with the outer scope, its visibility is separated and set to `public` by default. --- lib/rdoc/parser/ruby.rb | 1 + test/rdoc/test_rdoc_context.rb | 4 ++++ test/rdoc/xref_data.rb | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/lib/rdoc/parser/ruby.rb b/lib/rdoc/parser/ruby.rb index aab91e0790..2cc629ebdf 100644 --- a/lib/rdoc/parser/ruby.rb +++ b/lib/rdoc/parser/ruby.rb @@ -1782,6 +1782,7 @@ def parse_statements(container, single = NORMAL, current_method = nil, nest = 1 save_visibility = container.visibility + container.visibility = :public unless current_method non_comment_seen = true diff --git a/test/rdoc/test_rdoc_context.rb b/test/rdoc/test_rdoc_context.rb index ac425349d8..c4de04e083 100644 --- a/test/rdoc/test_rdoc_context.rb +++ b/test/rdoc/test_rdoc_context.rb @@ -928,7 +928,11 @@ def test_visibility_def assert_equal :protected, @c6.find_method_named('prot6').visibility assert_equal :public, @c6.find_method_named('pub6').visibility assert_equal :public, @c6.find_method_named('s_pub1').visibility + assert_equal :public, @c6.find_method_named('s_pub2').visibility assert_equal :public, @c6.find_method_named('s_pub3').visibility + assert_equal :public, @c6.find_method_named('s_pub4').visibility + assert_equal :private, @c6.find_method_named('s_priv1').visibility + assert_equal :protected, @c6.find_method_named('s_prot1').visibility end def util_visibilities diff --git a/test/rdoc/xref_data.rb b/test/rdoc/xref_data.rb index 42b5d1baf3..257b821f4f 100644 --- a/test/rdoc/xref_data.rb +++ b/test/rdoc/xref_data.rb @@ -75,6 +75,11 @@ def priv4() end public def pub5() end def priv5() end def self.s_pub1() end + class << self + def s_pub2() end + private + def s_priv1() end + end protected private def priv6() end @@ -84,6 +89,11 @@ def prot5() end public def pub6() end def prot6() end def self.s_pub3() end + class << self + def s_pub4() end + protected + def s_prot1() end + end end class C7