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
19 changes: 12 additions & 7 deletions lib/tapioca/gem/listeners/documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,25 @@ def documentation_comments(name, sigs: [])
end
return [] unless declaration

comments = declaration.definitions.flat_map(&:comments)
comments.uniq!
return [] if comments.empty?

lines = comments
.map { |comment| comment.string.gsub(/^#+ ?/, "") }
.reject { |line| IGNORED_COMMENTS.any? { |comment| line.include?(comment) } || rbs_comment?(line) }
# Definitions often share a comment block, such as a license header, so de-duplicate them
lines = declaration.definitions
.map { |definition| comment_lines(definition) }
.uniq
.flatten

# Strip leading and trailing blank lines, matching YARD's behavior
lines = lines.reverse_each.drop_while(&:empty?).reverse_each.drop_while(&:empty?)

lines.map! { |line| RBI::Comment.new(line) }
end

#: (Rubydex::Definition definition) -> Array[String]
def comment_lines(definition)
lines = definition.comments.map { |comment| comment.string.gsub(/^#+ ?/, "") }
lines.reject! { |line| IGNORED_COMMENTS.any? { |comment| line.include?(comment) } || rbs_comment?(line) }
lines
end

# @override
#: (NodeAdded event) -> bool
def ignore?(event)
Expand Down
72 changes: 72 additions & 0 deletions spec/tapioca/gem/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4468,6 +4468,78 @@ def something; end
assert_equal(output, compile(include_doc: false))
end

it "does not repeat comments shared by multiple definitions" do
Comment thread
vinistock marked this conversation as resolved.
add_ruby_file("bar.rb", <<~RUBY)
# rubocop:disable Style/Documentation
# Licensed under the Foo License, Version 2.0
module Namespace
# The answer
ANSWER = 42

# Does the thing
def self.thing; end
end
RUBY

add_ruby_file("baz.rb", <<~RUBY)
# Namespace also holds Baz
module Namespace
class Baz; end
end
RUBY

add_ruby_file("foo.rb", <<~RUBY)
# Licensed under the Foo License, Version 2.0
module Namespace
# The answer
ANSWER = 42

# Does the thing
def self.thing; end
end
RUBY

output = template(<<~RBI)
# Licensed under the Foo License, Version 2.0
# Namespace also holds Baz
module Namespace
class << self
# Does the thing
def thing; end
end
end

# The answer
Namespace::ANSWER = T.let(T.unsafe(nil), Integer)

class Namespace::Baz; end
RBI

assert_equal(output, compile(include_doc: true))
end

it "does not de-duplicate comment blocks that partially overlap" do
add_ruby_file("bar.rb", <<~RUBY)
# Licensed under the Foo License, Version 2.0
module Namespace; end
RUBY

add_ruby_file("foo.rb", <<~RUBY)
# Licensed under the Foo License, Version 2.0
# Namespace is defined here because of whatever
module Namespace; end
RUBY

output = template(<<~RBI)
# Licensed under the Foo License, Version 2.0
# Licensed under the Foo License, Version 2.0
# Namespace is defined here because of whatever
module Namespace; end
RBI

assert_equal(output, compile(include_doc: true))
end

it "properly processes void in type aliases" do
add_ruby_file("foo.rb", <<~RUBY)
module Foo
Expand Down
Loading