Skip to content

Commit 484c4e1

Browse files
committed
Simplify object variation
1 parent 60b480c commit 484c4e1

File tree

6 files changed

+26
-41
lines changed

6 files changed

+26
-41
lines changed

lib/rbs/annotate/formatter.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,10 @@ def format(newline_at_end:)
5959

6060
def self.each_part(doc, &block)
6161
if block
62-
document =
63-
case doc
64-
when String
65-
raise
66-
when RDoc::Comment
67-
document = doc.parse
68-
when RDoc::Markup::Document
69-
document = doc
70-
end
71-
72-
if document.file
73-
yield document
62+
if doc.file
63+
yield doc
7464
else
75-
document.each do |d|
65+
doc.each do |d|
7666
each_part(d, &block)
7767
end
7868
end
@@ -81,17 +71,10 @@ def self.each_part(doc, &block)
8171
end
8272
end
8373

84-
def self.translate(doc_or_comment)
85-
if doc_or_comment.file
74+
def self.translate(doc)
75+
if doc.file
8676
formatter = RDoc::Markup::ToMarkdown.new
87-
case doc_or_comment
88-
when RDoc::Markup::Document
89-
doc_or_comment
90-
when RDoc::Comment
91-
doc_or_comment.parse
92-
else
93-
raise "Unexpected comment class: #{doc_or_comment.class}"
94-
end.accept(formatter).strip.lines.map(&:rstrip).join("\n")
77+
doc.accept(formatter).strip.lines.map(&:rstrip).join("\n")
9578
end
9679
end
9780
end

lib/rbs/annotate/rdoc_annotator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def annotate_decls(decls, outer: [])
3939
def each_part(subjects, tester:)
4040
if block_given?
4141
subjects.each do |subject, docs|
42-
Formatter.each_part(subject.comment) do |doc|
42+
comment = subject.comment
43+
raise if comment.is_a?(String)
44+
Formatter.each_part(comment.parse) do |doc|
4345
if tester.test_path(doc.file || raise)
4446
yield [doc, subject]
4547
end

sig/annotate/formatter.rbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ module RBS
1515

1616
def format: (newline_at_end: bool) -> String
1717

18-
def self.translate: (RDoc::Markup::Document | RDoc::Comment) -> String?
18+
def self.translate: (RDoc::Markup::Document) -> String?
1919

20-
def self.each_part: (RDoc::Markup::Document | RDoc::Comment | String) { (RDoc::Markup::Document) -> void } -> void
21-
| (RDoc::Markup::Document | RDoc::Comment | String) -> Enumerator[RDoc::Markup::Document, void]
20+
def self.each_part: (RDoc::Markup::Document) { (RDoc::Markup::Document) -> void } -> void
21+
| (RDoc::Markup::Document) -> Enumerator[RDoc::Markup::Document, void]
2222
end
2323
end
2424
end

sig/annotate/rdoc_annotater.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module RBS
1717
end
1818

1919
interface _WithRDocComment
20-
def comment: () -> (RDoc::Markup::Document | RDoc::Comment | String)
20+
def comment: () -> (RDoc::Comment | String)
2121
end
2222

2323
def each_part: (Array[Object & _WithRDocComment], tester: _PathTester) { ([RDoc::Markup::Document, Object & _WithRDocComment]) -> void } -> void

stdlib/rdoc/0/code_object.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module RDoc
3030
# <!-- rdoc-file=lib/rdoc/code_object.rb -->
3131
# Our comment
3232
#
33-
attr_reader comment: Markup::Document | Comment | String
33+
attr_reader comment: Comment | String
3434

3535
# <!--
3636
# rdoc-file=lib/rdoc/code_object.rb
@@ -46,6 +46,6 @@ module RDoc
4646
# -->
4747
# Replaces our comment with `comment`, unless it is empty.
4848
#
49-
def comment=: (Markup::Document | Comment | String | nil) -> (Markup::Document | Comment | String)
49+
def comment=: (Comment | String | nil) -> (Comment | String | nil)
5050
end
5151
end

test/rbs/annotate/rdoc_source_test.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Hello3
5959
assert_predicate klass, :documented?
6060
assert_equal 1, klass.comment.parse.parts.size
6161

62-
assert_nil RBS::Annotate::Formatter.translate(klass.comment)
62+
assert_nil RBS::Annotate::Formatter.translate(klass.comment.parse)
6363
assert_equal "Document for Hello1", RBS::Annotate::Formatter.translate(klass.comment.parse.parts[0])
6464
end
6565
end
@@ -71,7 +71,7 @@ class Hello3
7171
klss[0].tap do |klass|
7272
refute_predicate klass, :documented?
7373

74-
assert_nil RBS::Annotate::Formatter.translate(klass.comment)
74+
assert_nil RBS::Annotate::Formatter.translate(klass.comment.parse)
7575
assert_equal "", RBS::Annotate::Formatter.translate(klass.comment.parse.parts[0])
7676
end
7777
end
@@ -111,7 +111,7 @@ class Hello
111111
assert_equal 1, consts.size
112112
consts[0].tap do |const|
113113
assert_equal "FOO", const.name
114-
assert_equal "Doc for FOO", RBS::Annotate::Formatter.translate(const.comment)
114+
assert_equal "Doc for FOO", RBS::Annotate::Formatter.translate(const.comment.parse)
115115
end
116116
end
117117

@@ -121,7 +121,7 @@ class Hello
121121
assert_equal 1, consts.size
122122
consts[0].tap do |const|
123123
assert_equal "VERSION", const.name
124-
assert_equal "Doc for Hello::VERSION", RBS::Annotate::Formatter.translate(const.comment)
124+
assert_equal "Doc for Hello::VERSION", RBS::Annotate::Formatter.translate(const.comment.parse)
125125
end
126126
end
127127

@@ -158,7 +158,7 @@ def m5; end
158158

159159
ms[0].tap do |m|
160160
assert_equal "m1", m.name
161-
assert_equal "Doc for m1", RBS::Annotate::Formatter.translate(m.comment)
161+
assert_equal "Doc for m1", RBS::Annotate::Formatter.translate(m.comment.parse)
162162
end
163163
end
164164

@@ -167,7 +167,7 @@ def m5; end
167167

168168
ms[0].tap do |m|
169169
assert_equal "m2", m.name
170-
assert_equal "Doc for m2", RBS::Annotate::Formatter.translate(m.comment)
170+
assert_equal "Doc for m2", RBS::Annotate::Formatter.translate(m.comment.parse)
171171
assert_equal "m1", m.is_alias_for.name
172172
end
173173
end
@@ -179,7 +179,7 @@ def m5; end
179179

180180
ms[0].tap do |m|
181181
assert_equal "m4", m.name
182-
assert_equal "Doc for m4", RBS::Annotate::Formatter.translate(m.comment)
182+
assert_equal "Doc for m4", RBS::Annotate::Formatter.translate(m.comment.parse)
183183
end
184184
end
185185

@@ -188,7 +188,7 @@ def m5; end
188188

189189
ms[0].tap do |m|
190190
assert_equal "m5", m.name
191-
assert_equal "Doc for m5", RBS::Annotate::Formatter.translate(m.comment)
191+
assert_equal "Doc for m5", RBS::Annotate::Formatter.translate(m.comment.parse)
192192
end
193193
end
194194
end
@@ -218,7 +218,7 @@ class <<self
218218

219219
attrs[0].tap do |attr|
220220
assert_equal "foo", attr.name
221-
assert_equal "Doc for foo", RBS::Annotate::Formatter.translate(attr.comment)
221+
assert_equal "Doc for foo", RBS::Annotate::Formatter.translate(attr.comment.parse)
222222
assert_equal "R", attr.rw
223223
end
224224
end
@@ -228,7 +228,7 @@ class <<self
228228

229229
attrs[0].tap do |attr|
230230
assert_equal "bar", attr.name
231-
assert_equal "Doc for bar and baz", RBS::Annotate::Formatter.translate(attr.comment)
231+
assert_equal "Doc for bar and baz", RBS::Annotate::Formatter.translate(attr.comment.parse)
232232
assert_equal "RW", attr.rw
233233
end
234234
end
@@ -238,7 +238,7 @@ class <<self
238238

239239
attrs[0].tap do |attr|
240240
assert_equal "baz", attr.name
241-
assert_equal "Doc for bar and baz", RBS::Annotate::Formatter.translate(attr.comment)
241+
assert_equal "Doc for bar and baz", RBS::Annotate::Formatter.translate(attr.comment.parse)
242242
assert_equal "RW", attr.rw
243243
end
244244
end

0 commit comments

Comments
 (0)