Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of dangling things #344

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/rbi/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def visit_class_node(node)
current_scope << scope
@scopes_stack << scope
visit(node.body)
scope.nodes.concat(current_sigs)
collect_dangling_comments(node)
@scopes_stack.pop
@last_node = nil
Expand Down Expand Up @@ -269,6 +270,7 @@ def visit_module_node(node)
current_scope << scope
@scopes_stack << scope
visit(node.body)
scope.nodes.concat(current_sigs)
collect_dangling_comments(node)
@scopes_stack.pop
@last_node = nil
Expand All @@ -278,7 +280,7 @@ def visit_module_node(node)
def visit_program_node(node)
@last_node = node
super

@tree.nodes.concat(current_sigs)
collect_orphan_comments
separate_header_comments
set_root_tree_loc
Expand All @@ -296,6 +298,7 @@ def visit_singleton_class_node(node)
current_scope << scope
@scopes_stack << scope
visit(node.body)
scope.nodes.concat(current_sigs)
collect_dangling_comments(node)
@scopes_stack.pop
@last_node = nil
Expand Down Expand Up @@ -476,7 +479,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
49 changes: 35 additions & 14 deletions test/rbi/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,36 @@ def foo; end
RBI
end

def test_parse_dangling_sigs
rbi = <<~RBI
class Foo
sig { void }
end

module Bar
class << self
sig { void }
end
sig { void }
end
sig { void }
sig { returns(A) }
RBI

out = Parser.parse_string(rbi)
assert_equal(rbi, out.string)
end

def test_parse_sig_standalone
rbi = <<~RBI
sig { void }
sig { returns(A) }
RBI

out = Parser.parse_string(rbi)
assert_equal(rbi, out.string)
end

def test_parse_methods_with_visibility
rbi = <<~RBI
private def m1; end
Expand Down Expand Up @@ -808,30 +838,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
Loading