Skip to content

Commit

Permalink
prototype rb: Fix crashed by self::CONST
Browse files Browse the repository at this point in the history
Closes: #2076
  • Loading branch information
tk0miya committed Nov 6, 2024
1 parent d9000d2 commit 12c3f77
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/rbs/prototype/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def process(node, decls:, comments:, context:)
when node.children[0].is_a?(Symbol)
TypeName.new(name: node.children[0], namespace: Namespace.empty)
else
const_to_name!(node.children[0])
const_to_name!(node.children[0], context: context)
end

value_node = node.children.last
Expand Down Expand Up @@ -429,20 +429,24 @@ def process_children(node, decls:, comments:, context:)
end
end

def const_to_name!(node)
def const_to_name!(node, context: nil)
case node.type
when :CONST
TypeName.new(name: node.children[0], namespace: Namespace.empty)
when :COLON2
if node.children[0]
namespace = const_to_name!(node.children[0]).to_namespace
namespace = const_to_name!(node.children[0], context: context).to_namespace
else
namespace = Namespace.empty
end

TypeName.new(name: node.children[1], namespace: namespace)
when :COLON3
TypeName.new(name: node.children[0], namespace: Namespace.root)
when :SELF
raise if context.nil?

context.namespace.to_type_name
else
raise
end
Expand Down
2 changes: 1 addition & 1 deletion sig/prototype/rb.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module RBS
# Returns a type name that represents the name of the constant.
# `node` must be _constant_ node, `CONST`, `COLON2`, or `COLON3` node.
#
def const_to_name!: (RubyVM::AbstractSyntaxTree::Node node) -> TypeName
def const_to_name!: (RubyVM::AbstractSyntaxTree::Node node, ?context: Context?) -> TypeName

# Returns a type name that represents the name of the constant.
# `node` can be `SELF` for `extend self` pattern.
Expand Down
3 changes: 3 additions & 0 deletions test/rbs/rb_prototype_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ module Foo
VERSION = '0.1.1'
FROZEN = 'str'.freeze
::Hello::World = :foo
self::MAX = 42
end
EOR

Expand All @@ -700,6 +701,8 @@ module Foo
FROZEN: "str"
::Hello::World: :foo
::Foo::MAX: 42
end
EOF
end
Expand Down

0 comments on commit 12c3f77

Please sign in to comment.