Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ def handle_meta_method_comment(comment, directives, node)
@container,
comment: comment,
directives: directives,
dont_rename_initialize: false,
line_no: line_no,
visibility: visibility,
singleton: @singleton || singleton_method,
Expand Down Expand Up @@ -722,7 +721,7 @@ def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:
)
end

private def internal_add_method(method_name, container, comment:, dont_rename_initialize: false, directives:, modifier_comment_lines: nil, line_no:, visibility:, singleton:, params:, calls_super:, block_params:, tokens:, type_signature_lines: nil) # :nodoc:
private def internal_add_method(method_name, container, comment:, directives:, modifier_comment_lines: nil, line_no:, visibility:, singleton:, params:, calls_super:, block_params:, tokens:, type_signature_lines: nil) # :nodoc:
meth = RDoc::AnyMethod.new(method_name, singleton: singleton)
meth.comment = comment
handle_code_object_directives(meth, directives) if directives
Expand All @@ -746,16 +745,10 @@ def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:
meth.calls_super = calls_super
meth.block_params ||= block_params if block_params
meth.type_signature_lines = type_signature_lines
container.add_method(meth)
record_location(meth)
meth.start_collecting_tokens(:ruby)
tokens.each do |token|
meth.token_stream << token
end

# Rename after add_method to register duplicated 'new' and 'initialize'
# defined in c and ruby.
if !dont_rename_initialize && method_name == 'initialize' && !singleton
# An instance method `initialize` is documented as `::new` unless the
# :notnew: directive is given
if method_name == 'initialize' && !singleton
if meth.dont_rename_initialize
meth.visibility = :protected
else
Expand All @@ -764,6 +757,13 @@ def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:
meth.visibility = :public
end
end

record_location(meth)
container.add_method(meth)
meth.start_collecting_tokens(:ruby)
tokens.each do |token|
meth.token_stream << token
end
end

# Find or create module or class from a given module name using Ruby lexical
Expand Down
25 changes: 25 additions & 0 deletions test/rdoc/parser/ruby_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,31 @@ def initialize(*args)
assert_equal expected, arglists
end

def test_class_new_and_initialize_are_registered_once
util_parser <<~RUBY
class A
# new doc
def self.new(x); super; end
# initialize doc
def initialize(x); end
end

class B
# initialize doc
def initialize(x); end
# new doc
def self.new(x); super; end
end
RUBY

a, b = @top_level.classes
assert_equal ['A::new'], a.method_list.map(&:full_name)
assert_equal 'new doc', a.method_list.first.comment.text
assert_equal ['::new'], a.methods_hash.keys
assert_equal ['B::new'], b.method_list.map(&:full_name)
assert_equal 'initialize doc', b.method_list.first.comment.text
end

def test_class_mistaken_for_module
util_parser <<~RUBY
class A::Foo; end
Expand Down