Skip to content

Commit

Permalink
Refactor extracting method visibility arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jul 31, 2024
1 parent 0a8634c commit 1ce2634
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lib/rdoc/parser/prism_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,12 +885,24 @@ def visibility_method_arguments(call_node, singleton:)
arguments_node = call_node.arguments
return unless arguments_node
symbols = symbol_arguments(call_node)
return symbols.map(&:to_s) if symbols # module_function :foo, :bar
return unless arguments_node.arguments.size == 1
arg = arguments_node.arguments.first
return unless arg.is_a?(Prism::DefNode)
# `module_function def foo; end` or `private_class_method def self.foo; end`
[arg.name.to_s] if singleton ? arg.receiver.is_a?(Prism::SelfNode) : arg.receiver.nil?
if symbols
# module_function :foo, :bar
return symbols.map(&:to_s)
else
return unless arguments_node.arguments.size == 1
arg = arguments_node.arguments.first
return unless arg.is_a?(Prism::DefNode)

if singleton
# `private_class_method def foo; end` `private_class_method def not_self.foo; end` should be ignored
return unless arg.receiver.is_a?(Prism::SelfNode)
else
# `module_function def something.foo` should be ignored
return if arg.receiver
end
# `module_function def foo; end` or `private_class_method def self.foo; end`
[arg.name.to_s]
end
end

def constant_path_string(node)
Expand Down
10 changes: 8 additions & 2 deletions test/rdoc/test_rdoc_parser_prism_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,17 @@ def test_undocumentable_change_visibility
util_parser <<~RUBY
class A
def m1; end
private 42, :m1 # maybe not Module#private
def self.m2; end
private 42, :m # maybe not Module#private
# ignore all non-standard `private def` and `private_class_method def`
private def self.m1; end
private_class_method def m2; end
private def to_s.m1; end
private_class_method def to_s.m2; end
end
RUBY
klass = @store.find_class_named 'A'
assert_equal [:public], klass.method_list.map(&:visibility)
assert_equal [:public] * 4, klass.method_list.map(&:visibility)
end

def test_method_visibility_change_in_subclass
Expand Down

0 comments on commit 1ce2634

Please sign in to comment.