Skip to content

Commit 7e5bb30

Browse files
committed
Reject documenting def inside block
1 parent 867870d commit 7e5bb30

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

lib/rdoc/parser/prism_ruby.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ def initialize(top_level, content, options, stats)
3535
@container = top_level
3636
@visibility = :public
3737
@singleton = false
38-
@include_extend_suppressed = false
38+
@in_proc_block = false
3939
end
4040

4141
# Suppress `extend` and `include` within block
4242
# because they might be a metaprogramming block
4343
# example: `Module.new { include M }` `M.module_eval { include N }`
4444

45-
def suppress_include_extend
46-
@include_extend_suppressed = true
45+
def with_in_proc_block
46+
@in_proc_block = true
4747
yield
48-
@include_extend_suppressed = false
48+
@in_proc_block = false
4949
end
5050

5151
# Dive into another container
@@ -54,11 +54,11 @@ def with_container(container, singleton: false)
5454
old_container = @container
5555
old_visibility = @visibility
5656
old_singleton = @singleton
57-
old_include_extend_suppressed = @include_extend_suppressed
57+
old_in_proc_block = @in_proc_block
5858
@visibility = :public
5959
@container = container
6060
@singleton = singleton
61-
@include_extend_suppressed = false
61+
@in_proc_block = false
6262
unless singleton
6363
# Need to update module parent chain to emulate Module.nesting.
6464
# This mechanism is inaccurate and needs to be fixed.
@@ -70,7 +70,7 @@ def with_container(container, singleton: false)
7070
@container = old_container
7171
@visibility = old_visibility
7272
@singleton = old_singleton
73-
@include_extend_suppressed = old_include_extend_suppressed
73+
@in_proc_block = old_in_proc_block
7474
@module_nesting.pop
7575
end
7676

@@ -484,7 +484,7 @@ def add_attributes(names, rw, line_no)
484484
end
485485

486486
def add_includes_extends(names, rdoc_class, line_no) # :nodoc:
487-
return if @include_extend_suppressed
487+
return if @in_proc_block
488488
comment = consecutive_comment(line_no)
489489
handle_consecutive_comment_directive(@container, comment)
490490
names.each do |name|
@@ -511,6 +511,8 @@ def add_extends(names, line_no) # :nodoc:
511511
# Adds a method defined by `def` syntax
512512

513513
def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singleton:, params:, calls_super:, block_params:, tokens:, start_line:, end_line:)
514+
return if @in_proc_block
515+
514516
receiver = receiver_name ? find_or_create_module_path(receiver_name, receiver_fallback_type) : @container
515517
meth = RDoc::AnyMethod.new(nil, name)
516518
if (comment = consecutive_comment(start_line))
@@ -525,14 +527,13 @@ def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singl
525527
handle_modifier_directive(meth, end_line)
526528
return unless should_document?(meth)
527529

528-
529530
if meth.name == 'initialize' && !singleton
530531
if meth.dont_rename_initialize
531-
visibility = :protected
532+
meth.visibility = :protected
532533
else
533534
meth.name = 'new'
534-
singleton = true
535-
visibility = :public
535+
meth.singleton = true
536+
meth.visibility = :public
536537
end
537538
end
538539

@@ -770,9 +771,8 @@ def visit_call_node(node)
770771
end
771772

772773
def visit_block_node(node)
773-
@scanner.suppress_include_extend do
774-
# include and extend inside block are not documentable
775-
# method definition might also not work but document it for now.
774+
@scanner.with_in_proc_block do
775+
# include, extend and method definition inside block are not documentable
776776
super
777777
end
778778
end

test/rdoc/test_rdoc_parser_prism_ruby.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -663,25 +663,22 @@ module A
663663
add_my_method :bar
664664
end
665665
666-
tap do
667-
# comment baz1
666+
metaprogramming do
667+
# class that defines this method is unknown
668668
def baz1; end
669669
end
670670
671-
self.tap do
672-
# comment baz2
673-
def baz2; end
674-
end
675-
676-
my_decorator def self.baz3; end
671+
my_decorator def self.baz2; end
677672
678-
self.my_decorator def baz4; end
673+
self.my_decorator def baz3; end
679674
end
680675
RUBY
681676
mod = @store.find_module_named 'A'
682677
methods = mod.method_list
683-
assert_equal ['A::foo', 'A#bar', 'A#baz1', 'A#baz2', 'A::baz3', 'A#baz4'], methods.map(&:full_name)
684-
assert_equal ['comment foo', 'comment bar', 'comment baz1', 'comment baz2'], methods.take(4).map { |m| m.comment.text.strip }
678+
unless accept_legacy_bug?
679+
assert_equal ['A::foo', 'A#bar', 'A::baz2', 'A#baz3'], methods.map(&:full_name)
680+
end
681+
assert_equal ['comment foo', 'comment bar'], methods.take(2).map { |m| m.comment.text.strip }
685682
end
686683

687684
def test_method_yields_directive

0 commit comments

Comments
 (0)