Skip to content

Commit 08114d0

Browse files
nobuhsbt
authored andcommitted
[Misc #19304] Pretend global methods to be in Object
Show global methods, which are defined as public in `Kernel`, like as defined in `Object`.
1 parent 9ed530b commit 08114d0

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

lib/rdoc/context.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,10 @@ def methods_by_type section = nil
10261026

10271027
each_method do |method|
10281028
next if section and not method.section == section
1029-
methods[method.type][method.visibility] << method
1029+
if (visibility = method.visibility) == :module_function
1030+
visibility = :public
1031+
end
1032+
methods[method.type][visibility] << method
10301033
end
10311034

10321035
methods
@@ -1259,6 +1262,10 @@ def upgrade_to_class mod, class_type, enclosing
12591262
klass
12601263
end
12611264

1265+
def pretend_object? # :nodoc:
1266+
false
1267+
end
1268+
12621269
autoload :Section, "#{__dir__}/context/section"
12631270

12641271
end

lib/rdoc/normal_module.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def inspect # :nodoc:
1515
]
1616
end
1717

18+
def initialize(name, *args) # :nodoc:
19+
super
20+
@pretend_object = name == "Kernel"
21+
end
22+
1823
##
1924
# The definition of this module, <tt>module MyModuleName</tt>
2025

@@ -29,6 +34,16 @@ def module?
2934
true
3035
end
3136

37+
##
38+
# Show public methods in Object class?
39+
40+
def pretend_object?
41+
if @pretend_object
42+
(@current_line_visibility || @visibility) == :public
43+
end
44+
end
45+
46+
##
3247
def pretty_print q # :nodoc:
3348
q.group 2, "[module #{full_name}:", "]" do
3449
q.breakable

lib/rdoc/parser/ruby.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def get_visibility_information tk, single # :nodoc:
222222
:public
223223
when 'module_function' then
224224
singleton = true
225-
:public
225+
:module_function
226226
else
227227
raise RDoc::Error, "Invalid visibility: #{tk.name}"
228228
end
@@ -1473,6 +1473,9 @@ def parse_method(container, single, tk, comment)
14731473
meth.add_tokens [token, newline, indent]
14741474
meth.add_tokens @token_stream
14751475

1476+
if !meth.singleton and container.pretend_object?
1477+
container = @top_level.object_class
1478+
end
14761479
parse_method_params_and_body container, single, meth, added_container
14771480

14781481
comment.normalize
@@ -1511,6 +1514,9 @@ def parse_method_params_and_body container, single, meth, added_container
15111514
meth.visibility = :public
15121515
end
15131516
end
1517+
if meth.visibility == :module_function
1518+
meth.visibility = :public
1519+
end
15141520

15151521
parse_statements container, single, meth
15161522
end
@@ -2351,7 +2357,14 @@ def update_visibility container, vis_type, vis, singleton # :nodoc:
23512357
container.set_visibility_for args, vis, singleton
23522358
end
23532359

2360+
# Move public methods from Kernel to Object
2361+
if container.pretend_object?
2362+
old_methods = container.methods_hash
2363+
container = @top_level.object_class
2364+
end
2365+
23542366
new_methods.each do |method|
2367+
old_methods&.delete(method.pretty_name)
23552368
case method
23562369
when RDoc::AnyMethod then
23572370
container.add_method method

test/rdoc/test_rdoc_parser_ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,7 @@ def test_parse_statements_identifier_module_function
27472747
assert_equal false, foo.singleton, 'instance method singleton'
27482748

27492749
assert_equal 'foo', s_foo.name, 'module function name'
2750-
assert_equal :public, s_foo.visibility, 'module function visibility'
2750+
assert_equal :module_function, s_foo.visibility, 'module function visibility'
27512751
assert_equal true, s_foo.singleton, 'module function singleton'
27522752
end
27532753

0 commit comments

Comments
 (0)