Skip to content

Commit

Permalink
Add new ParsingResult for comments with prefix
Browse files Browse the repository at this point in the history
We don’t want one ParsingResult for two `attr_reader` type assertions.
  • Loading branch information
soutaro committed Apr 24, 2024
1 parent 2da588f commit abdd0ae
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/rbs/inline/annotation_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ def last_comment
def add_comment(comment)
if last_comment.location.end_line + 1 == comment.location.start_line
if last_comment.location.start_column == comment.location.start_column
comments << comment
self
if prefix = comment.location.start_line_slice[..comment.location.start_column]
prefix.strip!
if prefix.empty?
comments << comment
self
end
end
end
end
end
Expand All @@ -55,12 +60,7 @@ def lines

# @rbs return: String
def content
content = +""
lines.each do |line, _|
content << line
content << "\n"
end
content
lines.map(&:first).join("\n")
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/rbs/inline/ast/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ def method_annotations

class RubyAlias < Base
attr_reader :node #:: Prism::AliasMethodNode

attr_reader :comments #:: AnnotationParser::ParsingResult?

# @rbs node: Prism::AliasMethodNode
Expand Down
2 changes: 0 additions & 2 deletions sig/generated/rbs/inline/ast/members.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ module RBS
end

# `private` call without arguments
#
class RubyPrivate < Base
attr_reader node: Prism::CallNode

Expand All @@ -130,7 +129,6 @@ module RBS
end

# `public` call without arguments
#
class RubyPublic < Base
attr_reader node: Prism::CallNode

Expand Down
11 changes: 11 additions & 0 deletions test/rbs/inline/annotation_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,15 @@ def test_inherits
assert_nil annotation.args
end
end

def test_lvar_decl_annotation
annots = AnnotationParser.parse(parse_comments(<<~RUBY))
attr_reader :foo #:: Foo
attr_reader :bar #:: Bar
RUBY

assert_equal 3, annots.size
assert_equal ":: Foo", annots[0].content
assert_equal ":: Bar", annots[1].content
end
end

0 comments on commit abdd0ae

Please sign in to comment.