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 committed Jan 4, 2023
1 parent 9c03b6e commit 90f8230
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/rdoc/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,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 @@ -1462,6 +1462,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 @@ -1500,6 +1503,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 @@ -2339,7 +2345,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
Original file line number Diff line number Diff line change
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 90f8230

Please sign in to comment.