Skip to content

Commit 711b2e2

Browse files
authored
Merge pull request #2624 from ruby/inline--comment
Inline attribute/method definitions have comments
2 parents 82e1477 + 583386b commit 711b2e2

File tree

8 files changed

+113
-6
lines changed

8 files changed

+113
-6
lines changed

lib/rbs/ast/ruby/comment_block.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ def line_location(start_line, end_line)
177177
Location.new(comment_buffer, start_offset, end_offset)
178178
end
179179

180+
def location()
181+
first_comment = comments[0] or raise
182+
last_comment = comments[-1] or raise
183+
184+
comment_buffer.rbs_location(first_comment.location.join last_comment.location)
185+
end
186+
180187
def parse_annotation_lines(start_line, end_line, variables)
181188
start_pos = comment_buffer.ranges[start_line].begin
182189
end_pos = comment_buffer.ranges[end_line].end

lib/rbs/ast/ruby/members.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,14 @@ class DefMember < Base
183183
attr_reader :name
184184
attr_reader :node
185185
attr_reader :method_type
186+
attr_reader :leading_comment
186187

187-
def initialize(buffer, name, node, method_type)
188+
def initialize(buffer, name, node, method_type, leading_comment)
188189
super(buffer)
189190
@name = name
190191
@node = node
191192
@method_type = method_type
193+
@leading_comment = leading_comment
192194
end
193195

194196
def location

lib/rbs/definition.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,25 @@ def comment
6666
when AST::Members::Base
6767
member.comment
6868
when AST::Ruby::Members::Base
69-
nil
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
7088
end
7189
end
7290

lib/rbs/environment.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,8 @@ def resolve_ruby_member(resolver, member, context:)
722722
member.buffer,
723723
member.name,
724724
member.node,
725-
member.method_type.map_type_name {|name, _, _| absolute_type_name(resolver, nil, name, context: context) }
725+
member.method_type.map_type_name {|name, _, _| absolute_type_name(resolver, nil, name, context: context) },
726+
member.leading_comment
726727
)
727728
when AST::Ruby::Members::IncludeMember
728729
resolved_annotation = member.annotation&.map_type_name {|name, _, _| absolute_type_name(resolver, nil, name, context: context) }

lib/rbs/inline_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def visit_def_node(node)
157157
method_type, leading_unuseds, trailing_unused = AST::Ruby::Members::MethodTypeAnnotation.build(leading_block, trailing_block, [])
158158
report_unused_annotation(trailing_unused, *leading_unuseds)
159159

160-
defn = AST::Ruby::Members::DefMember.new(buffer, node.name, node, method_type)
160+
defn = AST::Ruby::Members::DefMember.new(buffer, node.name, node, method_type, leading_block)
161161
current.members << defn
162162

163163
# Skip other comments in `def` node

sig/ast/ruby/comment_block.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ module RBS
112112

113113
def line_location: (Integer start_line, Integer end_line) -> Location
114114

115+
def location: () -> Location
116+
115117
private def leading_annotation?: (Integer index) -> bool
116118
end
117119
end

sig/ast/ruby/members.rbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ module RBS
5757
attr_reader name: Symbol
5858
attr_reader node: Prism::DefNode
5959
attr_reader method_type: MethodTypeAnnotation
60+
attr_reader leading_comment: CommentBlock?
6061

61-
def initialize: (Buffer, Symbol name, Prism::DefNode node, MethodTypeAnnotation) -> void
62+
def initialize: (Buffer, Symbol name, Prism::DefNode node, MethodTypeAnnotation, CommentBlock? leading_comment) -> void
6263

6364
def location: () -> Location
6465

test/rbs/definition_builder_test.rb

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,6 @@ class TypedAttributeTest
33733373
end
33743374
end
33753375

3376-
33773376
def test_ruby_attribute_members_multiple_names
33783377
SignatureManager.new do |manager|
33793378
manager.add_ruby_file("multiple_attributes.rb", <<~RUBY)
@@ -3405,4 +3404,81 @@ class MultipleAttributeTest
34053404
end
34063405
end
34073406
end
3407+
3408+
def test_ruby_attribute_members_docs
3409+
SignatureManager.new do |manager|
3410+
manager.add_ruby_file("a.rb", <<~RUBY)
3411+
class MultipleAttributeTest
3412+
# This is a document for attribute
3413+
#
3414+
# @rbs () -> String
3415+
attr_reader :test #: String
3416+
3417+
# Line 1
3418+
#
3419+
# Line 2
3420+
# @rbs () -> String -- this is ignored
3421+
#
3422+
# Line 3
3423+
attr_reader :test2 #: untyped
3424+
end
3425+
RUBY
3426+
3427+
manager.build do |env|
3428+
builder = DefinitionBuilder.new(env: env)
3429+
3430+
builder.build_instance(type_name("::MultipleAttributeTest")).tap do |definition|
3431+
definition.methods[:test].tap do |method|
3432+
assert_equal ["This is a document for attribute\n"], method.comments.map(&:string)
3433+
end
3434+
3435+
definition.methods[:test2].tap do |method|
3436+
assert_equal ["Line 1\n\nLine 2\n\nLine 3"], method.comments.map(&:string)
3437+
end
3438+
end
3439+
end
3440+
end
3441+
end
3442+
3443+
def test_ruby_def_members_docs
3444+
SignatureManager.new do |manager|
3445+
manager.add_ruby_file("a.rb", <<~RUBY)
3446+
class MultipleAttributeTest
3447+
# This is a document for foo method
3448+
#
3449+
# @rbs return: String?
3450+
def foo = nil
3451+
3452+
# Line 1
3453+
#
3454+
# Line 2
3455+
# @rbs () -> Integer
3456+
#
3457+
# Line 3
3458+
def bar = 123
3459+
3460+
# @rbs return: untyped
3461+
def baz = nil
3462+
end
3463+
RUBY
3464+
3465+
manager.build do |env|
3466+
builder = DefinitionBuilder.new(env: env)
3467+
3468+
builder.build_instance(type_name("::MultipleAttributeTest")).tap do |definition|
3469+
definition.methods[:foo].tap do |method|
3470+
assert_equal ["This is a document for foo method\n"], method.comments.map(&:string)
3471+
end
3472+
3473+
definition.methods[:bar].tap do |method|
3474+
assert_equal ["Line 1\n\nLine 2\n\nLine 3"], method.comments.map(&:string)
3475+
end
3476+
3477+
definition.methods[:baz].tap do |method|
3478+
assert_equal [], method.comments
3479+
end
3480+
end
3481+
end
3482+
end
3483+
end
34083484
end

0 commit comments

Comments
 (0)