From 08114d0310c7b6f7f1ce1e5efb8534025cdc335b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 4 Jan 2023 15:18:03 +0900 Subject: [PATCH] [Misc #19304] Pretend global methods to be in `Object` Show global methods, which are defined as public in `Kernel`, like as defined in `Object`. --- lib/rdoc/context.rb | 9 ++++++++- lib/rdoc/normal_module.rb | 15 +++++++++++++++ lib/rdoc/parser/ruby.rb | 15 ++++++++++++++- test/rdoc/test_rdoc_parser_ruby.rb | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/rdoc/context.rb b/lib/rdoc/context.rb index c6edfb473c..53671763a5 100644 --- a/lib/rdoc/context.rb +++ b/lib/rdoc/context.rb @@ -1026,7 +1026,10 @@ def methods_by_type section = nil each_method do |method| next if section and not method.section == section - methods[method.type][method.visibility] << method + if (visibility = method.visibility) == :module_function + visibility = :public + end + methods[method.type][visibility] << method end methods @@ -1259,6 +1262,10 @@ def upgrade_to_class mod, class_type, enclosing klass end + def pretend_object? # :nodoc: + false + end + autoload :Section, "#{__dir__}/context/section" end diff --git a/lib/rdoc/normal_module.rb b/lib/rdoc/normal_module.rb index 498ec4dde2..65c0b85d80 100644 --- a/lib/rdoc/normal_module.rb +++ b/lib/rdoc/normal_module.rb @@ -15,6 +15,11 @@ def inspect # :nodoc: ] end + def initialize(name, *args) # :nodoc: + super + @pretend_object = name == "Kernel" + end + ## # The definition of this module, module MyModuleName @@ -29,6 +34,16 @@ def module? true end + ## + # Show public methods in Object class? + + def pretend_object? + if @pretend_object + (@current_line_visibility || @visibility) == :public + end + end + + ## def pretty_print q # :nodoc: q.group 2, "[module #{full_name}:", "]" do q.breakable diff --git a/lib/rdoc/parser/ruby.rb b/lib/rdoc/parser/ruby.rb index 85f1cd0391..85898a51e1 100644 --- a/lib/rdoc/parser/ruby.rb +++ b/lib/rdoc/parser/ruby.rb @@ -222,7 +222,7 @@ def get_visibility_information tk, single # :nodoc: :public when 'module_function' then singleton = true - :public + :module_function else raise RDoc::Error, "Invalid visibility: #{tk.name}" end @@ -1473,6 +1473,9 @@ def parse_method(container, single, tk, comment) meth.add_tokens [token, newline, indent] meth.add_tokens @token_stream + if !meth.singleton and container.pretend_object? + container = @top_level.object_class + end parse_method_params_and_body container, single, meth, added_container comment.normalize @@ -1511,6 +1514,9 @@ def parse_method_params_and_body container, single, meth, added_container meth.visibility = :public end end + if meth.visibility == :module_function + meth.visibility = :public + end parse_statements container, single, meth end @@ -2351,7 +2357,14 @@ def update_visibility container, vis_type, vis, singleton # :nodoc: container.set_visibility_for args, vis, singleton end + # Move public methods from Kernel to Object + if container.pretend_object? + old_methods = container.methods_hash + container = @top_level.object_class + end + new_methods.each do |method| + old_methods&.delete(method.pretty_name) case method when RDoc::AnyMethod then container.add_method method diff --git a/test/rdoc/test_rdoc_parser_ruby.rb b/test/rdoc/test_rdoc_parser_ruby.rb index 3e2a85ffba..47fe39da92 100644 --- a/test/rdoc/test_rdoc_parser_ruby.rb +++ b/test/rdoc/test_rdoc_parser_ruby.rb @@ -2747,7 +2747,7 @@ def test_parse_statements_identifier_module_function assert_equal false, foo.singleton, 'instance method singleton' assert_equal 'foo', s_foo.name, 'module function name' - assert_equal :public, s_foo.visibility, 'module function visibility' + assert_equal :module_function, s_foo.visibility, 'module function visibility' assert_equal true, s_foo.singleton, 'module function singleton' end