Skip to content

Commit

Permalink
Fix parsing of dangling sigs
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 8e40e46 commit 0fd4b17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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
30 changes: 30 additions & 0 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

0 comments on commit 0fd4b17

Please sign in to comment.