Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Misc #19304] Pretend global methods to be in Object #961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rdoc/context.rb
Original file line number Diff line number Diff line change
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
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 @@ -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
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