Skip to content

Commit

Permalink
Fix parsing of type members and templates
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 40dc6ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 1 addition & 4 deletions lib/rbi/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,7 @@ def set_root_tree_loc

sig { params(node: T.nilable(Prism::Node)).returns(T::Boolean) }
def type_variable_definition?(node)
return false unless node.is_a?(Prism::CallNode)
return false unless node.block

node.message == "type_member" || node.message == "type_template"
node.is_a?(Prism::CallNode) && (node.message == "type_member" || node.message == "type_template")
end
end

Expand Down
12 changes: 9 additions & 3 deletions test/rbi/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def test_parse_sorbet_type_members_and_templates
class Foo
A = type_member
B = type_member(:in)
C = type_member(:out)
C = type_member(:out) {}
D = type_member(lower: A)
E = type_member(upper: A)
F = type_member(:in, fixed: A)
Expand All @@ -281,8 +281,14 @@ class Foo
end
RBI

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

# Make sure the type members and templates are not parsed as constants
cls = T.must(tree.nodes.grep(Class).first)
assert_equal(0, cls.nodes.grep(Const).size)
assert_equal(9, cls.nodes.grep(TypeMember).size)

assert_equal(rbi, tree.string)
end

def test_parse_root_tree_location
Expand Down

0 comments on commit 40dc6ea

Please sign in to comment.