Skip to content

Commit

Permalink
Reject documenting def inside block
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Feb 1, 2025
1 parent 8966718 commit 69bb46d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
30 changes: 15 additions & 15 deletions lib/rdoc/parser/prism_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ def initialize(top_level, content, options, stats)
@container = top_level
@visibility = :public
@singleton = false
@include_extend_suppressed = false
@in_proc_block = false
end

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

def suppress_include_extend
@include_extend_suppressed = true
def with_in_proc_block
@in_proc_block = true
yield
@include_extend_suppressed = false
@in_proc_block = false
end

# Dive into another container
Expand All @@ -54,11 +54,11 @@ def with_container(container, singleton: false)
old_container = @container
old_visibility = @visibility
old_singleton = @singleton
old_include_extend_suppressed = @include_extend_suppressed
old_in_proc_block = @in_proc_block
@visibility = :public
@container = container
@singleton = singleton
@include_extend_suppressed = false
@in_proc_block = false
unless singleton
# Need to update module parent chain to emulate Module.nesting.
# This mechanism is inaccurate and needs to be fixed.
Expand All @@ -70,7 +70,7 @@ def with_container(container, singleton: false)
@container = old_container
@visibility = old_visibility
@singleton = old_singleton
@include_extend_suppressed = old_include_extend_suppressed
@in_proc_block = old_in_proc_block
@module_nesting.pop
end

Expand Down Expand Up @@ -484,7 +484,7 @@ def add_attributes(names, rw, line_no)
end

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

def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singleton:, params:, calls_super:, block_params:, tokens:, start_line:, end_line:)
return if @in_proc_block

receiver = receiver_name ? find_or_create_module_path(receiver_name, receiver_fallback_type) : @container
meth = RDoc::AnyMethod.new(nil, name)
if (comment = consecutive_comment(start_line))
Expand All @@ -525,14 +527,13 @@ def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singl
handle_modifier_directive(meth, end_line)
return unless should_document?(meth)


if meth.name == 'initialize' && !singleton
if meth.dont_rename_initialize
visibility = :protected
meth.visibility = :protected
else
meth.name = 'new'
singleton = true
visibility = :public
meth.singleton = true
meth.visibility = :public
end
end

Expand Down Expand Up @@ -770,9 +771,8 @@ def visit_call_node(node)
end

def visit_block_node(node)
@scanner.suppress_include_extend do
# include and extend inside block are not documentable
# method definition might also not work but document it for now.
@scanner.with_in_proc_block do
# include, extend and method definition inside block are not documentable
super
end
end
Expand Down
19 changes: 8 additions & 11 deletions test/rdoc/test_rdoc_parser_prism_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,25 +663,22 @@ module A
add_my_method :bar
end
tap do
# comment baz1
metaprogramming do
# class that defines this method is unknown
def baz1; end
end
self.tap do
# comment baz2
def baz2; end
end
my_decorator def self.baz3; end
my_decorator def self.baz2; end
self.my_decorator def baz4; end
self.my_decorator def baz3; end
end
RUBY
mod = @store.find_module_named 'A'
methods = mod.method_list
assert_equal ['A::foo', 'A#bar', 'A#baz1', 'A#baz2', 'A::baz3', 'A#baz4'], methods.map(&:full_name)
assert_equal ['comment foo', 'comment bar', 'comment baz1', 'comment baz2'], methods.take(4).map { |m| m.comment.text.strip }
unless accept_legacy_bug?
assert_equal ['A::foo', 'A#bar', 'A::baz2', 'A#baz3'], methods.map(&:full_name)
end
assert_equal ['comment foo', 'comment bar'], methods.take(2).map { |m| m.comment.text.strip }
end

def test_method_yields_directive
Expand Down

0 comments on commit 69bb46d

Please sign in to comment.