From abdd0ae7487ba68483aec5711cbf1b8f9491f060 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Tue, 23 Apr 2024 17:54:00 +0900 Subject: [PATCH] Add new ParsingResult for comments with prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don’t want one ParsingResult for two `attr_reader` type assertions. --- lib/rbs/inline/annotation_parser.rb | 16 ++++++++-------- lib/rbs/inline/ast/members.rb | 1 - sig/generated/rbs/inline/ast/members.rbs | 2 -- test/rbs/inline/annotation_parser_test.rb | 11 +++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/rbs/inline/annotation_parser.rb b/lib/rbs/inline/annotation_parser.rb index 01fbbba..601efd3 100644 --- a/lib/rbs/inline/annotation_parser.rb +++ b/lib/rbs/inline/annotation_parser.rb @@ -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 @@ -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 diff --git a/lib/rbs/inline/ast/members.rb b/lib/rbs/inline/ast/members.rb index d954d76..8092ba6 100644 --- a/lib/rbs/inline/ast/members.rb +++ b/lib/rbs/inline/ast/members.rb @@ -262,7 +262,6 @@ def method_annotations class RubyAlias < Base attr_reader :node #:: Prism::AliasMethodNode - attr_reader :comments #:: AnnotationParser::ParsingResult? # @rbs node: Prism::AliasMethodNode diff --git a/sig/generated/rbs/inline/ast/members.rbs b/sig/generated/rbs/inline/ast/members.rbs index 9c3fe62..9b19780 100644 --- a/sig/generated/rbs/inline/ast/members.rbs +++ b/sig/generated/rbs/inline/ast/members.rbs @@ -121,7 +121,6 @@ module RBS end # `private` call without arguments - # class RubyPrivate < Base attr_reader node: Prism::CallNode @@ -130,7 +129,6 @@ module RBS end # `public` call without arguments - # class RubyPublic < Base attr_reader node: Prism::CallNode diff --git a/test/rbs/inline/annotation_parser_test.rb b/test/rbs/inline/annotation_parser_test.rb index e64fc1a..ea5b08a 100644 --- a/test/rbs/inline/annotation_parser_test.rb +++ b/test/rbs/inline/annotation_parser_test.rb @@ -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