Skip to content

Commit

Permalink
[Misc #19304] Pretend global methods to be in Object
Browse files Browse the repository at this point in the history
Show global methods, which are defined as public in `Kernel`, like as
defined in `Object`.
  • Loading branch information
nobu authored and hsbt committed Mar 15, 2024
1 parent 9ed530b commit 08114d0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/rdoc/context.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions lib/rdoc/normal_module.rb
Expand Up @@ -15,6 +15,11 @@ def inspect # :nodoc:
]
end

def initialize(name, *args) # :nodoc:
super
@pretend_object = name == "Kernel"
end

##
# The definition of this module, <tt>module MyModuleName</tt>

Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lib/rdoc/parser/ruby.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/rdoc/test_rdoc_parser_ruby.rb
Expand Up @@ -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

Expand Down

0 comments on commit 08114d0

Please sign in to comment.