Skip to content

Commit ca0af1c

Browse files
committed
Modify YardDoc listener to create RBS signatures
It identifies RBS signatures from method comments exported by gems.
1 parent e1ab989 commit ca0af1c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/tapioca/gem/listeners/yard_doc.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class YardDoc < Base
2323

2424
IGNORED_SIG_TAGS = T.let(["param", "return"], T::Array[String])
2525

26+
RBS_SIGNATURE_PREFIX = T.let(":", String)
27+
2628
#: (Pipeline pipeline) -> void
2729
def initialize(pipeline)
2830
YARD::Registry.clear
@@ -64,7 +66,13 @@ def documentation_comments(name, sigs: [])
6466

6567
comments = docstring.lines
6668
.reject { |line| IGNORED_COMMENTS.any? { |comment| line.include?(comment) } }
67-
.map! { |line| RBI::Comment.new(line) }
69+
.map! do |line|
70+
if line.strip.start_with?(RBS_SIGNATURE_PREFIX)
71+
RBI::RBSSig.new(line[1..])
72+
else
73+
RBI::Comment.new(line)
74+
end
75+
end
6876

6977
tags = yard_docs.tags
7078
tags.reject! { |tag| IGNORED_SIG_TAGS.include?(tag.tag_name) } unless sigs.empty?

spec/tapioca/gem/pipeline_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -4542,6 +4542,32 @@ def foo; end
45424542
assert_equal(output, compiled)
45434543
end
45444544

4545+
it "compiles RBS signatures" do
4546+
add_ruby_file("foo.rb", <<~RUBY)
4547+
class Foo
4548+
#: -> String
4549+
def foo; end
4550+
4551+
#: -> Integer
4552+
def self.bar; end
4553+
end
4554+
RUBY
4555+
4556+
output = template(<<~RBI)
4557+
class Foo
4558+
#: -> String
4559+
def foo; end
4560+
4561+
class << self
4562+
#: -> Integer
4563+
def bar; end
4564+
end
4565+
end
4566+
RBI
4567+
4568+
assert_equal(output, compile(include_doc: true))
4569+
end
4570+
45454571
it "compiles constants with nil values" do
45464572
add_ruby_file("foo.rb", <<~RUBY)
45474573
class Foo

0 commit comments

Comments
 (0)