Skip to content

Commit 2420152

Browse files
committed
Suppress include and extend inside method block
1 parent bf0b8cc commit 2420152

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/rdoc/parser/prism_ruby.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def initialize(top_level, content, options, stats)
3535
@container = top_level
3636
@visibility = :public
3737
@singleton = false
38+
@include_extend_suppressed = false
39+
end
40+
41+
# Suppress `extend` and `include` within block
42+
# because they might be a metaprogramming block
43+
# example: `Module.new { include M }` `M.module_eval { include N }`
44+
45+
def suppress_include_extend
46+
@include_extend_suppressed = true
47+
yield
48+
@include_extend_suppressed = false
3849
end
3950

4051
# Dive into another container
@@ -43,9 +54,11 @@ def with_container(container, singleton: false)
4354
old_container = @container
4455
old_visibility = @visibility
4556
old_singleton = @singleton
57+
old_include_extend_suppressed = @include_extend_suppressed
4658
@visibility = :public
4759
@container = container
4860
@singleton = singleton
61+
@include_extend_suppressed = false
4962
unless singleton
5063
@module_nesting.push container
5164

@@ -58,6 +71,7 @@ def with_container(container, singleton: false)
5871
@container = old_container
5972
@visibility = old_visibility
6073
@singleton = old_singleton
74+
@include_extend_suppressed = old_include_extend_suppressed
6175
@module_nesting.pop unless singleton
6276
end
6377

@@ -471,6 +485,7 @@ def add_attributes(names, rw, line_no)
471485
end
472486

473487
def add_includes_extends(names, rdoc_class, line_no) # :nodoc:
488+
return if @include_extend_suppressed
474489
comment = consecutive_comment(line_no)
475490
handle_consecutive_comment_directive(@container, comment)
476491
names.each do |name|
@@ -736,13 +751,22 @@ def visit_call_node(node)
736751
when :private_class_method
737752
_visit_call_public_private_class_method(node, :private) { super }
738753
else
754+
node.arguments&.accept(self)
739755
super
740756
end
741757
else
742758
super
743759
end
744760
end
745761

762+
def visit_block_node(node)
763+
@scanner.suppress_include_extend do
764+
# include and extend inside block are not documentable
765+
# method definition might also not work but document it for now.
766+
super
767+
end
768+
end
769+
746770
def visit_alias_method_node(node)
747771
@scanner.process_comments_until(node.location.start_line - 1)
748772
return unless node.old_name.is_a?(Prism::SymbolNode) && node.new_name.is_a?(Prism::SymbolNode)

test/rdoc/test_rdoc_parser_prism_ruby.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,35 @@ def d; end
19271927
assert_equal ['a', 'f'], mod.method_list.map(&:name)
19281928
end
19291929

1930+
def test_include_extend_suppressed_within_block
1931+
util_parser <<~RUBY
1932+
module M; end
1933+
module N; end
1934+
module O: end
1935+
class A
1936+
metaprogramming do
1937+
include M
1938+
extend N
1939+
class B
1940+
include M
1941+
extend N
1942+
end
1943+
include M
1944+
extend N
1945+
end
1946+
include O
1947+
extend O
1948+
end
1949+
RUBY
1950+
a, b = @store.all_classes
1951+
unless accept_legacy_bug?
1952+
assert_equal ['O'], a.includes.map(&:name)
1953+
assert_equal ['O'], a.extends.map(&:name)
1954+
end
1955+
assert_equal ['M'], b.includes.map(&:name)
1956+
assert_equal ['N'], b.extends.map(&:name)
1957+
end
1958+
19301959
def test_multibyte_method_name
19311960
content = <<~RUBY
19321961
class Foo

0 commit comments

Comments
 (0)