Skip to content

Commit b911923

Browse files
Merge pull request #3563 from Shopify/selective-comments-as-docs
Do not collect trailing comments of previous lines
2 parents 638f3a9 + 1ab311b commit b911923

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,9 @@ def collect_comments(node)
726726
comment = @comments_by_line[line]
727727
break unless comment
728728

729+
# a trailing comment from a previous line is not a comment for this node
730+
break if comment.trailing?
731+
729732
comment_content = comment.location.slice
730733

731734
# invalid encodings would raise an "invalid byte sequence" exception

lib/ruby_indexer/test/instance_variables_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,29 @@ def something; end
236236
assert_instance_of(Entry::SingletonClass, owner)
237237
assert_equal("Foo::<Class:Foo>", owner&.name)
238238
end
239+
240+
def test_class_instance_variable_comments
241+
index(<<~RUBY)
242+
class Foo
243+
# Documentation for @a
244+
@a = "Hello" #: String
245+
@b = "World" # trailing comment
246+
@c = "!"
247+
end
248+
end
249+
RUBY
250+
251+
assert_entry("@a", Entry::InstanceVariable, "/fake/path/foo.rb:2-4:2-6")
252+
entry = @index["@a"]&.first #: as Entry::InstanceVariable
253+
assert_equal("Documentation for @a", entry.comments)
254+
255+
assert_entry("@b", Entry::InstanceVariable, "/fake/path/foo.rb:3-4:3-6")
256+
entry = @index["@b"]&.first #: as Entry::InstanceVariable
257+
assert_empty(entry.comments)
258+
259+
assert_entry("@c", Entry::InstanceVariable, "/fake/path/foo.rb:4-4:4-6")
260+
entry = @index["@c"]&.first #: as Entry::InstanceVariable
261+
assert_empty(entry.comments)
262+
end
239263
end
240264
end

0 commit comments

Comments
 (0)