Skip to content

Commit 8cd37de

Browse files
committed
Refactor documents comment translation
1 parent 4bb8920 commit 8cd37de

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

lib/rbs/ast/ruby/comment_block.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ def leading_annotation?(index)
222222

223223
false
224224
end
225+
226+
def as_comment
227+
lines = [] #: Array[String]
228+
229+
each_paragraph([]) do |paragraph|
230+
case paragraph
231+
when Location
232+
lines << paragraph.local_source
233+
end
234+
end
235+
236+
string = lines.join("\n")
237+
238+
unless string.strip.empty?
239+
AST::Comment.new(string: string, location: location)
240+
end
241+
end
225242
end
226243
end
227244
end

lib/rbs/ast/ruby/declarations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def type
177177
Types::Bases::Any.new(location: nil)
178178
end
179179
end
180+
181+
def comment
182+
leading_comment&.as_comment
183+
end
180184
end
181185
end
182186
end

lib/rbs/definition.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,7 @@ def comment
6666
when AST::Members::Base
6767
member.comment
6868
when AST::Ruby::Members::Base
69-
if member.leading_comment
70-
lines = [] #: Array[String]
71-
72-
member.leading_comment.each_paragraph([]) do |paragraph|
73-
case paragraph
74-
when Location
75-
lines << paragraph.local_source
76-
end
77-
end
78-
79-
string = lines.join("\n")
80-
81-
unless string.strip.empty?
82-
AST::Comment.new(
83-
string: string,
84-
location: member.leading_comment.location
85-
)
86-
end
87-
end
69+
member.leading_comment&.as_comment
8870
end
8971
end
9072

sig/ast/ruby/comment_block.rbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ module RBS
115115
def location: () -> Location
116116

117117
private def leading_annotation?: (Integer index) -> bool
118+
119+
# Returns an comment object that contains the docs of from the comment block
120+
#
121+
# It ignores type annotations and syntax errors.
122+
#
123+
def as_comment: () -> AST::Comment?
118124
end
119125
end
120126
end

sig/ast/ruby/declarations.rbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ module RBS
101101
# - When type_anntoation is given, it returns the type from the annotation.
102102
# - When type_annotation is not given, it returns infered type from the right-hand-side of the constant assignment
103103
# - Or it returns `untyped` type
104+
#
104105
def type: () -> Types::t
106+
107+
# Returns the comment content extracted from the leading comment block
108+
#
109+
def comment: () -> AST::Comment?
105110
end
106111
end
107112
end

0 commit comments

Comments
 (0)