Skip to content

Commit

Permalink
Fix parsing order of dangling comments
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Aug 5, 2024
1 parent e63946e commit 8e40e46
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rbi/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def collect_dangling_comments(node)

last_node_last_line = node.child_nodes.last&.location&.end_line

last_line.downto(first_line) do |line|
first_line.upto(last_line) do |line|
comment = @comments_by_line[line]
next unless comment
break if last_node_last_line && line <= last_node_last_line
Expand Down
19 changes: 5 additions & 14 deletions test/rbi/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -808,30 +808,21 @@ module A
# B comment
class B; end
# A comment 3
# A comment 4
end
RBI
out = Parser.parse_string(rbi)
assert_equal(<<~RBI, out.string)
# A comment 1
# A comment 2
module A
# B comment
class B; end
# A comment 3
end
RBI
assert_equal(rbi, out.string)
end

def test_parse_collect_dangling_file_comments
rbi = <<~RBI
module A; end
# Orphan comment
# Orphan comment1
# Orphan comment2
RBI
out = Parser.parse_string(rbi)
assert_equal(<<~RBI, out.string)
module A; end
# Orphan comment
RBI
assert_equal(rbi, out.string)
end

def test_parse_params_comments
Expand Down

0 comments on commit 8e40e46

Please sign in to comment.