Skip to content

Commit 26f21a2

Browse files
authored
Merge pull request #2079 from tk0miya/support_self_CONST
prototype rb: Fix crashed by self::CONST
2 parents dba9ad6 + 506ac10 commit 26f21a2

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

lib/rbs/prototype/rb.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def process(node, decls:, comments:, context:)
368368
when node.children[0].is_a?(Symbol)
369369
TypeName.new(name: node.children[0], namespace: Namespace.empty)
370370
else
371-
const_to_name!(node.children[0])
371+
const_to_name!(node.children[0], context: context)
372372
end
373373

374374
value_node = node.children.last
@@ -429,20 +429,24 @@ def process_children(node, decls:, comments:, context:)
429429
end
430430
end
431431

432-
def const_to_name!(node)
432+
def const_to_name!(node, context: nil)
433433
case node.type
434434
when :CONST
435435
TypeName.new(name: node.children[0], namespace: Namespace.empty)
436436
when :COLON2
437437
if node.children[0]
438-
namespace = const_to_name!(node.children[0]).to_namespace
438+
namespace = const_to_name!(node.children[0], context: context).to_namespace
439439
else
440440
namespace = Namespace.empty
441441
end
442442

443443
TypeName.new(name: node.children[1], namespace: namespace)
444444
when :COLON3
445445
TypeName.new(name: node.children[0], namespace: Namespace.root)
446+
when :SELF
447+
raise if context.nil?
448+
449+
context.namespace.to_type_name
446450
else
447451
raise
448452
end

sig/prototype/rb.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module RBS
4444
# Returns a type name that represents the name of the constant.
4545
# `node` must be _constant_ node, `CONST`, `COLON2`, or `COLON3` node.
4646
#
47-
def const_to_name!: (RubyVM::AbstractSyntaxTree::Node node) -> TypeName
47+
def const_to_name!: (RubyVM::AbstractSyntaxTree::Node node, ?context: Context?) -> TypeName
4848

4949
# Returns a type name that represents the name of the constant.
5050
# `node` can be `SELF` for `extend self` pattern.

test/rbs/rb_prototype_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ module Foo
688688
VERSION = '0.1.1'
689689
FROZEN = 'str'.freeze
690690
::Hello::World = :foo
691+
self::MAX = 42
691692
end
692693
EOR
693694

@@ -700,6 +701,8 @@ module Foo
700701
FROZEN: "str"
701702
702703
::Hello::World: :foo
704+
705+
::Foo::MAX: 42
703706
end
704707
EOF
705708
end

0 commit comments

Comments
 (0)